-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFRAIoT.cpp
71 lines (56 loc) · 1.68 KB
/
IFRAIoT.cpp
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
#include "IFRAIoT.h"
DynamicJsonDocument _doc(1024);
IFRAIoT::IFRAIoT(char* username, char* password ){
_username = username;
_password = password;
_mqtt_client.setClient(_espClient);
_mqtt_client.setServer(IFRASERVER, MQTT_PORT);
// _mqtt_client.setCallback(callback);
}
bool IFRAIoT::wifiConnection(char *ssid, char *pass){
WiFi.begin(ssid, pass);
Serial.print("Start connect wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
WiFi.setAutoReconnect(true);
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
}
bool IFRAIoT::mqttConnection(char *mqtt_topic){
}
void IFRAIoT::addMeasure(char *var_id, float value){
if(_doc.size()<1){
_arr = _doc.to<JsonArray>();
}
JsonObject obj = _doc.createNestedObject();
obj["bn"] = var_id;
obj["n"] = var_id;
obj["v"] = value;
obj["bu"] = var_id;
obj["t"] = var_id;
}
void IFRAIoT::send(void){
if(!_mqtt_client.connected()) {
if (_mqtt_client.connect("ESP8266Client", _username, _password)) {
Serial.println("connected");
_mqtt_client.subscribe("organization/2/messages");
} else {
Serial.print("failed, rc=");
Serial.print(_mqtt_client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
return;
}
}else{
// serializeJsonPretty(_doc, Serial);
char message[2024];
serializeJsonPretty(_doc, message);
_mqtt_client.publish("organization/2/messages", message);
Serial.println(message);
}
_doc.clear();
_mqtt_client.loop();
}