IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
once I have uploaded the sketch to the ESP8266, it connects fine, it will receive the current state of the switch on the dashboard. But when I change the state of the switch, nothing happens, there is no change in the serial monitor. But if I reset the ESP8266, it will then receive the changed state once it has reconnected to Adafruit IO.
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
Buddyboy75 wrote:Yes it connects with adafruit IO fine, the serial monitor shows me what state the dashboard is in ie. "High" or "Low".
But when I click the button on the IO dashboard, nothing happens with the 8266, also no change on the serial monitor. But if I press the reset on my relay, the 8266 then receives the change state from adafruit IO.
For some reason, its not continuously refreshing, so will not recieve a change of state from adafruit IO, not with out pressing reset on my relay.,
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem
When it receives "val 1" the slider changes over to on (1) when it receives any other value (0,2,3 etc.) the slider changes to off (0).
Can I use MQTT to operate the relays?
Re: IO Output 'handleMessage' problem
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// the on off button feed turns this LED on/off
#define RELAY1 0
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "My SSID"
#define WLAN_PASS "My Password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "My Username"
#define AIO_KEY "My Key"
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure 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_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Subscribe loungelight = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/loungelight");
/*************************** 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();
void setup() {
pinMode(RELAY1, OUTPUT);
Serial.begin(115200);
delay(10);
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 & slider feed.
mqtt.subscribe(&loungelight);
}
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))) {
// Check if its the onoff button feed
if (subscription == &loungelight) {
Serial.print(F("Lounge Lights: "));
Serial.println((char *)loungelight.lastread);
if (strcmp((char *)loungelight.lastread, "1") == 0) {
digitalWrite(RELAY1, 1);
}
if (strcmp((char *)loungelight.lastread, "0") == 0) {
digitalWrite(RELAY1, 0);
}
}
// ping the server to keep the mqtt connection alive
if(! mqtt.ping())
mqtt.disconnect();
}
}
// 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... ");
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!");
}
Re: IO Output 'handleMessage' problem
Re: IO Output 'handleMessage' problem