Feather HUZZAH ESP8266 connect to Adafruit IO

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.
Locked
User avatar
GinaChiang
 
Posts: 7
Joined: Sun Dec 25, 2022 3:19 am

Feather HUZZAH ESP8266 connect to Adafruit IO

Post by GinaChiang »

I'm try to connect my ESP8266 to Adafruit IO but I always get: Disconnected from Adafruit IO (from io.statusText()). My IO_KEY is directly copy from the website so I think is not the mistyping issue.
I follow the adafruit IO setting guideline from this link https:
//learn.adafruit.com/adafruit-io-basics-esp8266-arduino/arduino-io-library

I'm using Arduino IDE for platform and my ESP8266 board version is 2.4.2.
My code list below:

Code: Select all

// Adafruit IO Publish Example
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

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

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
// #include "config.h"

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

// this int will hold the current count for our sketch
int count = 0;

#include <ESP8266WiFi.h>
#include "AdafruitIO_WiFi.h"
 
#define WIFI_SSID ""
#define WIFI_PASS ""
 
#define IO_USERNAME  "GinaChiang"
#define IO_KEY       ""

// Connect to Wi-Fi and Adafruit IO handel 
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);

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

void setup() {

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

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

  Serial.print("Connecting to Adafruit IO");

  // connect to io.adafruit.com
  io.connect();

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

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

}

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();

  // save count to the 'counter' feed on Adafruit IO
  Serial.print("sending -> ");
  Serial.println(count);
  counter->save(count);

  // increment the count by 1
  count++;

  // Adafruit IO is rate limited for publishing, so a delay is required in
  // between feed->save events. In this example, we will wait three seconds
  // (1000 milliseconds == 1 second) during each loop.
  delay(3000);

}

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by mikeysklar »

Can you paste in the full serial console output you are seeing?

Is your ESP8266 successfully getting an IP address and on-line? Can you ping the IP from another machine?

User avatar
GinaChiang
 
Posts: 7
Joined: Sun Dec 25, 2022 3:19 am

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by GinaChiang »

mikeysklar wrote: Sun Dec 25, 2022 1:04 pm Can you paste in the full serial console output you are seeing?

Is your ESP8266 successfully getting an IP address and on-line? Can you ping the IP from another machine?
The following screenshot is I taking from Serial Monitor
download/file.php?mode=view&id=96590

I think my ESP8266 is successfully getting an IP address and on-line
download/file.php?mode=view&id=96589

May I ask how to ping the IP from another machine?
Attachments
Screen Shot 2022-12-28 at 12.40.57 PM.png
Screen Shot 2022-12-28 at 12.40.57 PM.png (88.36 KiB) Viewed 377 times
Screen Shot 2022-12-28 at 12.37.02 PM.png
Screen Shot 2022-12-28 at 12.37.02 PM.png (44.56 KiB) Viewed 377 times

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by brubell »

What are the version numbers for the Adafruit IO Arduino and Adafruit MQTT libraries you have installed?

Let me know if you do not know how to check this, I can help.

User avatar
GinaChiang
 
Posts: 7
Joined: Sun Dec 25, 2022 3:19 am

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by GinaChiang »

brubell wrote: Wed Dec 28, 2022 2:36 pm What are the version numbers for the Adafruit IO Arduino and Adafruit MQTT libraries you have installed?

Let me know if you do not know how to check this, I can help.
I think my version of Adafruit IO Arduino and Adafruit MQTT libraries are 2.4.3 and 2.5.1.
Screen Shot 2022-12-30 at 1.49.02 PM.png
Screen Shot 2022-12-30 at 1.49.02 PM.png (28.74 KiB) Viewed 360 times
Screen Shot 2022-12-30 at 1.46.43 PM.png
Screen Shot 2022-12-30 at 1.46.43 PM.png (30.21 KiB) Viewed 360 times

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by brubell »

Please update Adafruit MQTT to 2.5.2 (latest) and try again.

User avatar
GinaChiang
 
Posts: 7
Joined: Sun Dec 25, 2022 3:19 am

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by GinaChiang »

brubell wrote: Fri Dec 30, 2022 11:12 am Please update Adafruit MQTT to 2.5.2 (latest) and try again.
I've update Adafruit MQTT to 2.5.2 but I still getting same result.

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by brubell »

Could you try updating the ESP8266 board support package to the absolute latest version?

User avatar
GinaChiang
 
Posts: 7
Joined: Sun Dec 25, 2022 3:19 am

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by GinaChiang »

brubell wrote: Tue Jan 03, 2023 2:09 pm Could you try updating the ESP8266 board support package to the absolute latest version?
Hi after updating I still getting same result.
Screen Shot 2023-01-04 at 3.29.00 PM.png
Screen Shot 2023-01-04 at 3.29.00 PM.png (64.67 KiB) Viewed 282 times
Is there any special requirement regarding to WIFI specification? Is this module works fine with either 2.4 and 5 GHz frequency of the wifi router?

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by brubell »

The ESP8266 only works on 2.4GHz wireless networks

User avatar
GinaChiang
 
Posts: 7
Joined: Sun Dec 25, 2022 3:19 am

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by GinaChiang »

brubell wrote: Fri Jan 06, 2023 12:05 pm The ESP8266 only works on 2.4GHz wireless networks
Thank you very much! I got another question that I have config.h file(download from example: adafruitio_22_environmental_monitor) to manage my Wifi and Adafruit IO connection. Just wondering what will happen if my wifi or Adafruit IO is suddenly not connected in the loop?

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by brubell »

GinaChiang wrote: Thu Jan 12, 2023 11:30 pm
brubell wrote: Fri Jan 06, 2023 12:05 pm The ESP8266 only works on 2.4GHz wireless networks
Thank you very much! I got another question that I have config.h file(download from example: adafruitio_22_environmental_monitor) to manage my Wifi and Adafruit IO connection. Just wondering what will happen if my wifi or Adafruit IO is suddenly not connected in the loop?
This depends on what your code/sketch looks like. If you can post it here, I can make a good guess at what it'll do.

User avatar
GinaChiang
 
Posts: 7
Joined: Sun Dec 25, 2022 3:19 am

Re: Feather HUZZAH ESP8266 connect to Adafruit IO

Post by GinaChiang »

brubell wrote: Fri Jan 13, 2023 12:16 pm
GinaChiang wrote: Thu Jan 12, 2023 11:30 pm
brubell wrote: Fri Jan 06, 2023 12:05 pm The ESP8266 only works on 2.4GHz wireless networks
Thank you very much! I got another question that I have config.h file(download from example: adafruitio_22_environmental_monitor) to manage my Wifi and Adafruit IO connection. Just wondering what will happen if my wifi or Adafruit IO is suddenly not connected in the loop?
This depends on what your code/sketch looks like. If you can post it here, I can make a good guess at what it'll do.
My code is attached below.

Code: Select all

// library for Wifi and Adafruit IO
#include <ESP8266WiFi.h>

// Wifi setting script
#include "config.h"

// library for TSL2591 sensor
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"

// library for wire
#include <Wire.h>

// library for battery monitoring LC709203
#include "Adafruit_LC709203F.h"

// set up the 'Lux' feed (The block setting need to change once test is passed)
// setup live also data can be stored
AdafruitIO_Feed *Lux1 = io.feed("lux-1");
AdafruitIO_Feed *Lux2 = io.feed("lux-2");
AdafruitIO_Feed *Lux3 = io.feed("lux-3");
AdafruitIO_Feed *Lux4 = io.feed("lux-4");
AdafruitIO_Feed *Lux5 = io.feed("lux-5");
AdafruitIO_Feed *batt = io.feed("batt");

// define sensor
Adafruit_TSL2591 tsl_1;  // TSL2591 #1
Adafruit_TSL2591 tsl_2;  // TSL2591 #2
Adafruit_TSL2591 tsl_3;  // TSL2591 #3
Adafruit_TSL2591 tsl_4;  // TSL2591 #4
Adafruit_TSL2591 tsl_5;  // TSL2591 #5

Adafruit_TSL2591 tsl[5] = {tsl_1, tsl_2, tsl_3, tsl_4, tsl_5};

// define battery monitoring
Adafruit_LC709203F lc;

// define address for multiplexer
#define TCAADDR 0x70

// lux value
float lux[5];

// check if adafruit io is reconnecting
bool IOreconnecting = false;

// Measuring Battery (analog pin)
#define VBATPIN A7

// Helper function for changing TCA output channel
/**************************************************************************/
/*
    Helper function for changing TCA output channel
*/
/**************************************************************************/
void tcaselect(uint8_t channel) {
  if (channel > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << channel);
  Wire.endTransmission();  
}

/**************************************************************************/
/*
    Initial setup for Wifi on ESP8266 and Adafruit IO connection
*/
/**************************************************************************/
void WifiAndIOSetup(void)
{
  Serial.print("Connecting to Adafruit IO");

  // connect to io.adafruit.com
  io.connect();

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

  // print out the Wifi and IO is successfully connected
  Serial.println();
  Serial.println(io.statusText());
}

/**************************************************************************/
/*
    Initial setup for TSL2591 sensor
*/
/**************************************************************************/
void SensorSetup(void)
{
  
  Serial.println(F("Connecting to sensor..."));
  // call tcaselect to set the channel, Before using any TSL2591.

  // TCA setting up channel and corresponding tsl sensor
  for (int i = 2; i < 7; i++)
  {
    // todo check multiplexer is correctly connected
    tcaselect(i);
    if (tsl[i-2].begin()) 
    {
      Serial.print(F("Found a TSL2591 sensor #")); Serial.println(i-1);
    } 
    else 
    {
      Serial.println(F("No sensor found ... check your wiring?"));
      while (1);
    }
  }
    
  /* Display some basic information on this sensor */
  displaySensorDetails();
  
  /* Configure the sensor */
  configureSensor();
}

/**************************************************************************/
/*
    Initial setup for LC709203 battery monitoring
*/
/**************************************************************************/
void BatteryMonitoringSetup(void)
{
    // For the Feather ESP32-S2, we need to enable I2C power first!
    // this section can be deleted for other boards
  #if defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
    // turn on the I2C power by setting pin to opposite of 'rest state'
    pinMode(PIN_I2C_POWER, INPUT);
    delay(1);
    bool polarity = digitalRead(PIN_I2C_POWER);
    pinMode(PIN_I2C_POWER, OUTPUT);
    digitalWrite(PIN_I2C_POWER, !polarity);
  #endif

  // use 6 for testing, 7 for formal
  tcaselect(7);
  if (!lc.begin()) {
    Serial.println(F("Couldnt find Adafruit LC709203F?\nMake sure a battery is plugged in!"));
    while (1) delay(10);
  }
  Serial.println(F("Found LC709203F"));
  Serial.print("Version: 0x"); Serial.println(lc.getICversion(), HEX);

  lc.setThermistorB(3950);
  Serial.print("Thermistor B = "); Serial.println(lc.getThermistorB());

  // tweak the calculation for the battery pack size
  // Pick the closest of the following values for battery size: 
  /*
  LC709203F_APA_100MAH,
  LC709203F_APA_200MAH,
  LC709203F_APA_500MAH,
  LC709203F_APA_1000MAH,
  LC709203F_APA_2000MAH,
  LC709203F_APA_3000MAH,
  */
  lc.setPackSize(LC709203F_APA_2000MAH);

  lc.setAlarmVoltage(3.8);

}

/**************************************************************************/
/*
    read the battery percentage and upload to the cloud
*/
/**************************************************************************/
void BatteryMonitoring(void)
{
  // use 6 for testing, 7 for formal
  tcaselect(7);
  Serial.print("Batt_Voltage:");
  Serial.print(lc.cellVoltage(), 3);
  Serial.print("\t");
  Serial.print("Batt_Percent:");
  Serial.print(lc.cellPercent(), 1);
  Serial.print("\t");
  Serial.print("Batt_Temp:");
  Serial.println(lc.getCellTemperature(), 1);

  // uplaod to the cloud
  batt->save(lc.cellPercent());


}

  /**************************************************************************/
/*
    Upload the sensor data from ESP8266 (need to be modified)
*/
/**************************************************************************/
void DataUpload(int i)
{
  Serial.println("Uploading to Adafruit IO");
  
  // switch to different feed according to the channel
  switch (i) {
    case 2:  
      Lux1->save(lux[0]);
      break;
    case 3:  // your hand is close to the sensor
      Lux2->save(lux[1]);
      break;
    case 4:  // your hand is a few inches from the sensor
      Lux3->save(lux[2]);
      break;
    case 5:  // your hand is nowhere near the sensor
      Lux4->save(lux[3]);
      break;
    case 6:
      Lux5->save(lux[4]);
      break;
    default:
      Serial.print("Sensor error");
  }

}

/**************************************************************************/
/*
    Performs a read using the Adafruit Unified Sensor API.
*/
/**************************************************************************/
void ReadLuxSensors(void)
{
   // TCA setting up channel and corresponding tsl sensor
  for (int i = 2; i < 7; i++)
  {
    // todo check multiplexer is correctly connected
    tcaselect(i);
    sensors_event_t event;
    tsl[i-2].getEvent(&event);
    /* Display the results (light is measured in lux) */
    if ((event.light == 0) |
        (event.light > 4294966000.0) | 
        (event.light <-4294966000.0))
    {
      /* If event.light = 0 lux the sensor is probably saturated */
      /* and no reliable data could be generated! */
      /* if event.light is +/- 4294967040 there was a float over/underflow */
      Serial.println(F("Invalid data (adjust gain or timing)"));
    }
    else
    {
      // store lux value
      lux[i-2] = event.light;
      Serial.print("Sensor channel: #"); Serial.print(i); Serial.print(" Lux: "); Serial.println(lux[i-2]);

      // upload data
      DataUpload(i);
    }
  }
}

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl[0].getSensor(&sensor);
  Serial.println(F("------------------------------------"));
  Serial.print  (F("Sensor:       ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:   ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:    ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:    ")); Serial.print(sensor.max_value); Serial.println(F(" lux"));
  Serial.print  (F("Min Value:    ")); Serial.print(sensor.min_value); Serial.println(F(" lux"));
  Serial.print  (F("Resolution:   ")); Serial.print(sensor.resolution, 4); Serial.println(F(" lux"));  
  Serial.println(F("------------------------------------"));
  Serial.println(F(""));
  delay(500);
}

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2591
*/
/**************************************************************************/
void configureSensor(void)
{
  for (int i = 2; i < 7; i++)
  {
    // todo check multiplexer is correctly connected
    tcaselect(i);
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
    //tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
    tsl[i-2].setGain(TSL2591_GAIN_MED);      // 25x gain
    //tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain
  
    // Changing the integration time gives you a longer time over which to sense light
    // longer timelines are slower, but are good in very low light situtations!
    //tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  // shortest integration time (bright light)
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
    tsl[i-2].setTiming(TSL2591_INTEGRATIONTIME_300MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  // longest integration time (dim light)
  }

  /* Display the gain and integration time for reference sake */  
  Serial.println(F("------------------------------------"));
  Serial.print  (F("Gain:         "));
  tsl2591Gain_t gain = tsl[0].getGain();
  switch(gain)
  {
    case TSL2591_GAIN_LOW:
      Serial.println(F("1x (Low)"));
      break;
    case TSL2591_GAIN_MED:
      Serial.println(F("25x (Medium)"));
      break;
    case TSL2591_GAIN_HIGH:
      Serial.println(F("428x (High)"));
      break;
    case TSL2591_GAIN_MAX:
      Serial.println(F("9876x (Max)"));
      break;
  }
  Serial.print  (F("Timing:       "));
  Serial.print((tsl[0].getTiming() + 1) * 100, DEC); 
  Serial.println(F(" ms"));
  Serial.println(F("------------------------------------"));
  Serial.println(F(""));
}

void setup() {
  // start the serial connection
  Serial.begin(9600);

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

  // NOTE!!! VERY IMPORTANT!!!
  // Must call this once manually before first call to tcaselect()
  Wire.begin();

  // WiFi and Adafruit setup
  WifiAndIOSetup();

  // Sensor setup
  SensorSetup();

  // Battery monitoring setup
  BatteryMonitoringSetup();

}

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();

  // Reading sensor data
  // ReadLuxSensors();

  // Battery monitoring
  BatteryMonitoring();

  // Adafruit IO is rate limited for publishing, so a delay is required in
  // between feed->save events. In this example, we will wait three seconds
  // (1000 milliseconds == 1 second) during each loop.
  // delay(10000);
  delay(5000);
}

And I got another file call config.h:
/************************ Adafruit IO Config *******************************/

// visit io.adafruit.com if you need to create an account,
// or if you need your Adafruit IO key.
#define IO_USERNAME ""
#define IO_KEY ""

/******************************* WIFI **************************************/

// the AdafruitIO_WiFi client will work with the following boards:
//   - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
//   - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
//   - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
//   - Feather M0 WiFi -> https://www.adafruit.com/products/3010
//   - Feather WICED -> https://www.adafruit.com/products/3056
//   - Adafruit PyPortal -> https://www.adafruit.com/product/4116
//   - Adafruit Metro M4 Express AirLift Lite ->
//   https://www.adafruit.com/product/4000
//   - Adafruit AirLift Breakout -> https://www.adafruit.com/product/4201
//   - Adafruit AirLift Shield -> https://www.adafruit.com/product/4285
//   - Adafruit AirLift FeatherWing -> https://www.adafruit.com/product/4264

// #define WIFI_SSID "Starbucks Free WiFi"
// #define WIFI_PASS "@starbucks-wifi"

#define WIFI_SSID ""
#define WIFI_PASS ""

#include "AdafruitIO_WiFi.h"

#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) ||         \
    defined(ADAFRUIT_PYPORTAL)
// Configure the pins used for the ESP32 connection
#if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
// Don't change the names of these #define's! they match the variant ones
#define SPIWIFI SPI
#define SPIWIFI_SS 10 // Chip select pin
#define NINA_ACK 9    // a.k.a BUSY or READY pin
#define NINA_RESETN 6 // Reset pin
#define NINA_GPIO0 -1 // Not connected
#endif
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS,
                   NINA_ACK, NINA_RESETN, NINA_GPIO0, &SPIWIFI);
#else
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
#endif

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”