I am building a temp monitor for my garage refrigerator. Esp8266, DS18B20, OLED Display. publish MQTT.
I am fairly new to this, but have managed to piece code together and everything is working.
Right now, it displays/updates the temperature and publishes to MQTT every minute.
Ideally, I would like to update the display every few seconds, and publish one reading to MQTT every 5 minutes. Right now, the display update and MQTT code are all in the main loop (running every minute).
How would I structure my code to make this happen? I am a noob, and still learning; and apologize now for the question.....
I've included my code in the post (note: taking no credit for the code, I pieced this together based on others examples on the web, have some cleanup to do, some dupes, etc....)
Garage_Fridge_post.ino (4.63 KB)
Please, just POST the code.
In code tags.
Robin2
April 20, 2020, 3:09pm
3
Have a look at how the code is organized in Several Things at a Time
Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.
...R
Let's try this...for the code post
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Streaming.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define SLEEP_DELAY_IN_SECONDS 30
#define ONE_WIRE_BUS 14 // DS18B20 pin
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DeviceAddress sensor1 = { 0x28, 0x85, 0x1A, 0xD9, 0x37, 0x19, 0x01, 0xE8 };
DeviceAddress sensor2 = { 0x28, 0x25, 0xF6, 0x02, 0x38, 0x19, 0x01, 0x85 };
const char* ssid = "XYZ";
const char* password = "CYZ";
const char* mqtt_server = "x.x.x.x";
const char* mqtt_username = "";
const char* mqtt_password = "";
const char* mqtt_topic = "ztemp4";
String TFreeze;
String TFridge;
WiFiClient espClient;
PubSubClient client(espClient);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
char temperatureString[6];
void setup() {
// setup serial port
Serial.begin(115200);
// setup WiFi
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
// setup OneWire bus
DS18B20.begin();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//float getTemperature() {
String getTemperature() {
Serial << "Requesting DS18B20 temperature..." << endl;
//float temp;
DS18B20.requestTemperatures();
Serial.print("Sensor 1(*C): ");
Serial.print(DS18B20.getTempC(sensor1));
Serial.print(" Sensor 1(*F): ");
Serial.println(DS18B20.getTempF(sensor1));
Serial.print("Sensor 2(*C): ");
Serial.print(DS18B20.getTempC(sensor2));
Serial.print(" Sensor 2(*F): ");
Serial.println(DS18B20.getTempF(sensor2));
String TempA;
String TempAE = "Freezer: ";
String TempB;
String TempBE = "Fridge: ";
TempA = (DS18B20.getTempF(sensor1));
TempB = (DS18B20.getTempF(sensor2));
TFreeze = (DS18B20.getTempF(sensor1));
TFridge = (DS18B20.getTempF(sensor2));
String Temptotal;
Temptotal =TempAE+TempA+" "+TempBE+TempB;
//Serial.println(Temptotal);
return Temptotal;
}
void loop() {
if (!client.connected()) {
reconnect();
}
delay(10);
client.loop();
delay(10);
String temperature = getTemperature();
// convert temperature to a string with two digits before the comma and 2 digits for precision
//dtostrf(temperature, 4, 4, temperatureString);
// send temperature to the serial console
Serial.println (temperature);
// send temperature to the MQTT topic
//client.publish(mqtt_topic, String(temperature));
client.publish(mqtt_topic, String(temperature).c_str(), true);
delay(5);
// display temperature
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,5);
display.print("Freezer: ");
display.setTextSize(2);
display.setCursor(0,16);
display.print(TFreeze);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("F");
display.setTextSize(1);
display.setCursor(0,35);
display.print("Fridge: ");
display.setTextSize(2);
display.setCursor(0,48);
display.print(TFridge);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("F");
display.display();
delay(60000); //one minute
system
April 20, 2020, 3:17pm
5
This sketch has 2x blink-without-delay()s running at different speeds...
unsigned long previousShortInterval;
unsigned long previousLongInterval;
const int shortInterval = 500;
const int longInterval = 5000;
void setup()
{
Serial.begin(9600);
Serial.println("setup()...");
Serial.println(".... 2x blink without delay()s ....");
} //setup
void loop()
{
if (millis() - previousShortInterval >= (unsigned long)shortInterval)
{
previousShortInterval = millis();
Serial.println("short");
}
if (millis() - previousLongInterval >= (unsigned long)longInterval)
{
previousLongInterval = millis();
Serial.println(" long");
}
} //loop