// Wemos D1 board, connected to a battery box and a DS18B20 temperature sensor // // For temperature reading // Libraries needed: // * OneWire // * DallasTemperature // // 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 = "12345678"; #define DEVICENAME "dummy/dummy" #define TOPIC DEVICENAME"/temperature" #define TOPICVOLTAGE DEVICENAME"/voltage" #define ONLINETOPIC DEVICENAME"/online" #define MQTTSERVER IPAddress(195, 160, 169, 11) // 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; uint16_t packetId2Pub; bool packet2Ack = false; float voltage = 0.0; int raw = 0; bool ready = false; char *ftoa( double f, char *a, int precision) { long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000}; char *ret = a; long heiltal = (long)f; itoa(heiltal, a, 10); while (*a != '\0') a++; *a++ = '.'; long desimal = abs((long)((f - heiltal) * p[precision])); itoa(desimal, a, 10); return ret; } void onMqttPublish(uint16_t packetId) { // TODO: Only when both packages were acknowledged... if (packetId == packetId1Pub) { packet1Ack = true; } if (packetId == packetId2Pub) { packet2Ack = true; } if (packet1Ack && packet2Ack) { ready = true; } } void onMqttConnect(bool sessionPresent) { char buf[7]; packetId1Pub = mqttClient.publish(TOPIC, 1, true, ftoa(tempC, buf, 2)); packetId2Pub = mqttClient.publish(TOPICVOLTAGE, 1, true, ftoa(voltage, buf, 5)); } void setup() { pinMode(GNDPIN, OUTPUT); pinMode(VCCPIN, OUTPUT); digitalWrite(GNDPIN, LOW); digitalWrite(VCCPIN, HIGH); Serial.begin(115200); Serial.println("ESP-Temperature-Reader-and-MQTT-Poster-via-WiFi"); // 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); raw = analogRead(A0); // 2.428V = 739 // Je Tick = 0,003285521V voltage = raw * 0.003285521; Serial.println("Connecting to WIFI"); // Connect to WiFi WiFi.begin(ssid, password); int timeout = 0; while (WiFi.status() != WL_CONNECTED) { timeout++; if (timeout>20) { // WIFI isn't available after 10 seconds -> abort mission, mission's a failure initiateDeepSleep(); } delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Print the IP address Serial.println(WiFi.localIP()); // Publish result to MQTT mqttClient.onConnect(onMqttConnect); mqttClient.onPublish(onMqttPublish); mqttClient.setServer(MQTTSERVER, 1883); 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); timeout++; if (timeout > 40) { // MQTT isn't available after 10 seconds -> abort mission, mission's a failure initiateDeepSleep(); } Serial.print("."); } Serial.println(""); initiateDeepSleep(); } void initiateDeepSleep() { Serial.println("initDeepSleep"); ESP.deepSleep(sleepTimeS * 1000000); delay(100); }