-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtt_esp8266.ino
144 lines (115 loc) · 3.35 KB
/
mqtt_esp8266.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>
#include <ArduinoOTA.h>
#include <SimpleDHT.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <Hash.h>
#define USE_SERIAL Serial
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 60
#define PIN 12
int pinDHT11 = 14;
SimpleDHT11 dht11;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// Update these with values suitable for your network.
const char* ssid = "RMR";
const char* password = "rahulmr123";
const char* mqtt_server = "192.168.1.99";
WiFiClient wclient;
PubSubClient client(wclient, mqtt_server);
long lastMsg = 0;
char msg[50];
int value = 0;
#define BUFFER_SIZE 100
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
pixels.begin();
setup_wifi();
client.set_callback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(const MQTT::Publish& pub) {
Serial.print("Message arrived [");
Serial.print(pub.topic());
Serial.print("] ");
// for (int i = 0; i < length; i++) {
// Serial.print((char)payload[i]);
// }
Serial.println(pub.payload_string());
extractLEDDetails(pub.payload_string());
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("esp8266", "hello world");
// ... and resubscribe
client.subscribe("brightness");
client.subscribe("saturation");
client.subscribe("hue");
client.subscribe("LIGHTON");
client.subscribe("LIGHTOFF");
} else {
Serial.print("failed, rc=");
//Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5004);
}
}
}
unsigned long interval;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
ArduinoOTA.handle();
//600000
if (millis() - interval > 600000) {
interval = millis();
currTempHumidity();
}
}