Browse Source

add the wong lib

Aaron Fischer 2 years ago
parent
commit
8a4f73d425

+ 176
- 0
influxdb-lib/ESP8266Influxdb.cpp View File

@@ -0,0 +1,176 @@
1
+#include "Arduino.h"
2
+#include "ESP8266Influxdb.h"
3
+#include <ESP8266WiFi.h>
4
+
5
+#define DEBUG_PRINT // comment this line to disable debug print
6
+
7
+#ifndef DEBUG_PRINT
8
+#define DEBUG_PRINT(a)
9
+#else
10
+#define DEBUG_PRINT(a) (Serial.println(String(F("[Debug]: "))+(a)))
11
+#define _DEBUG
12
+#endif
13
+
14
+Influxdb::Influxdb(const char *host, uint16_t port) : WiFiClient() {
15
+        _port = port;
16
+        _host = host;
17
+}
18
+
19
+DB_RESPONSE Influxdb::opendb(String db, String user, String password) {
20
+        _db = "db=" + db + "&u=" + user + "&p=" + password;
21
+}
22
+
23
+DB_RESPONSE Influxdb::opendb(String db) {
24
+        _db = "db=" + db;
25
+
26
+}
27
+
28
+DB_RESPONSE Influxdb::write(FIELD data) {
29
+        return write(data.postString());
30
+}
31
+
32
+DB_RESPONSE Influxdb::write(String data) {
33
+        if (!connect(_host, _port)) {
34
+                DEBUG_PRINT("connection failed");
35
+                _response = DB_CONNECT_FAILED;
36
+                return _response;
37
+        }
38
+        String postHead = "POST /write?" + _db + " HTTP/1.1\r\n";
39
+        postHead += "Host: " + String(_host) + ":" + String(_port) + "\r\n";
40
+        // postHead += "Content-Type: application/x-www-form-urlencoded\r\n";
41
+        postHead += "Content-Length: " + String(data.length()) + "\r\n\r\n";
42
+
43
+        DEBUG_PRINT("Writing data to " + String(_host) + ":" + String(_port));
44
+        print(postHead + data);
45
+        DEBUG_PRINT(postHead + data);
46
+
47
+        uint8_t t = 0;
48
+        // Check the reply whether writing is success or not
49
+        while (!available() && t < 200) {
50
+                delay(10);
51
+                t++;
52
+        }
53
+        if (t==200) {_response = DB_ERROR; return DB_ERROR; } // Return error if time out.
54
+
55
+#if !defined _DEBUG
56
+        if (available()) {
57
+                _response = (findUntil("204", "\r")) ? DB_SUCCESS : DB_ERROR;
58
+                return _response;
59
+        }
60
+#else
61
+        _response=DB_ERROR;
62
+        while (available()) {
63
+                String line = readStringUntil('\n');
64
+                if (line.substring(9,12)=="204")
65
+                        _response = DB_SUCCESS;
66
+                DEBUG_PRINT("(Responsed): " + line);
67
+        }
68
+        return _response;
69
+#endif
70
+        return DB_ERROR;
71
+}
72
+
73
+DB_RESPONSE Influxdb::query(String sql) {
74
+
75
+        if (!connect(_host, _port)) {
76
+                DEBUG_PRINT("connection failed");
77
+                _response = DB_CONNECT_FAILED;
78
+                return _response;
79
+        }
80
+
81
+        String url = "/query?";
82
+#if defined _DEBUG
83
+        url += "pretty=true&";
84
+#endif
85
+        url += _db;
86
+        url += "&q=" + URLEncode(sql);
87
+        DEBUG_PRINT("Requesting URL: ");
88
+        DEBUG_PRINT(url);
89
+
90
+        // This will send the request to the server
91
+        print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + _host +
92
+              ":" + _port + "\r\n" + "Connection: close\r\n\r\n");
93
+
94
+        // Read all the lines of the reply from server and print them to Serial
95
+        uint8_t t = 0;
96
+        while (!available() && t < 200) {
97
+                delay(10);
98
+                t++;
99
+        }
100
+        if (t==200) {_response = DB_ERROR; return DB_ERROR; }  // Return error if time out.
101
+
102
+        DEBUG_PRINT("Receiving....");
103
+        uint8_t i=0;
104
+        String line = readStringUntil('\n');
105
+        DEBUG_PRINT("[HEAD] " + line);
106
+
107
+        if (line.substring(9,12) == "200") {
108
+                while (available()) {
109
+                        line = readStringUntil('\n');
110
+                        DEBUG_PRINT("(HEAD) " + line);
111
+                        if (i < 6 ) i++; else return _response;
112
+                }
113
+                _response = DB_SUCCESS;
114
+        }
115
+        else{
116
+                _response = DB_ERROR;
117
+#if defined _DEBUG
118
+                while (available()) {
119
+                        line = readStringUntil('\n');
120
+                        DEBUG_PRINT("[HEAD] " + line);
121
+                }
122
+#endif
123
+        }
124
+
125
+        return _response;
126
+}
127
+
128
+DB_RESPONSE Influxdb::response() {
129
+        return _response;
130
+}
131
+
132
+/* -----------------------------------------------*/
133
+//        Field object
134
+/* -----------------------------------------------*/
135
+FIELD::FIELD(String m) {
136
+        measurement = m;
137
+}
138
+
139
+void FIELD::empty() {
140
+        _data = "";
141
+        _tag = "";
142
+}
143
+
144
+void FIELD::addTag(String key, String value) {
145
+        _tag += "," + key + "=" + value;
146
+}
147
+
148
+void FIELD::addField(String key, float value) {
149
+        _data = (_data == "") ? (" ") : (_data += ",");
150
+        _data += key + "=" + String(value);
151
+}
152
+
153
+String FIELD::postString() {
154
+        //  uint32_t utc = 1448114561 + millis() /1000;
155
+        return measurement + _tag + _data;
156
+}
157
+
158
+// URL Encode with Arduino String object
159
+String URLEncode(String msg) {
160
+        const char *hex = "0123456789abcdef";
161
+        String encodedMsg = "";
162
+
163
+        uint16_t i;
164
+        for (i = 0; i < msg.length(); i++) {
165
+                if (('a' <= msg.charAt(i) && msg.charAt(i) <= 'z') ||
166
+                    ('A' <= msg.charAt(i) && msg.charAt(i) <= 'Z') ||
167
+                    ('0' <= msg.charAt(i) && msg.charAt(i) <= '9')) {
168
+                        encodedMsg += msg.charAt(i);
169
+                } else {
170
+                        encodedMsg += '%';
171
+                        encodedMsg += hex[msg.charAt(i) >> 4];
172
+                        encodedMsg += hex[msg.charAt(i) & 15];
173
+                }
174
+        }
175
+        return encodedMsg;
176
+}

+ 72
- 0
influxdb-lib/ESP8266Influxdb.h View File

@@ -0,0 +1,72 @@
1
+/* Influxdb library
2
+
3
+   MIT license
4
+   Written by HW Wong
5
+ */
6
+
7
+#ifndef INFLUXDB_H
8
+#define INFLUXDB_H
9
+#include "Arduino.h"
10
+#include <ESP8266WiFi.h>
11
+
12
+enum DB_RESPONSE {DB_SUCCESS, DB_ERROR, DB_CONNECT_FAILED};
13
+
14
+// Url encode function
15
+String URLEncode(String msg);
16
+
17
+class FIELD
18
+{
19
+public:
20
+        FIELD(String m);
21
+
22
+        String measurement;
23
+
24
+        void addField(String key, float value);
25
+        void addTag(String key, String value);
26
+        void empty();
27
+        String postString();
28
+
29
+private:
30
+        String _data;
31
+        String _tag;
32
+
33
+};
34
+
35
+class Influxdb : private WiFiClient
36
+{
37
+public:
38
+        Influxdb(const char* host, uint16_t port);
39
+
40
+        DB_RESPONSE opendb(String db);
41
+        DB_RESPONSE opendb(String db, String user, String password);
42
+        DB_RESPONSE write(FIELD data);
43
+        DB_RESPONSE write(String data);
44
+        DB_RESPONSE query(String sql);
45
+        //uint8_t createDatabase(char *dbname);
46
+        DB_RESPONSE response();
47
+
48
+        using WiFiClient::available;
49
+        using WiFiClient::read;
50
+        using WiFiClient::flush;
51
+        using WiFiClient::find;
52
+        using WiFiClient::findUntil;
53
+        using WiFiClient::peek;
54
+        using WiFiClient::readBytes;
55
+        using WiFiClient::readBytesUntil;
56
+        using WiFiClient::readString;
57
+        using WiFiClient::readStringUntil;
58
+        using WiFiClient::parseInt;
59
+        using WiFiClient::setTimeout;
60
+
61
+private:
62
+        uint16_t _port;
63
+        const char* _host;
64
+        String _db;
65
+        DB_RESPONSE _response;
66
+
67
+};
68
+
69
+
70
+
71
+
72
+#endif

+ 1
- 0
influxdb-lib/README.md View File

@@ -0,0 +1 @@
1
+# ESP8266Influxdb

+ 63
- 0
influxdb-lib/examples/influxdb_write/influxdb_write.ino View File

@@ -0,0 +1,63 @@
1
+
2
+#include <Arduino.h>
3
+#include <ESP8266WiFi.h>
4
+#include <ESP8266WiFiMulti.h>
5
+#include <ESP8266Influxdb.h>
6
+
7
+const char *INFLUXDB_HOST = "host_or_ip";
8
+const uint16_t INFLUXDB_PORT = 8086;
9
+
10
+const char *DATABASE = "dbname";
11
+const char *DB_USER = "dbuser";
12
+const char *DB_PASSWORD = "dbpassword";
13
+
14
+ESP8266WiFiMulti WiFiMulti;
15
+Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
16
+
17
+void setup() {
18
+        Serial.begin(115200);
19
+        WiFiMulti.addAP("SSID", "PASSWORD");
20
+        while (WiFiMulti.run() != WL_CONNECTED) {
21
+                delay(100);
22
+        }
23
+        Serial.println("Ready");
24
+        influxdb.opendb(DATABASE, DB_USER, DB_PASSWORD);
25
+}
26
+
27
+void loop() {
28
+        // Writing data with influxdb HTTP API
29
+        // https://influxdb.com/docs/v0.9/guides/writing_data.html
30
+        Serial.println("Writing data to host " + String(INFLUXDB_HOST) + ":" +
31
+                       INFLUXDB_PORT + "'s database=" + DATABASE);
32
+        String data = "analog_read,method=HTTP_API,pin=A0 value=" + String(analogRead(A0));
33
+        influxdb.write(data);
34
+        Serial.println(influxdb.response() == DB_SUCCESS ? "HTTP write success"
35
+                       : "Writing failed");
36
+
37
+        // Writing data using FIELD object
38
+        // Create field object with measurment name=analog_read
39
+        FIELD dataObj("analog_read");
40
+        dataObj.addTag("method", "Field_object"); // Add method tag
41
+        dataObj.addTag("pin", "A0"); // Add pin tag
42
+        dataObj.addField("value", analogRead(A0)); // Add value field
43
+        Serial.println(influxdb.write(dataObj) == DB_SUCCESS ? "Object write success"
44
+                       : "Writing failed");
45
+
46
+        // Empty field object.
47
+        dataObj.empty();
48
+
49
+        // Querying Data
50
+        // https://influxdb.com/docs/v0.9/query_language/query_syntax.html
51
+        Serial.println("Querying data ........");
52
+
53
+        String sql = "select * from analog_read order by time desc limit 2";
54
+        if (influxdb.query(sql) == DB_SUCCESS) {
55
+                while (influxdb.available()) {
56
+                        String line = influxdb.readStringUntil('\n');
57
+                        Serial.println(line);
58
+                }
59
+        }
60
+        else
61
+                Serial.println("Query Failed");
62
+        delay(30000);
63
+}

+ 24
- 0
influxdb-lib/keywords.txt View File

@@ -0,0 +1,24 @@
1
+##################################################
2
+# Syntax Coloring Map For ESP8266 Influxdb library
3
+##################################################
4
+
5
+##################################################
6
+# Datatypes (KEYWORD1)
7
+##################################################
8
+
9
+Influxdb	   KEYWORD1
10
+FIELD		     KEYWORD1
11
+DB_RESPOND 	 KEYWORD1
12
+
13
+##################################################
14
+# Methods and Functions (KEYWORD2)
15
+##################################################
16
+
17
+addField 	  KEYWORD2
18
+addTag	 	  KEYWORD2
19
+empty	 	    KEYWORD2
20
+opendb	 	  KEYWORD2
21
+write	 	    KEYWORD2
22
+query	 	    KEYWORD2
23
+postString	KEYWORD2
24
+response    KEYWORD2

Loading…
Cancel
Save