Last active
June 1, 2018 01:59
-
-
Save sowbug/de6525ee2be78e1c5ba7334803d2bd18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WEMOS_SHT3X.h> | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
/************************* WiFi Access Point *********************************/ | |
#define WLAN_SSID "my-ssid" | |
#define WLAN_PASS "my-wifi-password" | |
/************************* Adafruit.io Setup *********************************/ | |
#define AIO_SERVER "io.adafruit.com" | |
#define AIO_SERVERPORT 1883 | |
#define AIO_USERNAME "my-aio-account" | |
#define AIO_KEY "my-aio-key" | |
/************ Global State (you don't need to change this!) ******************/ | |
// Create an ESP8266 WiFiClient class to connect to the MQTT server. | |
WiFiClient client; | |
// Store the MQTT server, username, and password in flash memory. | |
// This is required for using the Adafruit MQTT library. | |
const char *MQTT_SERVER = AIO_SERVER; | |
const char *MQTT_USERNAME = AIO_USERNAME; | |
const char *MQTT_PASSWORD = AIO_KEY; | |
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. | |
#define DEVICE_ID "0" // I couldn't figure out how to embed the device ID | |
// using this library, which requires strings to be in flash | |
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); | |
Adafruit_MQTT_Publish device = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/f/device"); | |
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/f/temperature-" DEVICE_ID); | |
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/f/humidity-" DEVICE_ID); | |
/*************************** 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(); | |
static String device_name; | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
device_name = String("esp8266-") + ESP.getChipId(); | |
// Connect to WiFi access point. | |
Serial.println(); Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(WLAN_SSID); | |
WiFi.begin(WLAN_SSID, WLAN_PASS); | |
Serial.println("wifi connecting..."); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.print("connected"); | |
Serial.println(); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
// 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)); | |
mqtt.disconnect(); | |
delay(5000); // wait 5 seconds | |
} | |
Serial.println("MQTT Connected!"); | |
} | |
static bool first_loop = true; | |
char last_now[64] = "--"; | |
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(); | |
if (first_loop) { | |
first_loop = false; | |
device.publish((device_name + "online").c_str()); | |
} | |
if (sht30.get() == 0) { | |
temperature.publish(sht30.fTemp); | |
humidity.publish(sht30.humidity); | |
} | |
// ping the server to keep the mqtt connection alive | |
if (!mqtt.ping()) { | |
mqtt.disconnect(); | |
} | |
// delay(1000 * 60 * 10); | |
delay(30 * 1000); | |
if (WiFi.status() != WL_CONNECTED) { | |
ESP.reset(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment