forked from mokoraden/cobarepo
/
script_koneksi_cloud_nodemcu.txt
107 lines (72 loc) · 2.34 KB
/
script_koneksi_cloud_nodemcu.txt
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 <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define LED 2
const char* wifiName = "Andro";
const char* wifiPass = "caberg2010";
//Web Server address to read/write from
const char *host = "http://displaymasjid.my.id/vokasiti/api.php";
void setup() {
pinMode(LED,OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifiName);
WiFi.begin(wifiName, wifiPass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP
}
void loop() {
HTTPClient http; //Declare object of class HTTPClient
Serial.print("Request Link:");
Serial.println(host);
http.begin(host); //Specify request destination
int httpCode = http.GET(); //Send the request
String payload = http.getString(); //Get the response payload from server
Serial.print("Response Code:"); //200 is OK
Serial.println(httpCode); //Print HTTP return code
Serial.print("Returned data from Server:");
Serial.println(payload); //Print request response payload
if(httpCode == 200)
{
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
// Parse JSON object
JsonObject& root = jsonBuffer.parseObject(payload);
if (!root.success()) {
Serial.println(F("Parsing failed!"));
return;
}
JsonObject& result_0 = root["result"][0];
// Decode JSON/Extract values
Serial.println(F("Response:"));
Serial.println(result_0["id_lampu"].as<char*>());
Serial.println(result_0["nama_lampu"].as<char*>());
Serial.println(result_0["status"].as<char*>());
// Serial.println(root["data"][1].as<char*>());
if (result_0["status"]== "on"){
Serial.println(F("SIAP NYALAKAN LAMPU"));
digitalWrite(LED,LOW);
}
if (result_0["status"]== "off"){
Serial.println(F("MATIKAN LAMPU"));
digitalWrite(LED,HIGH);
}
}
else
{
Serial.println("Error in response");
}
http.end(); //Close connection
delay(1); //GET Data at every 5 seconds
}