-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththingsboard.ino
55 lines (39 loc) · 1.47 KB
/
thingsboard.ino
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
#include <ESP8266WiFi.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include "ThingsBoard.h"
void networkSetup(){
WiFiManager wifiManager;
wifiManager.autoConnect("IOT");
Serial.println("");
Serial.println("WiFi connection");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// Use WiFiClient class to create TCP connections
WiFiClient client;
ThingsBoard tb(client);
#define TOKEN "pKVAnaW8IXKHUWWDHfZs"
#define THINGSBOARD_SERVER "10.10.11.8"
void sendData(float temperature_indoor, float humidity_indoor, float temperature_outdoor, float humidity_outdoor, uint16_t lux){
if (!tb.connected()) {
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
Serial.println("Failed to connect");
return;
}
}
Serial.println("Sending data...");
// Uploads new telemetry to ThingsBoard using MQTT.
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api
// for more details
tb.sendTelemetryFloat("temperature_indoor", temperature_indoor);
tb.sendTelemetryFloat("humidity_indoor", humidity_indoor);
tb.sendTelemetryFloat("temperature_outdoor", temperature_outdoor);
tb.sendTelemetryFloat("humidity_outdoor", humidity_outdoor);
tb.sendTelemetryInt("light", lux);
tb.loop();
}