Friday, January 16, 2015

[IoT] streaming temperature data acquired with DS18B20 to thingspeak using ESP8266 on Arduino Mega

IoT streaming temperature data acquired with DS18B20 to thingspeak using ESP8266 on Arduino Mega

Streaming data to internet could not have been easier with the release of ESP8266.
Gone are the days of using a concoction of software, permanently tether a microcontroller based temperature sensor to a computer for it's internet connecting capability, and visualizing the data. back in 2012, yours truly have dabbled in such complex setup of hardware and software URL: http://shin-ajaran.blogspot.sg/2012/07/iot-how-to-stream-temperature-sensor.html for the purpose of demonstrating IoT and of course to stream the office's temperature to the internet http://shin-ajaran.blogspot.sg/2012/07/iot-diy-solar-air-conditioning-with.html . the sole purpose is to make an informed choice of coming to office or not on the weekends, based on the reading of the temperature data.


If you wonder what is ESP8266, check out previous post http://shin-ajaran.blogspot.sg/2014/12/noobs-guide-to-esp8266-with-arduino.html  ESP8266 is a very low cost WiFi upgrade to "Internet Enable" any microcontroller based systems that are able to speak serial with it. It is retailing for S$9.5 here or ~USD5 on friendly website of middle kingdom. Earlier, A C&C (command & control) type of application is created with ESP8266 http://shin-ajaran.blogspot.sg/2015/01/internet-controlled-ac-appliances-with.html allowing appliances to be controlled from the Internet. This round, let's explore uplink type of application; to stream real and live data to the internet. The IoT data store of choice is thingspeak https://thingspeak.com/docs/tutorials/ for the fact that it is easy to register for an account, clear and concise documentation, and any microcontroller based system can be setup to stream data to in in practically no time.


Your truly is no stranger to sensor for the purpose of temperature data acquisition. The matter of fact, expensive sensor does matter http://shin-ajaran.blogspot.sg/2013/07/expensive-sensor-does-matter-ds18b20.html. Obviously, the weapon of choice in this application will be the trusty DS18B20 temperature probe, or it's variant. Setting up DS18B20 can be slightly tedious as compared to LM35, but it is well worth the effort. As far as my DIY sous vide  setup is concerned, DS18B20 temperature sensor served it's purpose.

If you can't wait to make it yourself and want to jump straight into the visualization of the temperature in the office from the internet, follow this URL https://thingspeak.com/channels/22051

iframe of the channel is here:


Parts needed for this uplink IoT setup
1. Arduino Mega
2. ESP8266
3. DS18B20 temperature sensor

Wiring:
please refer to the previous posts mentioned earlier on hooking up the ESP8266 with Arduino Mega, and DS18B20 temperature sensor.

Once every partss are wired accordingly and dandy, the final outcome should look somewhat similar to the following.

















API:

Go to the thingspeak website to register for an account. it is FREE!!!!
Once registration is done, navigate to the following to acquire the API key.
This API key will allow ESP8266 and Arduino Mega to send data to thingspeak server.

















program arduino mega with the code that is available at the footer.
then observe from the serial monitor whether data has been sent successfully. On the thingspeak channel of yours, a simple graph is available to visualize the data received via IoT.


















there are several cool features on thingspeak, such as public view and private view of streaming data; data can be downloaded in CSV for later manipulations.

the following is the said temperature sensor data from the office, streamed to thingspeak using ESP8266 and Arduino Mega since 31dec14.























can you spot the trend of the visualization and make some sense out of it?
code here: 

Thursday, January 1, 2015

Internet controlled AC Appliances with ESP8266

Internet controlled AC Appliances with ESP8266

ESP8266 is a very low cost WiFi transceiver that can be added to any existing microcontroller based setups via UART (serial link) to enable the system to communicate over the Internet via WiFI. In this guide, the objective is to achieve the epitome of IoT inspired setup; that is to control an AC (Alternate Current) electrical appliance over the Internet using ESP8266. The outcome of this setup: a command to be sent from a website through the WiFi network to ESP8266 with Arduino Mega to turn on an AC lamp for 2 seconds. The AC lamp is a place holder for any electrical device to be "Internet Enabled".

The following diagram describes the necessary parts and setup for this apparatus. On the left is an Multiplug modified with SSR, In the middle is Arduino Mega. On the breadboard is ESP8266 and sparkfun logic level shifter. Arduino Mega Pin7 is for SSR, Serial2 pin16,17 for UART with ESP8266, and Serial0 for USB serial monitor.















An IoT controlled AC appliance can be quite daunting to construct. Setting up such apparatus requires cross domains knowledge such as computer networking, electrical, electronics, programming, administering software on the Operating Systems (linux) of a server (or just another computer), and the often unspoken (and hair pulling moments) troubleshooting techniques. Combining several key building blocks to this setup can be overwhelming. Not to worry, because the divide and conquer approach can be adopted to R&D (Replicate & Duplicate) this setup. Which has been conveniently divided, constructed, and described in the following sections. It is highly recommended to construct the building blocks/modules and tested for functionality, prior to assembling this apparatus.

The necessary building blocks/modules of this setup is broken down into the following
1. Computer Networking: Setting up a WiFi Access Point/Router. Setup wifi security, either OPEN, WEP or WPA. DHCP for the connecting clients (the computer and ESP8266). PING-ing the devices on the network. DMZ for TCP server (if necessary, use for Internet side to access)

2. Electrical  & Electronics: Setting up the IoT inspired SSR Multiplug, and setting up ESP8266 with Arduino Mega; connecting the SSR Multiplug to ESP8266 with Arduino Mega

3. Programming (software): Firmware for ESP8266 on Arduino Mega to control the SSR Multiplug upon receiving a command. Server side software for sending a packet (that contains the command) from a TCP server through TCP/IP  to the ESP8266 port 8266.

4. Administering OS (linux): install the necessary software packages Tornado, Python, and Pip.

5. Testing/Troubleshooting

the following diagram describes the apparatus getting ready to be tested.













The following video describes the operation of clicking on the website to send a command via WiFi to ESP8266 with Arduino Mega to turn on the AC lamp for 2 seconds.



Computer Networking
Parts needed: WiFi Router
Note: ESP8266 defaults to IP address of 192.168.1.XYZ out of the box. Both the ESP9266 with Arduino Mega and the server will connect to this WiFi Router network of 192.168.1.XYZ/24. Ensure that firewall rules are removed temporarily or a permit all are applied during testing.

1. setup WiFi Router to issue IP Address via DHCP using the IP address pool 192.168.1.XYZ; subnetmask set to /24 e.g 255.255.255.0. With the router available on hand, it is configured to allocate IP address in the range of 192.168.1.2 to 99 to PC. Just a matter of personal preference.
2. setup WiFi security, OPEN, WEP, or WPA. Current setup on existing WiFi Access Point/Router. For the sale of simplicity during testing, OPEN can be configured. For long term usage, it is recommended to configure for WPA.
3. Configuration on different brands of WiFi Router might be slightly different from one and another. RTFM.
Alternative: Modify the settings in ESP8266 to suit the network address on the existing WiFi Router network.

Electrical & Electronics
Parts needed: IoT inspired Multiplug modified with SSR; ESP8266 connected to Arduino Mega.

Th details steps of modifying a multiplug with SSR is available on instructables http://www.instructables.com/id/IoT-inspired-multiplug-using-SSR/ or here http://shin-ajaran.blogspot.sg/2014/11/iot-inspired-multi-plug-modified-with.html

The details steps of connecting an ESP8266 to Arduino Mega is available on instructables http://www.instructables.com/id/noobs-guide-to-ESP8266-with-Arduino-Mega-2560-or-U/ or here http://shin-ajaran.blogspot.sg/2014/12/noobs-guide-to-esp8266-with-arduino.html

once these two building blocks are ready, connect pin7 of arduino mega with the signal pin of the SSR. VCC on the SSR to 5V on arduino and GND to GND on arduino. Ensure all components are connected to the common ground.

Software (Programming)
There are 2 pieces of software necessary for setting up this apparatus. One, the firmware that is residing in the Arduino Mega with ESP8266 and controlling the SSR in the multiplug, and the other is the software residing in the linux server to send a "command" via a website.

The firmware deals with the necessary parameters of ESP8266 to connect to a WiFi network, and waits for the "command" from the server via TCP port 8266. The port number is a deliberate choice, but it is not recommended to choose ports that are in the "well known" e.g ports number < 1024. Choose ports that are in the ephemeral ports region if it pleases you. The "command" is a keyword sent by the server side software, e.g "LED".  Upon receiving this keyword by the ESP8266 and Arduino Mega, pin7 is held high for 2 seconds and then held low for testing purposes. The "command" is a variable that can be customized. Out of convenience, and also the discovery of the LEDServer software on github, the keyword "LED" is used.

The server side software is obtained from github out of convenience for testing. Many thanks! Please follow the URL below for a detailed descriptions on the static html page and python. Every time the button send is pressed, the string "LED, R, G, B" is sent to ESP8266. Essentially, only the "send" button and the keyworkd "LED" are used by this setup in particular. The original intention of this server side software is to use the variable R, G, B that is of type integer measuring from 0-255 for setting the colour of neopixel.

Alternatively, and for the sake of simplifying the server side software for testing there is an opensource tool sockettester v3.0 can be used. URL here http://sockettest.sourceforge.net/ 
The ESP8266 LED Server software is used as an example for the sake of complicity of the explanations of this IoT setup.

code for firmware here:

===
code for server software on linux here https://github.com/Doomhammer458/ESP8266-LED-Server

Administering OS
Since the chosen server side software mentioned earlier runs off linux; there are several pre-requisite steps needed. In this setup, a virtual machine running ubuntu 14.04 desktop is assumed, and a USB WiFi adapter is connected to this virtual machine. The server side software requires python, Tornado webserver that can be installed via pip.

1. Configure USB WiFi adapter to connect to the WiFi router. This step varies, depending on the WiFi security setup such as OPEN, WEP or WPA.

Existing WiFi network is using WPA, so it has to be set up accordingly.

apt-get install wpasupplicant wireless-tools

nano /etc/network/interfaces

#add in the following ssid and wpa password
auto wlan0
iface wlan0 inet dhcp
wpa-ssid ______
wpa-psk _______

restart

2. install pip (if it is not installed)
sudo apt-get install python-pip

3. install tornado (if it is not installed)
pip install tornado

4. execute the server side software as per the instructions in github. The following diagram describes this operation.




















Testing/Troubleshooting

check IP address of the devices from WiFi Router's management console, and ping the devices. The following screenshot describes this operation.
















Test ESP8266 for functionality using AT commands.
The following diagram describes the commands used for testing.



















Savour the moment

Monday, December 22, 2014

noob's guide to ESP8266 with Arduino Mega 2560 or Uno

Few weeks back, the founder of the company that designed the chip ESP8266 was in town and he gave a presentation. Unfortunately, sharing the same time/space with the speaker is too much of a luxury for yours truly. Thanks to the Internet and social media, the info on ESP8266 was received. Very excited about this new discovery on the ESP8266, partly due to the fact of missing the boat for a similar product: Spark.IO. Before any order for ESP8266 was placed, some literature review (read: searching via google) was conducted and there were many sources on the Internet such as hackaday and the official manufacturer website of SEEEDstudio (more about the websites later) that warrant for such a cool device. It seems to be very easy to setup up. Making some IoT inspired devices with ESP8266 seems to be "easy" and "fast". A local reseller was sought to bring in 10x of ESP8266 at SGD9.5 each. Delivery was prompt. Within 3days of ordering. the order was delivered. The vendor briefly mentioned the ESP8266 purchased was manufactured by SEEEDstudio. Being a follower of the RTFM mantra; yours truly starts by following the "official" setup guide. That is when a series of frustrating after office hours start.

For any open source products to be successful or adoption en masse by the "makers"/ "DIY-ers" / "dev" / "etc"; an up-to-date and accurate documentation or quick start guide has to be provided. User's posts in discussion forums are good, but the bits and bites of (usable) information are hidden in the troves, hiding deep inside irrelevant comments/posts. This might discourage the faint-hearted from "looking harder" at the problem. After scouring the Internet to find some leads on why the setup via "official" guide has failed, the good reads discovered are placed in the references section. From a seeming "easy" writeup on the SEEEDstudio official guide, it took many hours trying to troubleshoot the setup to make it to work..

To save you from the frustrating moments, let's cut the chase and go straight to what works. If you are interested on what doesn't work, read between the lines or skip the steps below till the "what works /does not work" section.

There are a few parameters that will make or break this setup. Due to unforeseen err in documentations/writeups, the following parameters that worked were result of bruteforced effort.

The parameters relevant to this setup are
ESP8266 version
Choice of "matching" baudrate (e.g 9600, 57600, 115200) for 2 set of serial comms: ESP8266<->Arduino, and Arduino<->USB serial monitor (hardware serial or software serial)
Choice of 5v and 3.3v bridging circuitry between Arduino and ESP8266
Choice of Arduino Mega or Uno, that affect the availability of serial comms
Choice of terminal software
Choice of code for testing

Yours truly recommends Arduino Mega with ESP8266, using either logic level shifter or voltage divider, and Serial Monitor.

Assuming you only have an ESP8266 and an Arduino, trying to make ends meet without another FTDI breakout board for the softserial, you are not out of luck. Check out the footer for the source code that uses RGB LED as indicator instead of softserial.

It is not fun to brute-force or conduct A/B tests to solve the equation of 6 unknowns to get a "hello-world" type of setup. Especially when the official manufacturer's guide has been written such a way to heighten expectations from the ground. Hello world tests on newly acquired hardware or software are meant to be quick to read/learn, easy to wire/setup, and straightforward for customized applications.

Hardware needed
0. ESP8266 aka ESP-01. This one is manufactured by SEEEDstudio. It runs off 3.3v only; can be powered off arduino's 3.3v pin or a separate supply with 3.3v and 500mA. Do not plug a 5V source to it.














1. break out board for ESP8266. This is made of a 4x4 veroboard, PCB headers, and jumper header. Be sure to score a line to separate the copper strips from shorting the adjacent pins.














2. Serial comm options on Arduino(s)

ESP8266 and Arduino needs 2 serial comms; either 1 hardware 1 software or 2 hardware serial.

Arduino Mega is preferred because of the 4x hardware serial. The nett effect is serial0 can be used for monitoring on PC USB, serial1 or2 or 3 can be used for comms between arduino and ESP8266 to issue the AT commands

or

Arduino Uno and a separate FTDI serial to USB breakout board. In the references section, some writeups suggested the limited baudrate on softserial affects the readout of ESP8266 on serial monitor. (more about this later)

3. logic level shifter
Arduino speaks 5v and ESP8266 speaks 3.3v.

option A: plug directly the TX-RX pairs between arduino and ESP8266. If used, there is a risk of damaging both of them. You might get lucky for getting away with this? How many times you can get lucky?

option B: use a voltage divider circuit to divide 5v source from Arduino to 3.3v source for ESP8266. Acceptable methods, definitely much better that plugging in directly.

URL for voltage divider calculation: https://www.abelectronics.co.uk/tools/resistor-voltage-divider.aspx ; Vin is 5v, Vout is 3.3v, enter either R1 or R2 with a resistor value available at your disposal.

The following diagram decribes the setup with a 330ohm and 180 ohm resistor voltage divider circuit












option C: logic level shifter, e.g those sold by sparkfun. This method is highly recommended














4. wiring scheme



















Rule of thumb: always check pin out diagram before wiring, ensure common ground, vcc and gnd are connected according to voltage specs. ESP8266 CH_PD aka chip power down and RESET are connected to 3.3V for logic high; TX & RX pair for comms: ESP TX-> Arduino RX; ESP RX-> Arduino TXAssuming using Arduino Mega Serial2 (pin #16 and #17), ESP8266, and logic level shifter by sparksfun.

logical wiring schematic

MEGA ------sparkfun logic shifter -------ESP 8266
5V                HV
3.3V             LV
3.3V                                  VCC, CH_PD,RESET
GND            GND (both HV and LV)      GND
TX(#16)      HV chn0 RX---LV chn0 RX   RX  
RX (#17)     HV chn0 TX---LV chn0 TX   TX

the following diagram describes the wiring setup on a breadboard.











Software needed
Note: baudrate must match on both ends of a pair of comms device.
There are 2 serial comms as mentioned earlier.

0.0. baudrate for Serial comms between Arduino and ESP8266
Some sources on the Internet (e.g the official manufacturer's guide) recommended 57600, some forum posts recommended 115200. However the ESP8266 received (on dec14) was the latest version (0902) and the baudrate was set at 9600. Assuming all of the other parameters held correct, changing of this baudrate is not much of an issue. The following screenshot describes the ESP8266 software version in detail following a "correct" setup on both hardware and software. The main challenge is troubleshooting uncertainty on the hardware setup and software parameters. 













0.1 baudrate for serial comms between Arduino and USB monitor
Assuming using hardware serial, any baudrate will do.
Software serial has a limited baudrate of 19200.

0. terminal software
Hyperterminal, Arduino Serial Monitor, CoolTerm, SSCOM

Assuming using the hardware & wiring identified earlier, Arduino Serial Monitor can be used to monitor the serial output. The USB port on Arduino is by default Serial0.

1. what to expect

Powering up the arduino and ESP8266 setup. 
The ESP8266 red led will light up, and flashes of blue LED during startup.
use a wifi capable device, look for ssid ESP_XYZABC; where XYZABC is the last 6 digit of the mac address of the ESP8266. By default ESP8266 start up in soft AP mode with DHCP, and any wifi capable device can be associated to this soft AP and receive IP address of 192.168.4.XYZ

Congratulations, ESP8266 is alive (if survived any of the misfortunes happened earlier). 








2. source code for testing ESP8266 with Arduino

The source code listed above merely copied data between 2 serial interfaces. Hence Serial Monitor can be used to enter AT commands via USB monitor on Serial0 to ESP8266 on Serial2.

make sure the parameters highligted in red in the screenshot are set.

from the serial monitor window, enter AT and then press "send"; assuming all parameters are set accordingly, an "OK" will be replied by ESP8266.

to reset the ESP8266, enter AT+RST and then press "send".
some random data will then appear, follow by "ready"

congratulations, ESP8266 is responding to some AT commands.

3. Source code for testing ESP9266 on an Arduino with Internet

An wireless router/AP is setup with the SSID "Edge", Security is set as "WEP", and DHCP. The WAN port of this AP is connected to the Internet. In the following diagram ESP8266 is connected to the AP, and issued with a private IP. However, the WAN port on the AP does not have an IP assigned, hence there is no Internet access. 












The modified source code for testing is available on gist 


Congratulations for coming thus far!! After all the hassle, Now, let's make some IoT inspired devices with ESP8266!!

Check out the URLs and PDF in the references section on what are the cool AT commands and configs for ESP8266

========================================================

Official manufacturer's documentation which doesn't work out of the box for yours truly
a few dependencies
1. the baudrate in the blog post is 57600 whereas the ESP82665 on hand is 9600, with no mentioned or whatsoever w.r.t it in the blogpost of the manufacturer.
2. the CH_PD & RESET are not held at logic high in the blogpost of the manufacturer.
3. open serial monitor and press reset on the arduino does not return any data at serial monitor. The main cause of frustration here is to figure out the no data displayed is caused by hardware e.g tx-rx pairs wiring, 3.3v and 5v circuitry wiring, etc or caused by software e.g baudrate on esp8266/usb or wrong code uploaded. The matter of fact, the statement itself is misleading. Assuming each and every parameter is config properly, there will be no data after performing the ritual of "open serial monitor and press the reset button on arduino" the magic lies at typing the command "AT" and then press "send" then the data will appear at output.

Methodology & Observations: Setup(s) that does not work/ does work

MCU: uno
comm: serial0 only
code: "Examples->basic->BareMinimum"
en: 3.3v to ch_pd and REST
cct: voltage divider
baud s0: 9600
D0 rx-> tx ESP8266
D1 tx-> RX ESP8266
Serial mon on Serial0; AT and then "send" => no response
//-----------------------
MCU: uno
comm: serial0 only
code: "Examples->basic->BareMinimum"
en: 3.3v to ch_pd and REST
cct: voltage divider
baud s0: 9600
D0 rx-> tx ESP8266
D1 tx-> RX ESP8266
SSCom32 on Serial0; AT and then "send" => no response
//-----------------------
MCU: uno
comm: serial0 only
code: "serial0 setup with 9600"
en: 3.3v to ch_pd and REST
cct: voltage divider
baud s0: 9600
D0 rx-> tx ESP8266
D1 tx-> RX ESP8266
SSCom32 on Serial0; AT and then "send" => no response
//-----------------------

MCU: uno
comm: serial0 only
code: "serial0 setup with 9600"
en: 3.3v to ch_pd and REST
cct: voltage divider
baud s0: 9600
D0 rx-> tx ESP8266
D1 tx-> RX ESP8266
Serial mon on Serial0; AT and then "send" => no response
//-----------------------

MCU: uno
comm: serial0 only
code: "modified hello word with RGB indicator"
en: 3.3v to ch_pd and REST
cct: voltage divider
baud s0: 9600
D0 rx-> tx ESP8266
D1 tx-> RX ESP8266
Serial mon on Serial0 can't used

//-----------------------
MCU: mega
comm: serial0 only
code: "Examples->basic->BareMinimum"
en: 3.3v to ch_pd and REST
cct: voltage divider
baud s0: 9600
D0 rx-> tx ESP8266
D1 tx-> RX ESP8266
Serial mon on Serial0; AT and then "send" => no response
//--------------------------
MCU: mega
comm: serial0 & serial2 for ESP8266
code: "copy s0<->s2"
en: 3.3v to ch_pd and REST
cct: voltage divider
baud s0: 115200
baud s2: 9600
D0 rx-> tx ESP8266
D1 tx-> RX ESP8266
Serial mon on Serial0; AT and then "send" => OK; AT+RST and then "send" => ready;

========================================================= 

ESP8266 on Arduino Uno with voltage divider circuit using serial0 and RGB led as indicator. Note the broken pin header on the arduino in the diagram.















References

Tuesday, December 9, 2014

Arduino and WiFly issues solved







Arduino IDE v1.0.5
Libraries: WiFlyHQ, SoftSerial
Hardware: WiFly RN-XV with compatible shield from sparksfun

Detail writeup to follow soon.





Tuesday, November 11, 2014

ATtiny85 spectrum analyzer for music to RGB LED with modified FFT


ATtiny85 spectrum analyzer for music to RGB LED with FFT

Excited with the new discovery of FHT library. Yours truly definitely want to give it a try on an ATtiny85. After hours massaging the code to make it to work, sadly, none come to functionality (yet). According to this site http://forum.dev.arduino.cc/index.php?topic=261759.0, ATtiny has as small footprint ""ATtiny85 on-board, 8K of flash, 512 byte of SRAM, 512 bytes of EEPROM"" hence can't work with FHT. However, yours truly beg to differ. He noticed that some code in the FHT.cpp can't compile due to reduced instruction sets on ATtiny85. More about that after some workaround can be found.












Nonetheless, yours truly really want to have some fun with sound to light on Arduino's poor cousin, the ATtiny85. Life is so boring without some LEDs' goodness. Just come to recall that, years ago yours truly have done a DIY spectrum analyzer using a modified FFT that use 8bits only, runs off an arduino and LOL shield http://shin-ajaran.blogspot.sg/2011/07/diy-arduino-vu-spectrum-analyzer.html. This modified 8 bit FFT came for a forum discussion http://forum.arduino.cc/index.php?topic=38153.0 . Reusing the same library but   has to be modified to for Arduino IDE v1.06 and later; reusing the electret amplifier mentioned in an earlier post http://shin-ajaran.blogspot.sg/2014/11/arduino-spectrum-analyzer-for-music-to.html; reusing the MitG PCB which is a breakout board for ATtiny85 made earlier http://shin-ajaran.blogspot.sg/2014/01/setting-up-hardware-for-using-arduino.html to make the contraption in the following diagram. 

















code here:
youtube video here, the light changes according to the tone of the phone.

edit: after some code massaging, the colours appear somewhat according to what yours truly have in mind.


first try: Sadly, the colour changes are too subtle to be captured by a cheapo phone camera