-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ino
70 lines (54 loc) Β· 1.77 KB
/
code.ino
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
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <Wire.h>
#include <SparkFunHTU21D.h>
HTU21D myHTU21D;
const char* ssid = "YOUR_SSID"; // π Your Wi-Fi SSID
const char* password = "YOUR_PASSWORD"; // π Your Wi-Fi Password
unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER; // π Your ThingSpeak Channel ID
const char* myWriteAPIKey = "YOUR_WRITE_API_KEY"; // π Your ThingSpeak Write API Key
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
// π οΈ Initialize the sensor
Wire.begin();
myHTU21D.begin();
// πΆ Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
// π Initialize ThingSpeak
ThingSpeak.begin(client);
// π‘οΈπ§ Read data from the sensor
float humidity = myHTU21D.readHumidity();
float temperature = myHTU21D.readTemperature();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// π€ Send data to ThingSpeak
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Data successfully sent to ThingSpeak");
} else {
Serial.println("Error sending data: " + String(x));
}
// π΄ Disconnect Wi-Fi to save power
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
// π΄ Enter deep sleep for 15 minutes
Serial.println("Entering deep sleep for 15 minutes");
ESP.deepSleep(15 * 60 * 1000000); // Time in microseconds
}
void loop() {
// The ESP8266 will restart after deep sleep
}