· 6 years ago · Feb 07, 2020, 12:00 PM
1#include "ThingSpeak.h"
2#include <ESP8266WiFi.h>
3
4#define SECRET_SSID "your-ssid"
5#define SECRET_PASS "your-pass"
6
7#define SECRET_CH_ID 00000 //Ganti dengan Channel ThinkSpeak
8#define SECRET_WRITE_APIKEY "SECRET_WRITE_APIKEY" //Ganti dengan Write API KEY ThinkSpeak
9
10char ssid[] = SECRET_SSID;
11char pass[] = SECRET_PASS;
12
13int keyIndex = 0;
14WiFiClient client;
15
16unsigned long myChannelNumber = SECRET_CH_ID;
17const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
18
19int number = 0;
20
21void setup() {
22 Serial.begin(115200);
23 WiFi.mode(WIFI_STA);
24 ThingSpeak.begin(client);
25 }
26
27void loop() {
28 if(WiFi.status() != WL_CONNECTED){
29 Serial.print("Attempting to connect to SSID: ");
30 Serial.println(SECRET_SSID);
31 while(WiFi.status() != WL_CONNECTED){
32 WiFi.begin(ssid, pass);
33 Serial.print(".");
34 delay(5000);
35 }
36 Serial.println("\nConnected.");
37 }
38 number = analogRead(A0);
39 int x = ThingSpeak.writeField(myChannelNumber, 1, number, myWriteAPIKey);
40 if(x == 200){
41 Serial.println("Channel update successful.");
42 }else{
43 Serial.println("Problem updating channel. HTTP error code " + String(x));
44 }
45 delay(20000); // Wait 20 seconds to update the channel again
46
47}