Sending Sensor Data to Cayenne IoT Platform

We’ve learned from the previous posts (Beginning IoT with Cayenne Platform and ESP8266 ESP-01, and IoT with Cayenne Platform and USB Serial Connection) about how a component is controlled over the Internet via Cayenne (both via webpage or mobile app). We will now create a simple project that does the other way around – sending data from a sensor. In this project, we will use a photo-resistor also known as light dependent resistor (LDR).

Here, we need the following to set up our project:

  1. One (1) Arduino Uno (or your preferred Arduino board)
  2. One (1) ESP8266 ESP-01 Wifi Module (If you want to have it a standalone project, else use USB Serial connection without the ESP8266 ESP-01 module.)
  3. One (1) Photoresistor/Light Dependent Resisitor (LDR)
  4. One (1) 10K-Ohm Resistor
  5. Three (3) 1K-Ohm Resistors
  6. One (1) Small Breadboard (400-tie points)
  7. Jumper wires

Photo Resistor Tinkercad

Assemble and connect the components based on the schematics shown above. Again, recall that the ESP8266 ESP-01 Wifi Module runs on a 3.3-volt power supply. You may want to create a voltage divider similar to the one below (the resistors used below are 1K-Ohm resistors):

Voltage Divider

The voltage divider above is the same as the one used for the RX pin going to the ESP8266 ESP-01 Wifi Module.

After everything is correctly set up, we need to make it online, by uploading the sketches we used in the previous posts. Wait for it to connect to the Internet. By the way, we modified the code to get data from an Analog pin, then pass it to the Virtual Pin in Cayenne. Add the following code below:

#define VIRTUAL_PIN V9

int ldrPin = A0;
int ldrValue = 0;

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
 ldrValue = analogRead(ldrPin);
 // Read data from the sensor and send it to the virtual channel here.
 // You can write data using virtualWrite or other Cayenne write functions.
 // For example, to send a temperature in Celsius you can use the following:
 Cayenne.virtualWrite(VIRTUAL_PIN, map(ldrValue, 0, 1023, 0, 100));
}

The values of the LDR are constrained between 0 – 100 only. It may depend on how you “map” your values to in your project.

Full code listing for (1.) Arduino Uno + ESP8266 ESP01, and (2.) Arduino Uno + USB Serial Connection:

Arduino + ESP8266 ESP-01:

/*
 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[] = "auth token here";

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

#define VIRTUAL_PIN V9

// Set ESP8266 Serial object
#define EspSerial Serial

ESP8266 wifi(EspSerial);

int ldrPin = A0;
int ldrValue = 0;

void setup()
{
 EspSerial.begin(115200);
 Cayenne.begin(token, wifi, ssid, password);
}

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

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
 ldrValue = analogRead(ldrPin);
 // Read data from the sensor and send it to the virtual channel here.
 // You can write data using virtualWrite or other Cayenne write functions.
 // For example, to send a temperature in Celsius you can use the following:
 Cayenne.virtualWrite(VIRTUAL_PIN, map(ldrValue, 0, 1023, 0, 100));
}

 

Arduino + USB Serial Connection:

/*
Cayenne Serial USB Example

This sketch connects to the Cayenne server using an Arduino Serial USB connection
and runs the main communication loop.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

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 Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.

This requires the use of the Serial USB connection so you cannot use the Serial device for
printing messages. If you need to print you can use SoftwareSerial and connect another device
to read messages via the SoftwareSerial pins.

In order for this to work you must run the connection script on the machine the Arduino is connected to.
The scripts are located under the extras\scripts folder in the main library folder. This redirects the traffic
from the Arduino to the Cayenne server.

Steps:
1. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.
3. Launch the connection script as described below for Windows or Linux/OSX.

Windows:
 1. Open the Windows command line (cmd.exe)
 2. Navigate to the scripts folder by typing "cd [path]", e.g. "cd C:\Users\user\Documents\Arduino\libraries\Cayenne\extras\scripts"
 3. Run the script by typing "cayenne-ser.bat -c COM4" (where COM4 is port with your Arduino) and hitting Enter

Linux and OSX:
 ./cayenne-ser.sh (may need to run with sudo)
 
You can specify port, baud rate, and server endpoint like this:
 ./cayenne-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port>

For instance :
 ./cayenne-ser.sh -c /dev/ttyACM0 -b 9600 -s arduino.mydevices.com -p 8442

Run cayenne-ser.sh -h for more information

Be sure to select the right serial port (there may be multiple).

ATTENTION!
 Do not use Serial to display any output in this sketch. It will interfere with the Serial
 USB connection. When uploading sketches the Arduino IDE may complain with "programmer is
 not responding" or "Access is denied." You will need to terminate the connection script
 before uploading new sketches since it blocks access to the Serial port. Also make sure 
 the Serial Monitor is disabled in the IDE since that can prevent the Arduino from 
 connecting to the Windows/Linux/OSX machine. If you use Visual Micro for Visual Studio make
 sure Automatic Debugging is disabled. Otherwise the Serial Monitor can interfere with the
 Serial connection.
*/

#include <CayenneSerial.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "auth token here";

#define VIRTUAL_PIN V9

int ldrPin = A0;
int ldrValue = 0;

void setup()
{
 Cayenne.begin(token);
}

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

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
 ldrValue = analogRead(ldrPin);
 // Read data from the sensor and send it to the virtual channel here.
 // You can write data using virtualWrite or other Cayenne write functions.
 // For example, to send a temperature in Celsius you can use the following:
 Cayenne.virtualWrite(VIRTUAL_PIN, map(ldrValue, 0, 1023, 0, 100));
}

We then go to our own Cayenne account, and add another component there.

First thing to do after your Arduino board is connected to Cayenne is to add a new device / widget:
Cayenne USB Serial - 02 Add New Device

Go to Sensors, and click Luminosity:
Sensors - Luminosity

Then click Photoresistor:
Sensors - Photoresistor

Now, enter the following settings below. We will use Virtual Pin 9 for this instance:
Sensors - Photoresistor Settings

Widget Name: Photoresistor
Select Device : Your device where you connect your component (Arduino Board).
Connectivity : Virtual
Pin: V9
Choose Widget : Gauge (You may want to choose your desired widget)

Click Step 2. Add Sensor. We don’t need step one, since we will have our own code, which was uploaded already (from one of the steps above). After clicking the Add Sensor button, you should see something like this in your dashboard:
Sensors - Gauge

Notice the Gauge? It moves depending on the level of light the sensor (which is attached yo my Arduino Uno) is getting from the current environment/location. You may change the widget to Value or Graph widget respectively. Refer to screenshots below:

I tried this set up on an Arduino Pro Mini compatible board, and still works just fine:

img_4288

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s