-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempHumBox.ino
146 lines (119 loc) · 3.47 KB
/
TempHumBox.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <PubSubClient.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "myWiFiManager.h"
#include "DHT.h"
/***************************************/
// DHT
#define DHTPIN 13
#define DHTTYPE DHT11
#define WLPIN 12
char *thingsboardServer = "192.168.110.110";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
unsigned long lastSend;
DHT dht(DHTPIN, DHTTYPE);
/***************************************/
void setup() {
//cfg._AccessPointConnectingTimeOut=300;
Serial.begin(115200);
Serial.println("Initializing...");
myWiFiManagerSetup();
/**************************************/
dht.begin();
delay(10);
pinMode(WLPIN, INPUT);
delay(10);
delay(10);
thingsboardServer=cfg._Server_IP;
Serial.println("Connecting To TB Server");
client.setServer( thingsboardServer, cfg._PortNumber );
lastSend = 0;
/**************************************/
}
void loop() {
server.handleClient();
/*****************************************/
if(APisConnected)
{
if(!client.connected())
{
connectToTB();
}
if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
getAndSendTemperatureAndHumidityData();
lastSend = millis();
}
client.loop();
}
else
{
//Serial.println("Welcome,");
//Serial.println("Waiting for first config");
}
/****************************************/
}
void getAndSendTemperatureAndHumidityData()
{
Serial.println("Collecting sensors data.");
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
int w=digitalRead(WLPIN);
Serial.print("WaterLeakage: ");
Serial.print(w);
Serial.print(" %\w");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
String WaterLeakage = String(w);
String temperature = String(t);
String humidity = String(h);
// Just debug messages
Serial.print( "Sending WaterLeakage : [" );
Serial.print( WaterLeakage); Serial.println( ";" );
Serial.print( "Sending temperature and humidity : [" );
Serial.print( temperature ); Serial.print( "," );
Serial.print( humidity );
Serial.print( "] -> " );
// Prepare a JSON payload string
String payload = "{";
payload += String(cfg._Topic); payload += "_Temp:";
payload += temperature; payload += ",";
payload += String(cfg._Topic); payload += "_Hum:";
payload += humidity;
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
if(client.publish( "v1/devices/me/telemetry", attributes ))
{
Serial.println("Published: ");
Serial.println( attributes );
}else Serial.println("Error in publishing");
}
void connectToTB()
{
Serial.print("Connecting to ThingsBoard node ...");
if ( client.connect(cfg._ClientID, cfg._TOKEN, NULL) ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
delay( 5000 );
}
}