Last active
January 24, 2021 18:14
-
-
Save Adirael/ac0d3d484a1836bed73cf9b02994488b to your computer and use it in GitHub Desktop.
Send temperature readings from an NTC 3950 thermistor to MQTT and retrieve them in Home Assistant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sensor: | |
- platform: mqtt | |
unique_id: "repbox_temperature" | |
name: "Repbox Temperature" | |
state_topic: "homeassistant/sensor/repbox/temperature" | |
unit_of_measurement: "°C" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <NTC_Thermistor.h> | |
#define wifi_ssid "Eros" | |
#define wifi_password "Protomolecule" | |
//Static IP address configuration | |
IPAddress staticIP(192, 168, 1, 36); // ESP static ip | |
IPAddress gateway(192, 168, 1, 1); // IP Address of your WiFi Router (Gateway) | |
IPAddress subnet(255, 255, 255, 0); // Subnet mask | |
IPAddress dns(192, 168, 1, 1); // DNS | |
const char* deviceName = "repbox_temp"; | |
#define mqtt_server "" | |
#define mqtt_user "" | |
#define mqtt_password "" | |
#define temperature_topic "homeassistant/sensor/repbox/temperature" | |
// Pinout | |
#define SENSOR_PIN A0 | |
// Thermistor | |
#define REFERENCE_RESISTANCE 8000 | |
#define NOMINAL_RESISTANCE 100000 | |
#define NOMINAL_TEMPERATURE 25 | |
#define B_VALUE 3950 | |
NTC_Thermistor* thermistor = NULL; | |
// Connectivity | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(wifi_ssid); | |
WiFi.hostname(deviceName); | |
WiFi.config(staticIP, subnet, gateway, dns); | |
WiFi.begin(wifi_ssid, wifi_password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqtt_server, 1883); | |
// Init thermistor | |
thermistor = new NTC_Thermistor( | |
SENSOR_PIN, | |
REFERENCE_RESISTANCE, | |
NOMINAL_RESISTANCE, | |
NOMINAL_TEMPERATURE, | |
B_VALUE | |
); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
// If you do not want to use a username and password, change next line to | |
// if (client.connect("ESP8266Client")) { | |
if (client.connect("ESP8266Client", mqtt_user, 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); | |
} | |
} | |
} | |
bool checkBound(float newValue, float prevValue, float maxDiff) { | |
return !isnan(newValue) && (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); | |
} | |
long lastMsg = 0; | |
float temp = 0.0; | |
float diff = 1.0; | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
long now = millis(); | |
if (now - lastMsg > 1000) { | |
lastMsg = now; | |
float newTemp = thermistor->readCelsius(); | |
if (checkBound(newTemp, temp, diff)) { | |
temp = newTemp; | |
Serial.print("New temperature:"); | |
Serial.println(String(temp).c_str()); | |
client.publish(temperature_topic, String(temp).c_str(), true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment