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