MJRoBot (Marcelo Rovai)
Published © GPL3+

IoT Weather Station With RPi and ESP8266

Capturing local and remote data, logging then on an IoT platform.

IntermediateFull instructions provided8 hours18,440
IoT Weather Station With RPi and ESP8266

Things used in this project

Story

Read more

Schematics

Electrical Diagram

Code

Code snippet #4

Plain text
import Adafruit_DHT
DHT22Sensor = Adafruit_DHT.DHT22
DHTpin = 16
humidity, temperature = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)

if humidity is not None and temperature is not None:
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
    print('Failed to get reading. Try again!')

Code snippet #7

Plain text
import time
from w1thermsensor import W1ThermSensor  
ds18b20Sensor = W1ThermSensor()

while True:		
    temperature = ds18b20Sensor.get_temperature()
    print("The temperature is %s celsius" % temperature)
    time.sleep(1)

Code snippet #11

Plain text
import Adafruit_BMP.BMP085 as BMP085sensor = BMP085.BMP085()
print('Temp = {0:0.2f} *C'.format(sensor.read_temperature()))
print('Pressure = {0:0.2f} Pa'.format(sensor.read_pressure()))
print('Altitude = {0:0.2f} m'.format(sensor.read_altitude()))
print('Sealevel Pressure = {0:0.2f} Pa'.format(sensor.read_sealevel_pressure()))

Code snippet #16

Plain text
# DS18B20 1-Wire library
from w1thermsensor import W1ThermSensor 
ds18b20Sensor = W1ThermSensor() # By default GPIO 4 is used by library

# DHT22 Library 
import Adafruit_DHT
DHT22Sensor = Adafruit_DHT.DHT22
DHTpin = 16

# BMP180 library
import Adafruit_BMP.BMP085 as BMP085 
bmp180Sensor = BMP085.BMP085()

Code snippet #18

Plain text
def bmp180GetData(altitude):	
	temp = bmp180Sensor.read_temperature()
	pres = bmp180Sensor.read_pressure()
	alt =  bmp180Sensor.read_altitude()
	presSeaLevel = pres / pow(1.0 - altitude/44330.0, 5.255) 
	
	temp = round (temp, 1)
	pres = round (pres/100, 2) # absolute pressure in hPa (or mbar)
	alt = round (alt)
	presSeaLevel = round (presSeaLevel/100, 2) # absolute pressure in hPa (or mbar)

	return temp, pres, alt, presSeaLevel

Code snippet #19

Plain text
def getLocalData():
	global timeString
	global humLab
	global tempExt
	global tempLab
	global presSL
	global altLab
	global presAbs
	
	# Get time of reading
	now = datetime.datetime.now()
	timeString = now.strftime("%Y-%m-%d %H:%M")
	
	# Read Outside Temperature (1 meter distance)
	tempExt = round(ds18b20Sensor.get_temperature(), 1)
	
	tempLab, presAbs, altLab, presSL = bmp180GetData(altReal) 	
	humDHT, tempDHT = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)
	if humDHT is not None and tempDHT is not None:
		humLab = round (humDHT

Code snippet #20

Plain text
def sendDataTs():
	data = {
		"field1": tempLab, 
		"field2": tempExt, 
		"field3": humLab, 
		"field4": presSL, 
		"field5": altLab
		}
	ts.update(data)
	print ("[INFO] Data sent for 5 fields: ", tempLab, tempExt, humLab, presSL, altLab)

Code snippet #23

Plain text
/* NodeMCU ESP12-E */
#include <ESP8266WiFi.h>
WiFiClient client;
const char* MY_SSID = "ENTER WITH YOUR SSDID";
const char* MY_PWD = "ENTER WITH YOUR PASSWORD";

/* Thinkspeak */
const char* TS_SERVER = "api.thingspeak.com";
String TS_API_KEY ="ENTER WITH YOUR WRITE KEY";

Code snippet #25

Plain text
void setup() 
{
  ...
  Serial.begin(115200);
  delay(10);
  ...
  connectWifi();
  timer.setInterval(60000L, sendDataTS);
  ...
}

Code snippet #26

Plain text
void loop() 
{
  ...
  timer.run(); // Initiates SimpleTimer
}

Code snippet #27

Plain text
/***************************************************
 * Connecting WiFi
 **************************************************/
void connectWifi()
{
  Serial.print("Connecting to "+ *MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.println("");  
}

Code snippet #28

Plain text
***************************************************
 * Sending Data to Thinkspeak Channel
 **************************************************/
void sendDataTS(void)
{
   if (client.connect(TS_SERVER, 80)) 
   { 
     String postStr = TS_API_KEY;
     postStr += "&field6=";
     postStr += String(temp);
     postStr += "&field7=";
     postStr += String(hum);
     postStr += "&field8=";
     postStr += String(dataSensorUV);
     postStr += "\r\n\r\n";
   
     client.print("POST /update HTTP/1.1\n");
     client.print("Host: api.thingspeak.com\n");
     client.print("Connection: close\n");
     client.print("X-THINGSPEAKAPIKEY: " + TS_API_KEY + "\n");
     client.print("Content-Type: application/x-www-form-urlencoded\n");
     client.print("Content-Length: ");
     client.print(postStr.length());
     client.print("\n\n");
     client.print(postStr);
     delay(1000); 
   }
   sent++;
   client.stop();
}

Github

https://github.com/adafruit/Adafruit_Python_DHT

Github file

https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/DHT22_test.py

Github

https://github.com/adafruit/Adafruit_Python_BMP

Github file

https://github.com/Mjrovai/RPi-NodeMCU-Weather-Station/blob/master/IoT%20Weather%20Station/localDataToTS_v1_EXT.py

Github

https://github.com/mchwalisz/thingspeak

Credits

MJRoBot (Marcelo Rovai)

MJRoBot (Marcelo Rovai)

66 projects • 956 followers
Professor, Engineer, MBA, Master in Data Science. Writes about Electronics with a focus on Physical Computing, IoT, ML, TinyML and Robotics.

Comments