· 6 years ago · Apr 05, 2020, 06:58 AM
1#include "ThingSpeak.h"
2#include <ESP8266WiFi.h>
3#include "DHTesp.h"
4
5#define SECRET_SSID "AQEELA"
6#define SECRET_PASS "TanyaSebelah"
7
8#define SECRET_CH_ID 1031848 //Ganti dengan Channel ThinkSpeak
9#define SECRET_WRITE_APIKEY "EO9RMONOZTHI58KX" //Ganti dengan Write API KEY ThinkSpeak
10
11DHTesp dht;
12char ssid[] = SECRET_SSID;
13char pass[] = SECRET_PASS;
14
15int keyIndex = 0;
16WiFiClient client;
17
18unsigned long myChannelNumber = SECRET_CH_ID;
19const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
20
21int number = 0;
22
23void setup() {
24 dht.setup(D3, DHTesp::DHT11);
25 Serial.begin(115200);
26 WiFi.mode(WIFI_STA);
27 ThingSpeak.begin(client);
28}
29
30void loop() {
31 if (WiFi.status() != WL_CONNECTED) {
32 Serial.print("Attempting to connect to SSID: ");
33 Serial.println(SECRET_SSID);
34 while (WiFi.status() != WL_CONNECTED) {
35 WiFi.begin(ssid, pass);
36 Serial.print(".");
37 delay(5000);
38 }
39 Serial.println("\nConnected.");
40 }
41
42 number = analogRead(A0);
43 float temperature = dht.getTemperature();
44 float humidity = dht.getHumidity();
45
46 int x = ThingSpeak.setField(1, number);
47 float y = ThingSpeak.setField(2, temperature);
48 float z = ThingSpeak.setField(3, humidity);
49 if (x == 200 && y == 200 && z == 200) {
50 Serial.println("New Record has been Created");
51 ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
52 }else{
53 Serial.println("Problem updating channel. HTTP error code " + String(x));
54 Serial.println("Problem updating channel. HTTP error code " + String(y));
55 Serial.println("Problem updating channel. HTTP error code " + String(z));
56 }
57
58
59 delay(15000);
60}