Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ArduinoJson.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <Timers.h>
- const char *ssid = "DataJugling";
- const char *password = "jugle123";
- const char *url = "http://dataserver.local/";
- Timer requestTimer;
- DynamicJsonDocument doc;
- HTTPClient http;
- void setup() {
- Serial.begin(115200);
- requestTimer.begin(500);
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) delay(500);
- digitalWrite(LED_BUILTIN, HIGH);
- //Generating URL from server IP which is client gateway.
- //And URL have to look fine for HTTPClient
- //url = "http://" + WiFi.gatewayIP().toString() + "/";
- //url = "http://dataserver.local/";
- }
- void loop() {
- if(requestTimer.available()) {
- http.begin(url);
- int httpCode = http.GET();
- if (httpCode > 0) {
- String data = http.getString();
- deserializeJson(doc, data);
- JsonObject root = doc.as<JsonObject>();
- Serial.print("JSON decode: ");
- Serial.println(root["state"].as<bool>());
- } else {
- Serial.printf("[HTTP] Error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- requestTimer.restart();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement