Skip to content

Instantly share code, notes, and snippets.

@cormad
Created March 5, 2016 19:36
Show Gist options
  • Save cormad/adb2667e2b2c5350027c to your computer and use it in GitHub Desktop.
Save cormad/adb2667e2b2c5350027c to your computer and use it in GitHub Desktop.
Esp8266_Nodemcu v0.9 WoodStove Monitor
/*
Catalytic Wood Stove Monitor
This sketch is a working prototype with the ESP8266 board/library.
It connects to local Wifi
It connects to an MQTT server with PubSubClient then:
- publishes "Data" to local MQTT server where it is picked up by Node-Red
Uses a Digole Display for local information
As per usual all IOT projects are never ending and evolving :)
*/
#define _Digole_Serial_I2C_ //To tell compiler compile the special communication only,
#define V33 //if the version of firmware on display is V3.3 and newer, use it
#define RED 0xE0
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <max6675.h>
#include <Wire.h>
#include <DigoleSerial.h>
//--------I2C setup
#if defined(_Digole_Serial_I2C_)
#include <Wire.h>
DigoleSerialDisp mydisp(&Wire, '\x27'); //I2C:NodeMCU V0.9: SDA (data line) is on analog input pin 4, and SCL (clock line) is on analog input pin 5
#endif
#define DHTPIN 2
#define DHTTYPE DHT22
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxx";
const char* mqtt_server = "192.168.1.128"; // Your MQTT server adress
long previousMillis = 0; // Timer loop from http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
long interval = 60000; //
int ktcSO = 12; // D5 on NodeMCU V0.9
int ktcCS = 13; // D7 on NodeMCU V0.9
int ktcCLK = 14; // D5 on NodeMCU V0.9
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
DHT dht(DHTPIN, DHTTYPE, 20);
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
char humchar[10];
char tempchar[10];
char woodfchar[10];
char woodcchar[10];
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();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) { // Make unique
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world"); // For Testing purposes
// ... and resubscribe
client.subscribe("inTopic"); // For Testing purposes
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
mydisp.begin(); //initiate serial port for display
delay(50);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Wait a few seconds between measurements.
delay(5000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
//Read thermocouple temperature in C
float DC = ktc.readCelsius();
//Read thermocouple temperature in f
float DF = ktc.readFahrenheit();
// Check if any DHT22 reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
// Check if any thermocouple reads failed and exit early (to try again).
if (isnan(DC) || isnan(DF)) {
Serial.println("Failed to read from Thermo sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
Serial.print("Woodstove C = ");
Serial.print(ktc.readCelsius());
Serial.print("\t Woodstove F = ");
Serial.println(ktc.readFahrenheit());
mydisp.clearScreen(); // Configure Display
mydisp.setColor(RED);
mydisp.setFont(123);
mydisp.setPrintPos(5,2,0);
mydisp.print(woodfchar);
delay(500);
dtostrf(t,3, 1, tempchar); // dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
dtostrf(h,3, 1, humchar);
dtostrf(DC,3, 1, woodcchar);
dtostrf(DF,3, 1, woodfchar);
client.publish("esp8266/1/outTopic", msg); // MQTT data
client.publish("esp8266/1/temperature", tempchar);
client.publish("esp8266/1/humidity", humchar);
client.publish("esp8266/1/woodC", woodcchar);
client.publish("esp8266/1/woodF", woodfchar);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment