Skip to content

Instantly share code, notes, and snippets.

@shfitz
Last active December 16, 2022 02:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shfitz/da7ca7ab8f6e86d851b683764148a8f1 to your computer and use it in GitHub Desktop.
Save shfitz/da7ca7ab8f6e86d851b683764148a8f1 to your computer and use it in GitHub Desktop.
// include the libraries
#include <WiFiNINA.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// wifi setup
#define WLAN_SSID "networkname"
#define WLAN_PASS "password"
// a.io setup
#define AIO_SERVER "io.adafruit.com" //we're gonna use adafruit IO
#define AIO_SERVERPORT 8883 // this is fot the secure port
#define AIO_USERNAME "username-goes-here"
#define AIO_KEY "your-key-goes-here"
// Create a secure WiFiClient class to connect to the server.
WiFiSSLClient client;
// Setup the MQTT client class -pass the WiFi client, MQTT server, and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// MQTT paths for AIO follow the form: <username>/feeds/<feedname>
// Setup a publishing feed called 'sensor' for sending sensor data to the server
Adafruit_MQTT_Publish sensor = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/potVal");
// Setup a subscription feed called 'ledOnOff' for receiving data from the server
Adafruit_MQTT_Subscribe ledButton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/ledOnOff");
// variables for the sketch
unsigned long sensorVal = 0; // the library wants the sent info as an unsigned long
int sensorPin = A0;
int ledPin = LED_BUILTIN;
void setup() {
Serial.begin(9600);
delay(10);
// Connect to WiFi access point.
Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected!");
// Setup MQTT subscription for receiving data
mqtt.subscribe(&ledButton);
// configure LED as output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is the 'wait for incoming subscription packets' section
Adafruit_MQTT_Subscribe *subscription; // create a pointer to an Adafruit_MQTT_Subscribe object
// wait for a subscription message from the server
// readSubscription will wait for the specified number of ms before moving
// along to the rest of the program
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &ledButton) { // if the subscription is our ledButton
String ledValue = (char *)ledButton.lastread; // store the message in a string
// print to the serial monitor
Serial.print("Got: ");
Serial.println(ledValue);
// change the LED based on message received
if (ledValue == "ON") {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}
// read the sensor on A0
sensorVal = analogRead(A0);
// publish stuff!
Serial.println();
Serial.print("Sending value ");
Serial.print(sensorVal);
Serial.print("...");
// feed.publish() will send your value to the declared feed
// it returns a 0 if it fails, a 1 if it is successful
if (! sensor.publish(sensorVal)) {
Serial.println("Failed");
} else {
Serial.println("OK!");
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function. Will auto re-connect if can't communicate
void MQTT_connect() {
int ret;
// exit this function if already connected
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
int retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment