From b12ca08e7c92f58439e28f0e5bd9a90bedd44185 Mon Sep 17 00:00:00 2001 From: Dennis Rathjen Date: Sun, 21 Aug 2022 12:59:14 +0200 Subject: [PATCH] Preparations for send telemetry (no ssl) --- platformio.ini | 6 ++++++ src/PixelIt.ino | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/platformio.ini b/platformio.ini index d231798..ef082cd 100644 --- a/platformio.ini +++ b/platformio.ini @@ -49,6 +49,8 @@ lib_deps = adafruit/Adafruit BMP280 Library@^2.6.1 claws/BH1750@^1.2.0 robtillaart/Max44009@^0.5.2 + arduino-libraries/ArduinoHttpClient@^0.4.0 + Hash = https://github.com/bbx10/Hash_tng.git [env:ESP8266] platform = espressif8266@2.6.3 @@ -65,6 +67,8 @@ lib_deps = adafruit/Adafruit BMP280 Library@^2.6.1 claws/BH1750@^1.2.0 robtillaart/Max44009@^0.5.2 + arduino-libraries/ArduinoHttpClient@^0.4.0 + mr-glt/SHA-1 Hash@^1.1.0 [env:d1_mini] platform = espressif8266@2.6.3 @@ -81,3 +85,5 @@ lib_deps = adafruit/Adafruit BMP280 Library@^2.6.1 claws/BH1750@^1.2.0 robtillaart/Max44009@^0.5.2 + arduino-libraries/ArduinoHttpClient@^0.4.0 + mr-glt/SHA-1 Hash@^1.1.0 diff --git a/src/PixelIt.ino b/src/PixelIt.ino index 5f4db7a..6a30e66 100644 --- a/src/PixelIt.ino +++ b/src/PixelIt.ino @@ -40,6 +40,8 @@ #include "ColorConverterLib.h" #include #include +#include +#include // PixelIT Stuff #include "PixelItFont.h" #include "Webinterface.h" @@ -89,6 +91,9 @@ String ldrDevice = "GL5516"; unsigned long ldrPulldown = 10000; // 10k pulldown-resistor unsigned int ldrSmoothing = 0; +String serverAddress = "pixelit.bastelbunker.de"; +int serverPort = 80; + String btnPin[] = {"Pin_D0", "Pin_D4", "Pin_D5"}; bool btnEnabled[] = {false, false, false}; int btnPressedLevel[] = {LOW, LOW, LOW}; @@ -174,6 +179,7 @@ WiFiClient espClient; WiFiUDP udp; PubSubClient client(espClient); WiFiManager wifiManager; +HttpClient httpClient = HttpClient(espClient, serverAddress, serverPort); #if defined(ESP8266) ESP8266WebServer server(80); ESP8266HTTPUpdateServer httpUpdater; @@ -276,7 +282,7 @@ float temperatureOffset = 0.0f; float humidityOffset = 0.0f; float pressureOffset = 0.0f; float gasOffset = 0.0f; -bool sendStats = true; +bool sendTelemetry = true; bool checkUpdateScreen = true; // MP3Player Vars String OldGetMP3PlayerInfo; @@ -367,7 +373,7 @@ void SaveConfig() json["ldrPulldown"] = ldrPulldown; json["ldrSmoothing"] = ldrSmoothing; json["initialVolume"] = initialVolume; - json["sendStats"] = sendStats; + json["sendTelemetry"] = sendTelemetry; json["checkUpdateScreen"] = checkUpdateScreen; #if defined(ESP8266) @@ -702,9 +708,9 @@ void SetConfigVariables(JsonObject &json) initialVolume = json["initialVolume"].as(); } - if (json.containsKey("sendStats")) + if (json.containsKey("sendTelemetry")) { - sendStats = json["sendStats"].as(); + sendTelemetry = json["sendTelemetry"].as(); } if (json.containsKey("checkUpdateScreen")) @@ -1716,6 +1722,21 @@ String GetButtons() return json; } +String GetTelemetry() +{ + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.createObject(); + + root["uuid"] = sha1(GetChipID()); + root["version"] = VERSION; + root["type"] = isESP8266 ? "esp8266" : "esp32"; + + String json; + root.printTo(json); + + return json; +} + ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// void DrawText(String text, int bigFont, int colorRed, int colorGreen, int colorBlue, int posX, int posY) @@ -3130,7 +3151,7 @@ void setup() // Config menue timeout 180 seconds. wifiManager.setConfigPortalTimeout(180); - if (!wifiManager.autoConnect("PIXEL_IT")) + if (!wifiManager.autoConnect("PIXELIT")) { Log(F("Setup"), F("Wifi failed to connect and hit timeout")); delay(3000); @@ -3190,6 +3211,13 @@ void setup() mp3Player.begin(*softSerial); Log(F("Setup"), F("DFPlayer started")); mp3Player.volume(initialVolume); + + if (sendTelemetry == true) + { + Log(F("Setup"), F("Telemetry send")); + httpClient.sendHeader("User-Agent", "PixelIt"); + httpClient.post("/api/telemetry", "application/json", GetTelemetry()); + } } void loop()