Skip to content

Commit c0a7927

Browse files
committed
Initial commit. Basic posting is working.
0 parents  commit c0a7927

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
.vscode/

InfluxData.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
class InfluxData
3+
{
4+
5+
public:
6+
InfluxData(String measurement) : _measurement(measurement) {}
7+
8+
void addTag(String key, String value)
9+
{
10+
_tags += "," + key + "=" + value;
11+
}
12+
void addValue(String key, float value)
13+
{
14+
_values = (_values == "") ? (" ") : (_values += ",");
15+
_values += key + "=" + String(value);
16+
}
17+
18+
String toString()
19+
{
20+
return _measurement + _tags + _values;
21+
}
22+
23+
private:
24+
String _measurement;
25+
String _tags;
26+
String _values;
27+
28+
};

InfluxDb.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "Arduino.h"
2+
#include "InfluxDb.h"
3+
4+
Influxdb::Influxdb(const char *host, uint16_t port)
5+
{
6+
_port = String(port);
7+
_host = String(host);
8+
}
9+
10+
void Influxdb::setDb(const char *db)
11+
{
12+
_db = String(db);
13+
}
14+
15+
boolean Influxdb::post(InfluxData data)
16+
{
17+
return post(data.toString());
18+
}
19+
20+
boolean Influxdb::post(String data)
21+
{
22+
Serial.print("write ");
23+
Serial.println(data);
24+
25+
HTTPClient http;
26+
http.begin("http://" + _host + ":" + _port + "/write?db=" + _db);
27+
http.addHeader("Content-Type", "text/plain");
28+
29+
int httpResponseCode = http.POST(data);
30+
Serial.print(" -> Response code ");
31+
Serial.println(httpResponseCode);
32+
33+
String response = http.getString();
34+
Serial.println(" -> Response: \"" + response + "\".");
35+
36+
boolean success;
37+
if (httpResponseCode == 204)
38+
{
39+
40+
success = true;
41+
}
42+
else
43+
{
44+
Serial.println("#####\nPOST FAILED\n#####");
45+
success = false;
46+
}
47+
48+
http.end();
49+
return success;
50+
}

InfluxDb.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
#include "Arduino.h"
3+
#include <ESP8266HTTPClient.h>
4+
#include "InfluxData.h"
5+
6+
class Influxdb
7+
{
8+
public:
9+
Influxdb(const char *host, uint16_t port);
10+
11+
void setDb(const char *db);
12+
13+
boolean post(InfluxData data);
14+
boolean post(String data);
15+
16+
private:
17+
String _port;
18+
String _host;
19+
String _db;
20+
};

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Tobias Schürg
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ESP8266_Influx_DB

0 commit comments

Comments
 (0)