-
Notifications
You must be signed in to change notification settings - Fork 0
/
reciver.ino
104 lines (77 loc) · 2.14 KB
/
reciver.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
#include <SoftwareSerial.h>
#include <ThingerESP8266.h>
#include <ESP8266WiFi.h>
SoftwareSerial DataSerial(12, 13);
unsigned long previousMillis = 0;
const long interval = 3000;
String arrData[5];
#define USERNAME "mjajang"
#define DEVICE_ID "Tugas_Akhir"
#define DEVICE_CREDENTIAL "VwlKNW!-0y+18w1$"
#define LED_PIN 4
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
const char* ssid = "vivo1901";
const char* password = "singberkah";
int ldr, suara;
float temperature, humidity, gas;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
DataSerial.begin(9600);
WiFi.begin(ssid, password);
thing.add_wifi(ssid, password);
thing["Data"] >> [](pson & out){
out["gas"] = gas;
out["humidity"] = humidity;
out["ldr"] = ldr;
out["suara"] = suara;
out["temperature"] = temperature;
};
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
String data = "";
while(DataSerial.available()>0)
{
data += char(DataSerial.read());
}
data.trim();
if(data != "")
{
int index = 0;
for(int i=0; i<= data.length(); i++)
{
char delimiter = '#';
if(data[i] != delimiter)
arrData[index] += data[i];
else
index++;
}
if(index == 4)
{
Serial.println("Gas : " + arrData[0]);
Serial.println("Humidity : " + arrData[1]);
Serial.println("LDR : " + arrData[2]);
Serial.println("Suara : " + arrData[3]);
Serial.println("Temperature : " + arrData[4]);
Serial.println();
}
gas = arrData[0].toFloat();
humidity = arrData[1].toFloat();
ldr = arrData[2].toInt();
suara = arrData[3].toInt();
temperature = arrData[4].toFloat();
arrData[0] = "";
arrData[1] = "";
arrData[2] = "";
arrData[3] = "";
arrData[4] = "";
thing.handle();
}
DataSerial.println("Ya");
}
}