-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_protocol.cpp
102 lines (91 loc) · 2.76 KB
/
tcp_protocol.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
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
#include <Arduino.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include "higrow_sensors.h"
#include <StreamUtils.h>
#include "tcp_protocol.h"
TcpClient::TcpClient(IPAddress ip){
_ip = ip;
}
void TcpClient::end(){
_client.println("end");
delay(100);
_client.stop();
}
void TcpClient::begin(){
if (!_client.connect(_ip, PORT)){
Serial.println("Couldn't connect to the host");
return;
}
}
StaticJsonDocument<200> TcpClient::ConvertToJson(SensorData s){
//this shortens up the code for the convertion between struct and json
StaticJsonDocument<200> d; //I guess 200 bytes is more than enough
d["temp"]=s.temp;
d["humidity"] = s.hum;
d["soil_moisture"] = s.soil_moisture;
d["soil_salt"] = s.soil_salt;
d["lux"] = s.lux;
d["timestamp"] = s.timestamp;
d["mac"] = WiFi.macAddress(); // the identifier of every device
return d;
}
void TcpClient::SendData(SensorData s){
StaticJsonDocument<200> d = TcpClient::ConvertToJson(s);
WriteBufferingStream buff{_client, 200};
serializeJson(d, buff); //print it out to serial for testing
buff.flush();
}
void TcpClient::GetDateFromServer(){
StaticJsonDocument<200> d;
d["request"] = "date";
WriteBufferingStream buff(_client, 200);
serializeJson(d, buff);
buff.flush();
//Now wait for the packet to arrive
StaticJsonDocument<200> res = TcpClient::GetResponse();
int date = res["date"];
if (date == 0){
return; //don't set the date if the server hasn't answered
}
struct timeval sdate;
sdate.tv_sec = date;
settimeofday(&sdate, NULL);
}
StaticJsonDocument<200> TcpClient::GetResponse(){
int t = 0;
while (_client.available() == 0){
t++;
if (t >= TIMEOUT){
Serial.println("The server hasn't awnsered, aborting getting date from server...");
_client.stop();
StaticJsonDocument<100> err;
err["error"] = "error";
return err;
}
delay(500);
}
String response = _client.readString();
StaticJsonDocument<200> res;
DeserializationError err = deserializeJson(res, response);
if (err){
Serial.println("Couldn't parse the json..");
Serial.println(err.c_str());
}
return res;
}
void TcpClient::GetSleepTime(int timestosend, int * sleep_time){
StaticJsonDocument<200> d;
d["request"] = "time_reconnect";
WriteBufferingStream buff(_client, 200);
serializeJson(d, buff);
buff.flush();
//Now wait for the packet to arrive
StaticJsonDocument<200> res = TcpClient::GetResponse();
int reconnect_time = res["time_reconnect"];
Serial.printf("%d reconnect_time\n", reconnect_time);
if (reconnect_time == 0){
return; //aka maintain the default sleep time
}
//TODO: calculate sleep time with times to send and reconnect_time (this applies only on nodes connected to an hybrid node)
}