Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* Edited and compiled by Oduro Boateng
* whatsapp only : 0266302607
* electrotekgh@gmail.com
* www.sirboatengonline.com
*/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "Your router/Hotspot SSID"
#define WLAN_PASS "your router/Hotspot password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com" // dont change it
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "adafruit username"
#define AIO_KEY "adafruit token 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_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe Relay1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay1");
Adafruit_MQTT_Subscribe Relay2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay2");
Adafruit_MQTT_Subscribe Relay3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay3");
Adafruit_MQTT_Subscribe Relay4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay4");
/*************************** Sketch Code ************************************/
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
pinMode (5, OUTPUT);
pinMode (6, OUTPUT);
pinMode (7, OUTPUT);
pinMode (8, OUTPUT);
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(&Relay1);
}
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 == &Relay1) {
Serial.print(F("Got_Relay1: "));
Serial.println((char *)Relay1.lastread);
uint16_t num = atoi ((char *)Relay1.lastread);
digitalWrite (5, num);
}
if (subscription == &Relay2) {
Serial.print(F("Got_Relay2: "));
Serial.println((char *)Relay2.lastread);
uint16_t num = atoi ((char *)Relay2.lastread);
digitalWrite (6, num);
}
if (subscription == &Relay3) {
Serial.print(F("Got_Relay3: "));
Serial.println((char *)Relay3.lastread);
uint16_t num = atoi ((char *)Relay3.lastread);
digitalWrite (7, num);
}
if (subscription == &Relay4) {
Serial.print(F("Got_Relay4: "));
Serial.println((char *)Relay4.lastread);
uint16_t num = atoi ((char *)Relay4.lastread);
digitalWrite (8, num);
}
}
}
// Function to connect and reconnect as necessary to the MQTT server.
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