-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartWeatherMonitor.ino
More file actions
108 lines (99 loc) · 2.52 KB
/
SmartWeatherMonitor.ino
File metadata and controls
108 lines (99 loc) · 2.52 KB
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 <ESP8266WiFi.h>
#include <PubSubClient.h>
//-------- Customise these values -----------
const char* ssid = "YOU";
const char* password = "sbvizag2019";
//String command;
#include "DHT.h"
#define DHTPIN D2 // what pin we're connected to
#define LDR A0
int LDRVALUE ;
#define DHTTYPE DHT11 // define type of sensor DHT 11
DHT dht (DHTPIN, DHTTYPE);
#define ORG "5rxjjv"
#define DEVICE_TYPE "IOT"
#define DEVICE_ID "IOT0203"
#define TOKEN "17B91A0598"
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/Data/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
WiFiClient wifiClient;
PubSubClient client(server, 1883,wifiClient);
void setup() {
Serial.begin(115200);
Serial.println();
dht.begin();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
LDRVALUE=analogRead(LDR);
Serial.println(LDRVALUE);
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
delay(1000);
return;
}
PublishData(t,h,LDRVALUE);
if (!client.loop()) {
mqttConnect();
}
delay(100);
}
void mqttConnect() {
if (!client.connected()) {
Serial.print("Reconnecting MQTT client to "); Serial.println(server);
while (!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
initManagedDevice();
Serial.println();
}
}
void initManagedDevice() {
if (client.subscribe(topic)) {
Serial.println("subscribe to cmd OK");
} else {
Serial.println("subscribe to cmd FAILED");
}
}
void PublishData(float temp, float humid,int LDRVALUE){
if (!client.connected()) {
Serial.print("Reconnecting client to ");
Serial.println(server);
while (!!!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
Serial.println();
}
String payload = "{\"d\":{\"temperature\":";
payload += temp;
payload+="," "\"humidity\":";
payload += humid;
payload +="," "\" LDRVALUE\":";
payload +=LDRVALUE;
payload += "}}";
Serial.print("Sending payload: ");
Serial.println(payload);
if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
}