Temperature reading using ESP8266 12E
My new house is currently in construction, all is ready but the interior. Winter is coming and I want to see how efficient is the isolation and how the house will behave during winter long cold nights.
Great excuse for a new ESP project. I have already setup a raspberry pi with influx and grafana, so not all I need are some temperature readings.
I’ll be using an ESP8266 12E, bare bones, battery powered - since I don’t have yet electricity in the house.
ESP will ready the temperature and then go to sleep for 30 mins. For the WiFi connection I’m using ESP8266WiFiMulti because I want to define multiple WiFi networks where the device can connect to. I want to test it at the apartment first, make sure it works and then move it to the new house without having to write again the code on the ESP for the new WiFi network.
All sensible information are stored in a vars.ini
file like so:
[env]
build_flags =
-DWIFI_SSID_1="\"replaceme\""
-DWIFI_PASSWORD_1="\"replaceme\""
-DWIFI_SSID_2="\"replaceme\""
-DWIFI_PASSWORD_2="\"replaceme\""
-DWIFI_SSID_3="\"replaceme\""
-DWIFI_PASSWORD_3="\"replaceme\""
-DINFLUXDB_URL="\"replaceme\""
-DINFLUXDB_TOKEN="\"replaceme\""
You can define 3 WiFi networks, but if only one is needed then the other 2 can be removed. This is handled in the code using: #ifdef WIFI_SSID_1 ...
Here is the entire code that is written on the ESP device.
#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "interior-1"
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
#define INFLUXDB_ORG "Home"
#define INFLUXDB_BUCKET "weather"
// Time zone info
#define TZ_INFO "EET-2EEST,M3.5.0/3,M10.5.0/4"
// Conversion factor for micro seconds to seconds
#define uS_TO_S_FACTOR 1000000
// Time ESP32 will go to sleep (in seconds)
#define TIME_TO_SLEEP 1800
// Declare InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Declare Data point
Point sensor("temperature");
// GPIO where the DS18B20 is connected to
const int oneWireBus = 12;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature temperatureSensor(&oneWire);
void setup()
{
// Start the Serial Monitor
Serial.begin(115200);
delay(200);
// Start the DS18B20 sensor
temperatureSensor.begin();
// Setup wifi
WiFi.mode(WIFI_STA);
// add up to 3 Wifi networks if defined
#ifdef WIFI_SSID_1
wifiMulti.addAP(WIFI_SSID_1, WIFI_PASSWORD_1);
#endif
#ifdef WIFI_SSID_2
wifiMulti.addAP(WIFI_SSID_2, WIFI_PASSWORD_2);
#endif
#ifdef WIFI_SSID_3
wifiMulti.addAP(WIFI_SSID_3, WIFI_PASSWORD_3);
#endif
Serial.println("Connecting to wifi");
int wifiTries = 0;
while (wifiMulti.run() != WL_CONNECTED && wifiTries < 5)
{
wifiTries++;
Serial.print(".");
delay(100);
}
if (wifiMulti.run() == WL_CONNECTED)
{
Serial.println("Connected to wifi");
}
else
{
Serial.println("Error: Could not connect to wifi");
Serial.println("Go to sleep");
Serial.flush();
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}
Serial.println();
// Accurate time is necessary for certificate validation and writing in batches
// We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
// Syncing progress and the time will be printed to Serial.
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// Check server connection
if (client.validateConnection())
{
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
}
else
{
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
temperatureSensor.requestTemperatures();
float temperatureC = temperatureSensor.getTempCByIndex(0);
int temperatureTries = 0;
while (temperatureC == DEVICE_DISCONNECTED_C && temperatureTries < 3)
{
temperatureTries++;
delay(1000);
temperatureC = temperatureSensor.getTempCByIndex(0);
}
if (temperatureC != DEVICE_DISCONNECTED_C)
{
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println("ºC");
sensor.addTag("location", DEVICE);
// Clear fields for reusing the point. Tags will remain the same as set above.
sensor.clearFields();
// Store measured value into point
// Report RSSI of currently connected network
sensor.addField("temperature", temperatureC);
// Write point
if (!client.writePoint(sensor))
{
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
}
else
{
Serial.println("Error: Could not read temperature data");
}
Serial.println("Go to sleep");
Serial.flush();
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}
void loop()
{
// not needed for deepsleep
}