-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstep_end.ino
More file actions
188 lines (179 loc) · 4.37 KB
/
Copy pathstep_end.ino
File metadata and controls
188 lines (179 loc) · 4.37 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#define DHTPIN D4
#define DHTTYPE DHT11
#define SSD1306_LCDHEIGHT 64
#define led D7
#define buzzer D8
// OLED display TWI address
#define OLED_ADDR 0x3C
void callback(char* subtopic, byte* payload, unsigned int payloadLength);
const char* ssid = "YOUJNTU1S12";
const char* password = "YOUJNTU1S12";
//---------DEVICE CRED----------
#define ORG "ks6fk6"
#define DEVICE_TYPE "nodemcu"
#define DEVICE_ID "MAN11"
#define TOKEN "123456789"
String command;
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char subtopic[] = "iot-2/cmd/CSM/fmt/String";
char publishtopic[] = "iot-2/evt/CSM/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
//Serial.println(clientID);
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
Adafruit_SSD1306 display(-1);
float t;
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
DHT dht(DHTPIN,DHTTYPE);
void wifiConnect();
void mqttConnect();
void initManagedDevice();
void setup() {
wifiConnect();
Serial.begin(115200);
dht.begin();
Serial.println("DHT11:");
pinMode(D5,INPUT);
pinMode(D6,INPUT);
pinMode(buzzer,OUTPUT);
pinMode(led,OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.setTextColor(WHITE);
t=dht.readTemperature();
if(isnan(t))
{
Serial.println("Failed");
display.setTextSize(1);
display.print("Failed to read t from DHT11 sensot");
display.display();
display.clearDisplay();
return ;
}
digitalWrite(buzzer,LOW);
mqttConnect();
}
void loop() {
display.setTextSize(1);
display.setCursor(0,10);
display.print("Temperature is: " +String(t));
Serial.println("Temperature is: " +String(t));
int inc=digitalRead(D5);
int dec=digitalRead(D6);
if(inc==1 && dec==1)
{
t=t;
}
else if(inc==0 && dec==1)
{
t=t+1;
}
else if(inc==1 && dec==0)
{
t=t-1;
}
else if(inc==0 && dec==0)
{
t=t;
}
if(t<5 || t>30)
{
display.clearDisplay();
display.setCursor(0,10);
display.print("ALERT");
display.setCursor(10,20);
display.print("Temperature :"+String(t));
digitalWrite(led,HIGH);
tone(buzzer,1000);
delay(500);
digitalWrite(led,LOW);
noTone(buzzer);
}
display.display();
display.clearDisplay();
PublishData(t);
if (!client.loop())
{
mqttConnect();
}
delay(100);
}
void wifiConnect() {
Serial.print("Connecting to "); Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
}
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(subtopic)) {
Serial.println("subscribe to cmd OK");
} else {
Serial.println("subscribe to cmd FAILED");
}
}
void callback(char* subtopic, byte* payload, unsigned int payloadLength) {
Serial.print("callback invoked for sub topic: ");
Serial.println(subtopic);
command="";
for (int i = 0; i < payloadLength; i++) {
//Serial.println((char)payload[i]);
command += (char)payload[i];
}
Serial.println(command);
if(command=="itemp"){
t=t+1;
Serial.println("Temperature Increased");
}
else if(command=="dtemp"){
t=t-1;
Serial.println("Temperature Decreased");
}
}
void PublishData(int command){
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\":{\"command\":";
payload += command;
payload += "}}";
Serial.print("Sending payload: ");
Serial.println(payload);
if (client.publish(publishtopic, (char*) payload.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
}