---
# System prepended metadata

title: Weather info collector

---

# Weather info collector

By Henric (hh222yr)

This project aim to collect weather data (Temprature, Humidity and Pressure) and present it over time online. Using a ESP32 and a 2 diffrent sensors (DHT11 & BME280) and sending the datoa over Wi-Fi to ThingSpeak to be presented on a Graph over a coupel of days.

My Graph: https://henric.xyz/1DT305-project/

By following the tutorial you should be able to replicate the project in a day or so.


## Objective

I chose this project because I couldn't get a better idé of something more interesting to make at the time of creating it and it would be a bit intresting to see how the temrature changed on the balcony over the summer.

The Project collects Temprature, Humidity and Pressure and presents it online.
If collect the data from a DHT11 (Temprature and Humidity), a BME280 (Temprature, Humidity and Pressure) and from the ESP32 (Build in Temprature sensor gives the temprature of the core).

Hopefully the insight the project could give is a simple understanding of sensor colection and more interesing the way that data gets online and is presented.


## Material

Needed Hardware:

    
- [ ] ESP32 DEVKIT 1 (Any ESP32 Gen1 should work) : I Bought it on [Aliexpress](https://www.aliexpress.com/) for 82 sek.
It's a Development board with a ESP32 attached that is breadboard compatible (At least with 2 of them next to each other)
![](https://i.imgur.com/TEgsMiM.jpg)

- [ ] DHT11 : I Bought it on [Aliexpress](https://www.aliexpress.com/) for 8 sek.
It's a simple Temprature and Humidity sensor that have a 1°C Resolution
![](https://i.imgur.com/XtcDSEG.png)


- [ ] BME280 : I Bought it on [Aliexpress](https://www.aliexpress.com/) for 35 sek.
It's a Temprature, Humidity and Pressure sensor that have a 0.01°C Resolution
![](https://i.imgur.com/hra8VvY.png)

- [ ] A couple of dupont cables : I Bought 120 of them on [Aliexpress](https://www.aliexpress.com/) for 25 sek.
It's wires with simple connectors on them for use in breadborads
- [ ] Mini Breadboard / Half Breadboard x 2 : I Bought a pack of 10 on aliexpress for 90 sek.
They are for putting electrical component son them. I put 2 of them next to each other with a single power rail in beween them.
- [ ] Micro USB-B Cable : I got it from some phone at some point but you can find them about anyware nowadays
It's a USB-A Plug to a Micro USB-B Plug connected by a cable with 4 cores using the USB 2.0 protocol.
- [ ] 5V USB Power Bank : I Bought it on [NetOnNet ](https://www.netonnet.se/) for 50 sek.
It gives 5V 2A from a USB-A port, any generic USB power bank should work.


## Setup

### Software

Links:

    Notepad++ (https://notepad-plus-plus.org/)
    Python (https://www.python.org/downloads/)
    MicroPython (https://docs.micropython.org/en/latest/esp32/tutorial/intro.html)
    Ampy (https://github.com/scientifichackers/ampy)
    ThingSpeak (https://thingspeak.com/)
    main (https://github.com/Henric-H/1DT305-project/blob/master/main.py)
    BME280.py (https://github.com/Henric-H/1DT305-project/blob/master/BME280.py) [SOURCE:https://github.com/RuiSantosdotme/ESP-MicroPython/blob/master/code/WiFi/HTTP_Client_IFTTT_BME280/BME280.py]
    plotter.m (https://github.com/Henric-H/1DT305-project/blob/master/Plotter.m)

Steps:

1. Install Notepad++ (Optional you can use Notepad)
2. Install Python
3. Connect you ESP32 to your computer (This is also explined in the setup guide for the next step)
4. Follow guide to get and install micropyhon on your processor (Skip WEB-Repl we won't use it)
5. Create a new channel on ThingSpeak
6. Add a total of 6 Fields to your ThingSpeak channel (Name don't Matter)
7. Download `main.py`, `BME280.py` and `plotter.m`
8. Add your ThingSpeak Channel ID and ReadKey to `plotter.m`
9. Create a New Matlab Visualisation (Blank) and paste the code in `plotter.m` and make public
10. Open `main.py` and change the network credentials and your ThingSpeak Credentials to match yours
11. Install Ampy (See Linked guide) (Used for Transfering the files to the ESP32)
12. Reconect you ESP32 to your computer if you disconected it.
13. Find what COM port you have conected your device to
14. Upload `main.py` and `BME280.py` to the ESP32 using Ampy
15. Disconect ESP32 from Computer
16. Continue to Hardware Setup

### Hardware

Connect everything like this: (If you can't see ground lead switch to light theme on website/browser)
![](https://i.imgur.com/AZtWayB.jpg)


And connect a power bank to the micro USB-B port

- [ ] 3.3V to VDD on DHT11 and VIN in BME280
- [ ] GND to GND on DHT11 and GND on BME280
- [ ] D4 to Data on DHT11
- [ ] D21 to SDA on BME280
- [ ] D23 to SCL on BME280
- [ ] Power Bank to Mico USB-B port (This provides power)

It uses 2500mA in about 5 days (Power consuption could be lowered dramaticlly by removing powerleds and using a dedicated battery with a LDO)


## Platform

I chose to use ThingSpeak, it can receive data and present it on the web.

I specificaly picked ThingSpeak because it's Free (To a extent), uses a bakend with Matlab (that I already know), can take data by MQTT and handle timestamps itself.

## The code

`main.py`
```python
#importing all I need in this program.
import dht
import machine
from machine import Pin, I2C
import network
import esp32
import time
from umqtt.simple import MQTTClient
import re
import utime

# Library for BME280 found at: https://github.com/RuiSantosdotme/ESP-MicroPython/blob/master/code/WiFi/HTTP_Client_IFTTT_BME280/BME280.py
import BME280


# I2C Pin assignment
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)

# Put your network stuff here.
ESSID = 'YourNetwordName'
Password = 'YourPassword'

# Put ThingSpeak stuff here.
ThingSpeak_API_Key = "YourWriteKey"
ThingSpeak_Channel_ID = "YourChannelID"
MQTT_SERVER = "mqtt.thingspeak.com"

# Set up MQTT clent
MQTT_client = MQTTClient("umqtt_client", MQTT_SERVER)
    
# Create Topic for MQTT
MQTT_topic = "channels/{0}/publish/{1}".format(ThingSpeak_Channel_ID, ThingSpeak_API_Key)



# This is the thing that runs the other stuff cause it's main...
def main():

    # check if the device woke from a deep sleep
    if machine.reset_cause() == machine.DEEPSLEEP_RESET:
        print('woke from a deep sleep')

    print('Activate all systems')

    temp_DGT11, hum_DGT11 = get_data_DGT11(4)
    
    temp_BME280, hum_BME280, pres_BME280 = get_data_BME280()
    
    temp_core = get_data_ESP32()
    
    # Enable Network and Send Data then turn it off
    send_data(temp_DGT11, hum_DGT11, temp_core, temp_BME280, hum_BME280, pres_BME280)
    
    # Going to sleep for a couple of minutes
    print('Going to sleep')
    #time.sleep(240)
    machine.deepsleep(240000)



# This Function gets a pin id and return with the temperature and humidity from a DHT11 sensor
def get_data_ESP32():

    # Get Temperature of the ESP itself and convert to Celsius
    temp_core = ( esp32.raw_temperature() - 32 ) / 1.8
    print('Temperature in Core in Celsius')
    print(temp_core)
    
    return temp_core



# This Function gets a pin id and return with the temperature and humidity from a DHT11 sensor
def get_data_DGT11(x):

    print('Setting up and reading Temperature and Humidity')
    sens_t_h = dht.DHT11(machine.Pin(x))

    sens_t_h.measure()
    temp_DGT11 = sens_t_h.temperature()
    print('Temperature in Celsius')
    print(temp_DGT11)
    hum_DGT11 = sens_t_h.humidity()
    print('Humidity Relative Percentage')
    print(hum_DGT11)
    
    return temp_DGT11, hum_DGT11
    
    
    
# This is for reading the data from the BME280
def get_data_BME280():
    
    # Gets the Data from the BME280
    bme = BME280.BME280(i2c=i2c)
    temp_BME280 = bme.temperature
    hum_BME280 = bme.humidity
    pres_BME280 = bme.pressure
    
    # Removes extra stuff after the Data because ThingSpeak don't like it
    temp_BME280 = re.sub('C', '', temp_BME280)
    hum_BME280 = re.sub('%', '', hum_BME280)
    pres_BME280 = re.sub('hPa', '', pres_BME280)
    
    print('Data from BME280')
    print('Temperature: ', temp_BME280)
    print('Humidity: ', hum_BME280)
    print('Pressure: ', pres_BME280)
    
    return temp_BME280, hum_BME280, pres_BME280
    
    
    
 
# This is the Network stuff connecting and sending data and then disconecting the network
def send_data(temp_DGT11, hum_DGT11, temp_core, temp_BME280, hum_BME280, pres_BME280):
    
    print('Setting up network')
    wlan1 = network.WLAN(network.STA_IF)
    wlan1.active(True)
    
    # I know it allways return False the first time, but this is easier and makes sure conection is made
    # and the slight delay this makes don't realy matter.
    wlan1.connect(ESSID, Password)
    time_at_connect = utime.ticks_ms()
    while wlan1.isconnected() == False:
        pass
        if utime.ticks_diff(utime.ticks_ms(), time_at_connect) > 60000:
            print('Network error can not connect')
            break
    
    print('Established Network Access')
    
    # MQTT stuff for Conecting to ThingSpeak
    
    print('Create Data Package to ThingSpeak')
    # Combine Data for MQTT
    MQTT_data = "field1={0}&field2={1}&field3={2}&field4={3}&field5={4}&field6={5}".format(temp_DGT11, hum_DGT11, temp_core, temp_BME280, hum_BME280, pres_BME280)
    
    print('Send data to ThingSpeak')
    #Connect to ThingSpeak over MQTT
    MQTT_client.connect()
    MQTT_client.publish(MQTT_topic, MQTT_data)
    MQTT_client.disconnect() 
    
    print('A Short Pause')
    time.sleep(20)
    
    # Shut down Network
    #wlan1.active(False)
    #time.sleep(5)
    
    
    
# Endless Loop. Did it here because i did not want to push the main longer to the right.
while True:
    main()

```

`plotter.m`
```matlab
% Enter your MATLAB code below
readChId =              % Your Channel ID					
readKey = '' %Your Read API Key
[Data1, Time1] = thingSpeakRead(readChId,'fields',[1,4,3,2,5],...
    'NumPoints',500,'ReadKey',readKey);

[Data2, Time2] = thingSpeakRead(readChId,'fields',[6],...
    'NumPoints',500,'ReadKey',readKey);


%% title('Temperature and Humidity'); %%


%% Temperature and Humidity Plots %%
%%yyaxis left;%%
plot(Time1, Data1);
ylabel('Temprature and Humidity');
xlabel('Date & Time (GMT+0)');

%% Pressure Plots %%
yyaxis right;
plot(Time2, Data2);
ylabel('Pressure');

legend({'Temp DDT11 (C)','Temp BME280 (C)','Temp ESP32 (C)','Hum DDT11 (%)','Hum BME280 (%)','Pres BME280 (hPa)'});
legend('Location','best')

grid on;
```


## Data

### Transmission

The data is sent about every 4-6 minut, by Wi-Fi using MQTT.

The ESP32 goes into Deep Sleep to save power for 4 minutes and then takes 30sek to 2min to send the data or detect it can't connect to Wi-Fi.
I used Wi-Fi because i have the sensor on my balcony and I don't wand to clog up the Lo-Ra Network it I don't need to.
I chose MQTT because I was a bit familiar with the protocol before bacause I have used it in C and C++ before.
As I stated before battery consuption can become much batter if desoldering all power leds and bypassing the serial minitor and using a dedicated battery and a LDO with a potential addition of a charge circuit and a solar panel.
And I chose to always have the Graph in GPT+0 to remove the problem with Summer and Winter Time.

### Presenting data

A Picture of the Graph that have Temprature and Humidity read from the left Y Axis and Pressure read from the Right one:
![](https://i.imgur.com/JEXjmOY.png)
Link to the Live Graph: https://henric.xyz/1DT305-project/
More Graphs but less compact at: https://thingspeak.com/channels/1080128
Updated every 4-6 mins (Need to reloade page)
I use ThingSpeak because it was convinient as stated before
I could read data and automate say a AC if I had one.

## Finalizing the design

If I was planing on keeping the electronic in this state after the end of the course then I would solder it to a PCB (perf/proto board) and design a 3D printed case using Fusion 360 and print it with my 3D printer but I put it on a breadboard considering it just sits on a shelf on my Balcony and collects data.

![](https://i.imgur.com/jkDo354.jpg)

In the end i think the project serves it purpose and have a quite good battery time for my usage. If was fun to try and use Micropyhon because the slightly diffrent midset then programmig copared to C or C++. If I ware to Finalize the project one day I would hive it a propper case and rewrite thje Code in C or C++ and switch to a ESP8266 because I don't need the processing capability pf the ESP32 for such simple data collection.