Showing posts with label IOT. Show all posts
Showing posts with label IOT. Show all posts

IOT TUTORIAL: Esp8266 (NODEMCU): MICROPYTHON, DHT22, MQTT and INTERRUPT


I had a ESP8266 - NodeMCU - module laying around, so I decided to install inside an electrical panel, to gather also data from energy monitor. I used a DIN empty support to mount the ESP:

Flashing micropython to ESP8266

Micropython install on ESP8266 without pain: 
a. Download latest version from http://micropython.org/download#esp8266
b. Deploy the firmare, install with pip the esptool tool 
c. Erase flash with:
esptool.py --port /dev/ttyUSB0 erase_flash
d. Connect ESP8266
e. Check the connected serial port with
ls /dev
f. Change directory to the unzipped firmware directory
g. Flash the new firmware from bash with the following command.
esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin 
That's all, if not working, check USB port or reduce upload speed to 115200, or check micropython website tutorial.

Running a script

By default micropython at startup execute a main.py file. Write it in your favourite text editor and  upload it using WebREPL.

Connecting to ESP

There are two options to connect to ESP with micropython, serial REPL or WebREPL (OTA or On the air or via WIFI).

Serial prompt - REPL

If you connect to ESP via serial port, you'll be prompted with a python shell, import modules and execute commands. You can stop main.py from execution, and also you can soft or hard reset. This kind of interface is called REPL, and is always available.

Over the air - WebREPL

During startup, ESP will create an access point called MicroPython-xxxxxx to which you can connect directly. Download a local version of html file from Github and open the html inside the zip file. 
I prefer this way because with this interface you can upload main.py when needed.
You will be prompted to a web page like this.

On the bottom left corner the standard access point address, change after accordingly when the ESP is connected to your local network via WIFI.
Once uploaded a main.py file, after reset, inside WebREPL is possible to read output from print statements and stop main.py with CTRL-C if something went wrong.

ESP8266 + NODEMCU + MICROPYTHON PINOUT

Considering the following layout using micropython you have to refer to pin numbering used by GPIO naming. So for instance D0 pin is GPIO16, inside micropython is pin 16:

#import machine library
from machine import Pin

# create an output pin on pin #0
p0 = Pin(0, Pin.OUT)
# set the value to high
p0.value(1)

CONNECTING TO LOCAL WIFI NETWORK

To connect Nodemcu to Wifi:
#import libraryimport network 
#import sleep function from dedicated time libraryfrom utime import sleep
#defined function to check connection or reconnectdef do_connect():    sta_if = network.WLAN(network.STA_IF)    if not sta_if.isconnected():        print('Connecting to network Wireless...')        sta_if.active(True)        sta_if.connect('SSID', 'PASSWORD')        while not sta_if.isconnected():            pass
do_connect()
while True:
    #do your loop here
    ....
    do_connect()
    ...
    sleep(15) 
Once connected, every time you reset you have to change the IP number to the DHCP assigned one, without connecting to Access Point, or you can connect directly to access point and use the internal IP.
You can also disable AP on startup after do_connect() using:
ap_if.active(False)

SENDING MQTT MESSAGES TO BROKER

from umqtt.simple import MQTTClient
SERVER = b'192.168.1.xx' #put your broker addressCLIENT_ID = b'ESP_Epanel'TOPIC_T = b'HOME/EPANEL/TEMPERATURE'TOPIC_H = b'HOME/EPANEL/HUMIDITY'
#after connecting to WIFIclient = MQTTClient(CLIENT_ID, SERVER)client.connect()   #Connect to MQTT broker
while True:    client.publish(TOPIC_T, 22)    client.publish(TOPIC_H,45)

READING DHT22 SENSOR

DHT22 is an expensive and accurate sensor to monitor temperature and humidity. I used one with breakout board from Banggood.
Sometimes DHT22 fail to read and the simplest solution I found is to soft reset via code
from dht import DHT22
from machine import reset
sensor = DHT22(Pin(5))
while True:
    try:
        sensor.measure()
        t = sensor.temperature()
        h = sensor.humidity()
        if isinstance(t, float):
            msg = (b'{0:3.1f}'.format(t))
            client.publish(TOPIC_T, msg)
            print("Temperature: ", msg)
        if isinstance(h, float):
            msg = (b'{0:3.1f}'.format(h))
            client.publish(TOPIC_H, msg)
            print("Humidity: ", msg)
        else:
            print('Invalid sensor readings.')
    except OSError:
        print('Failed to read sensor.')
        reset()

PIN INTERRUPT HANDLING

Handling external interrupt in micropython is really simple:
from utime import time
from machine import reset  
#define a callback function where execute simple and short code 
def callback(pin_number):
    #refer to global variable
    global last_time
    last_lime = time()
 
#pin setup
p0 = Pin(12,mode=Pin.IN)
p1 = Pin(13,mode=Pin.IN)
 
#start interrupt
p0.irq(trigger=Pin.IRQ_FALLING, handler=callback)
p1.irq(trigger=Pin.IRQ_FALLING, handler=callback)

CONCLUSIONS

Put all together and you will have a connected MQTT node which read DHT22 sensor every 15 seconds, managing errors and as in my case count pulse input.

What I learned so far:
- Micropython is little tricky at the beginning, but stable, flexible and finally is Python.
- ESP8266 is not energy friendly, it produces a lot of heat, so is better to mount DHT22 far from ESP. In the near area surrounding the ESP the temperature reach a stable 26 degrees Celsius vs 20 degrees Celsius of surrounding.
- I hate pins, I would prefer to have screw connectors.


IOT RECIPE: 04 Headless Raspberry Pi programming

How many monitor you need to have to program a Raspberry Pi. 
No one, as you now: with ssh is possible to interact with Raspberry Pi from a remote machine. The majority of distributions enable SSH by default, so you can access it once connected to your local network with command ssh username@rpiname.local and replace rpiname with raspberry host name.
With text-only Nano editor is also always possible to develop directly on ssh interface. 

Is possible otherwise to develop a complex project like IOT Energy Monitor Webserver using a combination of SFTP, your favourite Text Editor and of course Terminal to run WebServer and debug.



SFTP Mac OSx Drive Mount



Cyberduck.io 

"Cyberduck is a libre FTP, SFTP, WebDAV, S3, Backblaze B2, Azure & OpenStack Swift browser for Mac and Windows."






ExpanDrive

"Map or Mount Amazon Cloud Drive, Google Drive, Dropbox, Box, OneDrive, SFTP, WebDAV, S3 and more as a Network Drive. Seamless access to files without sync."
ExpanDrive is not free.




Extreme text Editor

Sublime Text

"Sublime Text is a sophisticated text editor for code, markup and prose. You'll love the slick user interface, extraordinary features and amazing performance."




Atom Text Editor

"Atom is a text editor that's modern, approachable, yet hackable to the core—a tool you can customize to do anything but also use productively without ever touching a config file."





Terminal


Bash (Bourne Again Shell)

Terminal is the OSx shell. From here you can connect via ssh to the Raspberry Pi shell.
OSx terminal can be customized in colors and transparency.
Copy & Paste work as expected in terminal, and more than one session may be opened, if you need to run a server and rename/move/copy/list/... at the same time.


Ssh first connection

Once connected raspi to the local network, you need to find IP to access and configure the network name (and other configurations). Personally i use router DHCP list to find the assigned IP, but more options are available, like ip scanner.
The first connection to ssh host, terminal ask you if you want to add ssh key to known hosts, tap "yes" to gain access to the machine.

Ssh offending message (RSA key changed)

Sometime, when IP changes on Raspi, you are not more allowed to connect to remote raspi: an update is needed.
Use the command "ssh-keygen -R rpiname.local" to clean RSA key, then reconnect via ssh and generate another key, as a first connection.

Lost connection while running flask server

What happens if you lose connection while a flask server is running?
The session is closed and the server is terminated. To change interval here there is a guide to change timeout interval on Raspi.  
If you want to run the flask server on startup, follow the next step.

Running python script on startup via systemd

The simplest way to run a python script on startup on Debian distro (like Raspbian) is via UNIT files.
There are a lot of guides on the web, but the majority is related to init.d script. This approach is more simpler and effective (related also to running order). I had a lot of headache running python script via init.d cause "exited" python script status.

To run a script via systemctl follow these steps:
1) Be sure your python script works as expected
2) Paste the python executable version on top of the pythons script and save: #!/usr/bin/python
3) Make the script executable with chmod +x myscript.py
4) Create a configuration file with sudo nano /lib/systemd/system/flask.service
5) Paste the following content to the nano editor session and save

[Unit]
Description=Flask server

[Service]
Type=simple
ExecStart=/absolute_path_to_script.py
User=root
Group=root
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

6) Set unit file permission with: sudo chmod 644 /lib/systemd/system/flask.service
7) Load unit file with: sudo systemctl daemon-reload
8) Enable unit file with: sudo systemctl enable flask.service
9) Reboot

After reboot you can control status of the service, stop and restart using these commands:
  • sudo systemctl status flask.service
  • sudo systemctl stop flask.service
  • sudo systemctl start flask.service
  • sudo systemctl restart flask.service

IOT RECIPE 03: Pellet / Wood chips level sensors

In this post i'll cover actual sensor setup for pellet and wood chips bulk level monitoring.

Pellet bulk level sensors

How to monitor pellet bulk level if you can't access to warehouse?
My warehouse is a room covered by wood, with sloped floor and a moving reclaimer on the center. The total volume is approximately 12 cubic meters and once filled i've no access to the room.



Obviously i need to know residual pellet level, so a pellet bulk sensor is needed. While googling i've found few examples, all of them related to water level in tank and using ultrasonic distance sensor approach.

The room is more or less 2 by 3 meters: in this configuration, pellet or wood chips forms non uniform piles in the room, with lower point just above the reclaimer area, so is needed to use an array of sensors averaged to find the level: 9 ultrasonic sensors are arranged in a matrix configuration.

Ultrasonic sensors

For the project i've selected HC-SR04: the sensor is a low cost complete ultrasonic measurement device. The sensors are housed in waterproof electrical box.

Once activated a signal burst is emitted from the emitter, then the signal is captured from the receiver, mirrored by a surface. Using simple timing formula based on sound speed, the library output distance in cm. Simple. 3 cm to 2 meters is the real distance range, while effective usage suggests the sensor is not so accurate over time: in my experience temperature compensation is needed to give at least not wrong measurements (15% of fluctuation is possible).

Most libraries provides averaging for each reading, with 10 to 25 signal bursts before measurement average.

From distance to weight

Pellet weight in bulk storage is more or less 650 kg/mc.
To provide weight measurement the equation is simple:

Estimated weight = Average(empty distance - measured distance)*(area)*(bulk weight)


Data acquisition

To acquire 9 sensors outside warehouse a lot of cables are routed outside warehouse to an external waterproof electrical box.

The little circular window in the front of electrical box is PMMA window sealed with hot glue (my new electronic projects best friend). 

In the box i've installed an Arduino Mega with three additional sensors:
  • A remote DHT22 inside pellet warehouse to provide internal reading of temperature and humidity;
  • DHT22 to provide same readings of external conditions;
  • A three-in one sensor: light, pressure/altitude and temperature

Measurement routing

Distance between Arduino Mega and Raspberry PI is more or less 4 meters. After a bit of googling, i've found a 5 meters USB cable is reliable. I can confirm is true.
The USB cable connect Arduino to RPi inside the boiler room (gray cable coming from the top of the electrical box).


Inside the box a Raspberry PI with Node-Red with MQTT client (more details in the future).
Another Arduino is installed and connected to read out energy monitor.
RPi communicates with a central server (another RPi) using ethernet.


IOT RECIPE: 02 Energy Monitor

In this post i'll cover actual sensor setup for energy monitor.

Energy monitor

To evaluate power consumption i've routed main cable coming from utility energy monitor in a new electrical panel. In this panel i've installed:
  • 3 three-phase energy meter (16 kW of maximum power)
  • 1 single-phase energy meter
This is an internal view of my setup inside an ABB electrical panel (upper part, with two of these energy monitor) after thermal-magnetic circuit breaker and surge protector.

For this duty i preferred to go with an industrial solution, because i was not confident with DIY solutions on main power side.
My solution is a products from Italian manufacturer LOVATO:
LOVATO DMED300T2 is a compact (four modules) energy meter. The main advantage is the presence of two programmable pulse output (up to 1000 pulses/1kWh or programmable threshold).
The output is an open collector, in which each pulse has a duration of 60ms: an open collector is a transistor based output that can be read by an external PLC (like arduino).

The electrical connection requires only to route an input pin of the arduino to open collector and connect the two IC grounds to have the same reference level and let current from arduino to flow once open collector is open.

There are two methods to read these output, with interrupts or with pooling.
Because i had only one external interrupt, i had to go with pooling: loop forever and check if digital input is high or low. If is high, and previous one was low, than increment a counter and go on to next input. 

First of all, in my system maximum power is 16 kW: this means each hour i can have a maximum number of pulses of 16 * 1000 = 16.000 pulses/hour equal to 4.45 pulses/second, each one of 60 ms.
If entire loop timing is less than 60ms, no pulse is missed.

Because open collector is transistor based, i found no debounce routine is needed (situation like button press may need a stabilisation in input reading).

My piece of code for pooling is:

//INPUT SETUP
pinMode(LOVATO1, INPUT);           // set pin to input

digitalWrite(LOVATO1, HIGH);       // turn on pullup resistors

//INPUT LOOP
READ1 = digitalRead(LOVATO1);

if (READ1 != LASTSTATE1) {
    if (LASTSTATE1 == 1) {
      COUNTER1 ++;
    }
    LASTSTATE1 = READ1;
  }

Taking into account the time delta between reading A and reading B (60s in my case), is possible to average the energy consumption during this period, and to define average power consumption during the period.
Passing via serial milliseconds and pulse numbers between loops, i can reduce the time spent communicating with host, leaving calculation on other side.

Resulting chart of 1 day power consumption average (1 tick/30 minutes):



IOT RECIPE: 01 Introduction - Sensor web interface

I'll start a new series of posts on IOT matter from the end.
This is the web interface running on my local area network.



Running the site is a mini flask web server running on a Raspberry PI. In the screenshot, charts are provided by highcharts.com and Justgage javascript library.

In this series of posts i'll cover all the ingredients of my recipe:
- Sensors overview (DHT22, Energy, Light, Barometric pressure, Pellet height)
- Raspberry Pi data acquisition with Arduino
- Launchpad Tiva CC3200 with WiFi
- Node Red Engine
- MQTT, Mosquitto, Paho
- Rrdtool, circular timeseries database
- Flask server
- Charting with javascript
- Debian unit files
- Telegram Bot Api

Popular posts