-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.ino
More file actions
53 lines (42 loc) · 1.27 KB
/
client.ino
File metadata and controls
53 lines (42 loc) · 1.27 KB
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
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
const char* ssid = "SSID"; // SSID
const char* password = "password123x"; // Password
const char* host = "172.104.49.134"; // IP address socket server
const int port = 4444; // Port socket server
WiFiClient client;
StaticJsonBuffer<512> jsonBuffer;
void setup() {
Serial.begin(115200);
Serial.print("Memulai koneksi ke WiFi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi tersambung");
Serial.print("IP address anda: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, port)) {
Serial.println("Koneksi ke server socket gagal");
return;
}
Serial.println("Tersambung dengan server");
String url = "{\"action\":\"sub\",\"topic\":\"demo\"}\n";
client.print( url );
}
void loop() {
while(client.available()){
String line = client.readStringUntil(10);
JsonObject& root = jsonBuffer.parseObject(line);
if (!root.success()) {
Serial.println("Parsing JSON gagal");
return;
}
const char* data = root["data"];
Serial.print("Data masuk: ");
Serial.println(data);
}
}