- The idea of this tutorial is to provide examples of how to read sensor data with MicroPython devices.
- Then how to do some useful stuff with that sensor data, using a RPi running its bigger sibling, Python. You will find examples of code that can be used for measuring distance (Water Level), controlling relays (The Pump), Soil Moisture (Capacitive Soil Moisture), Temp & Humidity (DHT11) and Light level using a photoresistor. The RPi code shows how to send Push notifications, write data to a Google Sheet, request weather forecast data from Darksky and how to use HTTP requests with a web app to monitor and interface with your mPy devices.
- Secondly, to build an example code framework which can be quickly and easily modified to use for all future mPy projects. The setup being infinite mPy children contacting a Flask parent, who performs the complex functions the children cannot. This framework includes a standard boot.py for the children (Connect to WiFi, use NTP to set time) and a list of the definitions which need to be added to the parent web app to allow a new device to connect.
I built this system as a way of teaching myself about Flask, MicroPython and HTTP requests. The MicroPython devices are the nodes which read sensors and control the relays. They report their data back to the Flask server running on a RPi3 (This can be any device running Python3, even your PC or an AWS EC2 instance.)
The mPy nodes have the ability to transfer information between themselves using HTTP requests, i.e. one board takes the soil moisture level and reports to the server, another board then requests this information and uses it to decide whether to run the pump or not.
On the subject of water conservation, the pump controller is also able to request the weather forecast from the Pi. When the Pi receives this request, it contacts Darksky for the precipitation probability for the next 24 hours. The pump controller again uses this information to decide whether to run the pump or not i.e. if its going to rain before tomorrow's watering cycle, don't pump.
The pump I used was a tiny USB aquarium pump. Its a submersible pump and if it runs dry it will burn out. To prevent this I put a hcsr04 ultrasonic sensor at the top of the water tank to measure the distance to the top of the water. This is where the push notifications come in, if the pump is prevented from running by a low water level, the mPy board will contact the RPi, who will then send a notification to my phone using Slack.
This project is an expansion of a previous project of mine and as with that project, all the sensor data is saved to a Google Sheet, with individual worksheets for each board.
I wrote the code to run continuously, for example, if there a network timeout and it causes an exception which stops the board, the board keeps deepsleep-ing and resetting itself until it can run. This is fine but it leaves you unable to enter commands in REPL and the only way to change files on the board seems to be to erase the flash and install mPy again, then load on the edited files.
You will need to change the IP address contained in the MicroPython files to the IP address of whichever machine is running the Flask server.
To use Google Sheets, you will need to follow these instructions:
http://gspread.readthedocs.io/en/latest/oauth2.html
To get your Darksky API key, go to link below and register:
To set up Slack, go to this website and register, then install on your phone:
Comments