ESP8266 - 12 Logger

by Michal Choma in Circuits > Electronics

6269 Views, 36 Favorites, 0 Comments

ESP8266 - 12 Logger

intro.png
IMG_20170825_170244.jpg
IMG_20170825_170155.jpg
IMG_20170829_120450.jpg
IMG_20170826_165236.jpg
IMG_20170825_170314.jpg
IMG_20170825_170552.jpg
IMG_20170825_173834.jpg
IMG_20170825_165718.jpg

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

Parts

Here is material to buy (affiliate links to support me, if you want):

1x ESP8266 12 ,

Link: http://s.click.aliexpress.com/e/bMIJcPuy

http://s.click.aliexpress.com/e/KsioHhe

http://s.click.aliexpress.com/e/b3MsaQ5K

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

1xAdapter for ESP8266 12 (optional)

Link: http://s.click.aliexpress.com/e/c7PcPiRW

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

http://s.click.aliexpress.com/e/c3xsN4Rm

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

http://s.click.aliexpress.com/e/9Pn4cRq

1x Li-Ion charger (optional)

http://s.click.aliexpress.com/e/bztMAXPE

http://s.click.aliexpress.com/e/cL1G9tgM (to to solder wire to battery holder and usb cable)

2 x Ceramic Capacitor 100 nF

http://s.click.aliexpress.com/e/bFvGcnB6

1x Electrolytic Capacitor47microF (can use 470microFarad)

http://s.click.aliexpress.com/e/bjANVqU0

** capacitor packs

http://s.click.aliexpress.com/e/bU4HOM9S

http://s.click.aliexpress.com/e/bcwvHbiC

1x CP2102 USB to UART Serial module

http://s.click.aliexpress.com/e/btKG0HlO

1x DS18B20 1-Wire Temperature Sensor

http://s.click.aliexpress.com/e/bhmyP8ha

free shipping:

http://s.click.aliexpress.com/e/RsJdxHm

2x Tactile Pushbutton

http://s.click.aliexpress.com/e/DEGyCsC

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

http://s.click.aliexpress.com/e/bniqKsbA (long)

http://s.click.aliexpress.com/e/ceenN8qk (short)

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

http://s.click.aliexpress.com/e/znwH120

Resistors:

3x 10kΩ

2x 4.7kΩ

1x 2.2kΩ

1x 300kΩ

1x 100kΩ

Packs:

http://s.click.aliexpress.com/e/chhARZrK

http://s.click.aliexpress.com/e/bzLcEtPS

http://s.click.aliexpress.com/e/ojCdHao

Circuit

logger_2.2_chip.png
logger_2.2_bb.png

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!

Code

board_manager.png

*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 2OneWire oneWire(ONE_WIRE_BUS);DallasTemperature sensors(&oneWire);WiFiClient client;const char* ssid = "blablabla"; // here write your wificonst 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)// variablesfloat temp;  // temperature//float temp2; // for second thermometer, if you dont use second thermometer, just leave it uncommentedint volts;int raw; // for values from ADC = analog digital converter = "analog pin" like on Arduino A0// set adress for 1-Wire communicationDeviceAddress 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 resonatorvoid 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 reflasWiFi.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}

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

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.