DISCLAIMER: This tutorial is made for Ubuntu users. The steps haven't been tested for Windows/Mac/other Linux distros. The actual code is the same for PC/Mac/Linux, but the Getting Started section is for Ubuntu.
This tutorial will show you one way to send data to the cloud via WiFi, using MicroPython. The project will take about three hours, depending on how much you ctrl+C -> ctrl+V
, or try to learn and code it yourself.
- Beginner 🔰
- MicroPython 🐍
- LoPy4 :robot_face:
- ~ 3 ± 1 hours ⏲️
- WiFi 📶
- Linux 🐧
I have a balcony facing south, so it get's blasted with sunshine from sunrise to sunset. If facing south wasn't enough, it has also windows so it is basically a 40° oven. The fact that I have a cat means that I can't open the windows either. 🐱🔥
The purpose of this project is to put a sensor on the balcony, which then alerts me when the temperature is below a certain value, so I can open the balcony door at the right time to cool down the apartment.
- You need a computer with at least one USB A-port.
What? | Why? | Where? | 💵 | Note |
---|---|---|---|---|
LoPy4 | The actual brain that does all the processing | Pycom | €35 | Other microcontrollers are ok, but it needs WiFi-support |
3x jumper wires | Used to connect the LoPy4 to the sensor | Ebay | ~1€ | Make sure it's long enough. To connect the sensor directly to the board, you need male to female wires. If you use a breadboard you need male to male wires |
Temperature Sensor | Actually gathering data. | Ebay | ~3€ | |
Micro-USB Cable | Connecting the LoPy4 to the computer and powerbank | Ebay | ~2€ | You probably have one laying around at home |
Powerbank | To easily power the project without using an outlet | Ebay | ~5€ | OPTIONAL Don't buy the absolute cheapest one. Safety first. I used a 1000 mAh battery which lasted for a day or so. |
Breadboard | Makes it easy to connect everything when testing | Ebay | <5€ | OPTIONAL |
As stated in the beginning, this section is for Ubuntu-users.
First you should always make sure that your LoPy4 is up to date. Get the latest firmware here. Follow the steps in the update-software.
When updating, you will be asked if you'd like to flash the device. Flashing means resetting. You should flash your device. Please note that any code already saved on the the device will be lost.
You need an IDE that supports Pymakr. Two good choices are
There might be other IDEs but these two work great.
I chose VS Code, so it the following steps might differ a bit from Atom, but it should be basically the same.
Start your chosen IDE and install the plugin called Pymakr
. It can be found in the Extensions
section.
You also have to install Node.js if you haven't already.
Plug in your LoPy4 and it should be automatically detected in VS Code.
Copy the following code and hit Run
located in the bottom of VSCode.
import pycom
pycom.heartbeat(False)
pycom.rgbled(0xFFC0CB)
If everything works correctly, the onboard LED should become pink.
The temperature sensor requires an input voltage of 2.7 - 5.5V. The LoPy4 has a 3.3V out which is perfect. This way we don't need any kind of resistors in this project.
The connection is pretty straight forward. Just make sure to connect the pins correctly. Check your sensors sheet to see which way your sensor should go. It's important not to mess this up, because it could completely destroy your LoPy-board.
You could also use a breadboard to do the connection. Here is a guide on how to use a breadboard.
I decided to use Ubidots as my cloud-solution.
After registering an account to Ubidots, you can use this guide to connect your device to Ubidots.
The reason I chose Ubidots is because of it's ease to use. It makes it very simple to connect your device and send data to the cloud. You can easily create graphs and triggers. It is also free (but there is a pro version). If you are using the free version, you are limited to send 4000 packages to the cloud every 24 hours. This is around two per minute.
I first tried using Pybytes, but setting up an event/trigger was much more difficult and more advanced than it should be. When the temperature was under a certain value, I wanted to be notified somehow. In Pybytes you could create a Webhook and go via some steps to get notified. Ubidots has a built in feature that sends me an E-mail when the conditions of the event is met. I added Ubidots as a favorite, and now I get a notification on my phone when the condition is met.
I also added a trigger that notifies me when data hasn't been recieved for 60 minutes. This usually means that the thermometer has run out of battery and needs a recharge.
The code can be found here in this repository.
The data is being sent every 10 minutes. To get more accurate data, the data is gathered every minute. Then the average temperature is being sent to the cloud.
I'm using WiFi to connect the board to the internet. You could use SigFox or LoRa, but my apartment doesn't have a LoRa-station close enough to be able to use LoRa and I really didn't like the feel of Sigfox, so WiFi was the way to go for me.
In Ubidots you can represent the data in many different ways. Graphs, tables, thermometers. Follow this guide to connect your device to Ubidots. It will also show how to connect your device to your Wi-Fi network.
Just make sure to enter your WiFi SSID, password and Ubidots token.
TOKEN = "YOUR_UBIDOTS_TOKEN" #Put your TOKEN here
...
wlan.connect("wifi-SSID-here", auth=(WLAN.WPA2, "wifi-password-here"), timeout=5000)
I would love to have used LoRa instead of WiFi, because it consumes a lot less power, which would make the battery last a lot longer. It would also be cool if I could have connected the temperature to some blinds or curtains, so they would go up and down depending on the temperature.