-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266_BME280_Thingspeak.ino
108 lines (83 loc) · 2.53 KB
/
ESP8266_BME280_Thingspeak.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
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
#include <ESP8266WiFi.h>
#include <math.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
float temperature, humidity, pressure, altitude;
/*Put your SSID & Password*/
const char* ssid = "Nama-Wifi Anda"; // Enter SSID here
const char* password = "password wifi"; //Enter Password here
//Thingspeak and Firebase Authentication
String apiKey = "Write_API";
const char* server = "api.thingspeak.com";
WiFiClient client;
int delayMinute = 4 * 60000;
int ctt=0;
unsigned long currentMillis;
unsigned long previousMillisA = 0;
unsigned long previousMillisB = 0;
unsigned long intervalCheck = 4*60000;
void setup() {
Serial.begin(115200);
delay(100);
bme.begin(0x76);
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
ctt++;
if (ctt>60){
ESP.restart();
}
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentMillis = millis();
if ((currentMillis - previousMillisA) >= intervalCheck) {
previousMillisA = currentMillis;
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = bme.readPressure() / 100.0F;
//altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
Serial.println("Sending Package");
String postStr = apiKey;
postStr +="&field1=";
postStr += String(temperature);
postStr +="&field2=";
postStr += String(humidity);
postStr +="&field3=";
postStr += String(pressure);
// postStr +="&field4=";
// postStr += String(tpl);
// postStr +="&field5=";
// postStr += String(litre);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.println("Uploading…");
client.stop();
Serial.println("Data Sent! Yeay~");
}
else {
Serial.println("Cannot Reach Thingspeak");
ESP.restart();
}
}
}