forked from anklimov/rotary_mqtt_dimmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTT_dimmer_02.ino
359 lines (274 loc) · 7.85 KB
/
MQTT_dimmer_02.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // MQTT
#include <phi_interfaces.h>
const char *ssid = "Smartbox"; // cannot be longer than 32 characters!
const char *pass = "topsecret "; //
// Update these with values suitable for your network.
IPAddress server(192, 168, 8, 92);
WiFiClient wclient;
PubSubClient client(wclient, server);
int lanstat=0;
//#define LEDPWM 15
#define Encoder1ChnA 5
#define Encoder1ChnB 4
#define EncoderDetent 20
#define EncoderPush 0
#define ZeroCross 14
#define POut 2
//#define MaxVal 100
#define Channels 4
//#define Step MaxVal(ch)/EncoderDetent
char mapping1[]={'U','D'}; // This is a rotary encoder so it returns U for up and D for down on the dial.
phi_rotary_encoders my_encoder1(mapping1, Encoder1ChnA, Encoder1ChnB, EncoderDetent);
multiple_button_input* dial1=&my_encoder1;
char mapping[]={'P'}; // This is a list of names for each button.
byte pins[]={EncoderPush}; // The digital pins connected to the 6 buttons.
phi_button_groups my_btns(mapping, pins, 1);
multiple_button_input* pad1=&my_btns;
int channel=0;
long dimmervalue=0;
int val[Channels]={0,0,0,60};
char* items[]={"DimmedLight","RGBLight","",""};
int itemtype[Channels]={0,12,10,11}; //0-onBoard Dimmer, 1-Ext Dimmer, 2-Switch, 12-RGBVol, 10-RGBColour, 11-RGBSaturation
char inprefix[]="/myhome/in/";
char outprefix[]="/myhome/out/";
String subscribestr="/myhome/#";
int curchan=0;
int imp=0;
int MaxVal(short ch)
{
if (itemtype[ch]==10) return 365;
return 100;
}
long k=0;
long t=0;
//uint32_t savedPS;
void zero()
{
// if (!digitalRead(ZeroCross)) exit; //falled
detachInterrupt(ZeroCross); timer1_write(43000);
//noInterrupts();
//#define TIM_DIV1 0 //80MHz (80 ticks/us - 104857.588 us max)
//#define TIM_DIV16 1 //5MHz (5 ticks/us - 1677721.4 us max) <----
//#define TIM_DIV265 3 //312.5Khz (1 tick = 3.2us - 26843542.4 us max)
if (dimmervalue==1) digitalWrite(POut,0);
else
{
digitalWrite(POut,1);
imp=0;
if (dimmervalue>0)
{
k=micros();
timer1_write(dimmervalue);
// savedPS = xt_rsil(0); // this routine will allow level 2 and above
}
}
//interrupts();
}
void thandler()
{
// xt_wsr_ps(savedPS); // restore the state
// digitalWrite(POut,0);
if ((imp==0) && dimmervalue)
{
t=micros();
digitalWrite(POut,0);
timer1_write(2000); ///2000
imp=1;
}
else
{
digitalWrite(POut,1);
imp=0;
attachInterrupt(ZeroCross,zero,ONHIGH);
}
}
void UpdateChannel(int chan,int pub)
{
char buf[64];
Serial.print("Channel: ");
Serial.println(chan);
Serial.print("Value: ");
Serial.println(val[chan]);
while (strlen(items[chan])==0) chan--;
switch (itemtype[chan])
{
int intbuf[3];
case 0:
if (val[chan]>0)
{
/// analogWrite(LEDPWM,map(val[chan],0,MaxVal(0),0,PWMRANGE));
dimmervalue=map(val[chan],0,MaxVal(0),43000,1);
Serial.println(dimmervalue);
snprintf(buf,sizeof(buf),"%d",val[chan]);
}
else
{
digitalWrite(POut,1);
dimmervalue=0;
snprintf(buf,sizeof(buf),"%d",0);
}
break;
case 10:
case 11:
case 12:
for (short i=0;i<3;i++) intbuf[itemtype[chan+i]-10]=val[chan+i];
for (short i=0;i<3;i++) if (intbuf[i]<0) intbuf[i]=0;
snprintf (buf,sizeof(buf),"%d,%d,%d",intbuf[0],intbuf[1],intbuf[2]);
}
//Serial.println(dimmervalue);
if (pub)
{
String topic=outprefix+String(items[chan]);
client.publish(topic, buf);
Serial.print("Publish: ");
Serial.print(topic);
Serial.print(":");
Serial.println(buf);
}
}
void rotary_loop()
{
char temp;
//int t;
temp=dial1->getKey(); // Use the phi_interfaces to access the same keypad
switch (temp) {
case 'U': val[channel]+=MaxVal(channel)/EncoderDetent;
break;
case 'D': val[channel]-=MaxVal(channel)/EncoderDetent;
}
if (temp!=NO_KEY)
{
// Logic_DimmableLight(LEDPWM);
Serial.print("Key: "); Serial.println(temp);
Serial.print(t);Serial.print("-");Serial.print(k);Serial.print("=");Serial.println(t-k);
if (val[channel]>MaxVal(channel)) val[channel]=MaxVal(channel);
if (val[channel]<0)
{
val[channel]=0;
}
UpdateChannel(channel,1);
Serial.println(val[channel] );
}
temp=my_btns.getKey(); // Use phi_button_groups object to access the group of buttons
if (temp=='P')
{
switch (pad1->get_status()) {
case buttons_pressed:
curchan=channel;
if ((val[0] <0)||(Channels==1)) {val[0]=-val[0]; UpdateChannel(0,1);}
else
channel++;
break;
case buttons_held:
channel=curchan;
if (val[channel]>0) {val[channel]=-val[channel]; UpdateChannel(channel,1);}
}
if (channel>=Channels) channel=0;
// Serial.println(t);
}
}
int getInt(char ** chan)
{
if (!*chan) return -1;
int ch = atoi(*chan);
*chan=strchr(*chan,',');
if (*chan) *chan+=1;
// Serial.print("Par:"); Serial.println(ch);
return ch;
}
void callback(const MQTT::Publish& pub) {
//String strPayload = String(pub.payload_string());
const char * strTopic = (pub.topic().c_str()); //const_cast<char*>
Serial.println(strTopic);
Serial.println(pub.topic().c_str());
short intopic = strncmp(strTopic,inprefix,strlen(inprefix));
short outtopic = strncmp(strTopic,outprefix,strlen(outprefix));
char subtopic[20]="";
char * t;
if (t=strrchr (strTopic,'/')) strncpy(subtopic,t+1 , 20);
Serial.print(strTopic);
Serial.print("=");
char * strPayload = const_cast<char*> (pub.payload_string().c_str());
Serial.println(strPayload);
if (!intopic || !outtopic)
{
int Par1 = getInt(&strPayload);
int Par2 = getInt(&strPayload);
int Par3 = getInt(&strPayload);
int rgb = 0;
short k;
for (short ch=0;ch<Channels;ch++)
if (!(k=strcmp(subtopic,items[ch])) || (rgb>0))
{
switch (itemtype[ch]){
case 0:
case 1:val[ch]=Par1;
//if (!intopic)
UpdateChannel(ch,0);
break;
case 10:val[ch]=Par1;
if (!k) rgb=3;
break;
case 11:val[ch]=Par2;
if (!k) rgb=3;
break;
case 12:val[ch]=Par3;
if (!k) rgb=3;
}
if (rgb)
{
rgb--;
if (/*!intopic && */!rgb) UpdateChannel(ch-2,0);
}
}
}
}
unsigned long lastMqtt = 0;
void setup() {
Serial.begin(115200);
Serial.println("start");
pinMode(POut,OUTPUT);
pinMode(ZeroCross,INPUT);
attachInterrupt(ZeroCross,zero,ONHIGH); ///RISING - really on change
timer1_isr_init();
timer1_attachInterrupt(thandler);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE); //16
//interrupts();
//analogWriteFreq(500);
//analogWriteRange(255);
//pinMode(LEDPWM, OUTPUT);
}
void loop() {
rotary_loop();
switch (lanstat) {
case 0:
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
lanstat=1;
}
break;
case 1:
if (WiFi.waitForConnectResult() == WL_CONNECTED)
{lanstat=2;
Serial.println("WiFi connected");
}
break;
case 2:
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect("arduinoClient"+WiFi.macAddress())) {
client.publish("outTopic","hello world");
client.set_callback(callback);
for (short ch=0;ch<Channels;ch++) if (strlen(items[ch])>0) UpdateChannel(ch,1);
client.subscribe(subscribestr);
}
}
}
if (client.connected())
client.loop();
}
}