Skip to main content
Minor fixes
Source Link
Greenonline
  • 3.2k
  • 7
  • 37
  • 49

I have cobbled together, from the library examples, a program to log temperature and humidity to an MQTT server. The SSID/password, MQTT server and port, and the MQTT publish path can all be set at run time. Once they are set, the device will remember and re-use the settings next time it boots.

I am using the Arduino IDE to program an ESP8266-01. It, in turn, talks to a DHT11 sensor for temperature and humidity data.

I have cobbled together from the library examples a program to log temperature and humidity to an MQTT server. The SSID/password, MQTT server and port, and the MQTT publish path can all be set at run time. Once they are set, the device will remember and re-use the settings next time it boots.

I am using the Arduino IDE to program an ESP8266-01. It, in turn talks to a DHT11 sensor for temperature and humidity data.

I have cobbled together, from the library examples, a program to log temperature and humidity to an MQTT server. The SSID/password, MQTT server and port, and the MQTT publish path can all be set at run time. Once they are set, the device will remember and re-use the settings next time it boots.

I am using the Arduino IDE to program an ESP8266-01. It, in turn, talks to a DHT11 sensor for temperature and humidity data.

Source Link

I have cobbled together from the library examples a program to log temperature and humidity to an MQTT server. The SSID/password, MQTT server and port, and the MQTT publish path can all be set at run time. Once they are set, the device will remember and re-use the settings next time it boots.

I am using the Arduino IDE to program an ESP8266-01. It, in turn talks to a DHT11 sensor for temperature and humidity data.

The program is found at https://github.com/PabluskiNC/TempHumidityLogger

    #include <FS.h>                   //this needs to be first, or it all crashes and burns...

    #include "DHT.h"
    #define DHTPIN 2
    #define DHTTYPE DHT11
    DHT dht(DHTPIN, DHTTYPE);

    #include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

    //needed for library
    #include <DNSServer.h>
    #include <ESP8266WebServer.h>
    #include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

    #include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson

    #include <PubSubClient.h>
    WiFiClient espClient;
    PubSubClient client(espClient);
    long mqtt_lastMsg = 0;
    char mqtt_msg[50];
    int  mqtt_value = 0;

    //define your default values here, if there are different values in config.json, they are overwritten.
    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;
    }


    void setup() {
      // put your setup code here, to run once:
      Serial.begin(115200);
      Serial.println();

      //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<char[]> 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");
      }
      //end read



      // The extra parameters to be configured (can be either global or just in the setup)
      // After connecting, parameter.getValue() will get you the configured value
      // id/name placeholder/prompt default length
      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);

      //WiFiManager
      //Local intialization. Once its business is done, there is no need to keep it around
      WiFiManager wifiManager;

      //set config save notify callback
      wifiManager.setSaveConfigCallback(saveConfigCallback);

      //set static ip
      //wifiManager.setSTAStaticIPConfig(IPAddress(10, 0, 1, 99), IPAddress(10, 0, 1, 1), IPAddress(255, 255, 255, 0));

      //add all your parameters here
      wifiManager.addParameter(&custom_mqtt_server);
      wifiManager.addParameter(&custom_mqtt_port);
      wifiManager.addParameter(&custom_mqtt_topic);

      //reset settings - for testing
      //wifiManager.resetSettings();

      //set minimu quality of signal so it ignores AP's under that quality
      //defaults to 8%
      //wifiManager.setMinimumSignalQuality();

      //sets timeout until configuration portal gets turned off
      //useful to make it all retry or go to sleep
      //in seconds
      //wifiManager.setTimeout(120);

      //fetches ssid and pass and tries to connect
      //if it does not connect it starts an access point with the specified name
      //here  "AutoConnectAP"
      //and goes into a blocking loop awaiting configuration
      if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
        Serial.println("failed to connect and hit timeout");
        delay(3000);
        //reset and try again, or maybe put it to deep sleep
        ESP.reset();
        delay(5000);
      }

      //if you get here you have connected to the WiFi
      Serial.println("connected...yeey :)");

      //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();
        //end save
      }

      client.setServer(mqtt_server, stringToNumber(mqtt_port));
      client.setCallback(callback);
      Serial.println("local ip");
      Serial.println(WiFi.localIP());
      dht.begin();
    }

    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 callback(char* topic, byte* payload, unsigned int length) {
      Serial.print("Message arrived [");
      Serial.print(topic);
      Serial.print("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }
      Serial.println();

      // Switch on the LED if an 1 was received as first character
      if ((char)payload[0] == '1') {
        digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
        // but actually the LED is on; this is because
        // it is acive low on the ESP-01)
      } else {
        digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
      }

    }

    void reconnect() {
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        Serial.print(mqtt_server);
        Serial.print(":");
        Serial.print(mqtt_port);
        Serial.print("...");
        // Attempt to connect
        if (client.connect("ESP8266Client")) {
          Serial.println("connected");
          // Once connected, publish an announcement...
          client.publish(mqtt_topic, "Reconnected");
          // ... and resubscribe
          //client.subscribe("inTopic");
        } else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 5 seconds");
          // Wait 5 seconds before retrying
          delay(5000);
        }
      }
    }

    void loop() {
      // put your main code here, to run repeatedly:

      float h = dht.readHumidity();
      float t = dht.readTemperature();
      if (isnan(t) || isnan(h)) {
        Serial.println("Failed to read from DHT");
      } else {
        Serial.print("Temp:");
        Serial.print(t);
        Serial.print( "*C  Humidity:");
        Serial.print(h);
        Serial.println("%");
        if (!client.connected()) {
          reconnect();
        }
        client.loop();

        //long now = millis();
        //if (now - mqtt_lastMsg > 2000) {
        //  lastMsg = now;
        //  ++mqtt_value;
        char temp[7];
        dtostrf(t,4,2,temp); //convert float to char
        snprintf (mqtt_msg, 50, "%s C", temp);
        //  Serial.print("Publish message: ");
        //  Serial.println(msg);
        //  client.publish(mqtt_topic, msg);
        //}
        char pub_topic[34];
        strcpy(pub_topic,mqtt_topic);
        strcat(pub_topic,"/temperature");
        client.publish( pub_topic , mqtt_msg);
        Serial.print("Publish: ");
        Serial.print(pub_topic);
        Serial.print(" , ");
        Serial.println(mqtt_msg);

        dtostrf(h,4,2,temp); //convert float to char
        snprintf (mqtt_msg, 50, "%s %", temp);

        strcpy(pub_topic,mqtt_topic);
        strcat(pub_topic,"/humidity");
        client.publish(pub_topic,mqtt_msg);
        Serial.print("Publish: ");
        Serial.print(pub_topic);
        Serial.print(" , ");
        Serial.println(mqtt_msg);
      }
      delay(5000);
    }