Skip to content

Instantly share code, notes, and snippets.

@sandeepmistry
Created January 11, 2016 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandeepmistry/fbb5d161ddce1e51ee83 to your computer and use it in GitHub Desktop.
Save sandeepmistry/fbb5d161ddce1e51ee83 to your computer and use it in GitHub Desktop.
/*
This example connects to a WPA encrypted WiFi network,
using the WiFi Shield 101.
Then connects to the Arduino Cloud MQTT broker using
SSL, and sends updates on the status of an LED which
is controlled by a button.
Circuit:
* WiFi shield attached
* LED
* button
*/
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiSSLClient.h>
// include PubSubClient library
// https://github.com/knolleary/pubsubclient
#include <PubSubClient.h>
// WiFi settings
const char ssid[] = "yourNetwork"; // your network SSID (name)
const char password[] = "secretPassword"; // your network password
// MQTT settings
const char mqttServer[] = "mqtt.arduino.cc";
const int mqttPort = 8883;
const char mqttClientName[] = "arduinoWiFiSSLClient";
const char mqttUsername[] = "username"; // your MQTT username
const char mqttPassword[] = "password"; // your MQTT password
const char mqttTopic[] = "/username/001"; // your MQTT topic /<username>/topic
// You can also access the MQTT broker service
// using the same set of credentials using
// WebSockets via the following URI:
//
// wss://mqtt.arduino.cc:9002/
//
// This would allow you to subscribe and publish
// to MQTT topics using a web browser.
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int wifiStatus = WL_IDLE_STATUS; // the Wifi radio's status
int buttonState = 0; // variable for reading the pushbutton status
int oldButtonState = -1; // variable for storing the old pushbutton status
// Initialize the WiFi SSL client library
WiFiSSLClient wifiSSLClient;
// Initialize the PubSubClient
PubSubClient mqttClient(mqttServer, mqttPort, wifiSSLClient);
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
setupWiFi();
Serial.println(F("Connecting to MQTT broker ..."));
if (mqttClient.connect(mqttClientName, mqttUsername, mqttPassword)) {
Serial.println(F("Connected :D"));
} else {
Serial.println(F("Connection failed :("));
// don't continue:
while (true);
}
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input pullup:
pinMode(buttonPin, INPUT_PULLUP);
}
void setupWiFi() {
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while (wifiStatus != WL_CONNECTED) {
Serial.print(F("Attempting to connect to WPA SSID: "));
Serial.println(ssid);
// Connect to WPA/WPA2 network:
wifiStatus = WiFi.begin(ssid, password);
}
Serial.println(F("Connected to wifi"));
}
void loop() {
// let the MQTT client process events
mqttClient.loop();
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// compare button state to old, to see
// if it has changed
if (oldButtonState != buttonState) {
// it has changed store the new value:
oldButtonState = buttonState;
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (buttonState == LOW) {
Serial.println(F("Button pressed"));
// turn LED on:
digitalWrite(ledPin, HIGH);
// publish new value for topic to MQTT broker
mqttClient.publish(mqttTopic, "255");
} else {
Serial.println(F("Button released"));
// turn LED off:
digitalWrite(ledPin, LOW);
// publish new value for topic to MQTT broker
mqttClient.publish(mqttTopic, "0");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment