One Button to Rule Them All
Learn how to connect a simple web app written in JavaScript to Arduino Cloud.
Story
Following this tutorial you are going to learn how to make your Arduino or Genuino MKR1000 or WiFi Shield 101 interact with a simple webpage hosted in a browser via Arduino Cloud.
Connect to Arduino Cloud
If this is the first time you are using Arduino Cloud we highly encourage to follow the getting started guide.
Start by creating a new thing, we are going to call this thing “button”!
Once created, take note of the credentials, you will need to insert those in the Arduino Sketch.
Now you have to create a second set of credentials that are going to be used by the browser to connect to Arduino Cloud.
The Arduino Sketch
If you followed the Arduino Cloud getting started flow, you can start coding the Arduino sketch using the one you downloaded as a starting point.
Otherwise just copy-paste the one down here and start filling in the data required to connect to your wifi and your device credential.
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiSSLClient.h>
#include <MQTTClient.h>
const char ssid[] = "***";
const char pass[] = "***";
const char server[] = "mqtt.arduino.cc";
const char userName[] = "***";
const char deviceName[] = "button";
const char deviceId[] = "**************************";
const char devicePsw[] = "**************************";
MQTTClient client;
WiFiSSLClient net;
int ledPin = LED_BUILTIN;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.print("failed ... ");
delay(4000);
Serial.print("retrying ... ");
}
client.begin(server, 8883, net);
connect();;
}
void connect() {
Serial.println("connecting...");
while (!client.connect(deviceName, deviceId, devicePsw)) {
Serial.print(".");
}
Serial.println("connected!");
}
void loop() {
client.loop();
client.publish("/<username>/<devicename>/blink", "on");
Serial.println("publishing up");
digitalWrite(ledPin, HIGH);
delay(1000);
client.publish("/<username>/<devicename>/blink", "off");
Serial.println("publishing low");
digitalWrite(ledPin, LOW);
delay(1000);
}
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
Serial.print("incoming: ");
Serial.print(topic);
Serial.print(" - ");
Serial.print(payload);
Serial.println();
}
In this example sketch we are using the function client.publish()
to send messages to the Arduino Cloud. To be more specific, we are sending MQTT messages to the topic at this address:
/<username>/<devicename>/blink
Devices can send value to a topic while other devices can subscribe to that same topic and listen to it.
Because of how Arduino Cloud is configured, each thing can just publish messages to a topic after /<username>/<devicename>/
. Therefore two devices cannot publish to the same topic.
Every device, however, can subscribe to all the topic after /<username>/
, thus allowing each device to listen to all the messages coming from devices under the same user account.
We are going to simply modify this sketch to send a message to the topic /pressed
every time the button connected to the Arduino board will be pressed.
Read more: One Button to Rule Them All
JLCPCB – Prototype 10 PCBs for $2 + 2 days Lead Time
China’s Largest PCB Prototype Enterprise, 300,000+ Customers & 10,000+ Online Orders Per Day
Inside a huge PCB factory: https://www.youtube.com/watch?v=_XCznQFV-Mw