ESP8266/NodeMCU Deep Sleep

dsleep() function accepts microseconds as its parameter (1,000,000 microseconds = 1 second), and the multiplication performed inline here is just to make it clearer that we’re sleeping for 15 minutes total. With the current 1.5.1 SDK, the maximum value accepted is 4,294,967,295, which is about 71 minutes. Earlier SDK versions had a lower limit, closer to 35 minutes. One common way to extend this is to maintain a counter in NVM (i.e., Flash or RTC Memory), and increment the counter each time that the device wakes up. If it reaches a certain threshold, then do the work, otherwise go back to sleep.

node.dsleep(15 * 60 * 1000000)

In this case, you don’t need to connect GPIO16 to the RESET pin, and you don’t need to specify an alarm value when calling dsleep(). Or, rather, use a value of zero to specify that the ESP should sleep forever (until reset, that is):

node.dsleep(0)
1
node.dsleep(0)
This permits very long idle periods (hours, days, or weeks possible) so long as you have a reliable method of waking the device up by triggering the RESET pin.

ที่มาhttps://blog.falafel.com/esp8266nodemcu-deep-sleep/

Deep Sleep for the ESP8266 ESP-01

Because the ESP-01 does not have the necessary pin exposed for Deep Sleep (GPIO 16) we will need to do some surgical soldering on it.
We need to solder a very thin piece of wire from the reset pin to one of those very small pads on the esp chip.

Don’t worry, it’s a lot easier than it looks.

Get the piece of wire, solder it on the reset pin, tin the other end and bend it into position
What I did next is applying head somewhere near the end of the wire, this melted the solder on the wire and on the pad and I got a connection. Easy peasy.

You will also need to remove at least the red LED to lower consumption.

I have also added some hot glue on top of the wire, it’s quite easy to pull it away together with the esp’s pad otherwise.

ในขั้นตอน จะมีฟังก์ชั่นการทำงานดังนี้

– try to connect to wifi, if fail start in config mode
– read dht22 data
– post dht22 data to cloud

– go to sleep
– awake and repeat

 

ที่มาhttps://tzapu.com/minimalist-battery-powered-esp8266-wifi-temperature-logger/

 

วัดอุณหภูมิ ความชื้น ขึ้น LINE NOTIFY

void Line_Notify(String message);

#include <ESP8266WiFi.h>
#include <DHT.h>

#define DHTPIN D2

// Existing WiFi network
const char* ssid = “xxxxx”; // your wifi name
const char* password = “xxxxx”; // your wifi password

#define LINE_TOKEN “xxxxxxxxxxxxxxx” // LINE Notify token

#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println(“Weather station by kittipongint!”);

dht.begin();

// Connect to your WiFi network
WiFi.begin(ssid, password);
Serial.print(“Connecting”);

// Wait for successful connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to: “);
Serial.println(ssid);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
Serial.println(“”);
}
void read_sensor() {
delay(600000);

// 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);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(“Failed to read from DHT sensor!”);
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.print(” *C “);
Serial.print(f);
Serial.print(” *F\t”);
Serial.print(“Heat index: “);
Serial.print(hic);
Serial.print(” *C “);
Serial.print(hif);
Serial.println(” *F”);

String message = “%E0%B8%AD%E0%B8%B8%E0%B8%93%E0%B8%AB%E0%B8%A0%E0%B8%B9%E0%B8%A1%E0%B8%B4%20”;
// Send notificatin with LINE Notify
Line_Notify(message + t + “%C2%B0C”);
}

void loop() {
read_sensor();
}
void Line_Notify(String message) {
WiFiClientSecure client;

if (!client.connect(“notify-api.line.me”, 443)) {
Serial.println(“connection failed”);
return;
}

String req = “”;
req += “POST /api/notify HTTP/1.1\r\n”;
req += “Host: notify-api.line.me\r\n”;
req += “Authorization: Bearer ” + String(LINE_TOKEN) + “\r\n”;
req += “Cache-Control: no-cache\r\n”;
req += “User-Agent: ESP8266\r\n”;
req += “Content-Type: application/x-www-form-urlencoded\r\n”;
req += “Content-Length: ” + String(String(“message=” + message).length()) + “\r\n”;
req += “\r\n”;
req += “message=” + message;

client.print(req);

delay(30);

// Serial.println(“—–“);
while(client.connected()) {
String line = client.readStringUntil(‘\n’);
if (line == “\r”) {
break;
}
//Serial.println(line);
}
// Serial.println(“—–“);
}

 

ที่มา:https://kittipongint.com/weather-station-wemos-d1-mini/

Arduino Uno R3 + DHT22 + LCD 16×2

เพิ่งหัดเล่น ทดลองเล่น Arduino ดู  ฝึกสมอง เจอเวปให้ลอง เล่นไปเรื่อยๆ  เจอเวปลอง วัด อุณหภูมิ ความชื้น ออกจอ LCD ดูรูปก็ง่าย แต่ต่อตาม มีผิด (แล้วทำไมมันไม่แก้ฟะ) แถมปรากฎว่า โปรแกรมตัวอย่างที่ให้ ผิดอีก หรือเราโง่ก็ไม่รู้

จึงลอง งมแก้ code ใหม่ ให้ใช้งานได้ตรงจุดประสงค์

Arduino DHT22 LCD Temp humidity

ดังนี้

#include <DHT.h>
#include <LiquidCrystal.h>

// Pin connected to the sensor data pin
#define DHTPIN 7

// Display pins
LiquidCrystal lcd(12,11,5,4,3,2);

// Use the line according to the sensor model
// #define DHTTYPE DHT11 // Sensor DHT11
#define DHTTYPE DHT22 //DHT Sensor 22 (AM2302)
// #define DHTTYPE DHT21 // DHT Sensor 21 (AM2301)

// Definitions sensor: pin, type
DHT dht (DHTPIN, DHTTYPE);

// Array symbol degree
byte degree [8] =
{
B00001100,
B00010010,
B00010010,
B00001100,
B00000000,
B00000000,
B00000000,
B00000000,
};

void setup() {
// put your setup code here, to run once:
// Initialize the display
lcd.begin (16,2);
lcd.clear ();
// Create the custom character with the symbol of the degree
lcd.createChar (0,degree);
// Information on the initial display
lcd.setCursor (0,0);
lcd.print (“Temp. : “);
lcd.setCursor (13,0);
// Shows the symbol of the degree
lcd.write (byte (0));
lcd.print (“C”);
lcd.setCursor (0,1);
lcd.print (“Umid. : “);
lcd.setCursor (14,1);
lcd.print (“%”);

Serial.begin (9600);
Serial.println (“Waiting for data …”);
// Starts DHT sensor
dht.begin ();
}

void loop()
{
// put your main code here, to run repeatedly:
// Wait 2 seconds between the measurements
delay (2000);

// Moisture reading
float h = dht.readHumidity();
float t = dht.readTemperature();

// dht.readHumidity float h = ();
// Reading of temperature (Celsius)
// t = dht.readTemperature float ();

// Check if the sensor is responding
if (isnan (h) || isnan (t))
{
Serial.println (“Failed to read DHT sensor data !!!”);
return;
}

// Display the temperature in the serial monitor and display
Serial.print (“Temperature: “);
Serial.print (t);
lcd.setCursor(8,0);
lcd.print (t);
Serial.print (” C * “);
// Show the moisture in the serial monitor and display
Serial.print (“humidity: “);
Serial.print (h);
Serial.println (” %”);
lcd.setCursor (8,1);
lcd.print (h);
}

 

 

 
image

Credit:http://www.boxelectronica.com/en/temperature-humidity/146-humidity-and-temperature-dht22.html