// based on example code from the used libraries #include // this needs to be first, or it all crashes and burns... extern "C" { #include } #include // https://github.com/esp8266/Arduino #include #include // https://github.com/tzapu/WiFiManager #include #include #include // https://github.com/knolleary/pubsubclient #include // https://github.com/adafruit/DHT-sensor-library #include // https://github.com/bblanchon/ArduinoJson #define HARDWARE_WITTY // configure DHT sensor #define DHTPIN D4 // what pin the DHT is connected to //#define DHTTYPE DHT11 // DHT11 //#define DHTTYPE DHT21 // DHT21 (AM2301) #define DHTTYPE DHT22 // DHT22 (AM2302) #ifdef HARDWARE_WITTY #define CONFIGPIN D2 // config-button is connected to this pin #else #define CONFIGPIN D3 // config-button is connected to this pin #endif #define RGBRED D8 // red led channel on witty module #define RGBGRN D6 // green led channel on witty module #define RGBBLU D7 // blue led channel on witty module #define LDRPIN A0 // analog LDR input on witty module // define default values here, overwritten by values from config.json char mqtt_server[40]; char mqtt_port[6] = "1883"; char mqtt_topic[34] = "OutTopic"; // flag for saving data bool shouldSaveConfig = false; // callback notifying us of the need to save config void saveConfigCallback() { Serial.println("Should save config"); shouldSaveConfig = true; } // initialize modules ESP8266WebServer http_server(80); // webserver DHT dht(DHTPIN, DHTTYPE); // DHT sensor Ticker ticker; // LED status WiFiClient wifiClient; PubSubClient mqtt_client(wifiClient); float humidity, temperature; // raw values from the sensor float heatindex; // computed value from the sensor char str_humidity[10], str_temperature[10]; // rounded values as strings char str_heatindex[10]; // rounded value as string unsigned long previousMillis = 0; // last sensor read const long interval = 2000; // interval between readings long lastMsg = 0; // toggle LED state void toggle_led() { int state = digitalRead(BUILTIN_LED); // get the current state LED digitalWrite(BUILTIN_LED, !state); // set the opposite state } // gets called when WiFiManager enters configuration mode void configModeCallback (WiFiManager *myWiFiManager) { Serial.println("Entered config mode"); Serial.println(WiFi.softAPIP()); Serial.println(myWiFiManager->getConfigPortalSSID()); ticker.attach(0.1, toggle_led); // toggle led faster } // compare float values bool isEqual(float a, float b, float epsilon=0.001) { return fabs(a - b) <= epsilon * fabs(a); } // concatenate MQTT topic prefix to individual topic char* topic(const char* this_topic) { static char topic[34]; strcpy(topic, mqtt_topic); strcat(topic, "/"); strcat(topic, this_topic); return topic; } // get values from DHT sensor void read_sensor() { // wait at least 2 seconds seconds between measurements unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; float previousHumidity = humidity; float previousTemperature = temperature; float previousHeatindex = heatindex; humidity = dht.readHumidity(); // read humidity as a percent temperature = dht.readTemperature(); // read temperature as Celsius heatindex = dht.computeHeatIndex(temperature, humidity, false); // check if any reads failed and exit early (to try again) if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); return; } // convert the floats to strings and round to 2 decimal places dtostrf(humidity, 1, 2, str_humidity); dtostrf(temperature, 1, 2, str_temperature); dtostrf(heatindex, 1, 2, str_heatindex); if (!isEqual(humidity, previousHumidity)) { mqtt_client.publish(topic("humidity"), str_humidity); } if (!isEqual(temperature, previousTemperature)) { mqtt_client.publish(topic("temperature"), str_temperature); } if (!isEqual(heatindex, previousHeatindex)) { mqtt_client.publish(topic("heatindex"), str_heatindex); } Serial.print("Humidity: "); Serial.print(str_humidity); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(str_temperature); Serial.print(" °C\t"); Serial.print("Heat Index: "); Serial.print(str_heatindex); Serial.println(" °C"); } } // callback for MQTT, gets called if we receive a message void mqtt_callback(char* topic, byte* payload, unsigned int length) { char inData[length+1]; for(int i = 0; i < length; i++) { inData[i] = (char)payload[i]; } inData[length] = 0; Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); Serial.println(inData); StaticJsonBuffer<200> jsonBuffer; JsonObject& json = jsonBuffer.parseObject(inData); analogWrite(RGBRED, json["red"]); analogWrite(RGBGRN, json["green"]); analogWrite(RGBBLU, json["blue"]); } // make sure we're connected to MQTT broker void mqtt_reconnect() { // loop until we're reconnected while (!mqtt_client.connected()) { Serial.print("Attempting MQTT connection..."); // attempt to connect if (mqtt_client.connect(wifi_station_get_hostname(), topic("online"), MQTTQOS1, true, "0")) { Serial.println("connected"); // once connected, publish an announcement... mqtt_client.publish(topic("online"), "1", true); // ... and resubscribe: mqtt_client.subscribe("setrgb"); } else { Serial.print("failed, rc="); Serial.print(mqtt_client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } // convert string to integer int stringToNumber(String thisString) { int i, value, length; length = thisString.length(); char blah[(length + 1)]; for (i = 0; i < length; i++) { blah[i] = thisString.charAt(i); } blah[i] = 0; value = atoi(blah); return value; } void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(BUILTIN_LED, OUTPUT); // set led pin as output ticker.attach(0.5, toggle_led); // toggle led slowly during initialization pinMode(RGBRED, OUTPUT); pinMode(RGBGRN, OUTPUT); pinMode(RGBBLU, OUTPUT); pinMode(CONFIGPIN, INPUT); // flash-button on nodemcu pinMode(LDRPIN, INPUT); // clean FS, for testing //SPIFFS.format(); // read configuration from FS json Serial.println("mounting FS..."); if (SPIFFS.begin()) { Serial.println("mounted file system"); if (SPIFFS.exists("/config.json")) { // file exists, reading and loading Serial.println("reading config file"); File configFile = SPIFFS.open("/config.json", "r"); if (configFile) { Serial.println("opened config file"); size_t size = configFile.size(); // allocate a buffer to store contents of the file. std::unique_ptr buf(new char[size]); configFile.readBytes(buf.get(), size); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { Serial.println("\nparsed json"); strcpy(mqtt_server, json["mqtt_server"]); strcpy(mqtt_port, json["mqtt_port"]); strcpy(mqtt_topic, json["mqtt_topic"]); } else { Serial.println("failed to load json config"); } } } } else { Serial.println("failed to mount FS"); } // WiFiManager // extra parameters to be configured WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5); WiFiManagerParameter custom_mqtt_topic("topic", "mqtt topic", mqtt_topic, 32); // local intialization WiFiManager wifiManager; // reset settings if config-button is pressed if (!digitalRead(CONFIGPIN)) { Serial.println("config-button pressed, resetting wifi settings"); wifiManager.resetSettings(); } // set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode wifiManager.setAPCallback(configModeCallback); // set config save notify callback wifiManager.setSaveConfigCallback(saveConfigCallback); // add all your parameters here wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); wifiManager.addParameter(&custom_mqtt_topic); // fetches ssid and pass and tries to connect // if it does not connect it starts an access point if (!wifiManager.autoConnect()) { Serial.println("failed to connect and hit timeout"); // reset and try again ESP.reset(); delay(1000); } Serial.println("connected to WiFi"); ticker.detach(); digitalWrite(BUILTIN_LED, LOW); // read updated parameters strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(mqtt_topic, custom_mqtt_topic.getValue()); // save the custom parameters to FS if (shouldSaveConfig) { Serial.println("saving config"); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); json["mqtt_server"] = mqtt_server; json["mqtt_port"] = mqtt_port; json["mqtt_topic"] = mqtt_topic; File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { Serial.println("failed to open config file for writing"); } json.printTo(Serial); json.printTo(configFile); configFile.close(); } mqtt_client.setServer(mqtt_server, stringToNumber(mqtt_port)); mqtt_client.setCallback(mqtt_callback); dht.begin(); // initial read read_sensor(); mqtt_client.publish(topic("humidity"), str_humidity); mqtt_client.publish(topic("temperature"), str_temperature); mqtt_client.publish(topic("heatindex"), str_heatindex); // handle http requests http_server.on("/", [](){ read_sensor(); String response = "\r\n"; response += "\r\n"; response += "\r\n"; response += "\r\n"; response += "\r\n"; response += "Status\r\n"; response += "\r\n"; response += "\r\n"; response += "\r\n"; response += "
\r\n"; response += "

"; response += wifi_station_get_hostname(); response += "

\r\n"; response += "

Sensor Status

\r\n"; response += "\r\n"; response += "\r\n"; response += "\r\n"; response += "\r\n"; response += "
Temperature"; response += str_temperature; response += "°C
Humidity"; response += str_humidity; response += "%RH
Heat Index"; response += str_heatindex; response += "°C
\r\n"; response += "
\r\n"; response += "
\r\n"; response += "\r\n"; response += "\n"; http_server.send(200, "text/html", response); delay(100); }); http_server.on("/values", [](){ read_sensor(); String response = "updatetime\t"; response += previousMillis; response += "\n"; response += "temperature\t"; response += str_temperature; response += "\n"; response += "humidity\t"; response += str_humidity; response += "\n"; response += "heatindex\t"; response += str_heatindex; response += "\n"; http_server.send(200, "text/plain", response); delay(100); }); http_server.on("/temp", [](){ read_sensor(); char response[50]; snprintf(response, 50, "Temperature: %s °C", str_temperature); http_server.send(200, "text/plain", response); }); http_server.on("/humidity", [](){ read_sensor(); char response[50]; snprintf(response, 50, "Humidity: %s %", str_humidity); http_server.send(200, "text/plain", response); }); // start the web server http_server.begin(); Serial.println("HTTP server started"); } void loop() { // listen for http requests http_server.handleClient(); if (!mqtt_client.connected()) { mqtt_reconnect(); } mqtt_client.loop(); long now = millis(); if (now - lastMsg > 10000) { lastMsg = now; read_sensor(); } }