Skip to content

Instantly share code, notes, and snippets.

@robertpyke
Created April 6, 2018 17:38
Show Gist options
  • Save robertpyke/dc7270bedf00a2ae51609633e09c6358 to your computer and use it in GitHub Desktop.
Save robertpyke/dc7270bedf00a2ae51609633e09c6358 to your computer and use it in GitHub Desktop.
#include "arduino_secrets.h"
#include <SPI.h>
#include <WiFi101.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// Also have to actually override this in the PubSubClient.h
#define MQTT_MAX_PACKET_SIZE 4096
char mqttServer[] = MQTT_IP;
char clientId[] = THING_NAME;
char refreshTopic[] = "$aws/things/" THING_NAME "/shadow/get";
char publishTopic[] = "$aws/things/" THING_NAME "/shadow/update";
char publishPayload[MQTT_MAX_PACKET_SIZE];
char *subscribeTopic[5] = {
"$aws/things/" THING_NAME "/shadow/update/accepted",
"$aws/things/" THING_NAME "/shadow/update/rejected",
"$aws/things/" THING_NAME "/shadow/update/delta",
"$aws/things/" THING_NAME "/shadow/get/accepted",
"$aws/things/" THING_NAME "/shadow/get/rejected"
};
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
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
// Allow the hardware to sort itself out
delay(1500);
printWiFiData();
// MQTT connection
client.setServer(mqttServer, 1883);
client.setCallback(callback);
}
/**
This method is called whenever we receive a message on our subscribed MQTT topics.
*/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("MQTT Callback");
char buf[length];
strncpy(buf, (const char *)payload, length);
buf[length] = '\0'; // Ensure null termination.
Serial.println(topic);
Serial.println(buf);
if (String(topic) == "$aws/things/" THING_NAME "/shadow/update/delta") {
StaticJsonBuffer<MQTT_MAX_PACKET_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(buf);
// Test if parsing succeeds.
if (!root.success()) {
Serial.println("parseObject() failed");
return;
} else {
Serial.println("parseObject() parsed it real good");
}
// Check that the JSON object has everything we want/need
if (root["state"].success() && root["state"]["pin0"].success() && root["state"]["pin0"].is<int>()) {
// Pull out pin0 as an int.
int pin0Value = root["state"]["pin0"].as<int>();
Serial.print("Pin 0, desired: ");
Serial.println(pin0Value);
digitalWrite(LED_BUILTIN, pin0Value);
Serial.print("LED set to: ");
Serial.println(pin0Value);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");
for (int i = 0; i < 5; i++) {
Serial.print("Subscribing to: ");
Serial.print(subscribeTopic[i]);
if (client.subscribe(subscribeTopic[i]), 1) {
Serial.println(" ... succeeded");
} else {
Serial.println(" ... failed");
}
}
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
int count = 0;
void loop() {
count++;
if (!client.connected()) {
reconnect();
Serial.println("refreshing state..");
client.publish("$aws/things/" THING_NAME "/shadow/get", "");
}
delay(100);
Serial.println("looping..");
// Loop our client (check for outstanding messages, etc.)
client.loop();
if (count >= 10) {
Serial.println("publish state..");
sprintf(publishPayload, "{\"state\":{\"reported\":{\"pin0\":%d, \"rssi\":%d }},\"clientToken\":\"%s\"}",
digitalRead(LED_BUILTIN),
WiFi.RSSI(),
clientId
);
client.publish(publishTopic, publishPayload);
count = 0;
}
}
void printWiFiData() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5], HEX);
Serial.print(":");
Serial.print(bssid[4], HEX);
Serial.print(":");
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment