Add Library
This commit is contained in:
parent
e365b9dbd9
commit
3c47103b39
318 changed files with 56465 additions and 0 deletions
|
@ -0,0 +1,122 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library ESP8266 Adafruit IO Anonymous Time Query
|
||||
|
||||
Must use the latest version of ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board & Feather
|
||||
----> https://www.adafruit.com/product/2471
|
||||
----> https://www.adafruit.com/products/2821
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
#define WLAN_SSID "network"
|
||||
#define WLAN_PASS "password"
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 8883
|
||||
|
||||
WiFiClientSecure client;
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT);
|
||||
|
||||
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
|
||||
|
||||
// set timezone offset from UTC
|
||||
int timeZone = -4; // UTC - 4 eastern daylight time (nyc)
|
||||
int interval = 4; // trigger every X hours
|
||||
|
||||
int last_min = -1;
|
||||
|
||||
void timecallback(uint32_t current) {
|
||||
// adjust to local time zone
|
||||
current += (timeZone * 60 * 60);
|
||||
int curr_hour = (current / 60 / 60) % 24;
|
||||
int curr_min = (current / 60 ) % 60;
|
||||
int curr_sec = (current) % 60;
|
||||
|
||||
Serial.print("Time: ");
|
||||
Serial.print(curr_hour); Serial.print(':');
|
||||
Serial.print(curr_min); Serial.print(':');
|
||||
Serial.println(curr_sec);
|
||||
|
||||
// only trigger on minute change
|
||||
if(curr_min != last_min) {
|
||||
last_min = curr_min;
|
||||
|
||||
Serial.println("This will print out every minute!");
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.print(F("\nAdafruit IO anonymous Time Demo"));
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(F("."));
|
||||
}
|
||||
Serial.println(F(" WiFi connected."));
|
||||
|
||||
timefeed.setCallback(timecallback);
|
||||
mqtt.subscribe(&timefeed);
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
// wait 10 seconds for subscription messages
|
||||
// since we have no other tasks in this example.
|
||||
mqtt.processPackets(10000);
|
||||
|
||||
// keep the connection alive
|
||||
mqtt.ping();
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t 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) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,155 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library ESP8266 Example
|
||||
|
||||
Must use ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board & Feather
|
||||
----> https://www.adafruit.com/product/2471
|
||||
----> https://www.adafruit.com/products/2821
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
Error examples by Todd Treece for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "...your SSID..."
|
||||
#define WLAN_PASS "...your password..."
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883 // 8883 for MQTTS
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||
WiFiClient client;
|
||||
// or... use WiFiFlientSecure for SSL
|
||||
//WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
|
||||
/*************************** Error Reporting *********************************/
|
||||
|
||||
Adafruit_MQTT_Subscribe errors = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/errors");
|
||||
Adafruit_MQTT_Subscribe throttle = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/throttle");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
|
||||
// for some reason (only affects ESP8266, likely an arduino-builder bug).
|
||||
void MQTT_connect();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||
|
||||
// Setup MQTT subscription for onoff feed
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
|
||||
// Setup MQTT subscriptions for throttle & error messages
|
||||
mqtt.subscribe(&throttle);
|
||||
mqtt.subscribe(&errors);
|
||||
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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 our 'wait for incoming subscription packets' busy subloop
|
||||
// try to spend your time here
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(5000))) {
|
||||
if (subscription == &onoffbutton) {
|
||||
Serial.print(F("Got onoff: "));
|
||||
Serial.println((char *)onoffbutton.lastread);
|
||||
} else if(subscription == &errors) {
|
||||
Serial.print(F("ERROR: "));
|
||||
Serial.println((char *)errors.lastread);
|
||||
} else if(subscription == &throttle) {
|
||||
Serial.println((char *)throttle.lastread);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending photocell val "));
|
||||
Serial.print(x);
|
||||
Serial.print("...");
|
||||
if (! photocell.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t 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) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,183 @@
|
|||
/****************************************************************
|
||||
Adafruit MQTT Library, Adafruit IO SSL/TLS Example for AirLift
|
||||
|
||||
Must use the latest version of nina-fw from:
|
||||
https://github.com/adafruit/nina-fw
|
||||
|
||||
Works great with Adafruit AirLift ESP32 Co-Processors!
|
||||
--> https://www.adafruit.com/product/4201
|
||||
--> https://www.adafruit.com/product/4116
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Brent Rubell for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
*******************************************************************/
|
||||
|
||||
#include <WiFiNINA.h>
|
||||
#include <SPI.h>
|
||||
|
||||
// For AirLift Breakout/Wing/Shield: Configure the following to match the ESP32 Pins!
|
||||
#if !defined(SPIWIFI_SS)
|
||||
// Don't change the names of these #define's! they match the variant ones
|
||||
#define SPIWIFI SPI
|
||||
#define SPIWIFI_SS 11 // Chip select pin
|
||||
#define SPIWIFI_ACK 10 // a.k.a BUSY or READY pin
|
||||
#define ESP32_RESETN 9 // Reset pin
|
||||
#define ESP32_GPIO0 -1 // Not connected
|
||||
#define SET_PINS 1 // Pins were set using this IFNDEF
|
||||
#endif
|
||||
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "WLAN_SSID"
|
||||
#define WLAN_PASS "WIFI_PASSWORD"
|
||||
int keyIndex = 0; // your network key Index number (needed only for WEP)
|
||||
|
||||
int status = WL_IDLE_STATUS;
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
// Using port 8883 for MQTTS
|
||||
#define AIO_SERVERPORT 8883
|
||||
// Adafruit IO Account Configuration
|
||||
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
|
||||
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
|
||||
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// WiFiSSLClient for SSL/TLS support
|
||||
WiFiSSLClient client;
|
||||
|
||||
// Setup the MQTT client class by WLAN_PASSing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'test' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup()
|
||||
{
|
||||
//Initialize serial and wait for port to open:
|
||||
Serial.begin(115200);
|
||||
while (!Serial)
|
||||
{
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
// if the AirLift's pins were defined above...
|
||||
#ifdef SET_PINS
|
||||
WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);
|
||||
#endif
|
||||
|
||||
// check for the wifi module
|
||||
while (WiFi.status() == WL_NO_MODULE)
|
||||
{
|
||||
Serial.println("Communication with WiFi module failed!");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
String fv = WiFi.firmwareVersion();
|
||||
if (fv < "1.0.0")
|
||||
{
|
||||
Serial.println("Please upgrade the firmware");
|
||||
}
|
||||
|
||||
// attempt to connect to Wifi network:
|
||||
Serial.print("Attempting to connect to SSID: ");
|
||||
Serial.println(WLAN_SSID);
|
||||
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
|
||||
do
|
||||
{
|
||||
status = WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
delay(100); // wait until connected
|
||||
} while (status != WL_CONNECTED);
|
||||
Serial.println("Connected to wifi");
|
||||
printWiFiStatus();
|
||||
}
|
||||
|
||||
uint32_t x = 0;
|
||||
|
||||
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();
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending val "));
|
||||
Serial.print(x);
|
||||
Serial.print(F(" to test feed..."));
|
||||
if (!test.publish(x++))
|
||||
{
|
||||
Serial.println(F("Failed"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
// wait a couple seconds to avoid rate limit
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect()
|
||||
{
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t 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)
|
||||
{
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
||||
|
||||
void printWiFiStatus()
|
||||
{
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your board's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
|
||||
// print the received signal strength:
|
||||
long rssi = WiFi.RSSI();
|
||||
Serial.print("signal strength (RSSI):");
|
||||
Serial.print(rssi);
|
||||
Serial.println(" dBm");
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library ESP8266 Adafruit IO SSL/TLS example
|
||||
|
||||
Must use the latest version of ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board & Feather
|
||||
----> https://www.adafruit.com/product/2471
|
||||
----> https://www.adafruit.com/products/2821
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
SSL/TLS additions by Todd Treece for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "WLAN_SSID"
|
||||
#define WLAN_PASS "WIFI_PASSWORD"
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
// Using port 8883 for MQTTS
|
||||
#define AIO_SERVERPORT 8883
|
||||
// Adafruit IO Account Configuration
|
||||
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
|
||||
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
|
||||
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// WiFiFlientSecure for SSL/TLS support
|
||||
WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
// io.adafruit.com SHA1 fingerprint
|
||||
static const char *fingerprint PROGMEM = "77 00 54 2D DA E7 D8 03 27 31 23 99 EB 27 DB CB A5 4C 57 18";
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'test' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
delay(1000);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
delay(2000);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||
|
||||
// check the fingerprint of io.adafruit.com's SSL cert
|
||||
client.setFingerprint(fingerprint);
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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();
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending val "));
|
||||
Serial.print(x);
|
||||
Serial.print(F(" to test feed..."));
|
||||
if (! test.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
// wait a couple seconds to avoid rate limit
|
||||
delay(2000);
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t 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) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library ESP8266 Adafruit IO SSL/TLS example
|
||||
|
||||
Must use the latest version of ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board & Feather
|
||||
----> https://www.adafruit.com/product/2471
|
||||
----> https://www.adafruit.com/products/2821
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
Time additions by Todd Treece for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
#define WLAN_SSID "network"
|
||||
#define WLAN_PASS "password"
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 8883
|
||||
#define AIO_USERNAME "user"
|
||||
#define AIO_KEY "key"
|
||||
|
||||
WiFiClientSecure client;
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
|
||||
|
||||
// set timezone offset from UTC
|
||||
int timeZone = -4; // UTC - 4 eastern daylight time (nyc)
|
||||
int interval = 4; // trigger every X hours
|
||||
int hour = 0; // current hour
|
||||
|
||||
void timecallback(uint32_t current) {
|
||||
|
||||
// stash previous hour
|
||||
int previous = hour;
|
||||
|
||||
// adjust to local time zone
|
||||
current += (timeZone * 60 * 60);
|
||||
|
||||
// calculate current hour
|
||||
hour = (current / 60 / 60) % 24;
|
||||
|
||||
// only trigger on interval
|
||||
if((hour != previous) && (hour % interval) == 0) {
|
||||
Serial.println("Run your code here");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.print(F("Adafruit IO Time Demo"));
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(F("."));
|
||||
}
|
||||
Serial.println(F(" WiFi connected."));
|
||||
|
||||
timefeed.setCallback(timecallback);
|
||||
mqtt.subscribe(&timefeed);
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
// wait 10 seconds for subscription messages
|
||||
// since we have no other tasks in this example.
|
||||
mqtt.processPackets(10000);
|
||||
|
||||
// keep the connection alive
|
||||
mqtt.ping();
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t 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) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library ESP8266 Example
|
||||
|
||||
Must use ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board & Feather
|
||||
----> https://www.adafruit.com/product/2471
|
||||
----> https://www.adafruit.com/products/2821
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
// the on off button feed turns this LED on/off
|
||||
#define LED 2
|
||||
// the slider feed sets the PWM output of this pin
|
||||
#define PWMOUT 12
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "...your SSID..."
|
||||
#define WLAN_PASS "...your password..."
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883 // use 8883 for SSL
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||
WiFiClient client;
|
||||
// or... use WiFiFlientSecure for SSL
|
||||
//WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
|
||||
// for some reason (only affects ESP8266, likely an arduino-builder bug).
|
||||
void MQTT_connect();
|
||||
|
||||
void setup() {
|
||||
pinMode(LED, OUTPUT);
|
||||
pinMode(PWMOUT, OUTPUT);
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||
|
||||
// Setup MQTT subscription for onoff & slider feed.
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
mqtt.subscribe(&slider);
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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 our 'wait for incoming subscription packets' busy subloop
|
||||
// try to spend your time here
|
||||
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(5000))) {
|
||||
// Check if its the onoff button feed
|
||||
if (subscription == &onoffbutton) {
|
||||
Serial.print(F("On-Off button: "));
|
||||
Serial.println((char *)onoffbutton.lastread);
|
||||
|
||||
if (strcmp((char *)onoffbutton.lastread, "ON") == 0) {
|
||||
digitalWrite(LED, LOW);
|
||||
}
|
||||
if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) {
|
||||
digitalWrite(LED, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
// check if its the slider feed
|
||||
if (subscription == &slider) {
|
||||
Serial.print(F("Slider: "));
|
||||
Serial.println((char *)slider.lastread);
|
||||
uint16_t sliderval = atoi((char *)slider.lastread); // convert to a number
|
||||
analogWrite(PWMOUT, sliderval);
|
||||
}
|
||||
}
|
||||
|
||||
// ping the server to keep the mqtt connection alive
|
||||
if(! mqtt.ping()) {
|
||||
mqtt.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t 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) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
# Adafruit MQTT Library Arbitrary Data Publish Example
|
||||
|
||||
This example illustrates publishing an arbitrary data packet using the Adafruit MQTT library to an MQTT feed which can then be parsed by the included python subscriber client. Possible usage cases include adding metadata (collection time, sensor info etc) to a datapoint.
|
||||
|
||||

|
||||
|
||||
My motivation for this was wanting to be able to include metadata to a post.
|
||||
Specifically, I was playing around with a [Teviso RD3024 radiation sensor](http://www.teviso.com/en/products/radiation-sensor-rd3024.htm), and a salvaged Americium radiation source from a smoke detector, at varying distances from the sensor. I wanted a way to associate the collection time, and distance between the source and sensor with the actual radiation reading itself.
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Installing and configuring Mosquitto broker (minimal working setup):
|
||||
|
||||
####_Installing on Raspberry Pi/Linux:_
|
||||
|
||||
```bash
|
||||
sudo apt-get install mosquitto
|
||||
cd /etc/mosquitto/
|
||||
#See "Configuring Mosquitto Broker below"
|
||||
```
|
||||
|
||||
####_Installing On a Mac:_
|
||||
```bash
|
||||
brew install mosquitto
|
||||
cd /usr/local/etc/mosquitto
|
||||
#See "Configuring Mosquitto Broker below"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
####Configuring Mosquitto broker
|
||||
```bash
|
||||
sudo nano mosquitto.conf
|
||||
```
|
||||
Now we have to enable a password file to correctly interface with the Adafruit MQTT library. Scroll about two thirds of the way down until you see:
|
||||
|
||||
```bash
|
||||
# -----------------------------------------------------------------
|
||||
# Default authentication and topic access control
|
||||
# -----------------------------------------------------------------
|
||||
```
|
||||
|
||||
You should see `#password_file` about a paragraph after that.
|
||||
Change
|
||||
|
||||
```bash
|
||||
#password_file
|
||||
```
|
||||
|
||||
To
|
||||
|
||||
```bash
|
||||
password_file pwfile
|
||||
```
|
||||
|
||||
Now `ctrl-x` to save and exit.
|
||||
|
||||
You're almost done! We just have to create and populate the password file we just configured. The default user info is:
|
||||
* **Arduino Subscriber:**
|
||||
* Username: TestUser
|
||||
* Password: TestUser
|
||||
|
||||
* **Python Subscriber:**
|
||||
* Username: TestPy
|
||||
* Password: TestPy
|
||||
|
||||
```bash
|
||||
touch pwfile #create the password file
|
||||
mosquitto_passwd pwfile TestUser #Enter and confirm password when prompted
|
||||
mosquitto_passwd pwfile TestPy #Enter and confirm password when prompted
|
||||
```
|
||||
|
||||
####Running Mosquitto broker
|
||||
Now run Mosquitto broker to allow Arduino publisher and Python subscriber to communicate
|
||||
|
||||
```bash
|
||||
mosquitto
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Using Example Python Subscriber:
|
||||
|
||||
####Installing Python subscriber
|
||||
Install dependencies if you haven't already
|
||||
```bash
|
||||
cd ../Adafruit_MQTT_Library/examples/mqtt_arbitrary_buffer/python_subscriber
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
|
||||
####Installing Python subscriber
|
||||
Run python script with default values and watch your parsed data print out.
|
||||
```bash
|
||||
python subscriber.py #Add -h flag to see modifiable options
|
||||
```
|
||||
|
||||
Assuming that the Mosquitto broker is running in the background and the Adafruit_MQTT client (Arduino) is publishing, you should see the example data print out every 10 seconds.
|
||||
|
||||
```bash
|
||||
MQTT: Connection successful
|
||||
Connection successful
|
||||
Subscribed to /feeds/arb_packet
|
||||
Received char Array: "Hello!", val1: -4533, val2: 73102, val3: 3354...
|
||||
Received char Array: "Hello!", val1: -4533, val2: 83611, val3: 3354...
|
||||
Received char Array: "Hello!", val1: -4533, val2: 94115, val3: 3354...
|
||||
```
|
|
@ -0,0 +1,184 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library Arbitrary Data Example
|
||||
|
||||
Must use ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board & Feather
|
||||
----> https://www.adafruit.com/product/2471
|
||||
----> https://www.adafruit.com/products/2821
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Stuart Feichtinger
|
||||
Modifed from the mqtt_esp8266 example written by Tony DiCola for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "...your SSID..."
|
||||
#define WLAN_PASS "...your password..."
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define ARB_SERVER "...host computer ip address..."
|
||||
#define ARB_SERVERPORT 1883 // use 8883 for SSL
|
||||
#define ARB_USERNAME "TestUser"
|
||||
#define ARB_PW "TestUser"
|
||||
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||
WiFiClient client;
|
||||
// or... use WiFiFlientSecure for SSL
|
||||
//WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, ARB_SERVER, ARB_SERVERPORT, ARB_USERNAME, ARB_PW);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'arb_packet' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
#define ARB_FEED "/feeds/arb_packet"
|
||||
Adafruit_MQTT_Publish ap = Adafruit_MQTT_Publish(&mqtt, ARB_FEED);
|
||||
|
||||
|
||||
// Arbitrary Payload
|
||||
// Union allows for easier interaction of members in struct form with easy publishing
|
||||
// of "raw" bytes
|
||||
typedef union{
|
||||
//Customize struct with whatever variables/types you like.
|
||||
|
||||
struct __attribute__((__packed__)){ // packed to eliminate padding for easier parsing.
|
||||
char charAry[10];
|
||||
int16_t val1;
|
||||
unsigned long val2;
|
||||
uint16_t val3;
|
||||
}s;
|
||||
|
||||
uint8_t raw[sizeof(s)]; // For publishing
|
||||
|
||||
/*
|
||||
// Alternate Option with anonymous struct, but manual byte count:
|
||||
|
||||
struct __attribute__((__packed__)){ // packed to eliminate padding for easier parsing.
|
||||
char charAry[10]; // 10 x 1 byte = 10 bytes
|
||||
int16_t val1; // 1 x 2 bytes = 2 bytes
|
||||
unsigned long val2; // 1 x 4 bytes = 4 bytes
|
||||
uint16_t val3; // 1 x 2 bytes = 2 bytes
|
||||
-------------------
|
||||
TOTAL = 18 bytes
|
||||
};
|
||||
uint8_t raw[18]; // For publishing
|
||||
*/
|
||||
|
||||
} packet_t;
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
|
||||
// for some reason (only affects ESP8266, likely an arduino-builder bug).
|
||||
void MQTT_connect();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print(F("Connecting to "));
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(F("."));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println(F("WiFi connected"));
|
||||
Serial.println(F("IP address: ")); Serial.println(WiFi.localIP());
|
||||
|
||||
}
|
||||
|
||||
packet_t arbPac;
|
||||
|
||||
const char strVal[] PROGMEM = "Hello!";
|
||||
|
||||
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();
|
||||
|
||||
//Update arbitrary packet values
|
||||
strcpy_P(arbPac.s.charAry, strVal);
|
||||
arbPac.s.val1 = -4533;
|
||||
arbPac.s.val2 = millis();
|
||||
arbPac.s.val3 = 3354;
|
||||
|
||||
/*
|
||||
// Alternate Union with anonymous struct
|
||||
// (see union declaration above)
|
||||
|
||||
strcpy_P(arbPac.charAry, strVal);
|
||||
arbPac.val1 = -4533;
|
||||
arbPac.val2 = millis();
|
||||
arbPac.val3 = 3354;
|
||||
*/
|
||||
|
||||
if (! ap.publish(arbPac.raw, sizeof(packet_t)))
|
||||
Serial.println(F("Publish Failed."));
|
||||
else {
|
||||
Serial.println(F("Publish Success!"));
|
||||
delay(500);
|
||||
}
|
||||
|
||||
delay(10000);
|
||||
|
||||
|
||||
// ping the server to keep the mqtt connection alive
|
||||
// NOT required if you are publishing once every KEEPALIVE seconds
|
||||
/*
|
||||
if(! mqtt.ping()) {
|
||||
mqtt.disconnect();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print(F("Connecting to MQTT... "));
|
||||
|
||||
uint8_t retries = 3;
|
||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||
Serial.println(mqtt.connectErrorString(ret));
|
||||
Serial.println(F("Retrying MQTT connection in 5 seconds..."));
|
||||
mqtt.disconnect();
|
||||
delay(5000); // wait 5 seconds
|
||||
retries--;
|
||||
if (retries == 0) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
Serial.println(F("MQTT Connected!"));
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 865 KiB |
|
@ -0,0 +1 @@
|
|||
paho-mqtt>=1.1
|
|
@ -0,0 +1,114 @@
|
|||
'''MQTT subscriber for Adafruit MQTT library mqtt_arbitrary_buffer example'''
|
||||
import paho.mqtt.client as mqtt
|
||||
import argparse
|
||||
import struct
|
||||
import array
|
||||
import sys
|
||||
|
||||
return_str =[
|
||||
"Connection successful",
|
||||
"incorrect protocol version",
|
||||
"invalid client identifier",
|
||||
"server unavailable",
|
||||
"bad username or password",
|
||||
"not authorised"
|
||||
]
|
||||
|
||||
args = None
|
||||
|
||||
# The callback for when the client receives a CONNACK response from the server.
|
||||
def on_connect(client, userdata, rc):
|
||||
"""callback function on connect. Subscribes or exits depending on outcome"""
|
||||
print("MQTT: "),
|
||||
print(return_str[rc])
|
||||
if(rc > 1):
|
||||
print("Connection refused - " + return_str[rc])
|
||||
sys.exit(rc)
|
||||
# Subscribing in on_connect() means that if we lose the connection and
|
||||
# reconnect then subscriptions will be renewed.
|
||||
else:
|
||||
print(return_str[rc])
|
||||
client.subscribe(args.topic)
|
||||
print("Subscribed to {}".format(args.topic))
|
||||
|
||||
def on_disconnect(client, userdata, rc):
|
||||
"""Callback for disconnect"""
|
||||
if rc != 0:
|
||||
print("Unexpected disconnection.")
|
||||
client.reconnect()
|
||||
|
||||
# The callback for when a PUBLISH message is received from the server.
|
||||
def on_message(client, userdata, msg):
|
||||
try:
|
||||
pMsg = parseMsg(msg.payload)
|
||||
print("Received char Array: \"{}\", val1: {}, val2: {}, val3: {}...".format(pMsg[0], pMsg[1], pMsg[2], pMsg[3]))
|
||||
|
||||
except Exception as err:
|
||||
print err
|
||||
|
||||
|
||||
|
||||
def argBegin():
|
||||
parser = argparse.ArgumentParser(description='MQTT subscriber for Adafruit MQTT library mqtt_arbitrary_buffer example')
|
||||
parser.add_argument("--host", default="localhost", help='mqtt host to connect to. Defaults to localhost.')
|
||||
parser.add_argument("-p", "--port", default=1883, help='network port to connect to. Defaults to 1883.')
|
||||
parser.add_argument("-t", "--topic", nargs='*', default="/feeds/arb_packet", help="mqtt topic to subscribe to. May be repeated multiple times.")
|
||||
parser.add_argument("-u", "--username", default="testPy", help="provide a username (requires MQTT 3.1 broker)")
|
||||
parser.add_argument("-P", "--password", default="testPy", help="provide a password (requires MQTT 3.1 broker)")
|
||||
parser.add_argument("-k", "--keepalive", default=60, help="keep alive in seconds for this client. Defaults to 60.")
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def parseMsg(payload):
|
||||
"""Parses C struct from MQTT publisher Adafruit MQTT client to Python list"""
|
||||
|
||||
arr = array.array('B', payload) #convert python list to C-like array of unsigned char (B)
|
||||
|
||||
parsedStruct = struct.Struct('< 10s h L H') #define struct template (see below)
|
||||
'''
|
||||
Format of Struct from Adafruit MQTT client, Arduino, etc:
|
||||
|
||||
Adafruit MQTT client == Little endian (<)
|
||||
|
||||
Var NAME | C TYPE (python symbol) | size of member x bytes
|
||||
-------------------------------------------------------------------
|
||||
"charAry" | uchar (s) | 10s x 1 = 10 bytes
|
||||
"val1" | int16 / short (h) | 1h x 2 = 2 bytes
|
||||
"val2" | unsigned long (L) | 1L x 4 = 4 bytes
|
||||
"val3" | uint16/unsigned short(H)| 1H x 2 = 2 bytes
|
||||
------------------------------------------------------------------
|
||||
Total packet size = | 18 bytes |
|
||||
|
||||
See Section 7.3.2 of Python struct module documentation for complete format list
|
||||
https://docs.python.org/2/library/struct.html
|
||||
'''
|
||||
|
||||
charAry, val1, val2, val3 = parsedStruct.unpack_from(arr) #convert byte array to formatted struct
|
||||
charAry = charAry.rstrip(' \t\r\n\0') #remove trailing white space from buffer
|
||||
return charAry, val1, val2, val3
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
"""Wait for incoming message published by Adafruit MQTT client"""
|
||||
global args
|
||||
args = argBegin()
|
||||
client = mqtt.Client()
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
client.username_pw_set(args.username, args.password)
|
||||
client.connect(args.host, args.port, args.keepalive)
|
||||
|
||||
# Blocking call that processes network traffic, dispatches callbacks and
|
||||
# handles reconnecting.
|
||||
# Other loop*() functions are available that give a threaded interface and a
|
||||
# manual interface.
|
||||
client.loop_forever()
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
|
@ -0,0 +1,147 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library ESP8266 Example
|
||||
|
||||
Must use ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board & Feather
|
||||
----> https://www.adafruit.com/product/2471
|
||||
----> https://www.adafruit.com/products/2821
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "...your SSID..."
|
||||
#define WLAN_PASS "...your password..."
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883 // use 8883 for SSL
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||
WiFiClient client;
|
||||
// or... use WiFiFlientSecure for SSL
|
||||
//WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
|
||||
// for some reason (only affects ESP8266, likely an arduino-builder bug).
|
||||
void MQTT_connect();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||
|
||||
// Setup MQTT subscription for onoff feed.
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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 our 'wait for incoming subscription packets' busy subloop
|
||||
// try to spend your time here
|
||||
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(5000))) {
|
||||
if (subscription == &onoffbutton) {
|
||||
Serial.print(F("Got: "));
|
||||
Serial.println((char *)onoffbutton.lastread);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending photocell val "));
|
||||
Serial.print(x);
|
||||
Serial.print("...");
|
||||
if (! photocell.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
// ping the server to keep the mqtt connection alive
|
||||
// NOT required if you are publishing once every KEEPALIVE seconds
|
||||
/*
|
||||
if(! mqtt.ping()) {
|
||||
mqtt.disconnect();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t 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) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library ESP8266 Example
|
||||
|
||||
Must use ESP8266 Arduino from:
|
||||
https://github.com/esp8266/Arduino
|
||||
|
||||
Works great with Adafruit's Huzzah ESP board:
|
||||
----> https://www.adafruit.com/product/2471
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "network"
|
||||
#define WLAN_PASS "password"
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883
|
||||
#define AIO_USERNAME "user"
|
||||
#define AIO_KEY "key"
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||
WiFiClient client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'time' for subscribing to current time
|
||||
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
|
||||
|
||||
// Setup a feed called 'slider' for subscribing to changes on the slider
|
||||
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider", MQTT_QOS_1);
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes to the button
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff", MQTT_QOS_1);
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
int sec;
|
||||
int min;
|
||||
int hour;
|
||||
|
||||
int timeZone = -4; // utc-4 eastern daylight time (nyc)
|
||||
|
||||
void timecallback(uint32_t current) {
|
||||
|
||||
// adjust to local time zone
|
||||
current += (timeZone * 60 * 60);
|
||||
|
||||
// calculate current time
|
||||
sec = current % 60;
|
||||
current /= 60;
|
||||
min = current % 60;
|
||||
current /= 60;
|
||||
hour = current % 24;
|
||||
|
||||
// print hour
|
||||
if(hour == 0 || hour == 12)
|
||||
Serial.print("12");
|
||||
if(hour < 12)
|
||||
Serial.print(hour);
|
||||
else
|
||||
Serial.print(hour - 12);
|
||||
|
||||
// print mins
|
||||
Serial.print(":");
|
||||
if(min < 10) Serial.print("0");
|
||||
Serial.print(min);
|
||||
|
||||
// print seconds
|
||||
Serial.print(":");
|
||||
if(sec < 10) Serial.print("0");
|
||||
Serial.print(sec);
|
||||
|
||||
if(hour < 12)
|
||||
Serial.println(" am");
|
||||
else
|
||||
Serial.println(" pm");
|
||||
|
||||
}
|
||||
|
||||
void slidercallback(double x) {
|
||||
Serial.print("Hey we're in a slider callback, the slider value is: ");
|
||||
Serial.println(x);
|
||||
}
|
||||
|
||||
void onoffcallback(char *data, uint16_t len) {
|
||||
Serial.print("Hey we're in a onoff callback, the button value is: ");
|
||||
Serial.println(data);
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||
|
||||
timefeed.setCallback(timecallback);
|
||||
slider.setCallback(slidercallback);
|
||||
onoffbutton.setCallback(onoffcallback);
|
||||
|
||||
// Setup MQTT subscription for time feed.
|
||||
mqtt.subscribe(&timefeed);
|
||||
mqtt.subscribe(&slider);
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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 our 'wait for incoming subscription packets and callback em' busy subloop
|
||||
// try to spend your time here:
|
||||
mqtt.processPackets(10000);
|
||||
|
||||
// ping the server to keep the mqtt connection alive
|
||||
// NOT required if you are publishing once every KEEPALIVE seconds
|
||||
|
||||
if(! mqtt.ping()) {
|
||||
mqtt.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t retries = 3;
|
||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||
Serial.println(mqtt.connectErrorString(ret));
|
||||
Serial.println("Retrying MQTT connection in 10 seconds...");
|
||||
mqtt.disconnect();
|
||||
delay(10000); // wait 10 seconds
|
||||
retries--;
|
||||
if (retries == 0) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library Ethernet Example
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Alec Moore
|
||||
Derived from the code written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <SPI.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
#include <Ethernet.h>
|
||||
#include <EthernetClient.h>
|
||||
#include <Dns.h>
|
||||
#include <Dhcp.h>
|
||||
|
||||
/************************* Ethernet Client Setup *****************************/
|
||||
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
||||
|
||||
//Uncomment the following, and set to a valid ip if you don't have dhcp available.
|
||||
//IPAddress iotIP (192, 168, 0, 42);
|
||||
//Uncomment the following, and set to your preference if you don't have automatic dns.
|
||||
//IPAddress dnsIP (8, 8, 8, 8);
|
||||
//If you uncommented either of the above lines, make sure to change "Ethernet.begin(mac)" to "Ethernet.begin(mac, iotIP)" or "Ethernet.begin(mac, iotIP, dnsIP)"
|
||||
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
//Set up the ethernet client
|
||||
EthernetClient client;
|
||||
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
// You don't need to change anything below this line!
|
||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo"));
|
||||
|
||||
// Initialise the Client
|
||||
Serial.print(F("\nInit the Client..."));
|
||||
Ethernet.begin(mac);
|
||||
delay(1000); //give the ethernet a second to initialize
|
||||
|
||||
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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 our 'wait for incoming subscription packets' busy subloop
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(1000))) {
|
||||
if (subscription == &onoffbutton) {
|
||||
Serial.print(F("Got: "));
|
||||
Serial.println((char *)onoffbutton.lastread);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending photocell val "));
|
||||
Serial.print(x);
|
||||
Serial.print("...");
|
||||
if (! photocell.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
// ping the server to keep the mqtt connection alive
|
||||
if(! mqtt.ping()) {
|
||||
mqtt.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
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
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <Adafruit_SleepyDog.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include "Adafruit_FONA.h"
|
||||
|
||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||
|
||||
extern Adafruit_FONA fona;
|
||||
extern SoftwareSerial fonaSS;
|
||||
|
||||
boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) {
|
||||
Watchdog.reset();
|
||||
|
||||
Serial.println(F("Initializing FONA....(May take 3 seconds)"));
|
||||
|
||||
fonaSS.begin(4800); // if you're using software serial
|
||||
|
||||
if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1)
|
||||
Serial.println(F("Couldn't find FONA"));
|
||||
return false;
|
||||
}
|
||||
fonaSS.println("AT+CMEE=2");
|
||||
Serial.println(F("FONA is OK"));
|
||||
Watchdog.reset();
|
||||
Serial.println(F("Checking for network..."));
|
||||
while (fona.getNetworkStatus() != 1) {
|
||||
delay(500);
|
||||
}
|
||||
|
||||
Watchdog.reset();
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
Watchdog.reset();
|
||||
|
||||
fona.setGPRSNetworkSettings(apn, username, password);
|
||||
|
||||
Serial.println(F("Disabling GPRS"));
|
||||
fona.enableGPRS(false);
|
||||
|
||||
Watchdog.reset();
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
Watchdog.reset();
|
||||
|
||||
Serial.println(F("Enabling GPRS"));
|
||||
if (!fona.enableGPRS(true)) {
|
||||
Serial.println(F("Failed to turn GPRS on"));
|
||||
return false;
|
||||
}
|
||||
Watchdog.reset();
|
||||
|
||||
return true;
|
||||
}
|
169
libraries/Adafruit_MQTT_Library/examples/mqtt_fona/mqtt_fona.ino
Normal file
169
libraries/Adafruit_MQTT_Library/examples/mqtt_fona/mqtt_fona.ino
Normal file
|
@ -0,0 +1,169 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library FONA Example
|
||||
|
||||
Designed specifically to work with the Adafruit FONA
|
||||
----> http://www.adafruit.com/products/1946
|
||||
----> http://www.adafruit.com/products/1963
|
||||
----> http://www.adafruit.com/products/2468
|
||||
----> http://www.adafruit.com/products/2542
|
||||
|
||||
These cellular modules use TTL Serial to communicate, 2 pins are
|
||||
required to interface.
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include "Adafruit_FONA.h"
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_FONA.h"
|
||||
|
||||
/*************************** FONA Pins ***********************************/
|
||||
|
||||
// Default pins for Feather 32u4 FONA
|
||||
#define FONA_RX 9
|
||||
#define FONA_TX 8
|
||||
#define FONA_RST 4
|
||||
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
|
||||
|
||||
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
// Optionally configure a GPRS APN, username, and password.
|
||||
// You might need to do this to access your network's GPRS/data
|
||||
// network. Contact your provider for the exact APN, username,
|
||||
// and password values. Username and password are optional and
|
||||
// can be removed, but APN is required.
|
||||
#define FONA_APN ""
|
||||
#define FONA_USERNAME ""
|
||||
#define FONA_PASSWORD ""
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details.
|
||||
Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
// You don't need to change anything below this line!
|
||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||
|
||||
// FONAconnect is a helper function that sets up the FONA and connects to
|
||||
// the GPRS network. See the fonahelper.cpp tab above for the source!
|
||||
boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
// How many transmission failures in a row we're willing to be ok with before reset
|
||||
uint8_t txfailures = 0;
|
||||
#define MAXTXFAILURES 3
|
||||
|
||||
void setup() {
|
||||
while (!Serial);
|
||||
|
||||
// Watchdog is optional!
|
||||
//Watchdog.enable(8000);
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println(F("Adafruit FONA MQTT demo"));
|
||||
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
|
||||
Watchdog.reset();
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
Watchdog.reset();
|
||||
|
||||
// Initialise the FONA module
|
||||
while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) {
|
||||
Serial.println("Retrying FONA");
|
||||
}
|
||||
|
||||
Serial.println(F("Connected to Cellular!"));
|
||||
|
||||
Watchdog.reset();
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
Watchdog.reset();
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
void loop() {
|
||||
// Make sure to reset watchdog every loop iteration!
|
||||
Watchdog.reset();
|
||||
|
||||
// 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();
|
||||
|
||||
Watchdog.reset();
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending photocell val "));
|
||||
Serial.print(x);
|
||||
Serial.print("...");
|
||||
if (! photocell.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
txfailures++;
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
txfailures = 0;
|
||||
}
|
||||
|
||||
Watchdog.reset();
|
||||
// this is our 'wait for incoming subscription packets' busy subloop
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(5000))) {
|
||||
if (subscription == &onoffbutton) {
|
||||
Serial.print(F("Got: "));
|
||||
Serial.println((char *)onoffbutton.lastread);
|
||||
}
|
||||
}
|
||||
|
||||
// ping the server to keep the mqtt connection alive, only needed if we're not publishing
|
||||
//if(! mqtt.ping()) {
|
||||
// Serial.println(F("MQTT Ping failed."));
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
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
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library WINC1500 Example
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <SPI.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
#include <WiFi101.h>
|
||||
|
||||
/************************* WiFI Setup *****************************/
|
||||
#define WINC_CS 8
|
||||
#define WINC_IRQ 7
|
||||
#define WINC_RST 4
|
||||
#define WINC_EN 2 // or, tie EN to VCC
|
||||
|
||||
char ssid[] = "yournetwork"; // your network SSID (name)
|
||||
char pass[] = "yourpassword"; // your network password (use for WPA, or use as key for WEP)
|
||||
int keyIndex = 0; // your network key Index number (needed only for WEP)
|
||||
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883
|
||||
#define AIO_USERNAME "adafruitiousername"
|
||||
#define AIO_KEY "adafruitiokey"
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
//Set up the wifi client
|
||||
WiFiClient client;
|
||||
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
// You don't need to change anything below this line!
|
||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
#define LEDPIN 13
|
||||
|
||||
|
||||
void setup() {
|
||||
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
|
||||
|
||||
while (!Serial);
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo for WINC1500"));
|
||||
|
||||
// Initialise the Client
|
||||
Serial.print(F("\nInit the WiFi module..."));
|
||||
// check for the presence of the breakout
|
||||
if (WiFi.status() == WL_NO_SHIELD) {
|
||||
Serial.println("WINC1500 not present");
|
||||
// don't continue:
|
||||
while (true);
|
||||
}
|
||||
Serial.println("ATWINC OK!");
|
||||
|
||||
pinMode(LEDPIN, OUTPUT);
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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 our 'wait for incoming subscription packets' busy subloop
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(5000))) {
|
||||
if (subscription == &onoffbutton) {
|
||||
Serial.print(F("Got: "));
|
||||
Serial.println((char *)onoffbutton.lastread);
|
||||
|
||||
if (0 == strcmp((char *)onoffbutton.lastread, "OFF")) {
|
||||
digitalWrite(LEDPIN, LOW);
|
||||
}
|
||||
if (0 == strcmp((char *)onoffbutton.lastread, "ON")) {
|
||||
digitalWrite(LEDPIN, HIGH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending photocell val "));
|
||||
Serial.print(x);
|
||||
Serial.print("...");
|
||||
if (! photocell.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// attempt to connect to Wifi network:
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to SSID: ");
|
||||
Serial.println(ssid);
|
||||
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
|
||||
// wait 10 seconds for connection:
|
||||
uint8_t timeout = 10;
|
||||
while (timeout && (WiFi.status() != WL_CONNECTED)) {
|
||||
timeout--;
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
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
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
116
libraries/Adafruit_MQTT_Library/examples/mqtt_yun/mqtt_yun.ino
Normal file
116
libraries/Adafruit_MQTT_Library/examples/mqtt_yun/mqtt_yun.ino
Normal file
|
@ -0,0 +1,116 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library Arduino Yun Example
|
||||
|
||||
Make sure your Arduino Yun is connected to a WiFi access point which
|
||||
has internet access. Also note this sketch uses the Console class
|
||||
for debug output so make sure to connect to the Yun over WiFi and
|
||||
open the serial monitor to see the console output.
|
||||
|
||||
Works great with the Arduino Yun:
|
||||
----> https://www.adafruit.com/products/1498
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <Bridge.h>
|
||||
#include <Console.h>
|
||||
#include <BridgeClient.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Create a BridgeClient instance to communicate using the Yun's bridge & Linux OS.
|
||||
BridgeClient client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup() {
|
||||
Bridge.begin();
|
||||
Console.begin();
|
||||
Console.println(F("Adafruit MQTT demo"));
|
||||
|
||||
// Setup MQTT subscription for onoff feed.
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
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 our 'wait for incoming subscription packets' busy subloop
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(1000))) {
|
||||
if (subscription == &onoffbutton) {
|
||||
Console.print(F("Got: "));
|
||||
Console.println((char *)onoffbutton.lastread);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we can publish stuff!
|
||||
Console.print(F("\nSending photocell val "));
|
||||
Console.print(x);
|
||||
Console.print("...");
|
||||
if (! photocell.publish(x++)) {
|
||||
Console.println(F("Failed"));
|
||||
} else {
|
||||
Console.println(F("OK!"));
|
||||
}
|
||||
|
||||
// ping the server to keep the mqtt connection alive
|
||||
if(! mqtt.ping()) {
|
||||
Console.println(F("MQTT Ping failed."));
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Console.print("Connecting to MQTT... ");
|
||||
|
||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||
Console.println(mqtt.connectErrorString(ret));
|
||||
Console.println("Retrying MQTT connection in 5 seconds...");
|
||||
mqtt.disconnect();
|
||||
delay(5000); // wait 5 seconds
|
||||
}
|
||||
Console.println("MQTT Connected!");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue