-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT_esp8266_RGB.ino
171 lines (146 loc) · 5.03 KB
/
MQTT_esp8266_RGB.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
const char* host = "esp8266-rgb";
const char* update_path = "/firmware";
const char* update_username = "admin";
const char* update_password = "admin";
const char* ssid = "ssid name";
const char* password = "ssid password";
#define MQTT_SERVER "10.10.100.14" ///YourMQTTBroker'sIP
const int mqtt_port = 1883;
const char *mqtt_user = "orangepi";
const char *mqtt_pass = "orangepi";
uint32_t ms_button = 0;
const int RED = 12;
const int GREEN = 14;
const int BLUE = 4;
const int PIN_BUTTON = 2;
int r = 0;
int g = 0;
int b = 0;
boolean button_state = false;
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
WiFiClient wifiClient;
PubSubClient client(wifiClient, MQTT_SERVER, mqtt_port);
#define BUFFER_SIZE 100
String macToStr(const uint8_t* mac){
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
// if (i < 5){result += ':';}
}
return result;
}
void callback(const MQTT::Publish& pub){
String payload = pub.payload_string();
if(String(pub.topic()) == "/home/light/rgb1/rgb")
{
//rgb(234,213,98)
String dataSt = String(payload);
r = dataSt.substring(dataSt.indexOf('(')+1).toInt();
g = dataSt.substring(dataSt.indexOf(',')+1,dataSt.lastIndexOf(',')).toInt();
b = dataSt.substring(dataSt.lastIndexOf(',')+1).toInt();
r= map(r, 0, 255, 0, 1023);
analogWrite(RED, r);
g= map(g, 0, 255, 0, 1023);
analogWrite(GREEN, g);
b= map(b, 0, 255, 0, 1023);
analogWrite(BLUE, b);
Serial.println(dataSt);
button_state = true;
client.publish("home/light/rgb1/status/","lightOn");
if (r==0 && g==0 && b==0) {client.publish("home/light/rgb1/status/","lightOff");
button_state = false;}
}
}
void re_connect() {
//attempt to connect to the wifi if connection is lost
if(WiFi.status() != WL_CONNECTED){
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
//make sure we are connected to WIFI before attemping to reconnect to MQTT
if(WiFi.status() == WL_CONNECTED){
// Loop until we're reconnected to the MQTT server
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
//if connected, subscribe to the topic(s) we want to be notified about
// if (client.connect((char*) clientName.c_str())) {
// if (client.connect(MQTT::Connect("arduinoClient2")
// .set_auth(mqtt_user, mqtt_pass)))
if (client.connect(MQTT:: Connect(clientName.c_str()).set_auth(mqtt_user, mqtt_pass))) {
Serial.println("\tMQTT Connected");
client.set_callback(callback);
client.subscribe("/home/light/rgb1/rgb");
client.subscribe("home/light/rgb1/status");
}
//otherwise print failed for debugging
else{Serial.println("\tFailed."); abort();}
}
}
}
void setup() {
pinMode (12,OUTPUT);
pinMode (14,OUTPUT);
pinMode (4,OUTPUT);
pinMode (2,INPUT_PULLUP);
Serial.begin(115200);
// Status=digitalRead(Button);
// Serial.println(Status);
re_connect();
MDNS.begin(host);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
Serial.printf("HTTPUpdateServer ready! Open http://%s.local%s in your browser and login with username '%s' and password '%s'\n", host, update_path, update_username, update_password);
}
void loop(void) {
//reconnect if connection is lost
if (!client.connected() && WiFi.status() == 3) {re_connect();}
//maintain MQTT connection
//MUST delay to allow ESP8266 WIFI functions to run
delay(200);
httpServer.handleClient();
//boolean flag = true;
// Фиксируем нажатие кнопки
uint32_t ms = millis();
// Фиксируем нажатие кнопки
if( digitalRead(PIN_BUTTON) == LOW && !button_state && ( ms - ms_button ) > 50 ){
button_state = true;
Serial.println("Press key");
analogWrite(RED, 100);
analogWrite(GREEN, 100);
analogWrite(BLUE, 100);
client.publish("home/light/rgb1/status/","lightOn");
client.publish("/home/light/rgb1/rgb","(100,100,100)");
ms_button = ms;
}
// Фиксируем отпускание кнопки
if( digitalRead(PIN_BUTTON) == LOW && button_state && ( ms - ms_button ) > 50 ){
button_state = false;
Serial.println("Press key");
analogWrite(RED, LOW);
analogWrite(GREEN, LOW);
analogWrite(BLUE, LOW);
client.publish("home/light/rgb1/status/","lightOff");
client.publish("/home/light/rgb1/rgb","(0,0,0)");
ms_button = ms;
}
client.loop();
}