-
Notifications
You must be signed in to change notification settings - Fork 13
/
getControlJSON_LED.ino
79 lines (74 loc) · 2.47 KB
/
getControlJSON_LED.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
#include <Arduino.h>
#include <MAGELLAN_SIM7600E_MQTT.h>
#define LED_E15 15 //LED Builtin Board 4G
MAGELLAN_SIM7600E_MQTT magel;
void setup()
{
Serial.begin(115200);
magel.begin();
pinMode(LED_E15, OUTPUT);
digitalWrite(LED_E15, LOW);
//{1} JSON String
magel.getControlJSON([](String payload){
Serial.print("# Control incoming JSON: ");
Serial.println(payload);
String control = magel.deserializeControl(payload);
if(control == "{\"Lamp1\":1}")
{
digitalWrite(LED_E15, HIGH);
Serial.println("LED ON");
magel.sensor.add("Lamp1", digitalRead(LED_E15));
magel.control.ACK(magel.sensor.toJSONString()); //ACKNOWLEDGE control to magellan
magel.sensor.clear();
}
else if (control == "{\"Lamp1\":0}")
{
digitalWrite(LED_E15, LOW);
Serial.println("LED OFF");
magel.sensor.add("Lamp1", digitalRead(LED_E15));
magel.control.ACK(magel.sensor.toJSONString()); //ACKNOWLEDGE control to magellan
magel.sensor.clear();
}
else
{
magel.control.ACK(control); //ACKNOWLEDGE control to magellan if another control
}
});
//or {2} JSON Object
// magel.getControlJSON([](JsonObject docObject){
// String Lamp1 = docObject["Lamp1"]; // buffer value form control key "Lamp1" if not found this key value is "null"
// if(Lamp1.indexOf("null") == -1)
// {
// Serial.print("# Control incoming JSON Object: ");
// Serial.print("# [Key] => Lamp1: ");
// Serial.println(Lamp1);
// if(Lamp1 == "1")
// {
// digitalWrite(LED_E15, HIGH);
// Serial.println("LED ON");
// magel.sensor.add("Lamp1", digitalRead(LED_E15));
// magel.control.ACK(magel.sensor.toJSONString()); //ACKNOWLEDGE control to magellan
// magel.sensor.clear();
// }
// else if (Lamp1 == "0")
// {
// digitalWrite(LED_E15, LOW);
// Serial.println("LED OFF");
// magel.sensor.add("Lamp1", digitalRead(LED_E15));
// magel.control.ACK(magel.sensor.toJSONString()); //ACKNOWLEDGE control to magellan
// magel.sensor.clear();
// }
// }
// });
// prepare sensor to magellan by report control key with inial value
magel.report.send("Lamp1","0");
}
void loop()
{
magel.loop();
magel.subscribes([](){
magel.subscribe.control(); // subscribe server config content type JSON
});
magel.interval(10,[](){ //time interval function inside every 10000 millis
});
}