Skip to content

Instantly share code, notes, and snippets.

@dceejay
Last active January 5, 2021 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dceejay/8561e107e81cce862287ea3a3642845f to your computer and use it in GitHub Desktop.
Save dceejay/8561e107e81cce862287ea3a3642845f to your computer and use it in GitHub Desktop.
Wemos temperature monitor
#include <Arduino.h>
/* DS18B20 Temperature sensors - Deep Sleep - with Wifi and MQTT
*
* Connections:
* D0 -- RST
* Uses the folowing platformio.ini file
[env:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
monitor_speed = 115200
lib_deps =
lolhens/ESP8266MQTTClient@^1.1.1
bblanchon/ArduinoJson@^6.17.2
paulstoffregen/OneWire@^2.3.5
milesburton/DallasTemperature@^3.9.1
*/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>
#include <ESP8266MQTTClient.h>
#include <ArduinoJson.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Update these with values suitable for your network.
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqtt_broker = "mqtt://192.168.3.1:1883"; // Your MQTT broker
const char* mqtt_topic = "wemosds"; // An MQTT topic for messages to be sent to.
// Data wire is plugged into gpio 4 on the Wemos
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress Thermometer;
uint8_t sensor1[8] = { 0x28, 0xFF, 0x5E, 0x1C, 0x92, 0x16, 0x05, 0x4B };
uint8_t sensor2[8] = { 0x28, 0xFF, 0xA3, 0xBA, 0x01, 0x15, 0x03, 0xE5 };
uint8_t sensor3[8] = { 0x28, 0xEE, 0xAD, 0xD1, 0x01, 0x00, 0x00, 0xDE };
uint8_t sensor4[8] = { 0x28, 0x31, 0xB2, 0xD1, 0x01, 0x00, 0x00, 0x2D };
uint8_t sensor5[8] = { 0x28, 0xAF, 0x78, 0xD1, 0x01, 0x00, 0x00, 0x10 };
uint8_t sensor6[8] = { 0x28, 0xA5, 0x69, 0xEC, 0x00, 0x00, 0x00, 0x10 };
MQTTClient mqtt;
DynamicJsonDocument doc(256);
// sleep for this many seconds
const int sleepSeconds = 60;
int deviceCount = 0;
float tempC;
bool flag = false;
int retry = 0;
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (deviceAddress[i] < 0x10) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
if (i < 7) Serial.print(", ");
}
Serial.println("");
}
void surveybus() {
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
for (int i = 0; i < deviceCount; i++) {
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
sensors.getAddress(Thermometer, i);
printAddress(Thermometer);
}
}
void readall() {
// Send command to all the sensors for temperature conversion
sensors.requestTemperatures();
// Display temperature from each sensor
for (int i = 0; i < deviceCount; i++) {
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.println("C");
}
}
void setup() {
// Connect D0 to RST to wake up
pinMode(D0, WAKEUP_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ONE_WIRE_BUS, OUTPUT);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(115200);
Serial.println("\r\nTemp: Initialise.");
sensors.begin();
//surveybus(); // uncomment this to print out a list of all devices found
//readall();
sensors.requestTemperatures();
float tempC;
tempC = sensors.getTempC(sensor1);
doc["outdoor"] = tempC;
tempC = sensors.getTempC(sensor2);
if (tempC != -127 && tempC != 85) {
doc["spare"] = tempC;
}
tempC = sensors.getTempC(sensor3);
doc["boilerout"] = tempC;
tempC = sensors.getTempC(sensor4);
doc["boilerin"] = tempC;
tempC = sensors.getTempC(sensor6);
doc["indoor"] = tempC;
Serial.print("Temp: Read ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" sensors");
Serial.println("Wifi: Initialise.");
digitalWrite(LED_BUILTIN, HIGH);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
retry += 1;
if (retry == 15) {
Serial.println("Wifi: Fail");
ESP.deepSleep(sleepSeconds * 1000000);
}
}
//if you get here you have connected to the WiFi
Serial.print("Wifi: ");
Serial.println(WiFi.localIP());// Print the IP address
mqtt.onConnect([]() {
//Serial.println("MQTT: Connected");
String output;
serializeJson(doc, output);
mqtt.publish(mqtt_topic, output);
Serial.print("MQTT: Publish: ");
Serial.println(output);
flag = true;
});
mqtt.onDisconnect([]() {
Serial.printf("MQTT: Disconnected\r\n");
});
// Serial.println("MQTT: Initialise");
mqtt.begin(mqtt_broker);
}
void loop() {
mqtt.handle();
if (flag == true) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("ZZZZ:");
ESP.deepSleep(sleepSeconds * 1000000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment