I'm a little perplexed. I have a weather station I've built. The code runs great on USB plug in. Even runs well when plugged into LIPO, but here's the thing: On USB the loop runs the way it's supposed to reconnect every 50 seconds. On LIPO, it runs once and never runs again (despite the fact that the board and all connected devices remain powered.)
The code is below. Any thoughts?
#include <Wire.h> //Import the required libraries
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <SPI.h>
#include <WiFi101.h>
#include <ThingSpeak.h>
#define windSen 0
//#define uS_TO_S_FACTOR 1000000ULL //Conversion factor for micro seconds to seconds NOT USED
//#define TIME_TO_SLEEP 20 //Time ESP32 will go to sleep (in seconds) 9.5 minutes / 590 seconds NOT USED
#define BME_CS 13
#define BME_MOSI 12
#define BME_MISO 11
#define BME_SCK 10
#define lightSen 5
int status = WL_IDLE_STATUS;
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
WiFiClient client; //WiFi connection details
char ssid[] = "###MASKED FOR PRIVACY####"; //WiFi Name
char pass[] = "###MASKED FOR PRIVACY####"; //WiFi Password
unsigned long myChannelNumber = "###MASKED FOR PRIVACY####"; //Thingspeak channel number
const char * myWriteAPIKey = "###MASKED FOR PRIVACY####"; //Thingspeak API write key
int light = 0; //Variables to store sensor readings
int temp = 0;
int humid = 0;
int pressure = 0;
int wind = 0;
int windMap[2][32] = {{2000,1400,1000,600,500,400,320,280,240,220,200,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,38,36,34,32,30,28},{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,23,25,29,34,41,51,54,57,60,64,68,73}};
unsigned long startTime = 0;
unsigned long endTime = 0;
int counter = 0; //Counter to keep track of the number of wind speed revolutions
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
void setup() //Setup function - only function that is run in deep sleep mode
{
}
void loop() //Loop function
{
Serial.begin(9600);
pinMode(windSen, INPUT_PULLUP);
pinMode(lightSen, INPUT); //Define pin functions
counter = 0; //Reset wind speed counter
unsigned status;
delay(200); //Allow BME sensor to startup
status = bme.begin(); //Connect to BME sensor
if (!status)
{
Serial.println("Could not find a valid BME280 sensor");
}
int lastState = 0; //Variable to store the last state of wind sensor input
for (int i=0; i<= 6000; i++)
{
if (digitalRead(windSen) == HIGH && lastState == 0)
{
counter++;
lastState = 1;
}
else if (digitalRead(windSen) == LOW && lastState == 1)
{
lastState = 0;
}
delay(1);
}
recTempHumid (); //Take readings from BME sensor
recPress ();
recLight ();
calcWind ();
Serial.print("Temp: ");
Serial.println(temp);
Serial.print("Humidity: ");
Serial.println(humid);
Serial.print("Pressure: ");
Serial.println(pressure);
Serial.print("Wind: ");
Serial.println(wind);
Serial.print("Light: ");
Serial.println(light);
WiFi.begin(ssid, pass); //Connect to WiFi network
int timeout = 0;
while (WiFi.status() != WL_CONNECTED && timeout < 20)
{
delay(500);
Serial.print(".");
timeout++;
}
if(WiFi.status() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFi connected");
ThingSpeak.begin(client); //Initialise ThingSpeak
updateThingSpeak(); //Post the data to Thingspeak
}
else
{
Serial.println("");
Serial.println("WiFi connection failed");
}
Serial.flush();
Serial.println("Waiting until next check");
delay(50000);
}
void recTempHumid() //Function to record the temperature and humidity
{
temp = bme.readTemperature();
temp = temp * 9 / 5 + 32; // Convert C to F
humid = bme.readHumidity();
}
void recLight () //Function to record the light level
{
light = analogRead(lightSen);
}
void recPress() //Function to record the pressure
{
int presstemp = bme.readPressure();
pressure = presstemp / 3386.39; // Convert hPa to in/Hg
}
void calcWind() //Function to calculate the wind speed
{
int ave = 2000;
if(counter > 1) //Check that at least two ticks have been registered, one is meaningless
{
ave = 6000/(counter-1); //Calculate the average time between ticks
}
Serial.print("Average Tick Time: ");
Serial.println(ave);
if (ave < 28) //Discard readings faster than 102kph
ave = 28;
int index = 0;
while (ave < windMap[0][index]) //Search through the windMap array for the corressponding wind value
index++;
wind = windMap[1][index];
int mphconv;
mphconv = ((wind * 10000L + 5)/ 16090); //Convert the wind speed to mph
wind = mphconv;
Serial.print("Wind Speed: ");
Serial.println(wind);
}
void updateThingSpeak() //Function to post data to Thingspeak
{
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, humid);
ThingSpeak.setField(3, pressure);
ThingSpeak.setField(4, wind);
ThingSpeak.setField(5, light);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}