Multiple ESP8266 subscription help please
Posted: Tue Jun 21, 2016 10:33 am
I am new to this forum as well as ioT so please be kind.
In the following sketch everything works as expected except for the second button(onoffbuttonb)
I kinda think the solution is simple but I cannot figure it out. The sketch compiles but that means nothing really.
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
****************************************************/
// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
// DHT 11 sensor
#define DHTPIN 5
#define DHTTYPE DHT22
//#define LED 0
// WiFi parameters
#define WLAN_SSID "NETGEAR43"
#define WLAN_PASS "hxxxxxxxxxxxx"
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "xxxxxxx"
#define AIO_KEY "xxxxxxxxxxxx"
// DHT sensor
DHT dht(DHTPIN, DHTTYPE, 22);
// Functions
void connect();
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, client ID, username, and password in flash memory.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);/****************************** Feeds ***************************************/
// Setup feeds for temperature & humidity
const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature";
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);
const char HUMIDITY_FEED[] PROGMEM = AIO_USERNAME "/feeds/humidity";
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED);
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
const char ONOFFB_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoffb";
Adafruit_MQTT_Subscribe onoffbuttonb = Adafruit_MQTT_Subscribe(&mqtt, ONOFFB_FEED);
/*************************** Sketch Code ************************************/
void setup() {
mqtt.subscribe(&onoffbutton);
mqtt.subscribe(&onoffbuttonb);
// Init sensor
dht.begin();
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(115200);
Serial.println(F("Adafruit IO Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
delay(10);
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
// connect to adafruit io
connect();
}
/*************************************************************************************/
void loop() {
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
// Grab the current state of the sensor
int humidity_data = (int)dht.readHumidity();
int temperature_data = (int)dht.readTemperature();
// Publish data
if (! temperature.publish(temperature_data * 1.8 + 32))
Serial.println(F("Failed to publish temperature"));
else
Serial.println(F("Temperature published!"));
if (! humidity.publish(humidity_data))
Serial.println(F("Failed to publish humidity"));
else
Serial.println(F("Humidity published!"));
// Repeat every 10 seconds
delay(10000);
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))){
if (subscription == &onoffbutton){
Serial.print(F("On-Off button: "));
Serial.println((char *)onoffbutton.lastread);
if (strcmp((char *)onoffbutton.lastread, "ON") == 0){
digitalWrite(0, LOW);
}
if (strcmp((char *)onoffbutton.lastread, "OFF") == 0){
digitalWrite(0, HIGH);
}
Adafruit_MQTT_Subscribe *subscription2;
while ((subscription2 = mqtt.readSubscription(5000))){
if (subscription2 == &onoffbuttonb){
Serial.print(F("On-Off buttonb: "));
Serial.println((char *)onoffbuttonb.lastread);
if (strcmp((char *)onoffbuttonb.lastread, "ON") == 0){
digitalWrite(2, LOW);
}
if (strcmp((char *)onoffbuttonb.lastread, "OFF") == 0){
digitalWrite(2, HIGH);
}
}
}
}
}
}
// connect to adafruit io via MQTT
void connect(){
Serial.print(F("Connecting to Adafruit IO... "));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (ret) {
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: Serial.println(F("Connection failed")); break;
}
if(ret >= 0)
mqtt.disconnect();
Serial.println(F("Retrying connection..."));
delay(5000);
}
Serial.println(F("Adafruit IO Connected!"));
}
[code][/code]
In the following sketch everything works as expected except for the second button(onoffbuttonb)
I kinda think the solution is simple but I cannot figure it out. The sketch compiles but that means nothing really.
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
****************************************************/
// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
// DHT 11 sensor
#define DHTPIN 5
#define DHTTYPE DHT22
//#define LED 0
// WiFi parameters
#define WLAN_SSID "NETGEAR43"
#define WLAN_PASS "hxxxxxxxxxxxx"
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "xxxxxxx"
#define AIO_KEY "xxxxxxxxxxxx"
// DHT sensor
DHT dht(DHTPIN, DHTTYPE, 22);
// Functions
void connect();
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, client ID, username, and password in flash memory.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);/****************************** Feeds ***************************************/
// Setup feeds for temperature & humidity
const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature";
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);
const char HUMIDITY_FEED[] PROGMEM = AIO_USERNAME "/feeds/humidity";
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED);
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
const char ONOFFB_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoffb";
Adafruit_MQTT_Subscribe onoffbuttonb = Adafruit_MQTT_Subscribe(&mqtt, ONOFFB_FEED);
/*************************** Sketch Code ************************************/
void setup() {
mqtt.subscribe(&onoffbutton);
mqtt.subscribe(&onoffbuttonb);
// Init sensor
dht.begin();
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(115200);
Serial.println(F("Adafruit IO Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
delay(10);
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
// connect to adafruit io
connect();
}
/*************************************************************************************/
void loop() {
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
// Grab the current state of the sensor
int humidity_data = (int)dht.readHumidity();
int temperature_data = (int)dht.readTemperature();
// Publish data
if (! temperature.publish(temperature_data * 1.8 + 32))
Serial.println(F("Failed to publish temperature"));
else
Serial.println(F("Temperature published!"));
if (! humidity.publish(humidity_data))
Serial.println(F("Failed to publish humidity"));
else
Serial.println(F("Humidity published!"));
// Repeat every 10 seconds
delay(10000);
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))){
if (subscription == &onoffbutton){
Serial.print(F("On-Off button: "));
Serial.println((char *)onoffbutton.lastread);
if (strcmp((char *)onoffbutton.lastread, "ON") == 0){
digitalWrite(0, LOW);
}
if (strcmp((char *)onoffbutton.lastread, "OFF") == 0){
digitalWrite(0, HIGH);
}
Adafruit_MQTT_Subscribe *subscription2;
while ((subscription2 = mqtt.readSubscription(5000))){
if (subscription2 == &onoffbuttonb){
Serial.print(F("On-Off buttonb: "));
Serial.println((char *)onoffbuttonb.lastread);
if (strcmp((char *)onoffbuttonb.lastread, "ON") == 0){
digitalWrite(2, LOW);
}
if (strcmp((char *)onoffbuttonb.lastread, "OFF") == 0){
digitalWrite(2, HIGH);
}
}
}
}
}
}
// connect to adafruit io via MQTT
void connect(){
Serial.print(F("Connecting to Adafruit IO... "));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (ret) {
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: Serial.println(F("Connection failed")); break;
}
if(ret >= 0)
mqtt.disconnect();
Serial.println(F("Retrying connection..."));
delay(5000);
}
Serial.println(F("Adafruit IO Connected!"));
}
[code][/code]