Introduction: ESP8266 - 12 Logger

Update**

- here is group for IoT weather stations projects https://www.facebook.com/groups/1829154687394948/

Update*

- dont use GPIO 2 as signal pin for themometer DS18B20. Instead of that, use e.g. GPIO 5. Change also in code at beginning from 2 to 5.

After some looking about IoT on internet, I decide to make logger based on ESP8266 - 12. I decided for ESP8266 12 instead of ESP8266 01, because has more pins, and you need connect reset pin on GPIO 16 (on ESP8266 01 there is no GPIO 16, or must be good in soldering).

My logger should be project for weather station which is power on one Li-Ion battery. Because of this reason, I use sleep mode for every 15 minutes. Current during sleep is 0.37 mA, esp eat only 0.06 mA, but converter eat a lot.

I use adapter, which has inbuild 2 x 10kOhm resistors. VCC pin is connected with CH_PC via 103 resistor (10E4 ohms = 10 kOhms) and GPIO 15 is connected with GND with 103 resistor too. I dont use regulator on board

Step 1: Parts

Here is material to buy:

1x ESP8266 12 , I buy this, but you can buy esp8266 12 S, which can be bettter

1x power supply (optional) source + 1x power adapter (optional) adatper for source from 220V, check country plug

1xAdapter for ESP8266 12 (optional) adapter

1x 3.3V DC-DC Converter Step Down Buck Volt Regulator (marked asDD0503MA, the best according me DD0503MA) or Pololulu converters or Step up/ step down converter here, not such good

1x battery holder

1xLi-Ion battery (I have from old notebook battery pack)

1x Li-Ion charger (optional)

2 x Ceramic Capacitor 100 nF

1x Electrolytic Capacitor47microF (can use 470microFarad)

1x CP2102 USB to UART Serial moduleusb/ttl

1x DS18B20 1-Wire Temperature Sensor

2x Tactile Pushbutton

2x breadboards long or 1xlong breadboard + 1x short breadboard or 1x pcb (which is better, low capacitance)

1x pack of dupont cables (not such good, better buy thicker one, for good connection)

Resistors:

3x 10kΩ

2x 4.7kΩ

1x 2.2kΩ

1x 300kΩ

1x 100kΩ

Step 2: Circuit

Here is circuit, how connect all parts.

Note: on picture is voltage regulator MPC 1700, - just replace it with buck converter, described in part list.

You can replace battery with power source with adapter. On picture, there is not described pins on esp8266 12, esp8266 12 can have different pin schematic, so I add picture pins described on ESP chip.

Wiring of esp is a little complicated. Also try to use 470 microFarad electrolyt capacitor,instead of 47 microFarad, because of stability.

Esp chip need enough power (at least 300 mA, but sometimes higher, use better 500 mA) and capacitors!

Step 3: Code

*Update:

change in code this line:

instead of using #define ONE_WIRE_BUS 2 use #define ONE_WIRE_BUS 5

and connect signal pin from themometer to GPIO 5. You can use any GPIO, which is free.

For upload code, you need json line in your arduine ide + 1.6.7 version (I have 1.6.10). Choose board Generic ESP8266 module. Also you must install in board manager esp8266 boards, i Use 2.3.0 version. Check image above. When you upload code, you must set esp to flash mode - first press and keep button named flash, when you see that Arduino IDE compile it, and start upload, press and release reset button. In normal mode (release flash button) you can check how esp work on serial monitor etc.

This is code for ESP8266 12 logger. It measure values from ADC (just analog pin) and temperature from DS18b20. It use WiFiConfig library, because when you always connect to wifi, it harm chip. Library wifi config solve this problem. You can use more then 2 themometers DS18b20, but you must find adress of them. Check my previos instructables - Mpemba effect, where is code for find adress. Data are send to thingspeak. sleeping current using deepsleep function is 0.06 mA (esp), 0.36 mA (esp + voltage step up / down converter) when is ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000) is used, esp is automatically restating , so it begins from setup again

<p>#include<OneWire.h></p><p>#include <DallasTemperature.h> // library for DS18b20</p>#include "WiFiConfig.h"// library which solve problem with connecting to wifi
#define ONE_WIRE_BUS 2  // define pin, where you connect signal cable from DS18b20, I use GPIO 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


WiFiClient client;
const char* ssid = "blablabla"; // here write your wifi
const char* password = "blablabla";  // here password from your wifi, if you dont use any password, just write only this: const char* password = ""; 
const char* server = "api.thingspeak.com";
const char* api_key = "blablabla";  // here you write your thinspeak API KEY (write API KEY)

// variables
float temp;  // temperature
//float temp2; // for second thermometer, if you dont use second thermometer, just leave it uncommented
int volts;
int raw; // for values from ADC = analog digital converter = "analog pin" like on Arduino A0

// set adress for 1-Wire communication
DeviceAddress Probe01 = { 0x28, 0xFF, 0x9B, 0xB8, 0x70, 0x16, 0x04, 0xCA }; // here you must write adress of first termal sensor, comment when you use one thermometer
//DeviceAddress Probe02 = { 0x28, 0x43, 0xEB, 0x26, 0x00, 0x00, 0x80, 0x6D }; // here you must write adress of second termal sensor

#define ALLOWED_CONNECT_CYCLES 40     // how many esp try to connect wifi, if no wifi, it will sleep after 40 cycles
#define ALLOWED_READ_CYCLES 80        // how long will esp wait for measure temperature, mostly nothing
#define SLEEP_TIME_SECONDS 900      //900 for 15 minutes, but sometimes it depend on internal resonator

void setup()
{
  Serial.begin(115200);   //find your esp working frequency, it can be 9600, my ist 115200
  delay(10);
  WiFi.disconnect();    // ADC values fluctuate, they are crazy, problem can be in wifi connection, so try to disconnect it
  //WiFi.mode(WIFI_OFF); //uncomment if you have problems with ADC
  //WiFi.forceSleepBegin();  // uncomment if you have problems with ADC
  delay(10);
  raw = analogRead(A0);     // read analog value from ADC
  volts = map(raw, 0, 1023, 0, 1000) * 6; // just for better reading on graph, but it is not true value, you must calibrate it

// ESP.eraseConfig(); // If after changing WiFi settings (SSID or password) ESP8266  does not want to connect again, uncomment this line and reflas

WiFi.begin(ssid, password);

 ////////////////// connecting to wifi/////////////////////
  
  Serial.println("Connecting to Wifi ");
  uint8_t counter = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    counter++;
    if (counter > ALLOWED_CONNECT_CYCLES) {
      Serial.print("sleep");
      ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000); //sleeping
    }
  }

Serial.println("WiFi connected");

 //////////////// measure ////////////////////////
    if (client.connect(server, 80))
    {
      digitalWrite(0, HIGH);
  
      counter = 0;
      do 
      {
        sensors.begin();
  
        sensors.setResolution(Probe01, 12);
        //sensors.setResolution(Probe02, 12);
        sensors.requestTemperatures();
        temp = sensors.getTempC(Probe01); // if dont know adress, temp = sensors.getTempCByIndex(0);
        // temp2 = sensors.getTempC(Probe02);
        counter++;
        delay(10);
        
        if (counter > ALLOWED_READ_CYCLES) 
          {
            Serial.print("sleep");
            ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000);
          }
      } while (temperature == 85.0 || temperature == (-127.0));
    
    /////////////// sending data to thinspeak //////////////////
   
    String dataToThingSpeak = "";
    dataToThingSpeak += "GET /update?api_key=";
    dataToThingSpeak += api_key;

    dataToThingSpeak += "&field1=";
    dataToThingSpeak += temp;         // writing to field 1 temperature from first sensor
    dataToThingSpeak += "&field2=";
    dataToThingSpeak += raw;          // writing to field 2 temperature from second sensor  	dataToThingSpeak += "&field3=";
    dataToThingSpeak += volts;       // write to field 3 difference of both sensors  //dataToThingSpeak+="&field4=";
    //dataToThingSpeak+=volts;         // writing to field 4 temperature from second sensor
    dataToThingSpeak += " HTTP/1.1\r\nHost: a.c.d\r\nConnection: close\r\n\r\n";
    dataToThingSpeak += "";
    client.print(dataToThingSpeak);   

    Serial.println(temp);
    Serial.println(raw);
    Serial.println(volts);
    //Serial.println(temp2);
    }

 ///////////////// sleeping //////////////////////
  
  delay(100);
  Serial.print("sleep");
  ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000)

}

void loop()
{
  // no loop
}

Step 4: Stability Problem

When I power and try esp8266 12, it sometimes colaps at this line:

Serial.println("Connecting to Wifi ");

And it stop and first dot. Often it load a lot of weird lines.

Sometimes I set esp to flash mode - reset with reset button - switch to normal mode - reset. I think, the problem is in breadboard! Breadboard has high capacitance, and is not good for circuits with frequency higher then 1MHz. And wifi is around 2.4 GHz! Also contacts are not such good. If you use dupont cables, they are thin. So if you want, you can use a little thicker cables. I will try to use PCB with this circuit.

Here is my video what happens.

https://youtu.be/Uq046MqS4As

Step 5: ESP8266 Projects, Which Help Me...

How to connect and power ESP8266 - 12 + wificonfig library.

http://www.xpablo.cz/?p=996

capacitance of breadboard? here is eev video

https://www.youtube.com/watch?v=6GIscUsnlM0

introduction to esp

https://www.instructables.com/id/Getting-Started-with-the-ESP8266-ESP-12/

wiring for esp with buttons

https://www.instructables.com/id/ESP-12F-ESP8266-Module-Minimal-Breadboard-for-Flas/

I post here libraries in zip file, pls twice open.

Comments

author
tytower (author)2017-08-29

Quote "

When I power and try esp8266 12, it sometimes colaps at this line:

Serial.println("Connecting to Wifi ");

And it stop and first dot. Often it load a lot of weird lines."

Serial.println is a big program in itself so its drawing lots of memory . But you are not driving a display so you can just omit it in the final program Yeh?

author
Michal Choma (author)tytower2017-08-31

i post here video, how error lines looks like.

author
Michal Choma (author)tytower2017-08-29

I dont think that problem is in serial commands. Esp itself has a lot of memory.

About This Instructable

1,329views

26favorites

License:

Bio: I study Teaching physics and geography, I enjoy with laws of nature :D Be happy and fun with science.
More by Michal Choma:Measure Speed of Sound With Audacity!Charging Lithium - Ion Battery With Solar CellESP8266 - 12 Logger
Add instructable to: