Battery powered thermometer

My idea is to build few thermometers powered by battery to check temperature's rooms in my house. Theses thermometers will report temperature regularly to my server and I will display datas in a graphic web page.

I wanted :

  • to get temperatures every half hour (or more if i can)
  • battery life is at least 6 months (or more if i can)
  • to test esp8266 components


For my tests, I bought :

  • 18650 battery 4200 mAh 
  • Esp-01
  • DHT22 module for Esp-01

First idea

My first idea was to use Esp-01 because it is very small, so i bought a DHT22 module.And just plug Esp-01 on DHT22 module and power it and it is finish !

ESP-01

DHT22 Temperature & humidity sensor for esp01


And I upload this sketch in the esp01

Code formatting with http://hilite.me/
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>


#define TIME_PERIOD 30*60*1000

#ifndef STASSID
#define STASSID "myWifi";
#define STAPSK  "myPassword";
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

WiFiClient client;
HTTPClient http;


void WifiConnect(void) {
  digitalWrite(LED_BUILTIN, LOW);  
 
  Serial.println("WifiConnect()");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);   
    delay(250);
    Serial.print(".");
    digitalWrite(LED_BUILTIN, LOW);  
    delay(250);    
  }
    
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());

  digitalWrite(LED_BUILTIN, HIGH);   
  
}

void setup(void) {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("setUp()");

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  dht.begin();
  Serial.println("dht started");
    
}

void loop(void) {
  
  // Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
  static int last = 0;
  if ((millis() - last) > TIME_PERIOD) {
    if (WiFi.status() != WL_CONNECTED) WifiConnect();

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    saveTemp(String(WiFi.macAddress()),h,t);
    
    last=millis();
    WiFi.disconnect();
  }
}

String saveTemp(String capteur, float h, float t) {

  String url = "http://myserver/domotique/saveTemp.php?capteur="+urlencode(capteur)+"&temperature="+String(t)+"&humidity="+String(h);
  Serial.println(url);

  String payload = "error";
  if (http.begin(client, url)) {  // HTTP
      Serial.print("[HTTP] GET...");
      Serial.println(url);
      // start connection and send HTTP header
      int httpCode = http.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          payload = http.getString();
          //Serial.println(payload);          
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
   } else {
      Serial.printf("[HTTP} Unable to connect\n");
   }
  
  return payload;
}


String urlencode(String str)
{
    String encodedString="";
    char c;
    char code0;
    char code1;
    char code2;
    for (int i =0; i < str.length(); i++){
      c=str.charAt(i);
      if (c == ' '){
        encodedString+= '+';
      } else if (isalnum(c)){
        encodedString+=c;
      } else{
        code1=(c & 0xf)+'0';
        if ((c & 0xf) >9){
            code1=(c & 0xf) - 10 + 'A';
        }
        c=(c>>4)&0xf;
        code0=c+'0';
        if (c > 9){
            code0=c - 10 + 'A';
        }
        code2='\0';
        encodedString+='%';
        encodedString+=code0;
        encodedString+=code1;
        //encodedString+=code2;
      }
      yield();
    }
    return encodedString;
    
}

This sketch will upload every 30 minutes temperature and humidity to my server



Works fine but...

But...

First problem, As you see on the left of the graphic, temperature was +5°C higher than expected !
I thought my DHT22 module was faulty and then I realized ESP-01 was too close from DHT22 !!!
And the ESP-01 heat is distording DHT22 temperature
But it is sold to do it !!!!
So I connect wires between ESP-01 and DHT22 and miracle now temperature and humidty are ok


Second problem, the battery was empty after 15 hours !
So i check current consumption. It consummes ~90 mAh. So after 15 hours, it consumes 1350 mAh
I think I have a battery problem because i bought a 4200 mA not 1350 mA !
This is my first problem

Third problem, as it is done, it is consumming 90 mAh which is too high for any battery. Even if i bought a bigger battery, lifetime will still be in days and not in months


My second idea 

So my second idea was to put esp01 in sleep mode for 30 minutes and wakeup only to read and upload temperature to my sever and then to go back in sleep mode
During sleep mode, I think current consumption should be 20 uAh. So I will get a lifetime 4500x better. Perhaps more than 6 months. I have to try it

After few tests months :

I am using Wemos d1 mini. I change DHT22 to SI7021. It reads temperature every 30 minutes and then goes in deep sleep mode. Just connect battery to 5v pin. It is running for 65 days

With NodeMCU, it is running only for 22 days.



Commentaires

Posts les plus consultés de ce blog

Attiny13A sleep mode

Sensor Si7021 - Humidity and Temperature