/*************************************************** Adafruit MQTT Library ESP8266 Example Must use ESP8266 Arduino from: https://github.com/esp8266/Arduino Written by Tony DiCola for Adafruit Industries modified substantially by Kim Warwick-Oliver for own purposes. MIT license, all text above must be included in any redistribution ****************************************************/ #include #include #include #include #include #include "Adafruit_SSD1306.h" #include #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include #include #include SHT3X sht30(0x45); // sht30 address float humidity = 0.00 ; float temperature = 0.00 ; String dataString; char charBuf[100]; unsigned long previousMillis = 0; const long interval = 2000; #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 // use 8883 for SSL #define AIO_USERNAME "kimtest" #define AIO_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // Put Your adafruit ID here /************ Global State (you don't need to change this!) ******************/ // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // or... use WiFiFlientSecure for SSL //WiFiClientSecure client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); /****************************** Feeds ***************************************/ // Setup a feed called 'temp' for publishing. Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temp"); // Setup a feed called 'hum' for publishing. Adafruit_MQTT_Publish hum = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humidity"); /*************************** Sketch Code ************************************/ // Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). void MQTT_connect(); void setup() { Serial.begin(115200); Serial.println(); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //exit after config instead of connecting wifiManager.setBreakAfterConfig(true); //reset settings - for testing //wifiManager.resetSettings(); //tries to connect to last known settings //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" with password "password" //and goes into a blocking loop awaiting configuration if (!wifiManager.autoConnect("AutoConnectAP", "password")) { Serial.println("failed to connect, we should reset as see if it connects"); delay(3000); ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); Serial.println("local ip"); Serial.println(WiFi.localIP()); } void getTemperature() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval) { previousMillis = currentMillis; sht30.get(); humidity = (sht30.humidity); temperature = (sht30.cTemp); } } void loop() { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); getTemperature(); // Call routine to get data from SHT30 // Now we can publish temperature Serial.print(F("\nSending Temperature ")); Serial.print(temperature); Serial.print("..."); if (! temp.publish(temperature)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } // Now we can publish humidity Serial.print(F("\nSending Humidity ")); Serial.print(humidity); Serial.print("..."); if (! hum.publish(humidity)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } // ping the server to keep the mqtt connection alive // NOT required if you are publishing once every KEEPALIVE seconds /* if(! mqtt.ping()) { mqtt.disconnect(); } */ Serial.print(F("\nSleeping ..... ")); ESP.deepSleep(300000000); } // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); }