Created
April 5, 2020 13:43
-
-
Save noppelmax/35ec7b1fdfd46a9ebe8191e18f3b808b to your computer and use it in GitHub Desktop.
NewTemperaturSensorsCode_V2
This file contains hidden or 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 <ArduinoOTA.h> | |
| #include <ArduinoJson.h> | |
| #include <DHT.h> | |
| #include <ESP8266WiFi.h> | |
| #include <PubSubClient.h> | |
| const char* SSID = "vspace.one"; | |
| const char* PSK = "12345678"; | |
| /* Change Sensortype here! Use Rs in next designs */ | |
| #define BRUECKE | |
| #ifdef MASCHINENRAUM | |
| const char* NAME = "Maschinenraum"; | |
| const char* OTANAME = "Sensornode_Maschinenraum_01"; | |
| const char* MQTTNAME = "Sensornode_Maschinenraum_01"; | |
| #endif | |
| #ifdef BRUECKE | |
| const char* NAME = "Bruecke"; | |
| const char* OTANAME = "Sensornode_Bruecke_01"; | |
| const char* MQTTNAME = "Sensornode_Bruecke_01"; | |
| #endif | |
| WiFiClient espClient; | |
| PubSubClient mqttClient(espClient); | |
| #define LED_RED D0 | |
| #define LED_BLUE D2 | |
| #define LED_GREEN D1 | |
| #define DHT_DATA D7 | |
| #define LED_ONLINE LED_GREEN | |
| #define LED_ERROR LED_RED | |
| #define LED_MEASURING LED_BLUE | |
| #define MAJORVERSION 0 | |
| #define MINORVERSION 2 | |
| #define PATCHVERSION 0 | |
| #define MQTT_SERVER "10.1.0.15" | |
| #define MQTT_PORT 1883 | |
| #define MQTT_USER "" | |
| #define MQTT_PWD "" | |
| #define USE_MQTT 1 | |
| DHT dht(DHT_DATA, DHT22); | |
| int i = 0; | |
| void setup_wifi() { | |
| Serial.println(); | |
| Serial.print("Connecting to "); | |
| Serial.println(SSID); | |
| WiFi.begin(SSID, PSK); | |
| int i = 150; | |
| digitalWrite(LED_ONLINE, LOW); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(200); | |
| digitalWrite(LED_ONLINE, HIGH); | |
| delay(200); | |
| digitalWrite(LED_ONLINE, LOW); | |
| Serial.print("."); | |
| if ( i-- == 0 ) { | |
| // ERROR: TIMEOUT BREAK | |
| break; | |
| } | |
| } | |
| if ( WiFi.status() == WL_CONNECTED ) { | |
| digitalWrite(LED_ONLINE, HIGH); | |
| Serial.println(""); | |
| Serial.println("WiFi connected"); | |
| Serial.print("IP address: "); | |
| Serial.println(WiFi.localIP()); | |
| } else { | |
| digitalWrite(LED_ONLINE, LOW); | |
| digitalWrite(LED_ERROR, HIGH); | |
| Serial.println("Failed to connect..."); | |
| } | |
| } | |
| void setup() { | |
| pinMode(LED_RED, OUTPUT); | |
| pinMode(LED_GREEN, OUTPUT); | |
| pinMode(LED_BLUE, OUTPUT); | |
| delay(100); | |
| pinMode(DHT_DATA, INPUT); | |
| dht.begin(); | |
| Serial.begin(115000); | |
| Serial.print("Starting VSPACE Sensornode (Temp,Hum)\r\n"); | |
| Serial.print("Firmware Version: v"); | |
| Serial.print(MAJORVERSION); | |
| Serial.print("."); | |
| Serial.print(MINORVERSION); | |
| Serial.print("."); | |
| Serial.print(PATCHVERSION); | |
| Serial.print("\r\n"); | |
| Serial.println("Setup wifi"); | |
| setup_wifi(); | |
| mqttClient.setServer(MQTT_SERVER, MQTT_PORT); | |
| Serial.println("Setting up OTA"); | |
| ArduinoOTA.setHostname(OTANAME); | |
| ArduinoOTA.onStart([]() { | |
| Serial.println("Start"); | |
| }); | |
| ArduinoOTA.onEnd([]() { | |
| Serial.println("\nEnd"); | |
| }); | |
| ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | |
| Serial.printf("Progress: %u%%\r", (progress / (total / 100))); | |
| }); | |
| ArduinoOTA.onError([](ota_error_t error) { | |
| Serial.printf("Error[%u]: ", error); | |
| if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | |
| else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | |
| else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | |
| else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | |
| else if (error == OTA_END_ERROR) Serial.println("End Failed"); | |
| }); | |
| ArduinoOTA.begin(); | |
| Serial.println("OTA up"); | |
| } | |
| void reconnect() { | |
| while (!mqttClient.connected()) { | |
| Serial.println("Reconnecting MQTT..."); | |
| if (!mqttClient.connect(MQTTNAME)) { | |
| digitalWrite(LED_ERROR, HIGH); | |
| digitalWrite(LED_ONLINE, LOW); | |
| digitalWrite(LED_MEASURING, LOW); | |
| Serial.print("failed, rc="); | |
| Serial.print(mqttClient.state()); | |
| Serial.println(" retrying in 5 seconds"); | |
| delay(5000); | |
| } | |
| } | |
| digitalWrite(LED_ERROR, LOW); | |
| digitalWrite(LED_ONLINE, HIGH); | |
| digitalWrite(LED_MEASURING, HIGH); | |
| Serial.println("MQTT Connected..."); | |
| } | |
| void loop() { | |
| // wait for 30min | |
| if ( i != 60 * 15) { | |
| ArduinoOTA.handle(); | |
| i ++; | |
| delay(1000); | |
| return; | |
| } else { | |
| i = 0; | |
| } | |
| if (WiFi.status() != WL_CONNECTED) { | |
| Serial.println("Lost connection..."); | |
| setup_wifi(); | |
| } | |
| digitalWrite(LED_MEASURING, HIGH); | |
| delay(100); | |
| float temp = dht.readTemperature(); | |
| float hum = dht.readHumidity(); | |
| // Calibrate! TODO | |
| temp = temp + 0.5; | |
| Serial.print(temp); | |
| Serial.print("C "); | |
| Serial.print(hum); | |
| Serial.println("%"); | |
| if (isnan(temp) || isnan(hum)) { | |
| Serial.println("Failed to read from DHT sensor!"); | |
| digitalWrite(LED_ERROR, HIGH); | |
| digitalWrite(LED_MEASURING, LOW); | |
| return; | |
| } else { | |
| digitalWrite(LED_ERROR, LOW); | |
| } | |
| if (!mqttClient.connected() && USE_MQTT == 1) { | |
| reconnect(); | |
| } | |
| StaticJsonDocument<200> jsonBuffer; | |
| JsonObject root = jsonBuffer.to<JsonObject>(); | |
| root["status"] = "ok"; | |
| JsonObject data = root.createNestedObject("data"); | |
| data["value"] = temp; | |
| data["location"] = NAME; | |
| data["unit"] = "°C"; | |
| char messageTemp[200]; | |
| serializeJson(root, (char*)messageTemp, measureJson(root) + 1); | |
| if (USE_MQTT == 1) { | |
| mqttClient.publish("vspace/one/temperature", messageTemp); | |
| } | |
| data["value"] = hum; | |
| data["location"] = NAME; | |
| data["unit"] = "%"; | |
| char messageHum[200]; | |
| serializeJson(root, (char*)messageHum, measureJson(root) + 1); | |
| if (USE_MQTT == 1) { | |
| mqttClient.publish("vspace/one/humidity", messageHum); | |
| } | |
| digitalWrite(LED_MEASURING, LOW); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment