2016-07-05 22:12:06 +02:00
|
|
|
/***************************************************
|
|
|
|
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 *********************************/
|
|
|
|
|
2019-02-19 22:36:34 +01:00
|
|
|
#define WLAN_SSID "WLAN_SSID"
|
|
|
|
#define WLAN_PASS "WIFI_PASSWORD"
|
2016-07-05 22:12:06 +02:00
|
|
|
|
|
|
|
/************************* Adafruit.io Setup *********************************/
|
|
|
|
|
|
|
|
#define AIO_SERVER "io.adafruit.com"
|
2019-02-19 22:36:34 +01:00
|
|
|
// 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"
|
2016-07-05 22:12:06 +02:00
|
|
|
|
|
|
|
/************ 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.
|
2016-08-15 19:06:17 +02:00
|
|
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
2016-07-05 22:12:06 +02:00
|
|
|
|
2018-08-18 00:01:52 +02:00
|
|
|
// io.adafruit.com SHA1 fingerprint
|
2019-02-19 22:36:34 +01:00
|
|
|
const char *fingerprint = "77 00 54 2D DA E7 D8 03 27 31 23 99 EB 27 DB CB A5 4C 57 18";
|
2016-07-05 22:12:06 +02:00
|
|
|
|
|
|
|
/****************************** Feeds ***************************************/
|
|
|
|
|
2016-08-15 21:24:21 +02:00
|
|
|
// Setup a feed called 'test' for publishing.
|
2016-07-05 22:12:06 +02:00
|
|
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
2016-08-15 21:24:21 +02:00
|
|
|
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
|
2016-07-05 22:12:06 +02:00
|
|
|
|
|
|
|
/*************************** 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 verifyFingerprint();
|
|
|
|
|
|
|
|
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
|
|
|
|
verifyFingerprint();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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!"));
|
|
|
|
}
|
|
|
|
|
2016-07-05 22:16:46 +02:00
|
|
|
// wait a couple seconds to avoid rate limit
|
|
|
|
delay(2000);
|
|
|
|
|
2016-07-05 22:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void verifyFingerprint() {
|
|
|
|
|
|
|
|
const char* host = AIO_SERVER;
|
|
|
|
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
Serial.println(host);
|
|
|
|
|
|
|
|
if (! client.connect(host, AIO_SERVERPORT)) {
|
|
|
|
Serial.println("Connection failed. Halting execution.");
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client.verify(fingerprint, host)) {
|
|
|
|
Serial.println("Connection secure.");
|
|
|
|
} else {
|
|
|
|
Serial.println("Connection insecure! Halting execution.");
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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!");
|
|
|
|
}
|