diff --git a/esp-temperature/esp-temperature.ino b/esp-temperature/esp-temperature.ino index 794a2c1..8297a12 100644 --- a/esp-temperature/esp-temperature.ino +++ b/esp-temperature/esp-temperature.ino @@ -1,6 +1,6 @@ // Wemos D1 board, connected to a battery box and a DS18B20 temperature sensor // - + // For temperature reading // Libraries needed: // * OneWire @@ -8,37 +8,37 @@ // // Pinout: https://wiki.wemos.cc/products:d1:d1_mini // D0 = GPIO16 --> Connect D0 to RST for Deep Sleep-Wakeup - + #include #include - -const char* ssid = "CTDO-IoT"; -const char* password = ""; - -#define DEVICENAME "haus/bad" + +const char* ssid = "ENTER_YOUR_SSID_HERE"; +const char* password = "ENTER_YOUR_WLAN_PASS_HERE"; + +#define DEVICENAME "maintopic/devicename" #define TOPIC DEVICENAME"/temperature" #define ONLINETOPIC DEVICENAME"/online" -#define MQTTSERVER IPAddress(195, 160, 169, 11) // raum.ctdo.de: 195.160.169.11 +#define MQTTSERVER IPAddress(37, 187, 106, 16) // test.mosquitto.org = 37.187.106.16 const int sleepTimeS = 300; // Reduce this value for debugging. Increase if you want more battery life - + #define VCCPIN D7 #define ONE_WIRE_BUS D6 #define GNDPIN D5 - + OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); - + float tempC; - + // For WLAN & MQTT #include #include AsyncMqttClient mqttClient; uint16_t packetId1Pub; bool packet1Ack = false; - + bool ready = false; - + char *ftoa( double f, char *a, int precision) { long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000}; @@ -52,7 +52,7 @@ char *ftoa( double f, char *a, int precision) itoa(desimal, a, 10); return ret; } - + void onMqttPublish(uint16_t packetId) { Serial.println("** Publish acknowledged **"); Serial.print(" packetId: "); @@ -65,13 +65,13 @@ void onMqttPublish(uint16_t packetId) { ready = true; } } - + void onMqttConnect(bool sessionPresent) { char buf[7]; packetId1Pub = mqttClient.publish(TOPIC, 1, true, ftoa(tempC, buf, 2)); } - - + + void setup() { pinMode(GNDPIN, OUTPUT); pinMode(VCCPIN, OUTPUT); @@ -82,15 +82,18 @@ void setup() { // Start up the sensors library sensors.begin(); } - + void loop() { // Send the command to get temperature readings + Serial.println("Requesting Temperature"); sensors.requestTemperatures(); - + // You can have more than one DS18B20 on the same bus. // 0 refers to the first IC on the wire + Serial.println("Requesting Temperature from Device 0"); tempC = sensors.getTempCByIndex(0); - + +Serial.println("Connecting to WIFI"); // Connect to WiFi WiFi.begin(ssid, password); int timeout = 0; @@ -105,10 +108,10 @@ void loop() { } Serial.println(""); Serial.println("WiFi connected"); - + // Print the IP address Serial.println(WiFi.localIP()); - + // Publish result to MQTT mqttClient.onConnect(onMqttConnect); mqttClient.onPublish(onMqttPublish); @@ -116,7 +119,7 @@ void loop() { mqttClient.setKeepAlive(5).setCleanSession(false).setWill(ONLINETOPIC, 2, true, "no"); // .setCredentials("user", "pass").setClientId(DEVICENAME); Serial.println("Connecting to MQTT..."); mqttClient.connect(); - + timeout = 0; while (!ready) { delay(250); @@ -129,11 +132,9 @@ void loop() { Serial.print("."); } Serial.println(""); - - initiateDeepSleep(); } - + void initiateDeepSleep() { ESP.deepSleep(sleepTimeS * 1000000);