-
Notifications
You must be signed in to change notification settings - Fork 0
/
pIoT_JSON.h
70 lines (61 loc) · 2.43 KB
/
pIoT_JSON.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
/** Trivial JSON messages parser for pIoT base.
* The name of the first object identifies the type of message.
* A common fields is "address".
*
* Author: Dario Salvi (dariosalvi78 at gmail dot com)
*
* Licensed under the GPL license http://www.gnu.org/copyleft/gpl.html
*/
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <wiring.h>
#include <pins_arduino.h>
#endif
/** Separates an array into an array of strings.
* @param line a pointer to the line to be analysed
* @param arr a pre-initialized array of char*
* @param len the length of the resulting array
*/
void JSONtoStringArray(char* line, char** arr, int* len);
/** Looks for the start of a data, given its name
* example: search for "data" returns a pointer next to the : after "data" has been found
* @param line the line where to look into
* @param dataname the name of the data without the ".." and :
* @return the pointer where the data starts (after the :), NULL if not found
*/
char* JSONsearchDataName(char* line, char* dataname);
/** Converts a JSON property to a unsigned long.
* @param line the line that contains the property
* @param dataName the property to be parsed, should not include the \"...\":
* @return the parsed unsigned long
*/
unsigned long JSONtoULong(char* line, char* dataName);
/** Converts a JSON property to a long.
* @param line the line that contains the property
* @param dataName the property to be parsed, should not include the \"...\":
* @return the parsed long
*/
long JSONtoLong(char* line, char* dataName);
/** Converts a JSON property to a double.
* @param line the line that contains the property
* @param dataName the property to be parsed, should not include the \"...\":
* @return the parsed double
*/
double JSONtoDouble(char* line, char* dataName);
/** Converts a JSON property to boolean.
* @param line the line to be parsed
* @param dataName the name of the property
* @return the boolean value
*/
boolean JSONtoBoolean(char* line, char* dataName);
/** Reads the strings coming from the serial and calls the parsers.
* It listens for the time specified in millis and does not exit
* until that time has been reached.
* Spaces, tabs and new lines are removed automatically.
* @param millis the time to wait until some data is found
* @param a function that treats the message:
* - dataname is the name of the first object
* - msg is the entire JSON string
*/
void readSerial(int millis, void (*f)(char* dataName, char* msg));