ESP8266 Weather Station / Temperature Sensor

Introduction

ESP’s are great to give your sensors the possibility to extract the sensor data, because of the on-board WIFI chip, which can connect to your network.
Imagine you have multiple sensors, and you would like to access the measured data and show it in Home Assistant. You can either spin up a web server on your ESP and pull the data or send data from your ESP to your Home Assistant.

The advantage of the push strategy is power saving through deep sleep mode of ESP, which will be explained later.

Components

In my case the + and - pin label was incorrect and I had to swap them. So that the orange wire is attached to + (and not -).

Weather Station

The temperature sensor will be connected with the ESP over the GPIO’s. The ESP itself will send over WIFI the data (temperature) to your Home Assistant MQTT (Message Queuing Telemetry Transport). The Mosquitto broker Add-on (MQTT) need to be activated on your Home Assistant, in Add-on Store Overview. Don’t forget to set the username and password in MQTT config.

Assembling

WIREPIN
yellowG (ground)
orange3V3
cyanA0

Code

Don’t forget to set WIFI SSID, WIFI Password and also MQTT Server IP, username and password in the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

#define MQTT_VERSION MQTT_VERSION_3_1_1

// Wifi: SSID and password
const char* WIFI_SSID = "WifiName";
const char* WIFI_PASSWORD = "WifiPassword";

// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "office_temp";
const PROGMEM char* MQTT_SERVER_IP = "192.168.*.*";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "mqttUsername";
const PROGMEM char* MQTT_PASSWORD = "mqttPassword";

// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "office/sensor1";

// sleeping time
const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 600; // 10 minutes x 60 seconds

// Sensor and led
const int led = 13;
const int sensorPin = 0; // select the input pin for the potentiometer

WiFiClient wifiClient;
PubSubClient client(wifiClient);

// function called to publish the temperature
void publishData(String p_temperature) {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["temperature"] = p_temperature;
root.prettyPrintTo(Serial);
Serial.println("");
char data[200];
root.printTo(data, root.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC, data, true);
yield();
}

// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

double Thermistor(int RawADC) {
double Temp;
Temp = log(((10240000 / RawADC) - 9000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}

void setup() {
// init the serial
Serial.begin(115200);

// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.println("INFO: IP address: ");
Serial.println(WiFi.localIP());

// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

digitalWrite(led, 1);
publishData(String(Thermistor(analogRead(sensorPin)), 2));
digitalWrite(led, 0);

Serial.println("INFO: Closing the MQTT connection");
client.disconnect();

Serial.println("INFO: Closing the Wifi connection");
WiFi.disconnect();

ESP.deepSleep(SLEEPING_TIME_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
delay(500); // wait for deep sleep to happen
}

Home Assistant Config

To see the temperature in Home Assistant web interface, we have to add the sensor to configuration.yaml like that:

1
2
3
4
5
6
7
# Sensors
sensor:
- platform: mqtt
name: Office Temperature
state_topic: "office/sensor1"
value_template: "{{ value_json.temperature }}"
unit_of_measurement: '°C'

It will look like that in your interface:

Deep Sleep

ESP provides an excellent power saving feature through the deep sleep mode.
Deep sleep mode turns off everything except the RST pin, which is required to turn the ESP on again.
The following steps will be done within the code:

  • ESP will be started
  • Connecting to WIFI
  • Measure the temperature with the sensor
  • Send the data to MQTT
  • Go in deep sleep mode (turn everything off)
  • Restart the ESP once the timer ends (which is set in the code)

You need an additional wire from RST to D0 (purple wire in my case) to have that feature work.