Home > Projects > One Button to Rule Them All

One Button to Rule Them All

Summary of One Button to Rule Them All


This article demonstrates connecting a JavaScript web app to Arduino Cloud using an Arduino MKR1000 and a SparkFun button. The project involves configuring the hardware with Arduino Web Editor, setting up credentials in Arduino Cloud, and writing sketches for both the microcontroller and the browser-based client. The system uses MQTT protocols to toggle a status indicator on a webpage when the physical button is pressed, enabling seamless interaction between physical inputs and web interfaces via the cloud.

Parts used in the One Button to Rule Them All:

  • Arduino MKR1000
  • SparkFun big dome red button
  • Arduino Web Editor
  • Arduino Cloud

Learn how to connect a simple web app written in JavaScript to Arduino Cloud.

One Button to Rule Them All

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1
SparkFun big dome red button
× 1

Software apps and online services

Arduino Web Editor
Arduino Web Editor
Arduino Cloud
Arduino Cloud

Story

Attaching here the resulting modified sketch.

#include <WiFi101.h>
#include <WiFiSSLClient.h>
#include <MQTTClient.h>
int status = WL_IDLE_STATUS;
MQTTClient client;
WiFiSSLClient net;
int buttonPin = 2;
boolean buttonState = false;
boolean oldButtonState = false;
boolean lightStatus=false;
const char* ssid  = "***";     //  your network SSID (name)
const char* password = "***";  // your network password
const char* userName = "***";
const char* deviceName = "button";
const char* deviceId = "*****************************";
const char* devicePsw = "*****************************";
void setup() {
 Serial.begin(9600);
 WiFi.begin(ssid, password);
 client.begin("mqtt.arduino.cc", 8883, net);
 connect();
 pinMode(2, INPUT);
}
void connect() {
 Serial.print("checking wifi...");
 while ( status != WL_CONNECTED) {
   Serial.print("Attempting to connect to WPA SSID: ");
   Serial.println(ssid);
   // Connect to WPA/WPA2 network:
   status = WiFi.begin(ssid, password);
   // wait 10 seconds for connection:
   delay(4000);
 }
 Serial.println("connecting...");
 while (!client.connect(deviceName, deviceId, devicePsw)) {
   Serial.print(".");
 }
 Serial.println("connected!");
}
void loop() {
 client.loop();
 buttonState = digitalRead(buttonPin);
 if (!buttonState && oldButtonState) {
   lightStatus = !lightStatus;
   if (lightStatus) {
     client.publish("/<username>/button/status", "on");
     Serial.println("publishing on");
   } else {
     client.publish("/<username>/button/status", "off");
     Serial.println("publishing off");
   }
 }
 oldButtonState = buttonState;
 delay(50);
}
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
 Serial.print("incoming: ");
 Serial.print(topic);
 Serial.print(" - ");
 Serial.print(payload);
 Serial.println();
}

Now it is time to create our single webpage that respond whenever we click the button.

Start by downloading the base web-app sketch from Github.

In order to test the server locally on your computer you will need to use a local web-server.

You can use Python SimpleHTTPServer  for this. Just move to the folder “baseWebApp” inside the repo you downloaded and launch the server:

python -m SimpleHTTPServer 8080

If everything goes fine, you should be able to see the page in a browser at this address: http://0.0.0.0:8080.

Fill in the required fields in the top of the web page with the browser credential you got from cloud.arduino.cc.

It’s important that in the topic field you write the device name you want to subscribe to, followed by the topic. In our case: button/status.

If you press the big button you should see the “on” box turning red and green again according to the “status” of the button.

One Button to Rule Them All schematics

 

Code

Connect a button to a web page via Arduino Cloud — Read More

Arduino

Quick Solutions to Questions related to One Button to Rule Them All:

  • How do I connect my Arduino device to Arduino Cloud?
    You must create a new thing named "button" in the cloud interface and note the generated credentials to insert into your Arduino sketch.
  • What software is required to write the code for this project?
    The tutorial requires using the Arduino Web Editor to write and upload the sketch to the board.
  • Can multiple devices publish to the same topic under one user account?
    No, each device can only publish messages to a specific topic after its unique username and device name prefix.
  • How does the web page know when the button is pressed?
    The web application subscribes to the /username/button/status topic to receive messages sent by the Arduino when the button state changes.
  • What command is used to start the local web server for testing?
    You should run python -m SimpleHTTPServer 8080 from the baseWebApp folder to launch the local server.
  • Which library is the basic web sketch based on?
    The basic sketch is based on the paho mqtt js client library found in the myScript.js file.
  • What port number is used for the MQTT broker connection in the code?
    The code connects to the server at port 8883 for the Arduino side and port 9002 for the WebSocket connection in the browser.
  • How do you handle the button press logic in the loop function?
    The code checks if the current button state differs from the old state to trigger a status update and publish a message.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top