/* Read the temperature from a DS18b20 sensor * from a ESP8266 NodeMCU * 2021-06-06 Trs Creation * 2023-07-10 Trs,Jfs Use deep sleep to reduce consumption, send value via UDP frame */ #include // https://github.com/matmunk/DS18B20 #include //#include #include #include // Configuration static const uint8_t D2 = 4; // ESP8266 -- do not forget to pullup signal to 3V3 with ~1 kOhm static const uint16_t DEEP_SLEEP_AFTER_S = 10; static const uint16_t SLEEP_FOR_S = 30 * 60; // 30 minutes static const uint16_t cSendMeasurementInterval = 2000; static const char SSID[] = ""; static const char PASS[] = ""; static const char cTargetIP[] = ""; static const uint16_t cTargetPort = 14000; static const char cUDPPacketStart[] = "{\"sender\":\"ESPTempSender\",\"compile-date\":\"" __DATE__ "\",\"temperature\":\""; static const char cUDPPacketEnd[] = "\"}"; // working variables DS18B20 ds(D2); uint8_t selected; int32_t gSleepTime; ESP8266WiFiSTAClass wifiMulti; WiFiUDP UDP; IPAddress gTarget; void setup() { // --- Preparation and Info Serial.begin(115200); Serial.setTimeout(2000); // Wait for serial to initialize. while(!Serial) { } Serial.print("\r\n" __DATE__); Serial.println(" ESP Temperature sender. Sender on D2(4), Deep-sleep with reset from D0 to RST"); // --- Init temperature sensor selected = ds.selectNext(); if (selected) { Serial.println("Sensor found"); } else { Serial.println("Sensor not found, no values will be reported!"); } // --- Connect to WiFi network to send UDP frames to the server WiFi.begin(SSID, PASS); Serial.print("Connecting to "); Serial.print(SSID); int i = 0; while (WiFi.status() != WL_CONNECTED) { delay(250); Serial.print('.'); } Serial.println('\n'); Serial.print("Connected to "); Serial.println(WiFi.SSID()); // Tell us what network we're connected to Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer gTarget.fromString(cTargetIP); Serial.print("Sending to:\t"); Serial.print(gTarget); Serial.print(" Port: "); Serial.println(cTargetPort); // --- // System will go to deep sleep after this time gSleepTime = millis() + DEEP_SLEEP_AFTER_S * 1000; } // --- void UDPSendTemperature(float temperature) { UDP.beginPacket(gTarget, cTargetPort); UDP.write(cUDPPacketStart, sizeof(cUDPPacketStart) - 1); char tempBuff[10]; auto count = snprintf(tempBuff, 9, "%.1f", temperature); UDP.write(tempBuff, count); UDP.write(cUDPPacketEnd, sizeof(cUDPPacketEnd) - 1); // close packet and send it auto result = UDP.endPacket(); // For documentation purposes, also write the data to serial port Serial.print("Sent with result "); Serial.print(result); Serial.print(": "); Serial.print(cUDPPacketStart); Serial.print(tempBuff); Serial.println(cUDPPacketEnd); } void loop() { if (selected) { float fTemp = ds.getTempC(); Serial.print("Temperature is "); Serial.print(fTemp); Serial.println(" C"); UDPSendTemperature(fTemp); } else { Serial.println("Device not found!"); } int32_t diff = gSleepTime - millis(); if (diff > 0) { Serial.print("Time/ms until sleep: "); Serial.println(diff); } else { Serial.print("Going to deep sleep for "); Serial.print(SLEEP_FOR_S); Serial.println(" seconds ..."); ESP.deepSleep(SLEEP_FOR_S * 1e6); // in us // Since a reset is executed, the program will start over with the setup() function } delay(cSendMeasurementInterval); }