-
Notifications
You must be signed in to change notification settings - Fork 3
/
ESP8266_Blinds_mqtt_domoticz.ino
179 lines (158 loc) · 4.99 KB
/
ESP8266_Blinds_mqtt_domoticz.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
171
172
173
174
175
176
177
178
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <Stepper2.h>
#define DEBUG
#ifdef DEBUG
# define DEBUG_LOG(x) Serial.print(x)
# define DEBUG_LOGLN(x) Serial.println(x)
#else
# define DEBUG_LOG(x)
# define DEBUG_LOGLN(x)
#endif
// Update these with values suitable for your network.
// WiFi parameters
const char* ssid = "xxx";
const char* password = "xxx";
// MQTT parameters
const char* mqtt_server = "192.168.1.zz";
const char* topic_Domoticz_IN = "domoticz/in";
const char* topic_Domoticz_OUT = "domoticz/out";
byte willQoS = 0;
char willMessage[MQTT_MAX_PACKET_SIZE+1];
boolean willRetain = false;
int device_num = 2;
int Domoticz_IDX[] = { 35, 36 };
String Device_prefix = "Living_room";
String FullNames[] = {Device_prefix + "_Left", Device_prefix + "_Right"};
int cur_state[] ={0,0}; //current device status
//stepper parameters
const int rpm = 15; // max rpm on 28BYJ-48 is ~15
int pinOut[4] = { 14, 12, 13, 15 };
Stepper2 myStepper(pinOut);
byte maxStep = 4;
boolean motor_runing = false;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
DEBUG_LOG("Connecting to ");
DEBUG_LOGLN(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
DEBUG_LOG(".");
}
randomSeed(micros());
DEBUG_LOGLN("");
DEBUG_LOGLN("WiFi connected");
DEBUG_LOGLN("IP address: ");
DEBUG_LOGLN(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
DynamicJsonBuffer jsonBuffer( MQTT_MAX_PACKET_SIZE );
String messageReceived="";
// Affiche le topic entrant - display incoming Topic
DEBUG_LOG("MQTT Message arrived [");
DEBUG_LOG(topic);
DEBUG_LOGLN("] ");
// decode payload message
for (int i = 0; i < length; i++) {
messageReceived+=((char)payload[i]);
}
// display incoming message
DEBUG_LOG(messageReceived);
if ( strcmp(topic, topic_Domoticz_OUT) == 0 ) {
JsonObject& root = jsonBuffer.parseObject(messageReceived);
if (!root.success()) {
DEBUG_LOGLN("parsing Domoticz/out JSON Received Message failed");
return;
}
int idx = root["idx"];
byte Dom_cmd = root["nvalue"];
byte Dom_level;
byte loc_dev_num ;
for (byte i=0; i < device_num; i++) {
if (idx == Domoticz_IDX[i]) {
loc_dev_num = i;
break;
}
}
if( (idx == Domoticz_IDX[0]) || (idx == Domoticz_IDX[1])){
DEBUG_LOGLN("DEV_NUM:"+String(loc_dev_num));
if(Dom_cmd == 0){ //domoticz command open blinds
Dom_level = 0; //setting level 0%
DEBUG_LOGLN("Opening blinds");
blinds_set_level(loc_dev_num, Dom_level);
DEBUG_LOGLN("Blinds are open!");
}else if(Dom_cmd == 1){ //domoticz command close blinds
Dom_level = 100; //setting level 100%
DEBUG_LOGLN("Closing blinds");
blinds_set_level(loc_dev_num, Dom_level);
DEBUG_LOGLN("Blinds are closed!");
}else if(Dom_cmd == 2){ //domoticz command set percentage values
Dom_level = root["svalue1"];
DEBUG_LOGLN("Blind starting to set to "+String(Dom_level)+"%");
blinds_set_level(loc_dev_num, Dom_level);
DEBUG_LOGLN("Blind is set to "+String(Dom_level)+"%");
}
}
}
}
void blinds_set_level(byte dev_num, byte gotoLevel){
int goToStep = map (gotoLevel, 0, 100, 0, maxStep);
if(goToStep >= cur_state[dev_num] ) {
myStepper.setDirection(1); // clock-wise
for (int tempPos = cur_state[dev_num]; tempPos < goToStep; tempPos++) {
myStepper.turn(); // one full turn
cur_state[dev_num]++;
}
}else{
myStepper.setDirection(0);
for (int tempPos = cur_state[dev_num]; tempPos > goToStep; tempPos--) {
myStepper.turn(); // one full turn
cur_state[dev_num]--;
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
DEBUG_LOG("Attempting MQTT connection...");
// Create client ID device prefix + a random number
String clientId = Device_prefix + String(random(0xffff), HEX);
// Attempt to connect
//if you MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
DEBUG_LOGLN("connected");
//once connected to MQTT broker, subscribe command if any
client.subscribe(topic_Domoticz_IN);
client.subscribe(topic_Domoticz_OUT);
} else {
DEBUG_LOGLN("failed, rc=");
DEBUG_LOG(client.state());
DEBUG_LOGLN(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(6000);
}
}
} //end reconnect()
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
myStepper.setSpeed(rpm);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
delay(100);
}