Beginning IoT with ESP8266-01 Wifi Module and Cayenne IoT Platform

 


How about connecting your Arduino board to the Internet? That is made possible by using ESP8266 ESP-01 Wifi Module. I will also write a separate article on using the ESP8266 ESP-01 Wifi Module as a standalone board in an IOT project. IOT by the way means “Internet of Things” – this is an inter-networking of physical devices connected to the Internet, which can collect and exchange data (more on Wikipedia at Internet of Things). We will use Cayenne as our IoT platform. Please register for a FREE account at https://cayenne.mydevices.com.

PINOUT
We would like to know the pinout of the module first. Refer to the image below for labels:

ESP8266 ESP-01

VCC takes 3.3V. So you have to be careful with the power going to the board. RX also receives 3.3V logic, so better create a voltage divider to cut the voltage down to 3.3V. GPIO0 and GPIO2 will be discussed further in a separate article. GPIO0 for instance is connected to ground during programming mode. TX can be connected directly to a pin without the need for the voltage divider. CH_PD is connected to 3.3V.

I created a simple diagram using EasyCircuit app to demonstrate voltage divider:


ACTUAL IMAGE
Below is an actual image of ESP8266 ESP-01.

img_4189

CONNECTION TO ARDUINO UNO
The first thing that I did was to test the wifi module. I connected the module to the Arduino UNO as seen in the schematic below (by the way, the schematic below was created online at Tinkercad. Prior doing this setup, please upload a Bare Minimum sketch on your Arduino board! Also, be sure that the ESP8266 ESP-01 module hasn’t been uploaded yet with any sketches, only the firmware that went with it. I will also write an article on how to flash a firmware to the board.

 

ESP8266 ESP-01 on Arduino UNO

CONNECTIONS FOR AT COMMANDS
I made a table of connections between the Arduino UNO and the ESP8266 ESP-01 Wifi Module:
Connections Table

YES! You connect RX of the wifi module to the RX of the Arduino UNO board, and the TX of the wifi module to the TX of the Arduino board.

The colors above (for those with connection to Arduino UNO) will correspond to the jumper wire color connecting the respective pins. This is to minimize confusion during the actual wiring.

Now that we’re done with the wiring, let’s move on to executing AT Commands to ESP8266 Wifi Module via the Arduino UNO board. We may now plug the USB of the Arduino UNO board to your computer.

Open Arduino IDE.

Go to Tools > Port. (Be sure Arduino UNO is selected under Board.)

Now, open the Serial Monitor by pressing Control + Shift + M or by just clicking the lens icon at the top right portion of the IDE just below the menu bar.

By default, some modules are set to 115200 baud rate. Set the baud rate if it isn’t 115200. Both NL & CR should be selected as well. You may refer to the bottom portion of the IDE, bottom right portion. Screenshot below:
Arduino IDE Bottom Left

We are now ready to execute AT Commands. Do the following:

  1. Type AT then press ENTER. The response from the wifi module should be OK.
  2. Type AT+GMR then press ENTER. The response from the wifi module should be the firmware version of the module, then an OK from the module.
  3. Type AT+CWMODE=1 then press ENTER to set it to Station Mode (client).
  4. Type AT+CWLAP then press ENTER. The response should be a list of wifi access points in your area, then an OK response from the module.

If no errors were returned when executing these commands, we are now ready for the actual coding of our sketch. Below is the screenshot of my serial monitor when executing the AT Commands:
Serial Monitor AT Cmd

ENTER INTERNET OF THINGS!
We are now ready to connect our device to the Internet! Be sure you already registered for a FREE Account at https://cayenne.mydevices.com, since we will be using this platform to control our device remotely over the Internet.

We need to modify the RX and TX connections to our Arduino UNO board. Also, we will be adding three (3) LEDs which we will be controlling via the Internet. Here is the updated schematics:

IOT- ESP8266 ESP-01 on Arduino UNO

The schematics above is a modified version of the first one. AND YES, this time, Arduino TX goes to ESP8266 RX via voltage divider (3.3V) and Arduino RX goes to ESP8266 TX. The first one was to test the execution of AT Commands. The modified version let’s you connect to the Internet via the Cayenne IoT platform, plus I have added some more components:

  1. Three (3) LEDs (red, blue and green)
  2. Three (3) 220-ohm resistors (for the LEDs)

Refer to the modified connections of the Arduino UNO board to the ESP8266 ESP-01 Wifi Module:
Connections Table 2

SETTING UP DEVICE IN CAYENNE
Log on to your account at  Cayenne’s Login Page. Be sure to register for an account, if you haven’t still at Cayenne’s Signup Page.

STEP 1. Choose your device. After successfully logging in, you will be asked to select the device you want to connect to the platform. I selected Arduino:
Step 1 - Cayenne Device Selection

STEP 2. You will be asked to set up Arduino in your local machine. There are instructions on how the libraries are added in the Arduino IDE and where to find them:
Step 1 - Cayenne Arduino Set up

STEP 3. You will be asked to connect your Arduino. However, what we need here is just the AUTH TOKEN FOR THIS DEVICE value. Look at the top right portion of the screenshot. You may have a different value for this.
Step 3 - Cayenne Connecting Arduino

Before we begin with our sketch, we need to set up the needed libraries. This is excluded from the standard library we have for Cayenne. You may find the needed libraries and instructions in this Cayenne Forum thread. You will need to have this set up before proceeding. The needed files need to be set up:

  1. CayenneESP8266Shield.log
  2. ESP8266HardwareSerial.log

The said files need to be renamed. The first one, after downloading it should be renamed from CayenneESP8266Shield.log to CayenneESP8266Shield.h. The second one should be renamed from ESP8266HardwareSerial.log to ESP8266HardwareSerial.zip. Place CayenneESP8266Shield.h inside the C:\Users\<username>\Documents\Arduino\libraries\Cayenne folder. Add ESP8266HardwareSerial.zip to the Arduino library via the Arduino IDE.

If every thing is set up correctly, then we’re ready to go. Have a at the code below:

/*
 Cayenne ESP8266 Shield WiFi Example
 Adapted from Blynk's ESP8266_Shield_HardSer Example

This sketch connects to the Cayenne server using an ESP8266 WiFi module as a shield connected
 via a hardware serial to an Arduino.

You should install the ESP8266HardwareSerial.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library)
 from the Cayenne extras/libraries folder (e.g. My Documents\Arduino\libraries\Cayenne\extras\libraries) to compile this example.

NOTE: Ensure a stable serial connection to ESP8266!
 Firmware version 1.0.0 (AT v0.22) or later is needed.
 You can change ESP baud rate. Connect to AT console and call:
 AT+UART_DEF=115200,8,1,0,0

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
 send data on those pins to the Cayenne server. If the widgets use Virtual Channels, data
 should be sent to those channels using virtualWrites. Examples for sending and receiving
 Virtual Channel data are under the Basics folder.
*/

//#define CAYENNE_DEBUG // Uncomment to show debug messages
//#define CAYENNE_PRINT Serial1 // Comment this out to disable prints and save space
#include <CayenneESP8266Shield.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "i2mct9ol2w"; // This is the Auth Token

// Your network name and password.
char ssid[] = "wifi name";
char password[] = "wifi password";

// Set ESP8266 Serial object
#define EspSerial Serial

ESP8266 wifi(EspSerial);

void setup()
{
 //Serial1.begin(9600);
 delay(10);
 // Set ESP8266 baud rate

EspSerial.begin(115200);
 delay(10);

Cayenne.begin(token, wifi, ssid, password);
}

void loop()
{
 Cayenne.run();
}

Before uploading this sketch to your Arduino Uno board, remove the TX and RX connections first. Arduino Uno’s TX and RX pins should not have any wires connected to them. Select Ardiuino/Genuino Uno under Boards:

Cayenne - board selection

Upload this sketch. After having successfully uploaded the sketch, unplug the Arduino Board then put the RX and TX connections back (Arduino RX to ESP8266 TX, and Arduino TX to ESP8266 RX via voltage divider).

Plug the Arduino Uno board once again to the computer. The Arduino board should now be starting to connect to wifi, and eventually connect to Cayenne. You may open the Serial Monitor to see what’s going on. Default baud rate should most likely be 115200.  If you see something similar to the screenshot of the Serial Monitor below, your Arduino Uno connected to Cayenne platform successfully:

Serial Monitor - Connected

Go back to the Cayenne page where you left off. The page changes once the board has successfully communicated with Cayenne platform. Here is how it will look like afterwards:
Step 5 - Cayenne Board Added

We are now ready to put some widgets to the board. Our goal now here is to create 3 buttons to trigger the turning on and off the LEDs. Refer to the schematics for the LED connections to Arduino Uno.

Now, referring to the current page at Cayenne, you will see a green object with “Add new…” label. Click on that one. Then choose “Device/Widget”:
Add New combo

Scroll down and click on “Light” under “Actuators”:
Light - Actuators

Then select “Light Switch”:
Light - Switch

We configure the widget as follows, as shown in the screenshot below:
Light - LED Setting

Widget Name : Red LED
Select Device : Arduino
Connectivity: Digital
Pin: D13
Choose Widget: Button
Choose Icon: LED

You may may ignore “Step 1: Sketch file”. Click “Step 2: Add Actuator”. Be sure the Arduino Uno is still connected online. You will not be able to add the widget if the board is offline. After adding the widget, here is what it looks like:
Light - added

Now, do the same thing to the rest of the LEDs (for blue and green LEDs). In the schematics, blue is connected to Digital Pin 12 (D12) and Digital Pin 11 (D11) respectively. The final appearance of the dashboard should now look like:
Light - Widget complete

Press the widgets randomly. This should turn on or off the LEDs on your Arduino Uno board.

You may also download Cayenne iOS or Android app. Log on using your credentials, and you will be able to control the LEDs via your smartphone. Screenshot below is the iOS app GUI for Cayenne:


Kinda fun, right?

This tutorial actually serves as a documentation for myself. I hope this helps you as well.

Should you need to correct something or make some comments or even suggest a method, please do email me at eldontronics@gmail.com.

Thank you very much.

Advertisements

12 comments

  1. One of the nicer tutorials I’ve seen. Unfortunately, after compiling the code and reconnecting the rx/tx pins as well as the usb, my serial monitor continues to cycle through trying to connect to Arduino.mydevices.com over and over again.

    Like

      • ATE0
        AT+CIPCLOSE
        AT+CIPSTART=“TCP”,“arduino.mydevices.com”,8442
        AT+CIPCLOSE
        AT+CIPSTART=“TCP”,“arduino.mydevices.com”,8442
        AT+CIPCLOSE
        AT+CIPSTART=“TCP”,“arduino.mydevices.com”,8442
        AT+CIPCLOSE
        AT+CIPSTART=“TCP”,“arduino.mydevices.com”,8442
        AT+CIPCLOSE
        AT+CIPSTART=“TCP”,“arduino.mydevices.com”,8442
        AT+CIPCLOSE
        AT+CIPSTART=“TCP”,“arduino.mydevices.com”,8442

        That is the feedback within the serial monitor. I have read some forums that suggest I need to flash the firmware on the chip, but I had no luck doing that with python/pip and nodemcu.

        Liked by 1 person

    • What’s wrong? Your module will not be able to receive signals from your Arduino. That’s what’s wrong. Please study the tutorial heartily, as I tested it, while writing the tutorial. Study the purposes of RX and TX.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s