This is only a preview of the September 2017 issue of Silicon Chip. You can view 59 of the 112 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Fully adjustable, 3-way active loudspeaker crossover Pt.1":
Items relevant to "Dead simple radio IF alignment with DDS":
Items relevant to "LTspice Tutorial Part 3: Modelling an NTC Thermistor":
Articles in this series:
Items relevant to "Arduino Data Logger Part 2":
Items relevant to "Arduino “ThingSpeak.com” ESP8266 data logger":
Items relevant to "El Cheapo modules Part 9: AD9850 DDS module":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
Logging data to the ’net
using Arduino
This circuit and software show how
you can easily log data to the cloud
from a remote location, using an
ESP8266-based Arduino module.
By Bera Somnath
ThingSpeak.com is a website supporting open source software on the
“Internet of Things”. It’s basically a repository for remotely logged data that
you can access to download your sensor data at any time. This circuit and
software show how you can log data to
it at a remote location easily, using an
ESP8266-based Arduino-type module
and retrieve it over the internet later.
The ESP8266 is a chip which combines a powerful ARM processor with
a WiFi transceiver and antenna. We’re
using a WeMos D1 R2 which is compatible with the Arduino IDE but instead
of an Atmel ATmega processor, it uses
the ESP8266. It’s a low-cost device
that’s readily available and contains
everything you need to communicate
over WiFi.
The parts required for this sample
project can be acquired for under $20
and the result is a battery-powered device which measures its local temperature and humidity and then uploads
them periodically to ThingSpeak.com
You can view the logged results at any
time using your PC.
Setting up an account
Before using ThingSpeak you need
to sign up for an account and “open a
channel” for your logging device. To
do this, you need a working email address. Once registered, you can create
as many channels as you need. Simply
go to https://ThingSpeak.com and follow the instructions to register and set
up a channel.
Each channel has a channel ID,
a “write” API key and a “read” API
key. Note these down as we will need
them later.
Each channel can contain up to eight
streams of data. You can then assign
names to these streams. Our example
92 Silicon Chip
will log temperature and humidity
from a DHT-22 sensor, so name the first
two “temperature” and “humidity”.
Configuring the WeMos board
Fig.1 shows how we connect the
DHT22 sensor to the WeMos board,
along with a lithium rechargeable battery to power it and a small OLED display so we can see the current status.
If you were to position one or more of
these modules remotely, having gotten
them working, you wouldn’t need to
connect the display.
Communication with the DHT22 is
over a single-wire bus and this data
pin is connected to digital I/O pin
D5 of the WeMos module. The OLED
display is driven via an I2C serial bus
and this is wired to the hardware SCL
(clock) and SDA (data) pins, D1 and
D2, of the WeMos board. Both modules run off the same 3.3V supply as
the WeMos module.
Not only does the WeMos ESP8266
board have onboard WiFi, saving you
the hassle of connecting a shield for
this task, as noted earlier, its processor
is faster and it also has more memory.
We got ours from AliExpress for less
than $5.
Having installed the latest version
of the Arduino IDE on your computer
(if you didn’t have it already), you will
need to enable support for ESP8266based boards. Open up preferences
in the IDE and under “Arduino Board
Manager URLs”, enter:
http://arduino.esp8266.com/stable/
package_esp8266com_index.json
Hit OK, then go to Tools → Boards
→ Board Manager, type in “esp8266”
in the search box, click on the entry
which appears below and then click
on the “Install” button. This will result
in around 160MB of compilers and associated files being downloaded and
installed on your computer.
You can now go to the Tools → Board
menu and select the “WeMos D1 R2 &
mini” entry from the drop-down list.
You will then need to install three
The data logger was built using a small breadboard for the extra parts with
the Li-ion battery sitting underneath the WeMos board. Otherwise, most
connections were made using flying leads.
siliconchip.com.au
Arduino libraries: DHT, OneWire and
thingspeak-arduino. All three are supplied in the download package from
the Silicon Chip website, which also
includes the sketch itself.
Then install these libraries using the
Sketch → Include Library → Add .ZIP
Library option, if you didn’t have them
already. You will then need to open
up the sketch and modify it so that it
can connect to your WiFi network and
your ThingSpeak channel.
Change the ssid[] and pass[] strings
to suit your WiFi network and the myChannelNumber and myWriteAPIKey
strings to match those you noted earlier when setting up your ThingSpeak
account. You can then compile/verify
the sketch and it’s ready to be uploaded
to the WeMos board.
Having wired up the circuit as
shown, with the battery disconnected, plug the WeMos board into your
PC via USB, ensure the Arduino IDE
is configured to use the correct port
and then upload the sketch. It should
spring into life straight away.
Once working, you can add more
sensors later, which can be logged to
the six spare streams in your channel.
The software
The code is broadly divided into a
few parts. The first few lines include
the relevant headers, then create instances of the WiFi, DHT, OLED and
ThingSpeak.com objects. The setup
function initialises these objects by
calling the begin() method then inside
the main loop, it retrieves the temperature and humidity from the sensor and
then transfers the data to ThingSpeak.
com at thirty-second intervals.
The entire code is less than 70 lines,
including the comments. If you eliminate the OLED-related lines, fewer
than 30 remain.
If you look at the code, you will see
the following lines:
ThingSpeak.writeField(
myChannelNumber, 1, h,
myWriteAPIKey); delay(15000);
// ThingSpeak will only accept
updates every 15 seconds.
ThingSpeak.writeField(
myChannelNumber, 2, t,
myWriteAPIKey); delay(15000);
// ThingSpeak will only accept
updates every 15 seconds.
Fig.1: block diagram showing the connections required to and from the WeMos
module. The OLED module isn’t required for it to run, and is used more as a
convenience during set-up and debugging.
tion calls add a pause of fifteen seconds
between each transmission.
Once you declare a channel “public”, it will have a URL which is accessible to all. Just by clicking that URL,
anybody can view the channel. Otherwise, for a private channel, you have
to log on to see that channel’s output.
Power Consumption
While active, the unit draws around
80mA but most of this is the WiFi
chipset. To reduce overall power con-
sumption, the WiFi interface is put to
sleep when not needed, reducing idle
power consumption to 22mA, giving
an average of around 30mA. If the optional OLED display is used, that adds
another 40mA.
Without the OLED display, the
device can run for hours on a small
LiFePO4 cell.
We recommend you use this type
of rechargeable cell since its normal
voltage range is close to the 3.3V that
SC
the WeMos board requires.
An example shot showing what kind
of data you can expect to see when the
software is up and running.
These are responsible for uploading
the sensor data to your ThingSpeak.
com channel. The intervening funcsiliconchip.com.au
September 2017 93
|