#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <Arduino_JSON.h>
// Include the libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define the OLED parameters
#define SCREEN_WIDTH 128
#define SCREEN_HEIGTH 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
// Declare the objects
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGTH, &Wire, OLED_RESET);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//Your IP address or domain name with URL path
const char* serverName = "https://speech-development-tools.glitch.me/gpio/route?INFO=salutation";
// Update interval time set to 5 seconds
const long interval = 1000;
unsigned long previousMillis = 0;
String outputsState;
void setup() {
Serial.begin(115200);
if (! display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)){
Serial.println(F("SSD1306 aclocation failed"));
for(;;);
}
// Clear the Display
display.display();
delay(500);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("OK");
display.display();
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED ){
outputsState = httpGETRequest(serverName);
Serial.println(outputsState);
JSONVar myObject = JSON.parse(outputsState);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
// myObject.keys() can be used to get an array of all the keys in the object
JSONVar keys = myObject.keys();
for (int i = 0; i < keys.length(); i++) {
JSONVar value = myObject[keys[i]];
Serial.print("GPIO: ");
Serial.print(keys[i]);
Serial.print(" - SET to: ");
Serial.println(value);
pinMode(atoi(keys[i]), OUTPUT);
digitalWrite(atoi(keys[i]), atoi(value));
display.clearDisplay();
display.setCursor(3,30);
display.print(atoi(keys[i]));
display.setCursor(22, 35);
display.print('-');
display.setCursor(30,30);
display.println(atoi(value));
display.display();
Serial.print(atoi(keys[i]), atoi(value));
/*
pinMode(2, OUTPUT);
digitalWrite(2, 1);
*/
}
// save the last HTTP GET Request
previousMillis = currentMillis;
}
else {
Serial.println("WiFi Disconnected");
}
}
}
/*
HTTP Response code: 200
{"2":0}
JSON object = {"2":0}
GPIO: "2" - SET to: 0
2
*/
String httpGETRequest(const char* serverName) {
WiFiClientSecure *client = new WiFiClientSecure;
// set secure client without certificate
client->setInsecure();
HTTPClient https;
// Your IP address with path or Domain name with URL path
https.begin(*client, serverName);
// Send HTTP POST request
int httpResponseCode = https.GET();
String payload = "{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = https.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
https.end();
return payload;
}