/
SerialWriter.h
73 lines (54 loc) · 1.61 KB
/
SerialWriter.h
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
/*
SerialWriter.h - Library for
*/
#ifndef SerialWriter_h
#define SerialWriter_h
unsigned int ledPinOK = LEDPIN_OK; // a LED that lights green if everything is OK
String cID = CONTROLLER_ID;
#include "Arduino.h"
class SerialWriter
{
public:
SerialWriter();
void sendStatus(String myString);
void sendError(String myString);
void sendData(String sensorID, int value);
};
SerialWriter::SerialWriter(){}
void SerialWriter::sendStatus(String myString){
/*
How to use:
sendSerialStatusMsg(F("string"));
will produce this output (but in 1 line):
{
"type":"status",
"data":"string"
}
You can also embedd an object into the "string", will produce
input : {\"message\":\"Going to initialize sensor\", \"sensorID\": \"" + parseID + "\", \"dv_max\": \"" + dv_max + "\"}}
output (in one line):
{
"type":"status",
"data":{
"message": "my Message",
"sensorID": "parseID",
"dv_max": 123
}
}
*/
Serial.print("{\"type\":\"status\",\"cID\":\"" + cID + "\",\"data\":");
Serial.print(myString);
Serial.println(F("}"));
}
void SerialWriter::sendError(String myString){
Serial.print("{\"type\":\"error\",\"cID\":\"" + cID + "\",\"data\":\"");
Serial.print(myString);
Serial.println(F("\"}"));
}
void SerialWriter::sendData(String sensorID, int value){
Serial.println(sensorID);
digitalWrite(ledPinOK, HIGH);
Serial.println("{\"type\":\"dataPoint\",\"cID\":\"" + cID + "\", \"data\":{\"sensorID\": \"" + sensorID + "\", \"value\": " + value + "}}");
digitalWrite(ledPinOK, LOW);
}
#endif