Adafruit is open and shipping! Let's build back better, together!
0

IO Output 'handleMessage' problem
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

IO Output 'handleMessage' problem

by Buddyboy75 on Sun Jul 26, 2020 6:23 am

Im trying to construct a WIFI switch using the IO dashboard and linking with IFTTT for google home commands.

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.

I was playing around with this about a year ago, and it worked fine. I haven't changed anything since then. Have Adafruit changed anything their end??

Any help would be great.

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by brubell on Tue Jul 28, 2020 9:22 am

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.


Does the sketch initially connect to Adafruit IO?

Could you post your code - please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials..

brubell
 
Posts: 1178
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Sun Aug 02, 2020 3:42 am

Hi Brubell

Im using the standard adafruit Digital Output sketch:-

For Connection:-

#define IO_USERNAME "My_username"
#define IO_KEY "My_key"

#define WIFI_SSID "My_ssid"
#define WIFI_PASS "My_password"

#include "AdafruitIO_WiFi.h"
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);

Main Sketch-:

/************************** Configuration ***********************************/

#include "config.h"

/************************ Example Starts Here *******************************/

// digital pin 0
#define RELAY4 0

// set up the 'digital' feed
AdafruitIO_Feed *EntranceLight = io.feed("EntranceLight");

void setup() {

pinMode(RELAY4, OUTPUT);

// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while(! Serial);

// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();

// set up a message handler for the 'digital' feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
EntranceLight->onMessage(handleMessage);

// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}

// we are connected
Serial.println();
Serial.println(io.statusText());
EntranceLight->get();

}

void loop() {

// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();

}

// this function is called whenever an 'digital' feed message
// is received from Adafruit IO. it was attached to
// the 'digital' feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {

Serial.print("received <- ");

if(data->toPinLevel() == HIGH)
Serial.println("HIGH");
else
Serial.println("LOW");


digitalWrite(RELAY4, data->toPinLevel());
}

I just cant seem to pinpoint what im doing wrong.

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Sun Aug 02, 2020 4:37 am

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.,

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by brubell on Mon Aug 03, 2020 9:41 am

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.,

Is the toggle button on your dashboard set up similar to this page: https://learn.adafruit.com/adafruit-io- ... -2236585-7

Note that the Button On Text is 1 and the Button Off Text is 0

brubell
 
Posts: 1178
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Mon Aug 03, 2020 6:37 pm

Hi Brubell

Yes, i have made sure that the switch sends 1 & 0. I have updated all files on the IDE, when I get home tonight im gonna go through everything again, see if I can nut this out, its such a simple setup, shouldnt be too hard to sort.
Attachments
Screenshot_20200804-082216_Chrome.jpg
Screenshot_20200804-082216_Chrome.jpg (184.46 KiB) Viewed 143 times

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by brubell on Wed Aug 05, 2020 11:29 am

Ok, let me know how it goes or if you didn't get it sorted

brubell
 
Posts: 1178
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Wed Aug 05, 2020 5:52 pm

Thanks, will do.

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Fri Aug 07, 2020 8:26 am

Hey Brubell

So I managed to get a bit of spare time to go over everything, I've checked all of my updates (missed a couple!!) but still not staying connected (or whatever the problem is).

Followed the Digital I/O tutorial and reprogrammed the 8266 back to stock adafruit IO Digital output code, but still getting the same issue.

Just to clarify what I have so far. Network Config works fine, I can connect to adafruit I/O.

Connecting to Adafruit IO...............
Adafruit IO Connected.
received <- HIGH

But when I change the state of the slider on the dashboard (Adafruit IO) from 1 to 0, I get nothing.
When I reset the 8266, and reconnects to Adafruit IO, it will receive the new data (0) and will display "LOW".

My Guess is there is a problem Between the:-

void loop() {

// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();

Or a problem here:-

void handleMessage(AdafruitIO_Data *data)

{

Serial.print("received <- ");

if(data->toPinLevel() == HIGH)
Serial.println("HIGH");
else
Serial.println("LOW");

Before the SSL/TLS Cert update it worked flawlessly. I have only had problems since then. That's all the info I can Give. I'm a complete Noob at this, but a determined noob lol.

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by brubell on Fri Aug 07, 2020 9:46 am

Are you able to connect to IO using this sketch?
https://github.com/adafruit/Adafruit_MQ ... sp8266.ino

brubell
 
Posts: 1178
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Mon Aug 10, 2020 1:42 am

SO I tried that sketch and it seems to work fine, its publishing and subscribing like its supposed to.
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).

This is what i'm getting:-

Lounge Light Publish.png
IO Dashboard
Lounge Light Publish.png (5.44 KiB) Viewed 106 times

Lounge Light Publish1.png
Serial Monitor
Lounge Light Publish1.png (7.06 KiB) Viewed 106 times


Can I use MQTT to operate the relays?

Thanks Brubell

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by brubell on Mon Aug 10, 2020 9:40 am

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).


This seems correct to me.

Can I use MQTT to operate the relays?

You should be able to

brubell
 
Posts: 1178
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Wed Aug 12, 2020 2:52 am

Thankyou vey much for pointing me in the right direction Brubell

Finally cracked it, I now have a working smart home lighting system, just gotta connect via IFTT for my google home hub, and then onto the next smart home project.

Thanks Again.

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  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!");
}

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Re: IO Output 'handleMessage' problem

by brubell on Wed Aug 12, 2020 1:26 pm

No problem - thanks for debugging with me :)

brubell
 
Posts: 1178
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Output 'handleMessage' problem

by Buddyboy75 on Fri Aug 14, 2020 12:18 am

Im now having an issue with MQTT. Should I continue here or post in a new topic?

Buddyboy75
 
Posts: 9
Joined: Wed Jan 29, 2020 6:35 am

Please be positive and constructive with your questions and comments.