Adafruit MQTT Library ESP8266 Example
Adafruit MQTT Library ESP8266 Example
// 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);
void setup() {
//Serial.begin(115200);
delay(10);
pinMode(rel1, OUTPUT);
pinMode(rel2, OUTPUT);
pinMode(rel3, OUTPUT);
pinMode(rel4, OUTPUT);
digitalWrite(rel1,HIGH);
digitalWrite(rel2,HIGH);
digitalWrite(rel3,HIGH);
digitalWrite(rel4,HIGH);
//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());
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();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
// to test the tx line data
if (subscription == &onoff1){
// Serial.print(F("Got: "));
// Serial.println((char *)onoff1.lastread);
uint16_t state1 = atoi((char *)onoff1.lastread);
// Serial.println(state1);
digitalWrite(rel1,state1);
}
if (subscription == &onoff2){
// Serial.print(F("Got: "));
// Serial.println((char *)onoff2.lastread);
uint16_t state2 = atoi((char *)onoff2.lastread);
//Serial.println(state2);
digitalWrite(rel2,state2);
}
if (subscription == &onoff3){
//Serial.print(F("Got: "));
//Serial.println((char *)onoff3.lastread);
uint16_t state3 = atoi((char *)onoff3.lastread);
// Serial.println(state3);
digitalWrite(rel3,state3);
}
if (subscription == &onoff4){
// Serial.print(F("Got: "));
// Serial.println((char *)onoff4.lastread);
uint16_t state4 = atoi((char *)onoff4.lastread);
// Serial.println(state4);
digitalWrite(rel4,state4);
}
}