Weather Station: ESP8266 Deepsleep, SQL and Graphing

Light, Temperature and Humidity Sensors

When I planned to build a weather station, I was dreaming to have a full-fledged weather station that has wind speed, rain measurement, full-spectrum solar sensor, but turned out, that would not be cheap, and the purchase cost could end up at least $100. I gave up the full options and started to build one with $10, more or less. $10 is the cost of basic components of the weather station as the parts below.

Here are the parts:

  1. ESP8266 Wemos brand costs $2.39 pcs on Aliexpress . I would recommend Wemos brand because its EPS8266 is easier to program, update, and have 4MB flash or more.
  2. Wemos Charger-Boost Shield costs $1.39 pcs. This is another benefit to use this brand. It has a boost-up board for Lithium battery (nominal voltage = 3.7V) to a 5V for ESP8266. The board also come with charging option with a max charging current = 1A.
  3. *Note: There is a cheaper option for Lithium battery charging/boost up. This one costs $1.77 for 5pcs. However, when I used this board for ESP8266 (either Wemos's or a bare ESP8266), the deep-sleep mode of ESP8266 triggered a reset right after making the ESP8266 in a loop of sleep-reset-sleep, which is very annoying. If you know what was happening, please inbox me.

  4. Wemos also has several shields for temperature and humidity but I am going to build from individual components. Photoresistor (or light-dependent resistor -- ldr, cheap), a luminosity sensor such as BH1780 or TSL2561 (about 0.87-0.89c pcs), a temperature sensor such as DS18B20 (75c each), and a humidity and temperature combo such as DHT22 ($2.35 here) or SHT21 ($2.20 here). A total cost for the sensor ~$4.
  5. Lithium battery. I salvaged one from a 7.4V Canon Battery which is two 3.7V battery in series or 18650 Lithium battery . Each 18650 costs about $5 apiece. I have a picture show the tear-down the camera battery pack. Be careful though, short-circuiting happens when cutting through the plastic cover could generate extreme heat and burn.
  6. Salvaging Battery

  7. PCB board, jumper, wire, soldering, your time, maybe some debugging skills.

Let wire components together follows the schematic above.

Weather Station Schematics
Weather Station Schematics

Then, have a look for the task in the setup loop. It is simply a one-run of tasks and ends by a sleep command.

A final version
Weather Station

Now, time to upload the code using Arduino IDE to the ESP8266. This one below is the setup() loop

void setup() {
    Serial.begin(115200);
    Serial.println("Starting Node named " + String(SENSORNAME));
    setup_wifi();
    delay(100);
    Wire.begin();
    pinMode(ldrPin, INPUT);
    SHT21.begin();
    if(!tsl.begin()) {
        Serial.print("TSL2561 not found");
        while(1);
    }
    delay(100);
    
    ldr = analogRead(ldrPin);
    
    tsl.enableAutoRange(true);  
    tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);  
    delay(100);
    sensors_event_t event;
    tsl.getEvent(&event);
    if (event.light) lux = event.light;
    else Serial.println("Sensor overload");
    h = SHT21.getHumidity();
    t = SHT21.getTemperature();
    tempSensor.setWaitForConversion(false); 
    tempSensor.begin();                     
    delay(100);
    if (tempSensor.getDeviceCount() == 0) {
        Serial.printf("DS18x20 not found on pin %d\n", ds18b20);
        Serial.flush();
        delay(1000);
    }
    delay(100);
    tempSensor.requestTemperatures();
    t18 = tempSensor.getTempCByIndex(0);
    
    Serial.printf("\nLight: %d lux\t", lux);
    Serial.printf("LDR: %d /1024\t", ldr);
    Serial.printf("T: %0.2f *C\t", t);
    Serial.printf("H:%0.2f \t", h);
    Serial.printf("HIC: %0.2f \t", hic);
    delay(100);
    client.setServer(mqtt_server, mqtt_port);
    client.setCallback(callback);
    reconnect();
    delay(100);
    
    ESP.deepSleep(3e8); // 300 millions micro seconds, 300 seconds, 5 minutes;
    }

The ESP8266 executes the setup and measurement in the setup() block and turns to deepsleep mode at the end of setup() block. The main loop() is not needed in this setup. After a defined internal (5 minutes) in this case.

During debugging or setting up, command out the ESP.deepsleep() to have Serial readout continually. As always, the full code to upload to ESP8266 is hosted here (GitHub).

Remember to put on the jumper between RST and D0/GPIO16 for triggering a wake up after a period of deep-sleep.

  • Additional Photo
  • Here is my first version of a weather station, which I used the cheaper option of boost-up and charging board. This one results in restart of ESP8266 right after the ESP8266 moves to the deepsleep mode. And so I had a boot-reset loop on this version.

    deepsleep