IoT Cloud – Getting Started

Get familiar with the Arduino IoT Cloud and take your first steps into the world of connected objects.

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
A MKR1010 is also a perfect option, or you could use a MKR IOT Bundle (see below)
× 1
Arduino MKR IoT Bundle
Arduino MKR IoT Bundle
× 1
Breadboard (generic)
Breadboard (generic)
× 1
LED (generic)
LED (generic)
A generic 20mA LED
× 1
Resistor 221 ohm
Resistor 221 ohm
In our example we used a 150Ω resistor to limit the current flowing through the LED which will be powered by 3.3 Volts HIGH signal from the pin
× 1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
× 1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
× 1
Resistor 10k ohm
Resistor 10k ohm
This will act as a Pull-Down resistor on the button pin
× 1

Software apps and online services

Arduino Web Editor
Arduino Web Editor
Arduino IoT Cloud

Story

From the editor, GO TO IOT CLOUD and create a new property named toggle, with Type ON/OFF (Boolean), Permission Read only and Update When the value changes.

Once again EDIT CODE and we’ll be back to the editor. A quick glance at thingProperties.h will show that a new variable Toggle has been defined and associated to its property via ArduinoCloud.addProperty(...).

In our .ino file we’ll define the new pin and two variables related to the button state (more on this later)

#define BUTTON_PIN 5
int btnState;
int btnPrevState = 0;

btnPrevState is needed because we want the property to be updated only once when the button is pressed and not when it is released.

Then, in the setup() we set the pinMode for this pin to INPUT

pinMode(BUTTON_PIN, INPUT);

And finally we add these lines towards the end of the loop()

btnState = digitalRead(BUTTON_PIN);
if (btnPrevState == 0 && btnState == 1) {
 toggle = !toggle;
}
btnPrevState = btnState;

In this way the button acts as a toggle and when pressing it we should see the switch on the cloud changing accordingly and flipping between ON and OFF.

Amazing, right? How about uploading the new code and testing out how our circuit interacts with the Dashboard? Let’s do that.

Once again EDIT CODE and we’ll be back to the editor. A quick glance at thingProperties.h will show that a new variable Toggle has been defined and associated to its property via ArduinoCloud.addProperty(...).

In our .ino file we’ll define the new pin and two variables related to the button state (more on this later)

#define BUTTON_PIN 5
int btnState;
int btnPrevState = 0;

btnPrevState is needed because we want the property to be updated only once when the button is pressed and not when it is released.

Then, in the setup() we set the pinMode for this pin to INPUT

pinMode(BUTTON_PIN, INPUT);

And finally we add these lines towards the end of the loop()

btnState = digitalRead(BUTTON_PIN);
if (btnPrevState == 0 && btnState == 1) {
 toggle = !toggle;
}
btnPrevState = btnState;

In this way the button acts as a toggle and when pressing it we should see the switch on the cloud changing accordingly and flipping between ON and OFF.

Amazing, right? How about uploading the new code and testing out how our circuit interacts with the Dashboard? Let’s do that.

Once again EDIT CODE and we’ll be back to the editor. A quick glance at thingProperties.h will show that a new variable Toggle has been defined and associated to its property via ArduinoCloud.addProperty(...).

In our .ino file we’ll define the new pin and two variables related to the button state (more on this later)

#define BUTTON_PIN 5
int btnState;
int btnPrevState = 0;

btnPrevState is needed because we want the property to be updated only once when the button is pressed and not when it is released.

Then, in the setup() we set the pinMode for this pin to INPUT

pinMode(BUTTON_PIN, INPUT);

And finally we add these lines towards the end of the loop()

btnState = digitalRead(BUTTON_PIN);
if (btnPrevState == 0 && btnState == 1) {
 toggle = !toggle;
}
btnPrevState = btnState;

In this way the button acts as a toggle and when pressing it we should see the switch on the cloud changing accordingly and flipping between ON and OFF.

Amazing, right? How about uploading the new code and testing out how our circuit interacts with the Dashboard? Let’s do that.

Schematics

Schematic

Schematic for the circuit

Code

/*
Sketch generated by the Arduino IoT Cloud Thing “testThing”
https://create.arduino.cc/cloud/things/d276ab77-67cb-420b-9ea4-bd34cdf385d9

Arduino IoT Cloud Properties description

The following variables are automatically generated and updated when changes are made to the Thing properties

bool switchState;
int potentiometerValue;
bool ledState;

Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include “thingProperties.h”
#include <FTDebouncer.h>

#define LED_PIN 2
#define POT_PIN A1
#define BUTTON_PIN 5

FTDebouncer buttons;

void setup() {
pinMode(LED_PIN, OUTPUT);
buttons.addPin(BUTTON_PIN, LOW);
buttons.init();

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 3
*/
setDebugMessageLevel(2);

// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
}

void loop() {
buttons.update();
ArduinoCloud.update();
// Your code here
int angleSensor = analogRead(A1);
angle = map(angleSensor, 0, 1023, 0, 270);
}

void onLightChange() {
digitalWrite(LED_PIN, light);
Serial.print(“The light is “);
if (light) {
Serial.println(“ON”);
} else {
Serial.println(“OFF”);
}
}

void onPinActivated(uint8_t pinNr) {
// do something according to the _pinNR that is triggered. For instance:
Serial.println(pinNr);
toggle = !toggle;
}

void onPinDeactivated(uint8_t pinNr) {
// do something according to the _pinNR that is triggered. For instance:
Serial.println(pinNr);
}

This library makes it easier to implement push and release actions for physical buttons, without the burden to track time, buttons states and change of state between LOW and HIGH. Create a Debounce Manager, add a pin to keep track of and let the library call two functions for when the button is pressed or released. More info on the GitHub repository, as well as in the video.

Debounce Library for Arduino — Read More

Source : IoT Cloud – Getting Started


About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation and engineering. My writing style is clear and simple, and I am skilled in using infographics and diagrams. I am a great researcher and am able to present information in a well-organized and logical manner.

Follow Us:
LinkedinTwitter
Scroll to Top