CloudinoJS Examples

Javier Solis edited this page Oct 21, 2017 · 23 revisions

Hello World

Send Hello World text to the Cloudino Server Console

//Send text to Server Console
print('Hello World');

Timed Hello World

Send Hello World text to the Cloudino Server Console every second

//import Timer
require("Timer");

var x=0;
//Create timer every second (1000ms)
setInterval(function(){
    //Send text to Server Console
    print('Hello World '+x);
    x++;
},1000);

Blink

Turn the internal led ON and OFF every second

require("GPIO");
require("Timer");

pinMode(2, OUTPUT);

var led=false;
setInterval(function(){
    digitalWrite(2, led);
    led=!led;
},1000);

With Comments

//import GPIO and Timer
require("GPIO");
require("Timer");

//initialize digital GPIO02 (Internal Led) as an output.
pinMode(2, OUTPUT);

//Global Variable for led state
var led=false;

//Create timer every second (1000ms)
setInterval(function(){
    //turn the LED on or off based on led variable
    digitalWrite(2, led);
    //change led state
    led=!led;
},1000);

Read Analog

Read Analog Data and Post the data to defined IoT Server

//import Cloudino, Timer and GPIO
require("Cloudino");
require("Timer");
require("GPIO");

function read()
{
    //read analog data
    //return 10 bits value (0 - 1024) 
    //analog signal from (0 - 1 volts)
    var s=analogRead();
    //Post Analog data to defined Server
    Cloudino.post("analog",s);  
}

//Create timer every second (1000ms)
setInterval(read,1000);

DigitalWrite and On Server Messages

Turn On Led from Server Message "led"

//import Cloudino and GPIO
require("Cloudino");
require("GPIO");

//initialize digital pin 2 as an output (Internal led)
pinMode(2, OUTPUT);

function onLed(msg)
{
    //if received message is "ON"
    if(msg=="ON")
    {
        //turn the LED ON
        digitalWrite(2,HIGH);    
    }else
    {
        //turn the LED OFF
        digitalWrite(2,LOW);
    }
}

//Register to receive messages with topic "led" 
Cloudino.on("led",onLed);

Read DHT11 Sensor

Read a DHT11 Sensor

//import Cloudino, Timer and DHT11
require("Cloudino");
require("Timer");
require("DHT11");

//Create timer every second (1000ms)
setInterval(function(){
    //Read DHT11 on GPIO 14
    var sens=DHT11.read(14);
    //Post temperature an humidity data to defined Server
    Cloudino.post("temperature",sens.temperature);
    Cloudino.post("humidity",sens.humidity);
},1000);

Read BME280 Sensor

Read a BME280 Sensor

require("Cloudino");
require("Timer");
require("BME280");

setInterval(function(){
    //Read BME280 Sensor on SDA pin 5, SCL pin 4
    var sens=BME280.read(5,4);    
    Cloudino.post("BME280",sens);
},2000);

Read HC-SR04 Sensor

Read a HC-SR04 Sensor

require("Cloudino");
require("Timer");
require("HCSR04");

//Create timer every second (1000ms)
setInterval(function(){
    //Read HCSR04 on Trigger 5, Hecho 4 
    var dis=HCSR04.read(5,4);
    //Post distance en centimeters
    Cloudino.post("distance",dis);
},1000);

HTTP Get

Stream data to data.sparkfun.com, public url: https://data.sparkfun.com/streams/VGxL9RrYW8SmpLdOXyOR

//import Cloudino, Timer, HTTP and DHT11
require("Cloudino");
require("Timer");
require("DHT11");
require("HTTP");

//Create timer every second (1000ms)
setInterval(function() {
    //Read DHT11 on GPIO 14
    var sens = DHT11.read(14);
    //Post temperature an humidity data to data.sparkfun.com
    var ret = HTTP.get("https://data.sparkfun.com/input/VGxL9RrYW8SmpLdOXyOR?private_key=9YdKp5nDqVUkXx2pr7pg&humidity=2" + sens.humidity + "&temp=" + sens.temperature);
    if (ret !== undefined) {
        Cloudino.print(ret);
    } else {
        Cloudino.print("Error getting data");
    }
}, 1000);

RCSwitch Read Data

Read RC Code

require("Timer");
require("RCSwitch");
require("JSON");

//function RCSwitch.enableReceive(pin)
RCSwitch.enableReceive(4);

setInterval(function(){
    var r=RCSwitch.receive();
    if(r!=undefined)
    {
        print("Response:"+JSON.stringify(r));
    }
},1000);

RCSwitch Send Data

Send RC Code

require("Timer");
require("RCSwitch");

//function RCSwitch.enableTransmit(protocol,pin,pulseLength,repeat)
RCSwitch.enableTransmit(1,5,185,10);

var switch=false;

setInterval(function(){
    //function RCSwitch.send(code)
    if(switch)RCSwitch.send("110101010101010100000011");
    else RCSwitch.send("110101010101010100001100");
    switch=!switch;
},1000);
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.