You are on page 1of 28

mInoT

IoT for the MINT


Introduction
Let’s pretend, to measure the weight of air. Well I would be the King and you the Queen. I would
request that the royal alchemist shall solve this riddle.

Since I am not the King and as a result of that you’re not the Queen, I would use my kitchen scale to
solve this riddle. Put a Coke glass on it, the one with the 0.4l volume marker, pour some air into it
and look at the display to see how much weight I added to the empty glass.

Stefan setzen sechs. So at home I took an air pump, a closed cylinder with water included and started
the pump.

104,8 ml * 1,293 g
______________ = 0,135 g

1000 ml

The brain
The ESP8266 is a low-cost Wi-Fi microchip, with a full TCP/IP stack and microcontroller
capability, produced by Espressif Systems[1] in Shanghai, China.
We will be using a guy like this. I like the small
form factor and the power supply and
communication can be done by USB.

The only shortcoming is that it only has one


analog input.

The Software
I have chosen ESPEasy, because I know it does the job in many cases. It comes along with a
WebInterface, which in many cases makes programming unnecessary.

The Device List


Here you get a list of all sensors you want to add to your project. A list of supported devices is
available under https://www.letscontrolit.com/wiki/index.php?title=Devices.

There are tons of them and there is a playground with even more. I will start with a bunch of them,
since I will explain during this tutorial, how to attach the sensors to ESPEasy.
The Hogwarts Express
The BHT1750 is a Luxmeter. I have chosen this guy, since … I had it in stock. Good reason, isn’t it. The
real reason is, that it uses an I2C Bus. The line I2C is the
bus line operating between the Schuldorf and Darmstadt
main station platform 9 3/4 Monday to Friday during school
hours.

And you may attach a sensor to this bus line. The schedule
is printed on it’s back side.6

Let’s talk about pins:

Pin Comment Color


VCC Power + RED
GND Power - BLACK
SCL I2C Bus YELLOW
SDA I2C Bus GREEN
ADDR not connected
To get this build up we use a bread board. I used the design software Fritzing to build all schematics
in this tutorial.

At the end of the bus I pull up the bus to 3.3V using a 47K Ohm Resistor. The pin D1 is SCL and D2 is
SDA. So don’t use those pins if you would like to use the I2C bus.

In ESPEasy use the tab “Hardware” to check if everything has been set up correctly.

Even though you may


change this, it is a good
practice to stay with those
pins.

Using the I2C Bus


Judging from my emails, it is quite clear that the I2C bus can be very confusing for the newcomer. I
have lots of examples on using the I2C bus on the website, but many of these are using high level
controllers and do not show the detail of what is actually happening on the bus. This short article
therefore tries to de-mystify the I2C bus, I hope it doesn't have the opposite effect!

The physical I2C bus


This is just two wires, called SCL and SDA. SCL is the clock line. It is used to synchronize all data
transfers over the I2C bus. SDA is the data line. The SCL & SDA lines are connected to all devices on
the I2C bus. There needs to be a third wire which is just the ground or 0 volts. There may also be a
5volt wire is power is being distributed to the devices. Both SCL and SDA lines are "open drain"
drivers. What this means is that the chip can drive its output low, but it cannot drive it high. For the
line to be able to go high you must provide pull-up resistors to the 5v supply. There should be a
resistor from the SCL line to the 5v line and another from the SDA line to the 5v line. You only need
one set of pull-up resistors for the whole I2C bus, not for each device, as illustrated below:

The value of the resistors is not critical. I have seen anything from 1k8 (1800 ohms) to 47k (47000
ohms) used. 1k8, 4k7 and 10k are common values, but anything in this range should work OK. I
recommend 1k8 as this gives you the best performance. If the resistors are missing, the SCL and SDA
lines will always be low - nearly 0 volts - and the I2C bus will not work.
From: <http://www.robot-electronics.co.uk/i2c-tutorial>
Configuration of your lux meter

Easy enough isn’t it?

• First give your device a name. Keep it short, but clear


• With “Enabled” you turn the device on

• Select a reasonable interval. 5 seconds is good for testing, but if you want to have a week
worth of data, this may be an overkill
• No need to do calculations with this device, since it reports the desired value in Lux

Display your data


Displaying your data is really two clicks away now. I selected an OLED display (SSD1306) to do so. This
device works with the I2C bus you already know. Plug it into the spare place on the breadboard.
Sorry I did not have a yellow and green wire for the SCL/SDA, so I replaced it by white and red, from
which I have tons.
Add a device for this and have a look at the properties. You will see, the Luxmeter we have created in
line 2. The device name and value are printed here in square brackets. You get the manual if you click
on the blue question mark. In line 8 you see a variable %ip% which will give you the IP address of
your ESPEasy device. You will need it in the browser of your choice.

http://192.168.178.82/devices

Configuration of your display

YaBu – RS232
Computer Nerds like acronyms like yet another bus. This introduces our next sensor, which uses the
RS232 bus, the dust sensor SDS011.

If we turn this one around, we will find tons of pins. This sensor works with 5 volts. We make sure,
that we have enough power to operate it stable.

Pin Comment Color


5V Power + RED
GND Power - BLACK
TXD RS232 YELLOW
RXD RS232 GREEN

Then you will find two new pins TXD/RXD. What would be easier to connect it to TX/RX pins on the
Wemos.

Well, besides the warning below this will not work.


You must crossover the TX and RX lines. TX goes to
RX and vice versa.

STOP and I really mean STOP here. Unless told by a


teacher never ever connect a 5 volt device to a 3.3
volt device. I have killed much of my hardware not
knowing better.

If you are to use a new device come and ask. I have done the prep work here and the RX and TX ports
are using 3.3 volts, so we are all set. You will find such information from the data sheet. But only

cowards rtfm.

Beside the electrical specification you will find much background information here. https://cdn-
reichelt.de/documents/datenblatt/X200/SDS011-DATASHEET.pdf
There are more connections available, to connect the ESPEasy to your SDS011, but I could only get
the depicted option to work. Another word be patient. Gas sensors need time to give you stable
results.

Configuration of your Dust sensor

The pitfall

Nothing comes for free. You may use the


TX/RX pin for a sensor or serial
communication via USB cable. In the first
case, you have to disable the serial port in
Tools → Advanced.

Shouldn’t we code something funny like this


old Atari game pitfall?

Analog data
What shall I do with analog data coming from something as digital as a sensor? Because the sensor is
not digital. The sensor is composed by micro AL2O3 ceramic tube, Tin Dioxide (SnO2) sensitive layer,
measuring electrode and heater are fixed into a crust made by plastic and stainless steel net.
If mounted on a breakout
board you will find V+,
GND, D0 and A0 Pin.
Here, we have a digital
signal D0 though. Yes, but
… you all know a yes but
will say the opposite …
The digital pin is only a
threshold 0 below and 1
greater equal threshold.

We connect the A0
output to a voltage
divider. We use a
potentiometer, which
allows us to adjust Vout to
3.3 V. Shouldn’t 2 resistors do the job? Yes but … did I mention that I
am lazy and the multimeter does the job of the formula pretty well.

Configuration of your MQ-5 gas sensor

The analog pin A0 displays values from 0…1023 = 0 … 3.3 volts. So, you can do a simple 2-point
calibration. Looks easy, but … shouldn’t be the gas concentration be in ppm? Yes, it should, but didn’t
I tell you that I like to torture students with the real tricky staff? And this is not about electronics.
Calibrating the sensor

If we place the sensor in a cylinder with a known volume


asume 1000 cc and inject a known volume of gas 5cc, we
should be able to calculate the molarity of the gas, right?

The picture shows the senor with a 3D printed mount


inserted in the tube.

Then we measure the value of the gas sensor.


We repeat the process with different volumes
injected.

With this we should be able to callibrate the


sensor.
One wire … does not come alone ;-)
Well, one wire wouldn’t do the job. 1-Wire is a device communications bus system designed by
Dallas Semiconductor Corp.
that provides low-speed (16.3
kbps[1]) data, signaling, and
power over a single
conductor.

There are a couple of devices,


mostly temperature sensors
using this protocol. No, don’t
blame me if there should be
others. Let’s start with the
simple case. The DHT11
sensor. As you can see, we
have GND, DATA and VCC
pins.

Data can be assigned to any


digital pin on our ESP.

Configuration of your DHT11 gas sensor


The configuration is speaking for itself.
The

DS18B20 sensor
According to the ESPEasy man pages
one may connect this sensor to 3.3V.

Sensors and microcontrollers have a


voltage on which they operate. With
the original Arduino this has been 5
volts. Newer processors tend to reduce
this voltage to 3.3 volts. This is the
same with sensors. When preparing
this tutorial, I stumbled over that issue

and my sensor did not


work.

The magic between


3.3 and 5.0 volt
The ugly part is when a
web site states the sensor works with 3.3 to 5 volts and
you have a cheap Chinese clone and this one does not
follow the rules.

I ended up with a sensor talking 5V and a microcontroller


on a 3.3V basis. This converter board will do the magic.

First you need to connect it to power LV = 3.3V, HV=5.0V.


Common ground (GND) is shared, so connect it to any of
your ground terminals.

Then you have 4 input/output pins HV1-4 (5.0V) and LV1-4 (3.3V).

oIn our examples we connect the sensor to 5V and our ESP to 3.3 V.
Configuration of your DS18b20 temperature sensor

<<

In the tab tools you may run a command to turn it on

This will set GPIO 12 which is D6 to high 3.3 volts


until you turn it off with GPIO,12,0. Then we
have the pulse or longpulse commands, which
will turn D6 on for a time. I.e. longpulse,12,1,15
will turn on D6 for 15 seconds.

And there is PWM, Pulse Width Modulation. The


processor cannot continuously regulate the voltage
between 0 and 3.3 volts. But you can switch on, off, on,
off … the GPIO to make the motor believe that it has not
to operate on full power. The duty cycle 50% will be
equally distributed. The higher the duty cycle the more
power will be send to the motor. 100% means no low phases and 0% means no high phase. This
being said we can control our pump with pwm,12,512 which is 50% duty cycle.

Pulse Counter
The pulse counter is the device to count a PWM signal. Let’s assume, you are interested how much
air your pump is pumping. We connect the
counter after the pump with a tube.

That’s the mechanics. The water flow sensor


consists of a plastic valve body, flow rotor and
hall effect sensor.

When the air (could be also a liquid) starts


flowing, the rotor starts spinning and the hall
effect sensor measures the rotation using a
magnet attached to the rotor. The result is an
PWM signal.

And this is what we measure.

Configure your Hall Sensor


• Determine a pin for the sensor and connect the pin to your yellow cable on the sensor
• Connect red to +5V
• Connect black to ground
• Select the values to be reported
• Set mode type to low
could be (low, change, rising, falling). Please have a look at the PWM chart and reflect what
they are used for.

That’s it. Turn on your pump and think how to calibrate this sensor, so you get l/min displayed?
Error egressus circumdatus es
Or how do we calibrate a sensor.

Do you trust in the data you see on your sensor?


No, welcome to the real world Dealing with
errors is one thing you have to learn. Non scholae
sed vitae discimus. Why Latin, because I love
Asterix.

Imagine you have a scale sensor and get a result of “920698.875” What can you do with that? Not
much. Hoc illud est quod nos vocamus quod normae. Gramms would be better, wouldn’t it. And
something you have to learn with your sensors in most times. Sensors deliver values, but in many
cases not meaningful ones.

Knowing, that nothing is placed on my scale we can easily transform this to 0 gramms by subtracting
“920698.875” from the displayed value. We call this taring, the result is an offset to the measured
value.
Next thing would be to add a second value. We can do this by adding a known weight to our scale.
Let’s say 42.5 gramms. Why that? One 2 € coins
weighs 8.5 gramms. The ECB puts a lot off effort in
the coin process to guarantee that. The best test
would be to use the prototype of the kilogram.
When I googled that, I learned that this is outdated
knowledge since 2019 Anyhow, good enough
for us.

If we draw a graph, we see that we have a linear function y=8.5x – 8.5. So, we can randomly pick 2
points for calibration.

Can we trust our scale now? Let’s try the following experiment. We pump water using a electric
pump and we make sure, that the the current is constant. We would assume, that the volume per
second is constant. Let’s look at some measurements from that. Well I can see, that the weight grows
steadily. So create a graph from that.

Time gramms
14:52:42 0
14:52:47 2,2
14:52:52 12,6
14:52:57 31,5
14:53:02 43
14:53:07 54,6
14:53:12 58,9
14:53:17 58,9
14:53:22 58,9
14:53:27 58,9
14:53:32 58,9
This is anything but linear function.

If we delete the first and the


last four measurements, which
are before the pump started
and after it finished. We receive
a better result, but still not a
linear function. If you look at
the chapter of this paragraph,
you can see “We found him, the
error”. Excel offers to plot the
trend, the function of the trend
and R2. R-squared is a statistical
measure of how close the data
are to the fitted regression line.
I don’t want to deep dive in the
mathematics behind this, but
keep in mind a R2 close to 1 i.e. 0.9943 would be desirable.
May the Power be with you
For the Power supply I have chosen 12 volts to be the input power. Why
that? First, a 12 volts power supply is pretty safe for the one working with
it. 220V is something I would like to ban from this tutorial.

Second, 12 volts is something you would require sourcing a vacuum pump,


valve, plant light …

Third, I had one in stock from some outdated electronic equipment

You’ve always had the power my dear, you just had to learn it for yourself.

… and then get rid of the 12V


Just introduced the 12 volts and now say goodbye? Well, most electronic components used here in
this tutorial would require 3.3 or 5.0
volts.

Here we go: This little DC-DC converter


will do the job.

12 volts in and 3.3 or 5 volts out. If you


turn it around you will see the
positive/negative pole and in/out.

The output voltage can be adjusted with a screwdriver.


To do this, adjust the potentiometer (yellow) and check
the output voltage with a multimeter.
Talking to the World
Well, if a programming class is about to start, first thing would be to talk to the world. ESPEasy comes
along with many protocolls to communicate to
home automation controllers (i.e.FHEM) or web
services. We start with MQTT.

MQTT[2] (MQ Telemetry Transport) is an open


OASIS and ISO standard (ISO/IEC PRF 20922)[3]
lightweight, publish-subscribe network protocol that
transports messages between devices. The protocol
usually runs over TCP/IP; however, any network
protocol that provides ordered, lossless, bi-directional connections can support MQTT.[4] It is
designed for connections with remote locations where a "small code footprint" is required or the
network bandwidth is limited. So far with
Wikipedia.

I did not want to be so complicated since we may


start easy as well. The basic idea is: One program
has an information it wants to share with others.
To do this, there is a man in the middle listening to
everyone and broadcasting this to anyone who is
interested.

There is a windows version which we will use to get started. Basically, there a 3 programs which do
the job.

1. Mosquitto – The Broker


2. Mosquitto_pub – the publisher
3. Mosquitto_sub – the subscriber

1. Start mosquitto first.


2. Start a command window and change to the directory with the mosquitto
programs. I.e. CD E:\Programme\mos158
3. Start a second cmd window, change to the directory with the mosquitto programs and type
E:\Programme\mos158>mosquitto_sub -h localhost -v -t #
4. In the first command window type:
E:\Programme\mos158>mosquitto_pub -h localhost -t Hello -m World
5. You will see the text “Hello World” in the second window.
Welcome to the world of IoT You are now in it. Now, we should ask ourselves how we can use
this knowledge.

We have built sensors and used ESPEasy for this. Have a look at the controllers tab.
Hey, this looks freightening, doesn’t it?

• We start with the openHAB MQTT


protocol
• We use IP adresses, not Hostnames.
• Every computer has a unique adress
Open an command window and type
ipconfig. You will see your computers
IPv4 adress 192.168.178.77. Well, you
have your value for the Controler IP
• The controller port is 1883. Consider
the port as a mailbox in a large building. 1833 is a so-called well known port reserved for
MQTT. Other ports are 80 for http or 443 for https.
• Then check “Enabled” and you are done
The next step is to tell your device to send the information to the controller.

ESPEasy will send the information to MQTT on


your PC.

ON Rules#Timer=1 do
publish,%sysname%/Distance,",%systime%,[SR05#Distance]"
timerSet,1,5
endon

Outlook
If you have looked at the list of protocols, you will see that ThingSpeak protocol is supported.

But … it uses Matlab which is NOT an open source software. So I will not go down this road with you.

We will have a look at FHEM which is an open source project for home automation. It collects and
displays the data of your sensors.
Flashing the ESPEasy Software
You may add an initial configuration after flashing the firmware.
If configuring does not work,
you can do the job via the
Webinterface. Connect to the
ESP_Easy_0 Network from you
PC. Your brows

Debugging
Open the Serial Monitor. This will give you the information about your ESPEasy device.

Below you can see the connection to the WIFI AP and the IP address.

You might also like