Last active
November 28, 2018 12:47
-
-
Save li2hub/ac05af38d9f4f9f9e34a03dd0fe94779 to your computer and use it in GitHub Desktop.
This file contains 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 "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 | |
#define AIO_USERNAME "xxxxxxxxxx" | |
#define AIO_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
WiFiClient client; | |
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); | |
/****************************** Feeds ***************************************/ | |
Adafruit_MQTT_Publish IRsensor = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Analog"); | |
Adafruit_MQTT_Subscribe LEDcontrol = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/BUILTIN"); | |
/*************************** Sketch Code ************************************/ | |
void MQTT_connect(); | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
pinMode(D0,OUTPUT); | |
// 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()); | |
mqtt.subscribe(&LEDcontrol); | |
} | |
int x=0; | |
void loop() { | |
MQTT_connect(); | |
Adafruit_MQTT_Subscribe *subscription; | |
while ((subscription = mqtt.readSubscription(5000))) { | |
if (subscription == &LEDcontrol) { | |
Serial.print(F("Got: ")); | |
Serial.println((char *)LEDcontrol.lastread); | |
int Light_State = atoi((char *)LEDcontrol.lastread); | |
digitalWrite(D0, Light_State); | |
} | |
} | |
x=analogRead(A0); | |
Serial.print(F("\nSending analog val ")); | |
Serial.print(x); | |
Serial.print("..."); | |
if (! IRsensor.publish(x)) { | |
Serial.println(F("Failed")); | |
} else { | |
Serial.println(F("OK!")); | |
} | |
} | |
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!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment