Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By Vidor
#95673 Hello, sorry for my bad English but I use a translator.

I am not a programmer, only a meteorology fan and I have built a small station to know temperature, humidity and pressure following this tutorial and it works perfectly although sometimes I have to press the Reset button several times to start, I think it is a board problem. I want to add solar panels and a battery but to save energy I want to add the option of Deep Sleep.

The problem is that it takes too long to connect to the wifi and does not give it time to send the data to Thingspeak so it goes back into Deep Sleep.

Could someone give me any advice or tell me that I am doing wrong?.

In case it served something, the board is this, CH340G

Thanks

Code: Select all#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include "ThingSpeak.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>


const char* ssid = "ssid";   
const char* password = "pass";   

WiFiClient  client;

unsigned long Channel_ID = 1;
const char * API_Key = "xxxxxxxx";

unsigned long last_time = 0;
unsigned long Delay = 30000;

// Variables to store sensor readings
float temperature;
float humidity;
float pressure;


// Create a sensor object
Adafruit_BME280 bme;

void initBME(){
  if (!bme.begin(0x76)) {
    Serial.println("BME280 not properly connected!");
    while (1);
  }
}

void setup() {
  Serial.begin(9600); 
  initBME();
   
  WiFi.mode(WIFI_STA);   
 
  ThingSpeak.begin(client);

  // Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
  Serial.println("I'm awake, but I'm going into deep sleep mode for 30 seconds");
  ESP.deepSleep(30e6);

}

void loop() {
  if ((millis() - last_time) > Delay) {
   
    // Connect or reconnect to WiFi
    if(WiFi.status() != WL_CONNECTED){
      Serial.print("Connecting...");
      while(WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, password);
        delay(5000);     
      }
      Serial.println("\nConnected.");
    }

    // Obtaining a new sensor reading for all fields
    temperature = bme.readTemperature();
    Serial.print("Temperature (ºC): ");
    Serial.println(temperature);
    humidity = bme.readHumidity();
    Serial.print("Humidity (%): ");
    Serial.println(humidity);
    pressure = bme.readPressure() / 100.0F;
    Serial.print("Pressure (hPa): ");
    Serial.println(pressure);
   
    ThingSpeak.setField(1, temperature);
    ThingSpeak.setField(2, pressure);
    ThingSpeak.setField(3, humidity);
   
    int Data = ThingSpeak.writeFields(Channel_ID, API_Key);

    if(Data == 200){
      Serial.println("Channel updated successfully!");
    }
    else{
      Serial.println("Problem updating channel. HTTP error code " + String(Data));
    }
    last_time = millis();
  }
}
User avatar
By rooppoorali
#95795 Hi, I can see that you have called the ESP.deepSleep function in the setup function. As far as I can understand, it should be called inside the loop function. After sending your desired data to firebase.
User avatar
By Vidor
#95837
rooppoorali wrote:Hi, I can see that you have called the ESP.deepSleep function in the setup function. As far as I can understand, it should be called inside the loop function. After sending your desired data to firebase.


Thank you very much, I will try it.