0

rying to get IO example working (LED will not turn on/off)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

rying to get IO example working (LED will not turn on/off)

by bilsma on Sun Dec 13, 2015 11:36 am

Hello,

I am using the Adafruit IO Basics: Digital Output tutorial to just simply turn the built in led on/off on the ESP8266 Huzzah board and it is not working. The console (see below) indicates it is connecting to IO and accepting the flipping of the IO switch but the LED is just staying on all the time.

I modified this code very little and it appears this code is working on other demos so I have doubts there is anything wrong with the code but If it is I think I narrowed it down to this snippet: (See whole sketch below)
Code: Select all | TOGGLE FULL SIZE
while (subscription = mqtt.readSubscription(1000)) {

    // we only care about the lamp events
    if (subscription == &lamp) {

      // convert mqtt ascii payload to int
      char *value = (char *)lamp.lastread;
      Serial.print(F("Received: "));
      Serial.println(value);
      int current = atoi(value);

      // write the current state to the power switch tail
      digitalWrite(BUILTIN_LED, current == 1 ? HIGH : LOW);

    }

  }

For testing purposes I printed the "current" value to the console and it always ends up being "0" no matter if "on" or "off" is received. I am assuming that "int current = atoi(value);" is doing this but I do not understand why. While I am very new to this It seems that this value should be a 1 or 0 depending if it receives an "on" or "off" from io. I looked up the atoi() function and I see that it converts a string to an integer but I do not understand what value it will end up as. Does it convert all strings to a 0? If so wouldn't "digitalWrite(BUILTIN_LED, current == 1 ? HIGH : LOW);" always be HIGH?

Any help would be appreciated and if it not the code I have no IDEA what to look for on the IO site or my setup.

Console output:
Adafruit IO Example


Connecting to WIFIAP
...
WiFi connected
IP address:
192.168.1.198
Connecting to Adafruit IO... Adafruit IO Connected!
Received: OFF

Received: ON

Received: ON

Received: OFF

Received: ON


My whole Sketch:
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:
  ----> https://www.adafruit.com/product/2471
  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.
  Adafruit IO example additions by Todd Treece.
  MIT license, all text above must be included in any redistribution
 ****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

// function prototypes
void connect(void);

/****************************** Pins ******************************************/

//#define LAMP            2  // power switch tail

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "WIFIAP"
#define WLAN_PASS       "removed"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "bilsma"
#define AIO_KEY         "Removed"

/************ Global State (you don't need to change this!) ******************/

// 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.
// This is required for using the Adafruit MQTT library.
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  = __TIME__ AIO_USERNAME;
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 a feed called 'lamp' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp";
Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, LAMP_FEED);

/*************************** Sketch Code ************************************/

void setup() {

  // set power switch tail pin as an output
  pinMode(BUILTIN_LED, 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());

  // listen for events on the lamp feed
  mqtt.subscribe(&lamp);

  // connect to adafruit io
  connect();

}

void loop() {

  Adafruit_MQTT_Subscribe *subscription;

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }

  // this is our 'wait for incoming subscription packets' busy subloop
 while (subscription = mqtt.readSubscription(1000)) {

    // we only care about the lamp events
    if (subscription == &lamp) {

      // convert mqtt ascii payload to int
      char *value = (char *)lamp.lastread;
      Serial.print(F("Received: "));
      Serial.println(value);
      int current = atoi(value);

      // write the current state to the power switch tail
      digitalWrite(BUILTIN_LED, current == 1 ? HIGH : LOW);

    }

  }

}

// 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!"));

}

bilsma
 
Posts: 6
Joined: Mon Dec 13, 2010 11:45 pm

Re: rying to get IO example working (LED will not turn on/of

by aforaste1 on Sun Dec 13, 2015 1:31 pm

In your online dashboard did you set the toggle switch values to be "ON" and "OFF"? If so, try changing the values to "1" and "0" and then try again. It looks like your code is looking for a 0 or 1 value while your board is sending either on or off.
Last edited by aforaste1 on Sun Dec 13, 2015 8:08 pm, edited 1 time in total.

aforaste1
 
Posts: 18
Joined: Sun Nov 01, 2015 11:40 am

Re: rying to get IO example working (LED will not turn on/of

by bilsma on Sun Dec 13, 2015 3:26 pm

aforaste1,

Yes, that was it. Now it makes sense.

Thank you!

bilsma
 
Posts: 6
Joined: Mon Dec 13, 2010 11:45 pm

Please be positive and constructive with your questions and comments.