-
-
Save arrowcircle/5bb6062279ed4f63a8b2f18dfc8ac2b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "api_handler.h" | |
#include <string> | |
#include <vector> | |
int64_t lastApiUpdatedAt = 1 * 1000; | |
int apiPeriod = 5 * 1000; | |
HTTPClient http; | |
int errorCounter = 0; | |
void parseServerResponse(String payload) { | |
DynamicJsonBuffer jsonBuffer; | |
JsonObject& parsed = jsonBuffer.parseObject(payload); | |
if (!parsed.success()) { | |
Serial.println("Parsing failed"); | |
return; | |
} | |
Serial.println(payload); | |
} | |
void scrapeApi() { | |
if (WiFi.status() != WL_CONNECTED) { return; } | |
Serial.println("======= Scraping API"); | |
String url = "http://headers.jsontest.com/"; | |
http.begin(url); | |
int httpCode = http.GET(); | |
if (httpCode > 0) { | |
if (httpCode == HTTP_CODE_OK) { | |
errorCounter = 0; | |
String payload = http.getString(); | |
parseServerResponse(payload); | |
} | |
} else { | |
Serial.print("[HTTP] GET... failed, error: "); | |
Serial.println(http.errorToString(httpCode).c_str()); | |
printWiFiDebug(); | |
errorCounter++; | |
Serial.print("Error count: "); | |
Serial.println(errorCounter); | |
if (errorCounter > 3) { initWifi(); errorCounter = 0; } | |
} | |
http.end(); | |
yield(); | |
} | |
void scrapeApiHandler() { | |
int64_t currentMillis = millis(); | |
if (currentMillis > lastApiUpdatedAt + apiPeriod) { | |
scrapeApi(); | |
lastApiUpdatedAt = currentMillis; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SRC_API_HANDLER_H_ | |
#define SRC_API_HANDLER_H_ | |
#ifdef ESP8266 | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#elif defined(ESP32) | |
#include <esp_wifi.h> | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#endif | |
#include <ArduinoJson.h> | |
#include "wifi_handler.h" | |
extern HTTPClient http; | |
void scrapeApiHandler(); | |
#endif // SRC_API_HANDLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "main.h" | |
void setup() { | |
Serial.begin(9600); | |
initWifi(); | |
} | |
void loop() { | |
scrapeApiHandler(); | |
yield(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SRC_MAIN_H_ | |
#define SRC_MAIN_H_ | |
#include <Arduino.h> | |
#include <ArduinoJson.h> | |
#include "api_handler.h" | |
#include "wifi_handler.h" | |
#endif // SRC_MAIN_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[platformio] | |
env_default = esp12e | |
; env_default = esp32dev | |
[env:esp32dev] | |
platform = espressif32 | |
board = esp32dev | |
framework = arduino | |
upload_speed = 921600 | |
lib_deps = | |
ArduinoJson@5.13.2 | |
[env:esp12e] | |
platform = espressif8266 | |
board = esp12e | |
framework = arduino | |
upload_speed = 921600 | |
lib_archive = false | |
lib_deps = | |
ArduinoJson@5.13.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "wifi_handler.h" | |
void printWiFiDebug() { | |
Serial.print("Wifi status: "); | |
Serial.print(WiFi.status()); | |
Serial.print(" (Connected = "); | |
Serial.print(WL_CONNECTED); | |
Serial.print(")"); | |
long rssi = WiFi.RSSI(); | |
Serial.print("Wifi signal: "); | |
Serial.println(rssi); | |
} | |
bool isWifiConnected() { | |
if (WiFi.status() == WL_CONNECTED) { return true; } | |
return false; | |
} | |
void initWifi() { | |
Serial.println("Initializing WiFi"); | |
WiFi.disconnect(true); | |
WiFi.softAPdisconnect(true); | |
WiFi.mode(WIFI_STA); | |
wifi_set_sleep_type(NONE_SLEEP_T); | |
delay(100); | |
WiFi.begin("xxx", "xxx"); | |
int seconds = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.print("."); | |
seconds++; | |
if (seconds > 15) { Serial.println("Failed connecting to WiFi"); return; } | |
} | |
Serial.print("\nConnected to WiFI with local IP: "); | |
Serial.println(WiFi.localIP()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SRC_WIFI_HANDLER_H_ | |
#define SRC_WIFI_HANDLER_H_ | |
#include <Arduino.h> | |
#ifdef ESP8266 | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#elif defined(ESP32) | |
#include <esp_wifi.h> | |
#include <WiFi.h> | |
#include <WebServer.h> | |
#endif | |
bool isWifiConnected(); | |
void initWifi(); | |
void printWiFiDebug(); | |
#endif // SRC_WIFI_HANDLER_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment