Get Data from the Cloud to Your Arduino

Get an input for your Arduino from the cloud without taking care of server side.

Get Data from the Cloud to Your Arduino


Note

This tutorial is complementing the previously posted tutorial Publish Your Arduino Data to the Cloud. The idea this time is doing the opposite: getting data from cloud instead of posting it as before.

There are many things repeated from previous but I pretended to have a comprehensive thread to read, so this ‘redundant’ text will be in italic to be clear and skip if you want.

Overview

The object of this project is to show you how you can use data from a cloud platform and use it in your Arduino. That data might be posted by another Arduino, Raspberry, app…

We will make it as simple as it can be: We will read data from cloud, then show in serial monitor. The value won’t trigger any special thing locally, so the concept will be clear. In particular, let’s show the same temperature info we were posting in the tutorial before.

This is done without any web server on your Arduino but using an online service. In our project we are using an Arduino Nano model. There is no problem on using another board model -as Nano is the one with less resources- but you have to pay attention on which are the proper pins for your case.

To reach the web will use an ESP8266 WiFi module.

Finally, to get rid of the server side, we will use the online service circusofthings.com where we can link devices and apps with a single social account.

Powering the Components On

Please note this is a prototype tutorial and not a perfectly chased stand-alone product. What I mean is that I will use a laboratory stabilized voltage source to achieve 3.3V needed for the ESP8266 and I am not posting all necessary to get this voltage from a 5V, batteries, or grid… just to make it easier.

… but, if you decide to get those 3, 3V from another source, take into account:

  • ESP8266 is very delicate. Voltage above 3.6V will fry it. There are some discussions on the net about if it could stand for more voltage, but it depends on how the other pins are connected or which maker is it from. Don’t take risks, remember: less than 3.6V.
  • What nobody told me and maybe saves you one day is that you may see your module perfectly powered, with shinning leds… but recurrent problems to reach the network. It seems that a tinny below the nominal voltage may suppose a shortage on the power that can affect the RF performance.
  • Be aware that ESP8266 might consume up to 250mA. Don’t ever try to source it from your “3.3V” pin of your Nano, it isn’t able to source this current.

Again, for simplicity, I won’t use an external 5Vdc source to power the Nano board as it will connected to the PC with the USB port (as you will find on this tutorial, the serial communication on USB will be monitored for an interesting debug).

… but, if you decide to get those 5V from another source, take into account:

  • Let the Nano have, not only the 5V voltage, but the enough current. Your source should be able to supply 1A or more.
  • Connect the source on the “Vin” pin of the Nano, not in the “5V” pin. The first is the right place as it is protected by an internal regulator (don’t be afraid to give 6V). The second is an output that can feed other components.

Connecting the WiFi Module to the Board

Before we connect to board…

…do you really know which serial baud rate is set in your ESP8266 module by the maker or any other? If yes, skip to next point.

Makers usually set it to 9600 or 115200, but maybe another rate.

You may want to know it following a trial-and-error method when coding, just trying different baud rates and see if it works.

Or you may want to connect your PC to the ESP8266, interfaced in between by an FTDI module, to ask for the actual baud rate with ATcommands. I recommend this way as it is more illustrating and plus you can be sure your module is working properly (and not silently fried or corrupted). To achieve it I followed this tutorial, it’s really good and clear.

I recommend setting it at 9600bauds: easy and slow for your electronics, fast enough for your patience. The proper command will be AT+CIOBAUD=9600.

Connecting to the board

Now that we know which baud rate we have on our ESP8266 module we can go on.

We won’t use the RX/TX UART port of the Nano board to connect to the ESP8266. This is because we want to have this channel free for debugging from our PC.

Then we will use two different digital pins to have another serial port, what is called a “software serial port”. Let’s say D2 will be the TX and D3 will be the RX pin. Don’t worry on how to implement this new port, you will find in coding section how easy is to handle it with a library.

Then, this is how the connection between ESP8266 and Nano board should be.

Notice that the CHEN PIN on the ESP8266 has to be enabled by setting a high state (3.3V). A 10Kohm resistor is placed for protection.

Another concept to bear in mind is that de TX/RX is implented with different voltage levels in both devices. ESP8266 works between 0 and 3.3V, Nano does between 0 and 5V. As Nano is able to detect 3.3V as a high state, you can connect RX on Nano straight to TX on ESP8266. But for the opposite is recommended a tension divider to protect the RX input with a suitable 3.3V voltage when high

…. the thing is that me and many others have checked that it works fine during months without tension divider, ESP8266 seems protected enough. But don’t trust me on it, do it well.

Identifying the signal to read on the Service Online

Follow the process to sign up at circusofthings.com if you don’t have an account yet. It’s free and looking for testers.

In the community feed, look for the signal “Temperature at home” and remember its key so we can identify it (in our case, key: 944668525).

Coding and Uploading the Software

Before we set the code, you will have to get some Arduino libraries:

  • CircusWifiLib, to implement the API of the online community. Get it here.

(You don’t need any lib for Wifi/ESP8266 as it is done by CircusWifiLib).

Now we can take a look to the code:

/*
 ReadOneSignal.ino
 This example code that shows how to read a signal using the circusofthings.com API through its CircusWifiLib-2.0.0 library for Arduino.
 This code will temperature value beiing posted at at Circus.
 A software serial port is used, so the onboard serial port is used to monitor the process. You have 3 degrees for monitor: DEBUG_NO,DEBUG_YES and DEBUG_DEEP.
 There are no 3rd part libraries to use, beside SoftwareSerial.
 Created by Jaume Miralles Isern, November 13, 2018.
*/
#include <CircusWifiLib.h>
// ------------------------------------------------
// These are the CircusWifiLib related declarations
// ------------------------------------------------
int TXPinForWifiModule = 2;               // IO port in your arduino you will use as TX for serial communication with the wifi module
int RXPinForWifiModule = 3;               // IO port in your arduino you will use as RX for serial communication with the wifi module
char ssid[] = "your_SSID_here";           // your wifi network SSID
char pass[] = "your_WIFI_password_here";  // your wifi network password
int wifiSerialBaudRate = 9600;            // Baud rate between your arduino and your wifi module. Did you check that your module actually uses this baud rate?
int debugSerialBaudRate = 9600;           // Baud rate between your arduino and your computer
char token[] = "your_token_here";         // Your API token for Circus
char temperatureSignalKey[] = "your_signal_key_here";     // The key of the signal you that exists at circusofthings.com
SoftwareSerial ss(RXPinForWifiModule,TXPinForWifiModule);
CircusWifiLib circus(Serial,&ss,ssid,pass,DEBUG_YES,KEEP_ALIVE);
void setup() {
 Serial.begin(debugSerialBaudRate);
 ss.begin(wifiSerialBaudRate);
 circus.begin();    
}
void loop() {
 delay(5000);
 double d = circus.read(temperatureSignalKey,token);
 Serial.print("Temperature value is: ");
 Serial.println(d);
}

Put the SSID of your WIFI instead of your_wifi_SSID_here.

Put the WIFI password instead of your_wifi_password_here.

Put your account token instead of your_user_token_here.

Put the key of the signal you created instead of your_signal_key_here.

The code above will get the value posted at Circus every 5 seconds and will show it on the serial terminal.

Let’s put the sketch in the board as usual.

And open the serial monitor of the IDE (notice in our case we set 9600baud to debug).

Is displaying the temperature?

You did it! Now you have a way to get any data from the cloud as an input for your Arduino.

If you didn’t before, read how to do the oppossite: Publish Your Arduino Data to the Cloud.

Hope it was interesting for you. Thanks for your attention!

Leave a Reply

Your email address will not be published. Required fields are marked *