-
Notifications
You must be signed in to change notification settings - Fork 5
/
InfluxArduino.cpp
108 lines (83 loc) · 2.51 KB
/
InfluxArduino.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
103
104
105
106
107
108
#include <HTTPClient.h>
#include "InfluxArduino.hpp"
InfluxArduino::InfluxArduino()
{
}
InfluxArduino::~InfluxArduino()
{
}
void InfluxArduino::configure(const char database[],const char host[],const uint16_t port)
{
//copy these strings to private class pointers for future use
_database = new char[strlen(database)+1];
strcpy(_database,database); //strncpy fails for some reason
_host = new char[strlen(host)+1];
strcpy(_host,host);
_port = port;
}
void InfluxArduino::addCertificate(const char cert[])
{
//copy these strings to private class pointers for future use
_cert = new char[strlen(cert)+1];
strcpy(_cert,cert);
_isSecure = true;
}
void InfluxArduino::authorize(const char username[], const char password[])
{
//copy these strings to private class pointers for future use
_username = new char[strlen(username)+1];
strcpy(_username,username);
_password = new char[strlen(password)+1];
strcpy(_password,password);
_isAuthorised = true;
}
bool InfluxArduino::write(const char *measurement,const char *fieldString)
{
yield(); // reset esp watchdog
write(measurement,"",fieldString);
}
bool InfluxArduino::write(const char *measurement,const char *tagString,const char *fieldString)
{
yield(); // reset esp watchdog
HTTPClient http;
char uri[32];
sprintf(uri, "/write?db=%s", _database);
if(_isSecure)
{
http.begin(_host, _port, uri, _cert);
}
else
{
http.begin(_host, _port, uri);
}
http.addHeader("Content-Type", "text/plain"); // not sure what influx is looking for but this works?
if(_isAuthorised)
{
http.setAuthorization(_username,_password);
}
char writeBuf[512]; // ¯\_(ツ)_/¯
if(strlen(tagString) > 0)
{
sprintf(writeBuf,"%s,%s %s",measurement,tagString,fieldString); //no comma between tags and fields
}
else
{
//no tags
sprintf(writeBuf,"%s %s",measurement,fieldString); //no comma between tags and fields
}
Serial.println(writeBuf);
_latestResponse = http.POST(writeBuf);
http.end();
yield(); // reset esp watchdog
return _latestResponse == 204;
}
int InfluxArduino::getResponse()
{
yield(); // reset esp watchdog
return _latestResponse;
}
bool InfluxArduino::isSecure()
{
yield(); // reset esp watchdog
return _isSecure;
}