Create Your First IoT Application by Connecting Arduino to FAVORIOT Platform


Arduino-to-FAVORIOT Tutorial

code to send data to FAVORIOT platform from Arduino

    This sketch sends streams to FAVORIOT Platform using Ethernet shield
#include <SPI.h>
#include <Ethernet.h>

const int ON = 1;    // Constant to indicate that lights are on
const int OFF = 2;  // Constant to indicate that lights are off
const String APIKEY = "YOUR API KEY HERE"; // Replace with your FAVORIOT apikey
const String DEVICE = "YOUR DEVICE HERE"; // Replace with the id_developer of your device

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Your IP Address
IPAddress ip(192,168,1,16);

// IP Address
IPAddress server(182,54,199,106);

EthernetClient client; // Initialize the library instance
int ledPin = 5;        // Led pin number
int LDRPin = 7;        // LDR sensor pin number
String lights = "OFF";       // Current status
String newLights = "OFF";    // New status

// The setup routine runs once when you press reset
void setup() {
  pinMode(ledPin, OUTPUT);        // Initialize the digital pin as an output
  Serial.begin(9600);             // Start serial port
  Ethernet.begin(mac,ip);         // Start the Ethernet connection
  delay(2000);                    // Give the Ethernet shield a second to initialize

// The loop routine runs over and over again forever
void loop() {
  int val = analogRead(LDRPin);   // Read the value from the sensor
  if (val > 990) {  // This is the value limit between day or night with our LDR sensor. Maybe you need adjust this value.
    newLights = OFF;             // Now it's night. We have to turn on the LED
    digitalWrite(ledPin, HIGH);   // Turn the LED on (HIGH is the voltage level)
  else {
    newLights = ON;               // Now it's day. We have to turn off the LED
    digitalWrite(ledPin, LOW);    // Turn the LED off by making the voltage LOW
  if (lights != newLights) {        // Check if we have a change in status
    Serial.println(F("Send Stream"));
    lights = newLights;             // Status update and send stream
  // If there's incoming data from the net connection, send it out the serial port
  // This is for debugging purposes only
  while (client.available()) {
    char c =;

  if (!client.connected()) {
// Send stream to FAVORIOT
void sendStream()
  String txt = "";          // Text to send
  if ( lights == OFF ) {   // Alarm OFF
     txt = "OFF";
  } else {                  // Alarm ON
     txt = "ON";
  Serial.println(txt);      // For debugging purpose only

  if (client.connect(server, 80)) {   // If there's a successful connection
    // Build the data field
    String json = "{\"device_developer_id\":\""+DEVICE+"\",\"data\":{\"Light\":\""+txt+"\"}}";
    // Make a HTTP request
    client.println("POST /v1/streams HTTP/1.1");
    client.println(F("apikey: YOUR API KEY HERE"));
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    int thisLength = json.length();
    client.println("Connection: close");

  else {
    // If you didn't get a connection to the server:
    Serial.println(F("connection failed"));


  FAVORIOT Arduino Code for Wi-Fi shield

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "YOUR WI-FI Network SSID"; //  your network SSID (name)
char pass[] = "WI-FI Password";    // your network password (use for WPA, or use as key for WEP)

const String DEVICE = "DEVICE NAME"; // Replace with the id_developer of your device
String txt = "OFF";          // Text to send

int status = WL_IDLE_STATUS;

char server[] = "";    //  address for FAVORIOT Platform

WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  // check for the presence of the shield:
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
  Serial.println("Connected to wifi");

void loop() {

  // Json Data to send to Platform
  String json = "{\"device_developer_id\":\"YOUR DEVICE HERE\",\"data\":{\"light\":\""+txt+"\"}}";
  if (client.connect(server, 80)) {
    // Make a HTTP request:
    client.println("POST /v1/streams HTTP/1.1");
    client.println(F("apikey: YOUR API KEY HERE"));
    client.println("Content-Type: application/json");
    client.println("cache-control: no-cache");
    client.print("Content-Length: ");
    int thisLength = json.length();
    client.println("Connection: close");

  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c =;
  // if the server's disconnected, stop the client:
  if (!client.connected()) {

This section provide tutorials on connecting various arduino device to FAVORIOT IoT. This easy tutorial helps you build a system for turning LED ON and OFF based in the light sensor reading (LDR sensor) and send an email alert. For this, a Arduino able to measure the light is used. In this tutorial you will learn how to:

  • Connect an Arduino to FAVORIOT to send data streams using FAVORIOT HTTP REST API.
  • Build an Notification system on FAVORIOT by writing Event Rule to send an email.

Components used

  • 1 x Arduino Uno.
  • 1 x Ethernet Shield.
  • 1 x Breadboard
  • 1 x Light sensor (LDR)
  • 1 x Resistance. Value of 10 KΩ
  • 1 x Resistance. Value of 220Ω
  • 1 x Led

Arduino is programmed to send a data stream to FAVORIOT depending on the intensity of light.

All the data streams sent by Arduino is stored in FAVORIOT platform.

In addition to storing data, the true power of FAVORIOT is to let you build Apps quickly with simple rule based on if-else logic. In this scenario we are going to build a Alert App that sends an email to you in case that Arduino detects the lights are ON or OFF.

The connections in Arduino are extremely simple. Refer to the diagram below.

Screenshot of Example Documentation created with Slate

If you are registered in FAVORIOT, you have a default device already created for you. Go to the right platform on and see the device panel to see the devices that are present. Basically, you need the device_developer_id that might be something like defaultDevice@myusername. But if you want, you can create a new device and use it in this example.


Now, go to your “account setting” which is available on the top right corner in the dropdown and check your Apikey. It’s a big alphanumeric token like:
You need this apikey to complete the example.

From Arduino you have to build a HTTP request and send the data.

HTTP request
POST /streams HTTP/1.1
Accept: application/json
Content-Type: application/json
Connection: close

{ { "device_developer_id": "deviceDefault@FAVORIOT", "data": {"light":"ON"} } }

Alright then now your device must be sending streams when you turn on and turn off the lights.

It’s time to see whether you can view the data on the platform and check if we have new streams. Login to you account on and go to data stream tab.

Screenshot of Example Documentation created with Slate

You will see data like this in the data stream tab.

Screenshot of Example Documentation created with Slate

Great! Now as we are receiving data on our platform let’s send an email whenever new data comes. Go to the Rules tab below the data stream tab.

When inside the Rule tab click on Add New Rule button. A form will appear and fill in the details as described:

Field Details
Rule Name Short name for rule (.e.g: Light_rule)
Description Describe what the rule does (.e.g.: sends email when light turn on or off)
Device Name Select from the dropdown on which you want to create the rule
Data Field for device this is optional field and decribe to which data inside the device you are associating the rule.
Rule Describe the rule here (see more information below)
Then select what to do from dropdown (email or sms. More alert channel coming soon.)
To enter the email or sms here (based in your previous selection in previous step).
Message enter the short message you want to be attache with alert.

The rule should be described as follows:

(stream.Light === "ON") || (stream.Light === "OFF")

The syntax should be followed while describing the rule. stream. prefix (adding stream. is required) is followed the data field sent by device which is light in this case (the data sent by the device is temperature then you will write stream.temperature). You can multiple rule using || (OR) && (AND) logical operators.

Now, whenever the data comes to the platform the rule will be triggered and alert will be sent.

Congratulations! you have just created an IoT project from scratch. Now go ahead and let your imagination run wild. Show us what great things you can build.

If you are interested in becoming a Beta Developer, please send email to

If you are having trouble with connecting your device to our platform please contact us at

Full documentation and technical tutorial – HERE.


About IOT World

IOT Evangelist
This entry was posted in Internet of Things, Malaysia and tagged , . Bookmark the permalink.

Leave a Reply

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

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