This undertaking exhibits easy methods to construct a knowledge logger that publishes temperature, humidity and strain readings each 10 minutes to a Google Sheets spreadsheet using an ESP8266 12-E. Between sensor readings the ESP is in deep sleep mode and solely consumes 7μA. The ESP board is programmed using MicroPython and we use the IFTTT Webhooks service to combine the undertaking with Google sheets.
This venture can also be suitable with the ESP32. You simply should make a number of modifications within the code (we’ll present you what it’s essential to do).
All through this venture, you’ll study the next ideas:
- Learn temperature, humidity and strain with the BME280 and ESP8266/ESP32 using MicroPython;
- Make HTTP submit requests in MicroPython to ship sensor readings to a third-party service;
- Use IFTTT Webhooks providers to save lots of sensor readings in a Google Sheets spreadsheet;
- Put the ESP8266/ESP32 in deep sleep mode and wake it up with a timer.
- 1 MicroPython firmware
- 2 Creating Your IFTTT Account
- 3 Creating an Applet
- 4 Testing Your Applet
- 5 Elements Required
- 6 Importing Code to the ESP8266 12-E Chip
- 7 Putting in the BME280 library
- 8 Code
- 9 How the Code Works
- 10 Constructing the Circuit
- 11 Demonstration
- 12 Power Consumption
- 13 Powering the ESP8266/ESP32 with Batteries
- 14 Wrapping Up
On this challenge we’ll program the ESP8266/ESP32 with MicroPython firmware. To program the ESP8266 and ESP32 with MicroPython, we use uPyCraft IDE as a programming setting (you need to use some other appropriate programming surroundings). Comply with the subsequent tutorials to put in uPyCraft IDE and flash MicroPython firmware in your board:
Creating Your IFTTT Account
On this undertaking we’ll publish the sensor readings in a Google Sheets spreadsheet. To try this, use IFTTT Webhooks providers. So, it is advisable create a free account on IFTTT.
Go the official website ifttt.com, enter your e mail and full your account to get began.
Creating an Applet
Subsequent, it is advisable to create a brand new Applet. Comply with the subsequent steps to create a brand new Applet:
1) Go to “My Applets“and create a new Applet by clicking the “New Applet” button.
2) Click on on the “this” phrase that’s in a blue shade – as highlighted within the determine under.
three) Seek for the “Webhooks” service and choose the Webhooks icon.
four) Select the “Receive a web request” set off.
5) Give a reputation to the occasion. On this case, we’ll be using “bme_280_readings” and we advocate using that very same occasion identify as proven within the determine under. After that, click on the “Create trigger” button.
6) Click on the “that” phrase to proceed.
7) Seek for the “Google Sheets” service, and choose the Google Sheets icon.
eight) In the event you haven’t related with the Google Sheets service but, you have to click on the “Connect” button.
9) Select the “Add a row to spreadsheet” motion.
10) Then, full the motion fields. Give the spreadsheet a reputation, and depart the opposite fields as default. Lastly, click on the “Create action” button.
11) Your Applet ought to be created after you press the “Finish” button.
Testing Your Applet
Earlier than continuing with the undertaking, it is rather necessary to check your applet first. Comply with the subsequent steps to check your Applet.
1) Go to the Webhooks Service web page, and click on the “Documentation” button.
2) A web page as proven within the following determine will seem. The web page exhibits your distinctive API key. You shouldn’t share your distinctive API key with anybody.
Fill the “To trigger an Event” part as proven under – it’s highlighted with purple rectangles. Then, click on the “Test it” button.
Observe: save your IFTTT API key since you’ll want it later on this challenge.
three) The occasion must be efficiently triggered, and also you’ll get a inexperienced message as proven under saying “Occasion has been triggered“.
four) Go to your Google Drive. The IFTTT service ought to have created a folder referred to as “IFTTT” with the “MakerWebooks/bme280” folder inside. Inside this folder, you’ll discover the Bme_280_readings spreadsheet.
5) Open the spreadsheet. You must have the values you’ve crammed beforehand to check the Applet.
Proceed studying this tutorial to see how you can combine the IFTTT Google Sheets service together with your ESP8266 or ESP32.
Right here’s an inventory of elements you might want to construct the circuit for this venture:
For this venture we’ll be using the ESP8266 12-E as a result of it consumes much less energy in deep sleep mode (it doesn’t have a built-in programmer). You can even use an ESP8266 NodeMCU Package or an ESP32 improvement board.
You should use the previous hyperlinks or go on to MakerAdvisor.com/instruments to seek out all of the elements in your tasks at one of the best worth!
Importing Code to the ESP8266 12-E Chip
Importing code to the ESP-12E requires establishing a serial communication between your ESP8266 and an FTDI Programmer as proven within the schematic diagram under.
- RX – TX
- TX – RX
- VCC – VCC
- VCC – EN
- GND – GND
- GND – GPIO15
- GND – GPIO0
Putting in the BME280 library
The library to learn from the BME280 sensor isn’t a part of the usual MicroPython firmware. So, it is advisable add the library to your ESP8266/ESP32 board.
In uPyCraft IDE, create a brand new file by urgent the New File button (1). Copy the BME280 library code into it. The BME280 library code may be discovered within the following hyperlink:
Save the file by urgent the Save button (2).
Name this new file “BME280.py” and press okay.
Click on the Obtain and Run button.
Observe: should you’re having hassle importing a brand new script to your ESP board, you may have to re-flash the MicroPython firmware in your board: Flash/Add MicroPython Firmware to ESP. When the ESP is in deep sleep mode, you possibly can’t add new scripts. Be sure to reset your board, press the Cease button in uPyCraft IDE instantly to catch it awake, so you possibly can entry the saved scripts and add new ones.
The file ought to be saved on the system folder with the identify “BME280.py” as highlighted within the determine under.
Now, you need to use the library functionalities in your code by importing the library.
After importing the BME2820.py file to your ESP8266 or ESP32, copy the next code to the boot.py file. The boot.py file runs as soon as on boot (it means each time the ESP8266/ESP32 wakes up).
Earlier than importing the code to your board, it’s worthwhile to embrace your personal community credentials and your distinctive IFTTT API key. Proceed studying to see how to try this.
How the Code Works
Let’s take a fast take a look at the code and see the way it works.
You begin by importing the required modules. To work together with the GPIOs and skim knowledge from the sensor by way of I2C communication, you have to import the Pin and I2C courses from the machine module. You additionally import the entire machine module to have the ability to use features associated with deep sleep.
from machine import Pin, I2C
It’s essential to import the BME280 library that you simply beforehand uploaded to the board.
To hook up with your community, you could import the community module.
You want the urequests library. This library permits you to make HTTP requests. On this instance, we’ll make a request to the IFTTT providers to publish the sensor readings to a Google Sheets spreadsheet.
Lastly, import the sleep perform from the time module to have the ability to add delays to the code.
from time import sleep
Setting your community credentials
It’s essential to add your SSID and password within the following variables:
ssid = ‘REPLACE_WITH_YOUR_SSID’
password = ‘REPLACE_WITH_YOUR_PASSWORD’
Together with your IFTTT API key
Insert your distinctive API key from the Webhooks IFTTT service within the following line:
api_key = ‘REPLACE_WITH_YOUR_WEBHOOKS_IFTTT_API_KEY’
Setting the sleep time
On this instance we’ve set the sleep time to 10 minutes (600000 milliseconds). Because of this each 10 minutes the ESP wakes up, takes the readings, and publishes them in your Google Sheets spreadsheet. The sleep time is about within the ms_sleep_time variable in milliseconds:
ms_sleep_time = 600000
Warning: in the event you set a really brief interval, you might exceed the restrict of requests imposed by the IFTTT providers.
Connecting to the community
The next strains join the ESP8266/ESP32 to your router, and print the ESP IP tackle:
station = community.WLAN(community.STA_IF)
whereas station.isconnected() == False:
Deep sleep perform (ESP8266)
Then, create a perform referred to as deep_sleep() that accepts as argument the variety of milliseconds we would like the ESP8266 to be in deep sleep mode.
def deep_sleep(msecs) :
# configure RTC.ALARM0 to have the ability to wake the system
rtc = machine.RTC()
rtc.irq(set off=rtc.ALARM0, wake=machine.DEEPSLEEP)
# set RTC.ALARM0 to fireside after X milliseconds (waking the system)
# put the gadget to sleep
Word: should you’re using an ESP32, you don’t have to create/use this perform.
Studying temperature, humidity, and strain
Then, create an I2C occasion referred to as i2c. You want this to create an I2C reference to the BME280 sensor. Cross as arguments the SCL and SDA pins, in addition to a frequency. Don’t fear concerning the frequency, a worth of 10000 Hz works simply high quality for this sensor.
The ESP8266 default I2C pins are GPIO 5 (SLC) and GPIO four (SDA).
i2c = I2C(scl=Pin(5),sda=Pin(four), freq=10000)
The ESP32 default I2C pins are GPIO 22 (SCL) and GPIO 21 (SDA). Remark the earlier line and uncomment the next line should you’re using an ESP32 board.
#i2c = I2C(scl=Pin(22),sda=Pin(21), freq=10000)
Then, use attempt to besides statements. Within the attempt assertion, we learn from the sensor and make an HTTP submit request to the IFTTT providers.
The temperature is saved on the temp variable, the humidity on the hum variable, and the strain on the pres variable.
bme = BME280.BME280(i2c=i2c)
temp = bme.temperature
hum = bme.humidity
pres = bme.strain
Uncomment the next strains, if you wish to get temperature in Fahrenheit levels.
# uncomment for temperature in Fahrenheit
#temp = (bme.read_temperature()/100) * (9/5) + 32
#temp = str(spherical(temp, 2)) + ‘F’
Then, retailer the sensor readings in JSON format within the sensor_readings variable. That is required to make the HTTP POST request on IFTTT.
sensor_readings = ‘value1’:temp[:-1], ‘value2’:hum[:-1], ‘value3’:pres[:-3]
Word: the BME280 library returns the readings with models (C for temperature, % for humidity, and hpa for strain). To make it simpler to analyse the info later, we take away the models by including [:-1] to take away the final character from the string. The [:-3] for the strain removes the final three characters from the string (hpa).
HTTP Publish request
A basic HTTP POST request in MicroPython using the urequests library has the next format:
request= requests.submit(<your_url>, json=<json_data>, headers=<headers>)
The primary parameter is the URL during which you’ll make the request. As we’ve seen within the IFTTT Applet, we have to use the next URL (by which api_key will probably be changed with your personal API key):
The JSON parameter ought to include knowledge in JSON format. In our case, that’s the sensor_readings variable:
The headers parameter incorporates details about the request. For our request, the headers ought to be as follows:
request_headers = ‘Content material-Sort’: ‘software/json’
Lastly, we make the request using the publish() technique with the knowledge we’ve outlined earlier:
request = urequests.publish(
‘http://maker.ifttt.com/trigger/bme_280_readings/with/key/’ + api_key,
Lastly, print the request and shut the connection:
If studying knowledge from the sensor or publishing the sensor readings fails, the besides assertion runs and we print the ‘Failed to read/publish sensor readings.’ message.
besides OSError as e:
print(‘Did not learn/publish sensor readings.’)
The besides statements in MicroPython forestall your board from crashing in case an error happens.
Going to sleep
After making the HTTP POST request, we put the ESP8266/ESP32 in deep sleep mode. Use the next line in the event you’re using an ESP8266:
Remark the earlier line and uncomment the next line should you’re using an ESP32:
Notice: to make use of deep sleep with the ESP32 with timer get up, we simply want to make use of the deepsleep() technique and cross as an argument the sleep time in milliseconds.
Constructing the Circuit
The BME280 sensor we’re using communicates by way of I2C communication protocol.
Comply with one of many subsequent schematics relying on the board you’re using.
Essential: you must solely join GPIO16 to the RST pin after importing the code. This connection is required in order that the ESP8266 is ready to wake itself up.
Notice: for those who’re using an ESP8266 12-E, we advocate using an adapter to have the ability to use it with a breadboard to make the wiring simpler. Alternatively, you possibly can made a PCB at residence like we did. We used the etching method.
ESP8266 NodeMCU Package
For those who’re using an ESP8266 NodeMCU package, you additionally want to attach the RST pin to GPIO 16 (D0) after importing the code.
For those who’re using an ESP32, you simply have to wire the circuit as proven within the schematic diagram:
Observe: for those who’re using a special board, double-check the pinout on your board.
After importing the required information to your board within the following order:
The ESP8266/ESP32 will begin publishing the sensor readings on the Google Sheets spreadsheet.
When the ESP8266 12-E is in deep sleep mode it consumes about 7uA to 8uA.
When the ESP8266 wakes up and connects to Wi-Fi, the present consumption can go as much as 80mA.
Notice: in case you’re using a ESP improvement board, you’ll get a better present consumption in deep sleep.
Powering the ESP8266/ESP32 with Batteries
In the event you construct this undertaking with the ESP8266 12-E chip, it makes use of little or no energy throughout deepsleep (roughly 7uA). You possibly can energy it using batteries and they’ll final for a number of months (and even years relying on the capability).
We plan to create a Half 2 of this venture using a battery powered circuit. In the meantime, you’ll be able to learn this tutorial to energy the ESP using LiPo and Li-ion batteries.
With this challenge you’ve discovered easy methods to construct a datalogger that publishes sensor readings to a Google Sheets spreadsheet. All through the directions, you’ve discovered the best way to use the BME280, make HTTP POST requests and use deep sleep with ESP8266/ESP32 using MicroPython.
We hope you’ve discovered this venture fascinating. You might also like:
Thanks for studying.