Chris' Arduino Smart Home Solution working in my own house
A Modular Smart Home Solution as an alternative to KNX and Loxone! close to series-production readiness and already working in my own house
Components and supplies
piggyback for mainboard
DIN Rail B6504121
Arduino Ethernet Rev. 3
Raspberry Pi 3 Model B
Housing for Sensor board GH02KS030/010 Größe 1
sensor board
RGB Shield for Arduino
DHT22 Temperature Sensor
Echo Dot
JUNG LS5236TSM - Pushbutton sensor ( alternative to gira)
PIR Motion Sensor (generic)
Mainboard
24 V Relais ( e.g. WAGO 788-304)
Arduino Ethernet Rev. 3
Pushbutton sensor 2, 3-gang 24 V zero-voltage with inscription space
Tools and machines
Soldering iron (generic)
drilling machine
saw
Apps and platforms
Arduino IDE
https://www.openhab.org/
EAGLE
Project description
Code
JSON File for a Standard Room on Habpanel (Openhab2)
json
import to habpanel
1{ 2 "template": "<style>\ 3 button {\ 4 border: 0px solid white;\ 5 background: rgba(0,0,0,0.5);\ 6 color: white; \ 7 height: 1.5em;\ 8 width: 1.5em;\ 9 float: left;\ 10 font-size: 3em;\ 11 margin-right: 0px;\ 12 line-height: 50%;\ 13 padding: 0 0 0 0;\ 14 }\ 15</style>\ 16\ 17<!-------------------------------- Hauptlicht komplett an -------------------------------> \ 18<div ng-switch on=\\"itemValue(config.light_item)\\">\ 19 <div style=\\"border: 1px solid green;\ 20 border-radius: 15px;\ 21 display: flex;\ 22 position: absolute; \ 23 background-color:initial;\ 24 top: 0%; left: 0%; width: 50%; height: 40%; \ 25 align-items:center;\ 26 \\" ng-switch-when=\\"OFF\\">\ 27 <button \ 28 style=\\" \ 29 background-color: Transparent; \ 30 border-radius: 15px; \ 31 text-align: center;\ 32 width: 100%; height: 100%;\ 33 \\"\ 34 ng-click=\\"sendCmd(config.light_item, 'ON')\\">\ 35 <img src=\\"/static/whatever/licht_off.png\\" height=\\"80%\\" />\ 36 </button>\ 37 </div> \ 38 <div style=\\"border: 1px solid green;\ 39 border-radius: 15px;\ 40 display: flex;\ 41 position: absolute; \ 42 background-color:initial;\ 43 top: 0%; left: 0%; width: 50%; height: 40%; \ 44 align-items:center;\ 45 \\" ng-switch-when=\\"ON\\">\ 46 <button \ 47 style=\\" \ 48 background-color: Transparent; \ 49 border-radius: 15px; \ 50 text-align: center;\ 51 width: 100%; height: 100%;\ 52 \\"\ 53 ng-click=\\"sendCmd(config.light_item, 'OFF')\\">\ 54 <img src=\\"/static/whatever/licht_on.png\\" height=\\"80%\\" />\ 55 </button>\ 56 </div> \ 57 \ 58 <div style=\\"border: 1px solid green;\ 59 border-radius: 15px;\ 60 display: flex;\ 61 position: absolute; \ 62 background-color:initial;\ 63 top: 0%; left: 0%; width: 50%; height: 40%; \ 64 align-items:center;\ 65 \\" ng-switch-default>\ 66 <button \ 67 style=\\" \ 68 background-color: Transparent; \ 69 border-radius: 15px; \ 70 text-align: center;\ 71 width: 100%; height: 100%;\ 72 \\"\ 73 ng-click=\\"sendCmd(config.light_item, 'ON')\\">\ 74 <img src=\\"/static/whatever/licht_off.png\\" height=\\"80%\\" />\ 75 </button>\ 76 </div>\ 77</div>\ 78<!-------------------------------- Hauptlicht komplett an -------------------------------> \ 79\ 80<!-------------------------------- LED Spots komplett an -------------------------------> \ 81<div ng-switch on=\\"itemValue(config.led_light_item)\\">\ 82 <div style=\\"border: 1px solid green;\ 83 border-radius: 15px;\ 84 display: flex;\ 85 position: absolute; \ 86 background-color:initial;\ 87 top: 0%; left: 50%; width: 50%; height: 40%; \ 88 align-items:center;\ 89 \\" ng-switch-when=\\"OFF\\">\ 90 <button \ 91 style=\\" \ 92 background-color: Transparent; \ 93 border-radius: 15px; \ 94 text-align: center;\ 95 width: 100%; height: 100%;\ 96 \\"\ 97 ng-click=\\"sendCmd(config.led_light_item, 'ON')\\">\ 98 <img src=\\"/static/whatever/light_diffused2.png\\" height=\\"80%\\" />\ 99 </button>\ 100 </div> \ 101 <div style=\\"border: 1px solid green;\ 102 border-radius: 15px;\ 103 display: flex;\ 104 position: absolute; \ 105 background-color:initial;\ 106 top: 0%; left: 50%; width: 50%; height: 40%; \ 107 align-items:center;\ 108 \\" ng-switch-when=\\"ON\\">\ 109 <button \ 110 style=\\" \ 111 background-color: Transparent; \ 112 border-radius: 15px; \ 113 text-align: center;\ 114 width: 100%; height: 100%;\ 115 \\"\ 116 ng-click=\\"sendCmd(config.led_light_item, 'OFF')\\">\ 117 <img src=\\"/static/whatever/licht_on.png\\" height=\\"80%\\" />\ 118 </button>\ 119 </div> \ 120 \ 121 <div style=\\"border: 1px solid green;\ 122 border-radius: 15px;\ 123 display: flex;\ 124 position: absolute; \ 125 background-color:initial;\ 126 top: 0%; left: 50%; width: 50%; height: 40%; \ 127 align-items:center;\ 128 \\" ng-switch-default>\ 129 <button \ 130 style=\\" \ 131 background-color: Transparent; \ 132 border-radius: 15px; \ 133 text-align: center;\ 134 width: 100%; height: 100%;\ 135 \\"\ 136 ng-click=\\"sendCmd(config.led_light_item, 'ON')\\">\ 137 <img src=\\"/static/whatever/light_diffused2.png\\" height=\\"80%\\" />\ 138 </button>\ 139 </div>\ 140</div>\ 141<!-------------------------------- LED Spots komplett an -------------------------------> \ 142\ 143\ 144<!------------------------------------------------------------------------------- LED Licht Spots \ 145 \ 146<div style=\\"border: 1px solid black;\ 147 border-radius: 15px;\ 148 display: flex;\ 149 position: absolute; \ 150 background-color:initial;\ 151 top: 0%; left: 50%; width: 50%; height: 40%; \ 152 align-items:center;\ 153 \ \ \ \ \\"> \ 154 \ \ \ \ \ \ <button \ 155\ \ \ \ \ \ \ \ style=\\" \ 156 \ \ background-color: Transparent; \ 157 \ \ border-radius: 15px; \ 158\ \ \ \ \ \ \ \ \ text-align: center;\ 159 \ \ width: 100%; height: 100%;\ 160\ \ \ \ \ \ \ \ \\"\ 161 \ ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 162 <img src=\\"/static/whatever/light_diffused2.png\\" width=\\"100%\\" />\ 163 \ \ \ </button>\ 164</div>\ 165-->\ 166\ 167<!------------------------------------------------------------------------------- Temperature -->\ 168\ 169<div style=\\"border: 1px solid red;\ 170 border-radius: 15px;\ 171 display: flex;\ 172 position: absolute; \ 173 background-color:initial;\ 174 top: 40%; left: 0%; width: 50%; height: 20%; \ 175 align-items:center;\ 176 \ \ \ \ \\"> \ 177\ \ \ \ \ \ <img src=\\"/static/whatever/temp_temperatur.png\\" height=\\"90%\\" /> \ 178 <span style=\\"color: cyan; font-size: 14pt\\"> {{itemValue(config.temperature_item)}} </span>\ 179</div> \ 180\ 181<!------------------------------------------------------------------------------- Temperature -->\ 182\ 183\ 184\ 185<!------------------------------------------------------------------------------ Feuchtigkeit -->\ 186<div style=\\"border: 1px solid red;\ 187 border-radius: 15px;\ 188 display: flex;\ 189 position: absolute; \ 190 background-color:initial;\ 191 top: 60%; left: 0%; width: 50%; height: 20%; \ 192 align-items:center;\ 193\ \ \ \ \ \ align:center;\ 194 \ \ \ \ \\"> \ 195\ \ \ \ \ \ <img src=\\"/static/whatever/wetter_feuchtigkeit2.png\\" height=\\"90%\\" />\ 196 <span style=\\"color: cyan; font-size: 14pt\\"> {{itemValue(config.humidity_item)}} </span>\ 197\ \ \ \ \ \ \ 198</div> \ 199<!------------------------------------------------------------------------------- Feuchtigkeit --> \ 200\ 201\ 202<!------------------------------------------------------------------------------- tbd -->\ 203<div style=\\"border: 1px solid yellow;\ 204 border-radius: 15px;\ 205 display: flex;\ 206 position: absolute; \ 207 background-color:initial;\ 208 top: 40%; left: 50%; width: 50%; height: 40%; \ 209 align-items:center;\ 210 \ \ \ \ \\"> \ 211 tbd\ 212</div> \ 213<!------------------------------------------------------------------------------- tbd -->\ 214\ 215</div> \ 216\ 217\ 218\ 219\ 220<!-------------------------------- Shutter Up -------------------------------> \ 221<div ng-switch on=\\"itemValue(config.shutter_up_item)\\">\ 222<div align=\\"center\\" style=\\"border: 1px solid green;\ 223 border-radius: 15px;\ 224 display: flex;\ 225 position: absolute; \ 226 background-color:initial;\ 227 top: 80%; left: 0%; width: 33%; height: 20%; \ 228 align-items:center;\ 229\ \ \ \ \ \ align: center;\ 230\ \ \ \ \ text-align: center; \ 231 \ \ \ \ \\"\ 232 ng-switch-when=\\"OFF\\" >\ 233 <button \ 234 style=\\" \ 235 background-color: Transparent; \ 236 border-radius: 15px; \ 237 text-align: center;\ 238 width: 100%; height: 100%;\ 239 \\"\ 240 ng-click=\\"sendCmd(config.shutter_up_item, 'ON')\\">\ 241 \ \ \ \ \ \ \ <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 242 \ \ \ </button>\ 243 </div> \ 244 <div align=\\"center\\" style=\\"border: 1px solid green;\ 245 border-radius: 15px;\ 246 display: flex;\ 247 position: absolute; \ 248 background-color:initial;\ 249 top: 80%; left: 0%; width: 33%; height: 20%; \ 250 align-items:center;\ 251\ \ \ \ \ \ align: center;\ 252\ \ \ \ \ text-align: center; \\" \ 253 ng-switch-when=\\"ON\\">\ 254\ \ \ \ \ \ \ \ 255 \ \ \ \ <button \ 256 style=\\" \ 257 background-color: DeepSkyBlue3; \ 258 border-radius: 15px; \ 259 text-align: center;\ 260 width: 100%; height: 100%;\ 261 \\"\ 262 ng-click=\\"sendCmd(config.shutter_up_item, 'OFF')\\">\ 263 <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 264 </button>\ 265 </div> \ 266 \ 267 <div align=\\"center\\" style=\\"border: 1px solid green;\ 268 border-radius: 15px;\ 269 display: flex;\ 270 position: absolute; \ 271 background-color:initial;\ 272 top: 80%; left: 0%; width: 33%; height: 20%; \ 273 align-items:center;\ 274\ \ \ \ \ \ align: center;\ 275\ \ \ \ \ text-align: center; \\" \ 276 ng-switch-default>\ 277 <button \ 278 style=\\" \ 279 background-color: Transparent; \ 280 border-radius: 15px; \ 281 text-align: center;\ 282 width: 100%; height: 100%;\ 283 \\"\ 284 ng-click=\\"sendCmd(config.shutter_up_item, 'ON')\\">\ 285 <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 286\ \ \ \ \ \ </button>\ 287 </div>\ 288</div> \ 289<!-------------------------------- Shutter Up -------------------------------> \ 290 \ 291\ 292\ 293<div align=\\"center\\" style=\\"border: 1px solid blue;\ 294 border-radius: 15px;\ 295 display: flex;\ 296 position: absolute; \ 297 background-color:initial;\ 298 top: 80%; left: 33%; width: 33%; height: 20%; \ 299 align-items:center;\ 300\ \ \ \ \ \ align: center;\ 301\ \ \ \ \ text-align: center; \ 302 \ \ \ \ \\"> \ \ \ \ 303\ \ <button style=\\" \ 304\ \ \ background-color: Transparent; \ 305\ \ \ border-radius: 15px; \ 306\ \ \ text-align: center;\ 307 width: 100%; height: 100%;\ 308\ \ \ \\"\ 309\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'CLOSED' && itemValue(config.w_tilt_item) == 'CLOSED' \\";\ \ \ \ \ \ \ \ 310\ \ \ >\ 311\ \ <img src=\\"/static/whatever/fts_fenster.png\\" height=\\"60%\\" />\ 312 </button>\ 313\ <button style=\\" \ 314\ \ \ background-color: #FFBF00; \ 315\ \ \ border-radius: 15px; \ 316\ \ \ text-align: center;\ 317 width: 100%; height: 100%;\ 318\ \ \ \\"\ 319\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'OPEN' && itemValue(config.w_tilt_item) == 'CLOSED' \\";\ \ \ \ \ \ \ \ 320\ \ \ >\ 321\ \ <img src=\\"/static/whatever/fts_fenster_gekippt.png\\" height=\\"60%\\" />\ 322 </button>\ 323\ <button style=\\" \ 324\ \ \ background-color: #FF4000; \ 325\ \ \ border-radius: 15px; \ 326\ \ \ text-align: center;\ 327 width: 100%; height: 100%;\ 328\ \ \ \\"\ 329\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'OPEN' && itemValue(config.w_tilt_item) == 'OPEN' \\";\ \ \ \ \ \ \ \ 330\ \ \ >\ 331\ \ <img src=\\"/static/whatever/fts_fenster_offen.png\\" height=\\"60%\\" />\ 332 </button>\ 333\ <button style=\\" \ 334\ \ \ background-color: Transparent; \ 335\ \ \ border-radius: 15px; \ 336\ \ \ text-align: center;\ 337 width: 100%; height: 100%;\ 338\ \ \ \\"\ 339\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'CLOSED' && itemValue(config.w_tilt_item) == 'OPEN' \\";\ \ \ \ \ \ \ \ 340\ \ \ >\ 341\ \ <img src=\\"/static/whatever/fts_fenster_offen.png\\" height=\\"60%\\" />\ 342 </button>\ 343</div>\ 344\ 345\ 346\ 347<!-------------------------------- Shutter down -------------------------------> \ 348<div ng-switch on=\\"itemValue(config.shutter_down_item)\\">\ 349<div align=\\"center\\" style=\\"border: 1px solid green;\ 350 border-radius: 15px;\ 351 display: flex;\ 352 position: absolute; \ 353 background-color:initial;\ 354 top: 80%; left: 66%; width: 33%; height: 20%; \ 355 align-items:center;\ 356\ \ \ \ \ \ align: center;\ 357\ \ \ \ \ text-align: center; \ 358 \ \ \ \ \\"\ 359 ng-switch-when=\\"OFF\\" >\ 360 <button \ 361 style=\\" \ 362 background-color: Transparent; \ 363 border-radius: 15px; \ 364 text-align: center;\ 365 width: 100%; height: 100%;\ 366 \\"\ 367 ng-click=\\"sendCmd(config.shutter_down_item, 'ON')\\">\ 368 \ \ \ \ \ \ \ <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 369 \ \ \ </button>\ 370 </div> \ 371 <div align=\\"center\\" style=\\"border: 1px solid green;\ 372 border-radius: 15px;\ 373 display: flex;\ 374 position: absolute; \ 375 background-color:initial;\ 376 top: 80%; left: 66%; width: 33%; height: 20%; \ 377 align-items:center;\ 378\ \ \ \ \ \ align: center;\ 379\ \ \ \ \ text-align: center; \\" \ 380 ng-switch-when=\\"ON\\">\ 381\ \ \ \ \ \ \ \ 382 \ \ \ \ <button \ 383 style=\\" \ 384 background-color: DeepSkyBlue3; \ 385 border-radius: 15px; \ 386 text-align: center;\ 387 width: 100%; height: 100%;\ 388 \\"\ 389 ng-click=\\"sendCmd(config.shutter_down_item, 'OFF')\\">\ 390 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 391 </button>\ 392 </div> \ 393 \ 394 <div align=\\"center\\" style=\\"border: 1px solid green;\ 395 border-radius: 15px;\ 396 display: flex;\ 397 position: absolute; \ 398 background-color:initial;\ 399 top: 80%; left: 66%; width: 33%; height: 20%; \ 400 align-items:center;\ 401\ \ \ \ \ \ align: center;\ 402\ \ \ \ \ text-align: center; \\" \ 403 ng-switch-default>\ 404 <button \ 405 style=\\" \ 406 background-color: Transparent; \ 407 border-radius: 15px; \ 408 text-align: center;\ 409 width: 100%; height: 100%;\ 410 \\"\ 411 ng-click=\\"sendCmd(config.shutter_down_item, 'ON')\\">\ 412 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 413\ \ \ \ \ \ </button>\ 414 </div>\ 415</div> \ 416<!-------------------------------- Shutter down -------------------------------> \ 417 \ 418\ 419\ 420\ 421\ 422\ 423\ 424\ 425\ 426\ 427\ 428\ 429\ 430\ 431\ 432\ 433\ 434\ 435\ 436\ 437\ 438\ 439\ 440\ 441\ 442\ 443\ 444\ 445\ 446\ 447\ 448<!------------------------------------------------------------------------------- Rolladen Gartenseite -->\ 449<!--\ 450<div align=\\"center\\" style=\\"border: 1px solid green;\ 451 border-radius: 15px;\ 452 display: flex;\ 453 position: absolute; \ 454 background-color:initial;\ 455 top: 80%; left: 0%; width: 100%; height: 20%; \ 456 align-items:center;\ 457\ \ \ \ \ \ align: center;\ 458\ \ \ \ \ text-align: center; \ 459 \ \ \ \ \\"> \ \ \ \ 460\ \ \ <button style=\\" \ 461 \ \ background-color: Transparent; \ \ \ \ 462\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 463\ \ \ \ \ \ \ \ \ text-align: center;\ 464 \ \ width: 100%; height: 100%;\ 465\ \ \ \ \ \ \ \ \\"\ 466\ \ \ \ \ \ \ ng-click=\\"sendCmd(config.blind_item, 'UP')\\">\ 467 \ \ \ <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 468 </button> \ 469 <button style=\\" \ 470 \ \ background-color: Transparent; \ 471\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 472\ \ \ \ \ \ \ \ \ text-align: center;\ 473 \ \ width: 100%; height: 100%;\ 474\ \ \ \ \ \ \ \ \\"\ 475\ \ \ \ \ \ \ ng-if=\\"itemValue(config.w_contact_item) == 'STOP'\\";\ \ \ \ \ \ \ \ 476\ \ \ \ \ \ \ ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 477\ \ \ <img src=\\"/static/whatever/wetter_.png\\" height=\\"90%\\" />\ 478 </button>\ 479 <button style=\\" \ 480 \ \ background-color: Transparent; \ 481\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 482\ \ \ \ \ \ \ \ \ text-align: center;\ 483 \ \ width: 100%; height: 100%;\ 484\ \ \ \ \ \ \ \ \\"\ 485\ \ \ \ \ \ \ ng-if=\\"itemValue(config.w_contact_item) != 'STOP'\\" || \\"itemValue(config.w_contact_item) != 'UP'\\" ;\ \ \ \ \ \ \ \ 486\ \ \ \ \ \ \ ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 487\ \ \ <img src=\\"/static/whatever/fts_fenster.png\\" height=\\"60%\\" />\ 488 </button>\ 489\ 490 <button style=\\"\ background-color: Transparent; \ 491\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 492\ \ \ \ \ \ \ \ \ margin-right: 0;\ \ \ \ \ \ \ \ \ \ 493\ \ \ \ \ \ \ \ \ text-align: center;\ 494 \ \ width: 100%; height: 100%;\\" \ 495 ng-click=\\"sendCmd(config.blind_item, 'DOWN')\\">\ 496 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 497 </button> \ 498</div> \ 499--> \ 500\ 501<!-- rechts 10 % 100% --> \ 502<!--\ 503<div style=\\"border: 1px solid cyan;\ 504 border-radius: 15px;\ 505 display: flex;\ 506 position: absolute; \ 507 background-color:initial;\ 508 top: 0%; left: 80%; width: 20%; height: 80%; \ 509 align-items:center;\ 510 \ \ \ \ \\" ng-init=\\"model={ name: 'My dummy', item: 'My_Item', ... }\\">> \ 511\ 512 <p>\ 513 <button ng-click=\\"sendCmd(config.blind_item, 'UP')\\">\ 514 <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 515 </button> \ 516 \ 517 <br />\ 518 <button ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 519 <widget-icon iconset=\\"'eclipse-smarthome-classic'\\" icon=\\"'blinds-20'\\" size=\\"45\\" state=\\"itemValue(config.blind_item)\\" />\ 520 </button>\ 521\ 522 \ 523 \ 524 <br /> \ 525 <button ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 526 <img src=\\"/static/whatever/control_arrow_down.png\\" height=\\"100%\\" />\ 527\ 528 </button>\ 529 \ 530 <br /> \ 531 <button style=\\"margin-right: 0\\" ng-click=\\"sendCmd(config.blind_item, 'DOWN')\\">\ 532 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 533 </button>\ 534\ 535</div> \ 536-->", 537 "name": "Standard Room", 538 "author": "Christian Kreidel", 539 "description": "", 540 "settings": [ 541 { 542 "type": "item", 543 "id": "light_item", 544 "description": "light_item)", 545 "default": "light_item)", 546 "label": "light_item)" 547 }, 548 { 549 "type": "item", 550 "id": "led_light_item", 551 "label": "led_light_item", 552 "default": "led_light_item", 553 "description": "led_light_item" 554 }, 555 { 556 "type": "item", 557 "id": "temperature_item", 558 "label": "temperature_item", 559 "default": "temperature_item", 560 "description": "temperature_item" 561 }, 562 { 563 "type": "item", 564 "id": "humidity_item", 565 "label": "humidity_item", 566 "default": "humidity_item", 567 "description": "humidity_item" 568 }, 569 { 570 "type": "item", 571 "id": "shutter_up_item", 572 "label": "shutter_up_item", 573 "default": "shutter_up_item", 574 "description": "shutter_up_item" 575 }, 576 { 577 "type": "item", 578 "id": "shutter_down_item", 579 "label": "shutter_down_item", 580 "default": "shutter_down_item", 581 "description": "shutter_down_item" 582 }, 583 { 584 "type": "item", 585 "id": "w_open_item", 586 "label": "w_open_item", 587 "default": "w_open_item", 588 "description": "w_open_item" 589 }, 590 { 591 "type": "item", 592 "id": "w_tilt_item", 593 "label": "w_tilt_item", 594 "default": "w_tilt_item", 595 "description": "w_tilt_item" 596 } 597 ] 598}
Openhab2 rules for Standard Room
json
some rules to control light and blinds by pushbutton and/or hanpanel standard room widget.
1var Shutter_state = 0 2 3// each time a button is used it poublish a ON value on its topic 4// The status of the Relais is read to toggle it by each keystroke 5//// 6rule "EG_GZ_Licht_Toggle_per_ON" 7when 8 Item EG_GZ_T0 received command ON 9then 10 logInfo("eg_gz", "eg_gz licht change") 11 if (EG_GZ_R6.state == ON) { // Relais status is on 12 sendCommand(EG_GZ_R6, OFF) // switch Relais off 13 sendCommand(EG_GZ_T0, OFF) // reset button to off 14 logDebug("eg_gz", "eg_gz light off") 15 logInfo("eg_gz", "eg_gz light off") 16 } 17 else 18 { 19 if (EG_GZ_R6.state == OFF) { // Relais is off 20 sendCommand(EG_GZ_R6, ON) // relais on 21 logDebug("eg_gz", "eg_gz light on") 22 logInfo("eg_gz", "eg_gz light on") 23 } 24 else { 25 sendCommand(EG_GZ_R6, ON) // relais status not initialised 26 logDebug("eg_gz", "light on") 27 logInfo("eg_gz", "light on") 28 29 } 30 } 31end 32 33 34// 35rule "EG_GZ_Lighting_off" 36when 37 Item EG_GZ_T0 received command OFF 38then 39 logInfo("eg_gz", "eg_gz licht cahnged") 40 sendCommand(EG_GZ_R6, OFF) 41 logDebug("eg_gz", "eg_gz light off") 42 logInfo("eg_gz", "eg_gz light off") 43end 44 45 46rule "EG_GZ_Blinds_Up" 47when 48 Item EG_GZ_T2 received command ON 49then 50 logInfo("eg_gz", "eg_gz blind up or stop") 51 logInfo("eg_gz",receivedCommand.toString) 52 logInfo("eg_gz",EG_GZ_Rolladen.state.toString) 53 if (Shutter_state == 1 ) // shutter is moving 54 { 55 Shutter_state = 0; // save new status stop 56 sendCommand(EG_GZ_Rolladen, STOP) // stop blind item 57 sendCommand(EG_GZ_T4, OFF) // reset button status 58 sendCommand(EG_GZ_T2, OFF) // reset button status 59 logInfo("eg_gz","Rolladen changed from moving to stop") 60 } 61 else // state is stop 62 { 63 Shutter_state = 1; // new status is up 64 sendCommand(EG_GZ_Rolladen, UP) // start moving up 65 logInfo("eg_gz","Rolladen changed to up") 66 } 67end 68 69 70rule "EG_GZ_Rolladen_down" 71when 72 Item EG_GZ_T4 received command ON 73then 74 logInfo("eg_gz", "eg_gz Rollo down or stop") 75 logInfo("eg_gz",receivedCommand.toString) 76 logInfo("eg_gz",EG_GZ_Rolladen.state.toString) 77 if (Shutter_state == 2 ) // currently moving down 78 { 79 Shutter_state = 0; // new status is stop 80 sendCommand(EG_GZ_T4, OFF) // reset button status 81 sendCommand(EG_GZ_T2, OFF) // reset button status 82 sendCommand(EG_GZ_Rolladen, STOP) // stop blind item 83 logInfo("eg_gz","Rolladen changed from moving to stop") 84 } 85 else // state is stop 86 { 87 Shutter_state = 2; // new status is moding down 88 sendCommand(EG_GZ_Rolladen, DOWN) // start moving down 89 logInfo("eg_gz","Rolladen changed to down") 90 } 91end 92 93// these rules are needed because openhab panel transmits on and of values when button is pushed 94 95rule "EG_GZ_Rolladen_off_button_up" 96when 97 Item EG_GZ_T2 received command OFF 98then 99 Shutter_state = 0; 100 sendCommand(EG_GZ_Rolladen, STOP) 101 logInfo("eg_gz","Rolladen changed from moving to stop") 102end 103 104rule "EG_GZ_Rolladen_off_button_down" 105when 106 Item EG_GZ_T4 received command OFF 107then 108 Shutter_state = 0; 109 sendCommand(EG_GZ_Rolladen, STOP) 110 logInfo("eg_gz","Rolladen changed from moving to stop") 111end 112 113
Openhab 2 Items file to use with Mainboard Sketch
json
Item file to usw with the mainboard sketch
1// PAramter 2String EG_GZ_Vers "EG_GZ_Vers [%s] " {mqtt="<[mosquitto:/Haus/EG/GZ/Vers:state:default]"} 3String EG_GZ_UpdateTime "EG_GZ_UpdateTime [%s] " {mqtt="<[mosquitto:/Haus/EG/GZ/UpdateTime:state:default]"} 4 5 6// Sensoren Raum A 7Number EG_GZ_DHT_Temp "Guest Room Thermostat [%.2f] C" <temperature> ["CurrentTemperature"] {mqtt="<[mosquitto:/Haus/EG/GZ/A_DHT_Temp:state:default]"} 8Number EG_GZ_DHT_Humi "Humidity Guest Room [%.2f] %" ["CurrentHumidity"] {mqtt="<[mosquitto:/Haus/EG/GZ/A_DHT_Humi1:state:default]"} 9Number EG_GZ_DS_Temp "EG_GZ_DS_Temp [%.2f] C" <temperature> ["CurrentTemperature"] {mqtt="<[mosquitto:/Haus/EG/GZ/DS_Temp:state:default]"} 10Number EG_GZ_DS_V1 "EG_GZ_DS_V1 [%.2f] V" {mqtt="<[mosquitto:/Haus/EG/GZ/V1:state:default]"} 11Number EG_GZ_DS_V2 "EG_GZ_DS_V2 [%.2f] V" {mqtt="<[mosquitto:/Haus/EG/GZ/V2:state:default]"} 12Switch EG_GZ_A_B "Motion_A" {mqtt="<[mosquitto:/Haus/EG/GZ/A_B:command:ON:1],<[mosquitto:/Haus/EG/GZ/A_B:command:OFF:0]"} 13 14// Sensoren Raum B 15Switch EG_GZ_B_B "Motion_B" {mqtt="<[mosquitto:/Haus/EG/GZ/B_B:command:ON:1],<[mosquitto:/Haus/EG/GZ/B_B:command:OFF:0]"} 16 17 18 19// Taster 20Switch EG_GZ_T0 "Licht Gaestezimmer" ["Lighting"] {mqtt="<[mosquitto:/Haus/EG/GZ/T0:command:ON:1],<[mosquitto:/Haus/EG/GZ/T0:command:OFF:2]"} 21Switch EG_GZ_T1 "EG_GZ_T1" ["Lighting"] {mqtt="<[mosquitto:/Haus/EG/GZ/T1:command:ON:1],<[mosquitto:/Haus/EG/GZ/T1:command:OFF:2]"} 22Switch EG_GZ_T2 "EG_GZ_T2"["Lighting"] {mqtt="<[mosquitto:/Haus/EG/GZ/T2:command:ON:1],<[mosquitto:/Haus/EG/GZ/T2:command:OFF:2]"} 23Switch EG_GZ_T3 "EG_GZ_T3" {mqtt="<[mosquitto:/Haus/EG/GZ/T3:command:ON:1],<[mosquitto:/Haus/EG/GZ/T3:command:OFF:2]"} 24Switch EG_GZ_T4 "EG_GZ_T4" {mqtt="<[mosquitto:/Haus/EG/GZ/T4:command:ON:1],<[mosquitto:/Haus/EG/GZ/T4:command:OFF:2]"} 25Switch EG_GZ_T5 "EG_GZ_T5" {mqtt="<[mosquitto:/Haus/EG/GZ/T5:command:ON:1],<[mosquitto:/Haus/EG/GZ/T5:command:OFF:2]"} 26 27Switch EG_GZ_T6 "EG_GZ_T6" {mqtt="<[mosquitto:/Haus/EG/GZ/T6:command:ON:1],<[mosquitto:/Haus/EG/GZ/T6:command:OFF:2]"} 28Switch EG_GZ_T7 "EG_GZ_T7" {mqtt="<[mosquitto:/Haus/EG/GZ/T7:command:ON:1],<[mosquitto:/Haus/EG/GZ/T7:command:OFF:2]"} 29Switch EG_GZ_T8 "EG_GZ_T8" {mqtt="<[mosquitto:/Haus/EG/GZ/T8:command:ON:1],<[mosquitto:/Haus/EG/GZ/T8:command:OFF:2]"} 30Switch EG_GZ_T9 "EG_GZ_T9" {mqtt="<[mosquitto:/Haus/EG/GZ/T9:command:ON:1],<[mosquitto:/Haus/EG/GZ/T9:command:OFF:2]"} 31Switch EG_GZ_T10 "EG_GZ_T10" {mqtt="<[mosquitto:/Haus/EG/GZ/T10:command:ON:1],<[mosquitto:/Haus/EG/GZ/T10:command:OFF:2]"} 32Switch EG_GZ_T11 "EG_GZ_T11" {mqtt="<[mosquitto:/Haus/EG/GZ/T11:command:ON:1],<[mosquitto:/Haus/EG/GZ/T11:command:OFF:2]"} 33Switch EG_GZ_T12 "EG_GZ_T2" {mqtt="<[mosquitto:/Haus/EG/GZ/T12:command:ON:1],<[mosquitto:/Haus/EG/GZ/T12:command:OFF:2]"} 34Switch EG_GZ_T13 "EG_GZ_T13" {mqtt="<[mosquitto:/Haus/EG/GZ/T13:command:ON:1],<[mosquitto:/Haus/EG/GZ/T13:command:OFF:2]"} 35Switch EG_GZ_T14 "EG_GZ_T14" {mqtt="<[mosquitto:/Haus/EG/GZ/T13:command:ON:1],<[mosquitto:/Haus/EG/GZ/T14:command:OFF:2]"} 36Switch EG_GZ_T15 "EG_GZ_T15" {mqtt="<[mosquitto:/Haus/EG/GZ/T15:command:ON:1],<[mosquitto:/Haus/EG/GZ/T15:command:OFF:2]"} 37// Aktoren 38Switch EG_GZ_R9 " Gest Room Lighting" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R9:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R9:command:OFF:0]"} 39Switch EG_GZ_R10 "Guest Room Socket" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R10:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R10:command:OFF:0]"} 40Switch EG_GZ_R11 "Guest Room tbd1 " <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R11:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R11:command:OFF:0]"} 41Switch EG_GZ_R12 "Guest Room tbd2" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R12:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R12:command:OFF:0]"} 42Switch EG_GZ_R13 "Guest Room tbd3" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R13:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R13:command:OFF:0]"} 43Switch EG_GZ_R14 "Guest Room tbd4" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R14:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R14:command:OFF:0]"} 44Switch EG_GZ_R15 "Guest Room tbd5" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R15:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R15:command:OFF:0]"} 45Switch EG_GZ_R16 "Guest Room Heating" {mqtt=">[mosquitto:/Haus/EG/GZ/R16:command:ON:1],>[mosquitto:/Haus/EG/GZ/R16:command:OFF:0]"} 46 47 48Rollershutter EG_GZ_Rolladen "Guest Room Blind" <rollershutter> (gScatterEG_S1) ["Switchable"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/F1:command:UP:2],>[mosquitto:/Haus/EG/WZ_Kue/F1:command:DOWN:1],>[mosquitto:/Haus/EG/WZ_Kue/F1:command:STOP:0],>[mosquitto:/Haus/EG/WZ_Kue/F1:command:MOVE:3]"} 49Switch Roll_Hoch "Hoch" ["Lighting"] 50Switch Roll_Runter "Runter" ["Lighting"] 51 52 53 54// W_O : 55// W_K : kipp 56Contact EG_GZ_W_O "WinContactOpen" {mqtt="<[mosquitto:/Haus/EG/GZ/A_W1:state:OPEN:1],<[mosquitto:/Haus/EG/GZ/A_W1:state:CLOSED:0]"} 57Contact EG_GZ_W_K "WinContactTilted" {mqtt="<[mosquitto:/Haus/EG/GZ/A_W2:state:OPEN:1],<[mosquitto:/Haus/EG/GZ/A_W2:state:CLOSED:0]"} 58
JSON File for a Standard Room on Habpanel (Openhab2)
json
import to habpanel
1{ 2 "template": "<style>\ 3 button {\ 4 border: 0px solid white;\ 5 background: rgba(0,0,0,0.5);\ 6 color: white; \ 7 height: 1.5em;\ 8 width: 1.5em;\ 9 float: left;\ 10 font-size: 3em;\ 11 margin-right: 0px;\ 12 line-height: 50%;\ 13 padding: 0 0 0 0;\ 14 }\ 15</style>\ 16\ 17<!-------------------------------- Hauptlicht komplett an -------------------------------> \ 18<div ng-switch on=\\"itemValue(config.light_item)\\">\ 19 <div style=\\"border: 1px solid green;\ 20 border-radius: 15px;\ 21 display: flex;\ 22 position: absolute; \ 23 background-color:initial;\ 24 top: 0%; left: 0%; width: 50%; height: 40%; \ 25 align-items:center;\ 26 \\" ng-switch-when=\\"OFF\\">\ 27 <button \ 28 style=\\" \ 29 background-color: Transparent; \ 30 border-radius: 15px; \ 31 text-align: center;\ 32 width: 100%; height: 100%;\ 33 \\"\ 34 ng-click=\\"sendCmd(config.light_item, 'ON')\\">\ 35 <img src=\\"/static/whatever/licht_off.png\\" height=\\"80%\\" />\ 36 </button>\ 37 </div> \ 38 <div style=\\"border: 1px solid green;\ 39 border-radius: 15px;\ 40 display: flex;\ 41 position: absolute; \ 42 background-color:initial;\ 43 top: 0%; left: 0%; width: 50%; height: 40%; \ 44 align-items:center;\ 45 \\" ng-switch-when=\\"ON\\">\ 46 <button \ 47 style=\\" \ 48 background-color: Transparent; \ 49 border-radius: 15px; \ 50 text-align: center;\ 51 width: 100%; height: 100%;\ 52 \\"\ 53 ng-click=\\"sendCmd(config.light_item, 'OFF')\\">\ 54 <img src=\\"/static/whatever/licht_on.png\\" height=\\"80%\\" />\ 55 </button>\ 56 </div> \ 57 \ 58 <div style=\\"border: 1px solid green;\ 59 border-radius: 15px;\ 60 display: flex;\ 61 position: absolute; \ 62 background-color:initial;\ 63 top: 0%; left: 0%; width: 50%; height: 40%; \ 64 align-items:center;\ 65 \\" ng-switch-default>\ 66 <button \ 67 style=\\" \ 68 background-color: Transparent; \ 69 border-radius: 15px; \ 70 text-align: center;\ 71 width: 100%; height: 100%;\ 72 \\"\ 73 ng-click=\\"sendCmd(config.light_item, 'ON')\\">\ 74 <img src=\\"/static/whatever/licht_off.png\\" height=\\"80%\\" />\ 75 </button>\ 76 </div>\ 77</div>\ 78<!-------------------------------- Hauptlicht komplett an -------------------------------> \ 79\ 80<!-------------------------------- LED Spots komplett an -------------------------------> \ 81<div ng-switch on=\\"itemValue(config.led_light_item)\\">\ 82 <div style=\\"border: 1px solid green;\ 83 border-radius: 15px;\ 84 display: flex;\ 85 position: absolute; \ 86 background-color:initial;\ 87 top: 0%; left: 50%; width: 50%; height: 40%; \ 88 align-items:center;\ 89 \\" ng-switch-when=\\"OFF\\">\ 90 <button \ 91 style=\\" \ 92 background-color: Transparent; \ 93 border-radius: 15px; \ 94 text-align: center;\ 95 width: 100%; height: 100%;\ 96 \\"\ 97 ng-click=\\"sendCmd(config.led_light_item, 'ON')\\">\ 98 <img src=\\"/static/whatever/light_diffused2.png\\" height=\\"80%\\" />\ 99 </button>\ 100 </div> \ 101 <div style=\\"border: 1px solid green;\ 102 border-radius: 15px;\ 103 display: flex;\ 104 position: absolute; \ 105 background-color:initial;\ 106 top: 0%; left: 50%; width: 50%; height: 40%; \ 107 align-items:center;\ 108 \\" ng-switch-when=\\"ON\\">\ 109 <button \ 110 style=\\" \ 111 background-color: Transparent; \ 112 border-radius: 15px; \ 113 text-align: center;\ 114 width: 100%; height: 100%;\ 115 \\"\ 116 ng-click=\\"sendCmd(config.led_light_item, 'OFF')\\">\ 117 <img src=\\"/static/whatever/licht_on.png\\" height=\\"80%\\" />\ 118 </button>\ 119 </div> \ 120 \ 121 <div style=\\"border: 1px solid green;\ 122 border-radius: 15px;\ 123 display: flex;\ 124 position: absolute; \ 125 background-color:initial;\ 126 top: 0%; left: 50%; width: 50%; height: 40%; \ 127 align-items:center;\ 128 \\" ng-switch-default>\ 129 <button \ 130 style=\\" \ 131 background-color: Transparent; \ 132 border-radius: 15px; \ 133 text-align: center;\ 134 width: 100%; height: 100%;\ 135 \\"\ 136 ng-click=\\"sendCmd(config.led_light_item, 'ON')\\">\ 137 <img src=\\"/static/whatever/light_diffused2.png\\" height=\\"80%\\" />\ 138 </button>\ 139 </div>\ 140</div>\ 141<!-------------------------------- LED Spots komplett an -------------------------------> \ 142\ 143\ 144<!------------------------------------------------------------------------------- LED Licht Spots \ 145 \ 146<div style=\\"border: 1px solid black;\ 147 border-radius: 15px;\ 148 display: flex;\ 149 position: absolute; \ 150 background-color:initial;\ 151 top: 0%; left: 50%; width: 50%; height: 40%; \ 152 align-items:center;\ 153 \ \ \ \ \\"> \ 154 \ \ \ \ \ \ <button \ 155\ \ \ \ \ \ \ \ style=\\" \ 156 \ \ background-color: Transparent; \ 157 \ \ border-radius: 15px; \ 158\ \ \ \ \ \ \ \ \ text-align: center;\ 159 \ \ width: 100%; height: 100%;\ 160\ \ \ \ \ \ \ \ \\"\ 161 \ ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 162 <img src=\\"/static/whatever/light_diffused2.png\\" width=\\"100%\\" />\ 163 \ \ \ </button>\ 164</div>\ 165-->\ 166\ 167<!------------------------------------------------------------------------------- Temperature -->\ 168\ 169<div style=\\"border: 1px solid red;\ 170 border-radius: 15px;\ 171 display: flex;\ 172 position: absolute; \ 173 background-color:initial;\ 174 top: 40%; left: 0%; width: 50%; height: 20%; \ 175 align-items:center;\ 176 \ \ \ \ \\"> \ 177\ \ \ \ \ \ <img src=\\"/static/whatever/temp_temperatur.png\\" height=\\"90%\\" /> \ 178 <span style=\\"color: cyan; font-size: 14pt\\"> {{itemValue(config.temperature_item)}} </span>\ 179</div> \ 180\ 181<!------------------------------------------------------------------------------- Temperature -->\ 182\ 183\ 184\ 185<!------------------------------------------------------------------------------ Feuchtigkeit -->\ 186<div style=\\"border: 1px solid red;\ 187 border-radius: 15px;\ 188 display: flex;\ 189 position: absolute; \ 190 background-color:initial;\ 191 top: 60%; left: 0%; width: 50%; height: 20%; \ 192 align-items:center;\ 193\ \ \ \ \ \ align:center;\ 194 \ \ \ \ \\"> \ 195\ \ \ \ \ \ <img src=\\"/static/whatever/wetter_feuchtigkeit2.png\\" height=\\"90%\\" />\ 196 <span style=\\"color: cyan; font-size: 14pt\\"> {{itemValue(config.humidity_item)}} </span>\ 197\ \ \ \ \ \ \ 198</div> \ 199<!------------------------------------------------------------------------------- Feuchtigkeit --> \ 200\ 201\ 202<!------------------------------------------------------------------------------- tbd -->\ 203<div style=\\"border: 1px solid yellow;\ 204 border-radius: 15px;\ 205 display: flex;\ 206 position: absolute; \ 207 background-color:initial;\ 208 top: 40%; left: 50%; width: 50%; height: 40%; \ 209 align-items:center;\ 210 \ \ \ \ \\"> \ 211 tbd\ 212</div> \ 213<!------------------------------------------------------------------------------- tbd -->\ 214\ 215</div> \ 216\ 217\ 218\ 219\ 220<!-------------------------------- Shutter Up -------------------------------> \ 221<div ng-switch on=\\"itemValue(config.shutter_up_item)\\">\ 222<div align=\\"center\\" style=\\"border: 1px solid green;\ 223 border-radius: 15px;\ 224 display: flex;\ 225 position: absolute; \ 226 background-color:initial;\ 227 top: 80%; left: 0%; width: 33%; height: 20%; \ 228 align-items:center;\ 229\ \ \ \ \ \ align: center;\ 230\ \ \ \ \ text-align: center; \ 231 \ \ \ \ \\"\ 232 ng-switch-when=\\"OFF\\" >\ 233 <button \ 234 style=\\" \ 235 background-color: Transparent; \ 236 border-radius: 15px; \ 237 text-align: center;\ 238 width: 100%; height: 100%;\ 239 \\"\ 240 ng-click=\\"sendCmd(config.shutter_up_item, 'ON')\\">\ 241 \ \ \ \ \ \ \ <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 242 \ \ \ </button>\ 243 </div> \ 244 <div align=\\"center\\" style=\\"border: 1px solid green;\ 245 border-radius: 15px;\ 246 display: flex;\ 247 position: absolute; \ 248 background-color:initial;\ 249 top: 80%; left: 0%; width: 33%; height: 20%; \ 250 align-items:center;\ 251\ \ \ \ \ \ align: center;\ 252\ \ \ \ \ text-align: center; \\" \ 253 ng-switch-when=\\"ON\\">\ 254\ \ \ \ \ \ \ \ 255 \ \ \ \ <button \ 256 style=\\" \ 257 background-color: DeepSkyBlue3; \ 258 border-radius: 15px; \ 259 text-align: center;\ 260 width: 100%; height: 100%;\ 261 \\"\ 262 ng-click=\\"sendCmd(config.shutter_up_item, 'OFF')\\">\ 263 <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 264 </button>\ 265 </div> \ 266 \ 267 <div align=\\"center\\" style=\\"border: 1px solid green;\ 268 border-radius: 15px;\ 269 display: flex;\ 270 position: absolute; \ 271 background-color:initial;\ 272 top: 80%; left: 0%; width: 33%; height: 20%; \ 273 align-items:center;\ 274\ \ \ \ \ \ align: center;\ 275\ \ \ \ \ text-align: center; \\" \ 276 ng-switch-default>\ 277 <button \ 278 style=\\" \ 279 background-color: Transparent; \ 280 border-radius: 15px; \ 281 text-align: center;\ 282 width: 100%; height: 100%;\ 283 \\"\ 284 ng-click=\\"sendCmd(config.shutter_up_item, 'ON')\\">\ 285 <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 286\ \ \ \ \ \ </button>\ 287 </div>\ 288</div> \ 289<!-------------------------------- Shutter Up -------------------------------> \ 290 \ 291\ 292\ 293<div align=\\"center\\" style=\\"border: 1px solid blue;\ 294 border-radius: 15px;\ 295 display: flex;\ 296 position: absolute; \ 297 background-color:initial;\ 298 top: 80%; left: 33%; width: 33%; height: 20%; \ 299 align-items:center;\ 300\ \ \ \ \ \ align: center;\ 301\ \ \ \ \ text-align: center; \ 302 \ \ \ \ \\"> \ \ \ \ 303\ \ <button style=\\" \ 304\ \ \ background-color: Transparent; \ 305\ \ \ border-radius: 15px; \ 306\ \ \ text-align: center;\ 307 width: 100%; height: 100%;\ 308\ \ \ \\"\ 309\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'CLOSED' && itemValue(config.w_tilt_item) == 'CLOSED' \\";\ \ \ \ \ \ \ \ 310\ \ \ >\ 311\ \ <img src=\\"/static/whatever/fts_fenster.png\\" height=\\"60%\\" />\ 312 </button>\ 313\ <button style=\\" \ 314\ \ \ background-color: #FFBF00; \ 315\ \ \ border-radius: 15px; \ 316\ \ \ text-align: center;\ 317 width: 100%; height: 100%;\ 318\ \ \ \\"\ 319\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'OPEN' && itemValue(config.w_tilt_item) == 'CLOSED' \\";\ \ \ \ \ \ \ \ 320\ \ \ >\ 321\ \ <img src=\\"/static/whatever/fts_fenster_gekippt.png\\" height=\\"60%\\" />\ 322 </button>\ 323\ <button style=\\" \ 324\ \ \ background-color: #FF4000; \ 325\ \ \ border-radius: 15px; \ 326\ \ \ text-align: center;\ 327 width: 100%; height: 100%;\ 328\ \ \ \\"\ 329\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'OPEN' && itemValue(config.w_tilt_item) == 'OPEN' \\";\ \ \ \ \ \ \ \ 330\ \ \ >\ 331\ \ <img src=\\"/static/whatever/fts_fenster_offen.png\\" height=\\"60%\\" />\ 332 </button>\ 333\ <button style=\\" \ 334\ \ \ background-color: Transparent; \ 335\ \ \ border-radius: 15px; \ 336\ \ \ text-align: center;\ 337 width: 100%; height: 100%;\ 338\ \ \ \\"\ 339\ \ \ ng-if=\\"itemValue(config.w_open_item) == 'CLOSED' && itemValue(config.w_tilt_item) == 'OPEN' \\";\ \ \ \ \ \ \ \ 340\ \ \ >\ 341\ \ <img src=\\"/static/whatever/fts_fenster_offen.png\\" height=\\"60%\\" />\ 342 </button>\ 343</div>\ 344\ 345\ 346\ 347<!-------------------------------- Shutter down -------------------------------> \ 348<div ng-switch on=\\"itemValue(config.shutter_down_item)\\">\ 349<div align=\\"center\\" style=\\"border: 1px solid green;\ 350 border-radius: 15px;\ 351 display: flex;\ 352 position: absolute; \ 353 background-color:initial;\ 354 top: 80%; left: 66%; width: 33%; height: 20%; \ 355 align-items:center;\ 356\ \ \ \ \ \ align: center;\ 357\ \ \ \ \ text-align: center; \ 358 \ \ \ \ \\"\ 359 ng-switch-when=\\"OFF\\" >\ 360 <button \ 361 style=\\" \ 362 background-color: Transparent; \ 363 border-radius: 15px; \ 364 text-align: center;\ 365 width: 100%; height: 100%;\ 366 \\"\ 367 ng-click=\\"sendCmd(config.shutter_down_item, 'ON')\\">\ 368 \ \ \ \ \ \ \ <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 369 \ \ \ </button>\ 370 </div> \ 371 <div align=\\"center\\" style=\\"border: 1px solid green;\ 372 border-radius: 15px;\ 373 display: flex;\ 374 position: absolute; \ 375 background-color:initial;\ 376 top: 80%; left: 66%; width: 33%; height: 20%; \ 377 align-items:center;\ 378\ \ \ \ \ \ align: center;\ 379\ \ \ \ \ text-align: center; \\" \ 380 ng-switch-when=\\"ON\\">\ 381\ \ \ \ \ \ \ \ 382 \ \ \ \ <button \ 383 style=\\" \ 384 background-color: DeepSkyBlue3; \ 385 border-radius: 15px; \ 386 text-align: center;\ 387 width: 100%; height: 100%;\ 388 \\"\ 389 ng-click=\\"sendCmd(config.shutter_down_item, 'OFF')\\">\ 390 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 391 </button>\ 392 </div> \ 393 \ 394 <div align=\\"center\\" style=\\"border: 1px solid green;\ 395 border-radius: 15px;\ 396 display: flex;\ 397 position: absolute; \ 398 background-color:initial;\ 399 top: 80%; left: 66%; width: 33%; height: 20%; \ 400 align-items:center;\ 401\ \ \ \ \ \ align: center;\ 402\ \ \ \ \ text-align: center; \\" \ 403 ng-switch-default>\ 404 <button \ 405 style=\\" \ 406 background-color: Transparent; \ 407 border-radius: 15px; \ 408 text-align: center;\ 409 width: 100%; height: 100%;\ 410 \\"\ 411 ng-click=\\"sendCmd(config.shutter_down_item, 'ON')\\">\ 412 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 413\ \ \ \ \ \ </button>\ 414 </div>\ 415</div> \ 416<!-------------------------------- Shutter down -------------------------------> \ 417 \ 418\ 419\ 420\ 421\ 422\ 423\ 424\ 425\ 426\ 427\ 428\ 429\ 430\ 431\ 432\ 433\ 434\ 435\ 436\ 437\ 438\ 439\ 440\ 441\ 442\ 443\ 444\ 445\ 446\ 447\ 448<!------------------------------------------------------------------------------- Rolladen Gartenseite -->\ 449<!--\ 450<div align=\\"center\\" style=\\"border: 1px solid green;\ 451 border-radius: 15px;\ 452 display: flex;\ 453 position: absolute; \ 454 background-color:initial;\ 455 top: 80%; left: 0%; width: 100%; height: 20%; \ 456 align-items:center;\ 457\ \ \ \ \ \ align: center;\ 458\ \ \ \ \ text-align: center; \ 459 \ \ \ \ \\"> \ \ \ \ 460\ \ \ <button style=\\" \ 461 \ \ background-color: Transparent; \ \ \ \ 462\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 463\ \ \ \ \ \ \ \ \ text-align: center;\ 464 \ \ width: 100%; height: 100%;\ 465\ \ \ \ \ \ \ \ \\"\ 466\ \ \ \ \ \ \ ng-click=\\"sendCmd(config.blind_item, 'UP')\\">\ 467 \ \ \ <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 468 </button> \ 469 <button style=\\" \ 470 \ \ background-color: Transparent; \ 471\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 472\ \ \ \ \ \ \ \ \ text-align: center;\ 473 \ \ width: 100%; height: 100%;\ 474\ \ \ \ \ \ \ \ \\"\ 475\ \ \ \ \ \ \ ng-if=\\"itemValue(config.w_contact_item) == 'STOP'\\";\ \ \ \ \ \ \ \ 476\ \ \ \ \ \ \ ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 477\ \ \ <img src=\\"/static/whatever/wetter_.png\\" height=\\"90%\\" />\ 478 </button>\ 479 <button style=\\" \ 480 \ \ background-color: Transparent; \ 481\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 482\ \ \ \ \ \ \ \ \ text-align: center;\ 483 \ \ width: 100%; height: 100%;\ 484\ \ \ \ \ \ \ \ \\"\ 485\ \ \ \ \ \ \ ng-if=\\"itemValue(config.w_contact_item) != 'STOP'\\" || \\"itemValue(config.w_contact_item) != 'UP'\\" ;\ \ \ \ \ \ \ \ 486\ \ \ \ \ \ \ ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 487\ \ \ <img src=\\"/static/whatever/fts_fenster.png\\" height=\\"60%\\" />\ 488 </button>\ 489\ 490 <button style=\\"\ background-color: Transparent; \ 491\ \ \ \ \ \ \ \ \ border-radius: 15px; \ 492\ \ \ \ \ \ \ \ \ margin-right: 0;\ \ \ \ \ \ \ \ \ \ 493\ \ \ \ \ \ \ \ \ text-align: center;\ 494 \ \ width: 100%; height: 100%;\\" \ 495 ng-click=\\"sendCmd(config.blind_item, 'DOWN')\\">\ 496 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 497 </button> \ 498</div> \ 499--> \ 500\ 501<!-- rechts 10 % 100% --> \ 502<!--\ 503<div style=\\"border: 1px solid cyan;\ 504 border-radius: 15px;\ 505 display: flex;\ 506 position: absolute; \ 507 background-color:initial;\ 508 top: 0%; left: 80%; width: 20%; height: 80%; \ 509 align-items:center;\ 510 \ \ \ \ \\" ng-init=\\"model={ name: 'My dummy', item: 'My_Item', ... }\\">> \ 511\ 512 <p>\ 513 <button ng-click=\\"sendCmd(config.blind_item, 'UP')\\">\ 514 <i class=\\"glyphicon glyphicon-menu-up\\"></i>\ 515 </button> \ 516 \ 517 <br />\ 518 <button ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 519 <widget-icon iconset=\\"'eclipse-smarthome-classic'\\" icon=\\"'blinds-20'\\" size=\\"45\\" state=\\"itemValue(config.blind_item)\\" />\ 520 </button>\ 521\ 522 \ 523 \ 524 <br /> \ 525 <button ng-click=\\"sendCmd(config.blind_item, 'STOP')\\">\ 526 <img src=\\"/static/whatever/control_arrow_down.png\\" height=\\"100%\\" />\ 527\ 528 </button>\ 529 \ 530 <br /> \ 531 <button style=\\"margin-right: 0\\" ng-click=\\"sendCmd(config.blind_item, 'DOWN')\\">\ 532 <i class=\\"glyphicon glyphicon-menu-down\\"></i>\ 533 </button>\ 534\ 535</div> \ 536-->", 537 "name": "Standard Room", 538 "author": "Christian Kreidel", 539 "description": "", 540 "settings": [ 541 { 542 "type": "item", 543 "id": "light_item", 544 "description": "light_item)", 545 "default": "light_item)", 546 "label": "light_item)" 547 }, 548 { 549 "type": "item", 550 "id": "led_light_item", 551 "label": "led_light_item", 552 "default": "led_light_item", 553 "description": "led_light_item" 554 }, 555 { 556 "type": "item", 557 "id": "temperature_item", 558 "label": "temperature_item", 559 "default": "temperature_item", 560 "description": "temperature_item" 561 }, 562 { 563 "type": "item", 564 "id": "humidity_item", 565 "label": "humidity_item", 566 "default": "humidity_item", 567 "description": "humidity_item" 568 }, 569 { 570 "type": "item", 571 "id": "shutter_up_item", 572 "label": "shutter_up_item", 573 "default": "shutter_up_item", 574 "description": "shutter_up_item" 575 }, 576 { 577 "type": "item", 578 "id": "shutter_down_item", 579 "label": "shutter_down_item", 580 "default": "shutter_down_item", 581 "description": "shutter_down_item" 582 }, 583 { 584 "type": "item", 585 "id": "w_open_item", 586 "label": "w_open_item", 587 "default": "w_open_item", 588 "description": "w_open_item" 589 }, 590 { 591 "type": "item", 592 "id": "w_tilt_item", 593 "label": "w_tilt_item", 594 "default": "w_tilt_item", 595 "description": "w_tilt_item" 596 } 597 ] 598}
Sketch for Arduino Ethernet + RGB Shiled as MQTT Client control RGB strip by Alexa
arduino
Use Ardunio Ethernet and RGB Shield to control rgb strip by Alexa ( respectively openhab2) aditionally you need - Openhab2 Items File - Openhab2 Rules File
1#define EthernetAvail 2#define Debug_MQTT 3 4 5#ifdef EthernetAvail // -------- 6 #include <Ethernet2.h> 7 #include <PubSubClient.h> // http://knolleary.net/arduino-client-for-mqtt/ 8#endif 9 10#include <SPI.h> 11#include <String.h> 12 13 14void callback(char* topic, byte* payload, unsigned int length); 15 16 17#ifdef EthernetAvail 18// MAC Adresse des Ethernet Shields 19byte mac[] = {0x90,0xA2,0xDA,0x10,0xe4,0x15 }; 20//the IP address for the shield: 21byte ip[] = { 192,168,0,230 }; 22// IP des MQTT Servers 23byte server[] = { 192, 168, 0, 30}; 24#endif 25 26int SENS_UPDATE_CNT =5000; 27#define TASTER_WAIT 100 28#define V_MAJOR 1 // nur eine ziffer erlaubt 29#define V_MINOR 1 30 31bool subs=0; 32bool get_vers=0; 33bool get_update_time=0; 34 35// ----------- General Variables 36int counter = 0; 37#include <Arduino.h> 38 39///////// Main Information for Controller Class 40boolean gotAMessage = false; // whether or not you got a message from the client yet 41String text=""; 42 43uint8_t IntRed; // 0..255 44uint8_t IntGreen; // 0..255 45uint8_t IntBlue; // 0..255 46 47// ********************* RGB 48// for RGB control 49int PinGreen =5; // LED connected to digital pin 5 50int PinBlue =6; // LED connected to digital pin 6 51int PinRed =3; // LED connected to digital pin 3 52 53/* undefine this if you don't want blinking LED for diagnosis */ 54#define LED_PIN 13 55#define SER_BAUD 9600 56 57int val = 0; 58int Counter = 0; 59 60#ifdef EthernetAvail 61 // =========================================================== 62 // MQTT Callback Funktion 63 // =========================================================== 64 void callback(char* topic, byte* payload, unsigned int length) { 65 // 66 String StringRGB=""; 67 unsigned int i = 0; 68 // temp variable to convert message to string 69 char message_buff[40]; 70 #ifdef Debug_MQTT 71 Serial.println("Message arrived: topic: " + String(topic)); 72 Serial.println("Length: " + String(length, DEC)); 73 #endif 74 75 // copy payload to char "\\0" ended 76 for (i = 0; i < length; i++) { 77 message_buff[i] = payload[i]; 78 } 79 message_buff[i] = '\\0'; 80 81 // convert message to string 82 String msgString = String(message_buff); 83 #ifdef Debug_MQTT 84 Serial.println("Payload: " + msgString); 85 #endif 86 87 // parse string for rgb values , seperated by ; 88 for (size_t i=0;i<msgString.length();i++) // start wirth first character 89 { 90 Serial.println("substring "); 91 Serial.println(msgString.substring(i,i+1)); 92 if ((msgString.substring(i,i+1)== ";") ) // check if seperater character reachred 93 { 94 Serial.println("String RGB " + StringRGB); 95 // komma is reached, copy data to int refering to color counter, e.g. Counter =0 red ; counter =1 green 96 if (Counter==0) 97 { 98 #ifdef Debug_MQTT 99 Serial.println("Counter Red"); 100 Serial.println(Counter); 101 Serial.println(StringRGB); 102 #endif 103 IntRed=StringRGB.toInt(); 104 StringRGB=""; 105 Counter++; 106 } 107 else 108 { 109 if (Counter==1) 110 { 111 #ifdef Debug_MQTT 112 Serial.println("Counter Green"); 113 Serial.println(Counter); 114 Serial.println(StringRGB); 115 #endif 116 IntGreen=StringRGB.toInt(); 117 StringRGB=""; 118 Counter++; 119 } 120 else 121 { 122 if (Counter==2) 123 { 124 #ifdef Debug_MQTT 125 Serial.println("Counter Blue"); 126 Serial.println(Counter); 127 Serial.println(StringRGB); 128 #endif 129 IntBlue=StringRGB.toInt(); 130 StringRGB=""; 131 Counter++; 132 } 133 } 134 } 135 } 136 else // add char to StringRGB if not == ";" 137 { 138 StringRGB+=msgString.substring(i,i+1); 139 } 140 } // end for loop 141 msgString="\\0"; 142 StringRGB="\\0"; 143 setRGBVal(); 144 Counter=0; 145} // End Callback function 146#endif 147 148 149#ifdef EthernetAvail 150 151// Ethernet Client as MQTT Clients 152EthernetClient ethClient; 153PubSubClient mqttClient(server, 1883, callback, ethClient); 154#endif 155 156 157 158void reconnect() 159{ 160 #ifdef EthernetAvail 161 // connect to mosquitto server 162 if (!mqttClient.connected()) { 163 subs=mqttClient.connect("RGB", "INSERT_MOSQUITTO_PW_HERE", "INSERT_MOSQUITTO_PW_HERE"); // adapt your mosquitto settings here 164 // subscribe to topic 165 subs=mqttClient.subscribe("/Haus/WZ/RGB"); 166 } 167 #endif 168} 169 170void setup() 171{ 172 // use serial if to putput received messages in debug mode 173 #ifdef Debug_MQTT 174 Serial.begin(9600); 175 Serial.println("starting"); 176 #endif 177 // Init Ethernets 178 #ifdef EthernetAvail 179 Ethernet.begin(mac); 180 #endif 181 182 #ifdef LED_PIN 183 pinMode(LED_PIN, OUTPUT); 184 #endif 185 186 /*#ifdef EthernetAvail 187 if (Ethernet.begin(mac) == 0) { 188 #ifdef Serial.println("Failed to configure Ethernet using DHCP \Static IP will be used"); #endif 189 // initialize the ethernet device not using DHCP: 190 Ethernet.begin(mac, ip, gateway, subnet); 191 #endif */ 192 193 // connect to mosquitto 194 reconnect(); 195} 196 197 198 199 200void loop() 201{ 202 # wait to receive topic 203 #ifdef EthernetAvail 204 mqttClient.loop(); 205 #endif 206 207} // end loop 208 209 210// function to update rgb output 211void setRGBVal() 212 { 213 analogWrite(PinRed, IntRed); 214 analogWrite(PinGreen, IntGreen); 215 analogWrite(PinBlue, IntBlue); 216 Serial.println("Rot: " + String(IntRed,DEC)); 217 Serial.println("Grn: " + String(IntGreen,DEC)); 218 Serial.println("Blau: " + String(IntBlue,DEC)); 219 } 220 221 222
Openhab2 Rules File for RGB
json
Openhab2 rules to generate a mqtt message on WIFI_RGB_1_RGB item
1import org.openhab.core.library.types.* 2 3var HSBType hsbValue 4var int redValue 5var int greenValue 6var int blueValue 7var String RGBvalues 8 9rule "Set RGB 1 value" 10 when 11 Item fWIFI_RGB_1 changed 12 then 13 hsbValue = fWIFI_RGB_1.state as HSBType 14 15 redValue = (hsbValue.red.intValue*2.55).intValue 16 greenValue = (hsbValue.green.intValue*2.55).intValue 17 blueValue = (hsbValue.blue.intValue*2.55).intValue 18 19 20 RGBvalues= redValue.toString + ";" + greenValue.toString + ";" + blueValue.toString + ";" 21 sendCommand( WIFI_RGB_1_RGB, RGBvalues ) 22 23 logInfo( "fWIFI_RGB_1", RGBvalues ) 24 end
Openhab 2 Items file to use with Mainboard Sketch
json
Item file to usw with the mainboard sketch
1// PAramter 2String EG_GZ_Vers "EG_GZ_Vers [%s] " {mqtt="<[mosquitto:/Haus/EG/GZ/Vers:state:default]"} 3String EG_GZ_UpdateTime "EG_GZ_UpdateTime [%s] " {mqtt="<[mosquitto:/Haus/EG/GZ/UpdateTime:state:default]"} 4 5 6// Sensoren Raum A 7Number EG_GZ_DHT_Temp "Guest Room Thermostat [%.2f] C" <temperature> ["CurrentTemperature"] {mqtt="<[mosquitto:/Haus/EG/GZ/A_DHT_Temp:state:default]"} 8Number EG_GZ_DHT_Humi "Humidity Guest Room [%.2f] %" ["CurrentHumidity"] {mqtt="<[mosquitto:/Haus/EG/GZ/A_DHT_Humi1:state:default]"} 9Number EG_GZ_DS_Temp "EG_GZ_DS_Temp [%.2f] C" <temperature> ["CurrentTemperature"] {mqtt="<[mosquitto:/Haus/EG/GZ/DS_Temp:state:default]"} 10Number EG_GZ_DS_V1 "EG_GZ_DS_V1 [%.2f] V" {mqtt="<[mosquitto:/Haus/EG/GZ/V1:state:default]"} 11Number EG_GZ_DS_V2 "EG_GZ_DS_V2 [%.2f] V" {mqtt="<[mosquitto:/Haus/EG/GZ/V2:state:default]"} 12Switch EG_GZ_A_B "Motion_A" {mqtt="<[mosquitto:/Haus/EG/GZ/A_B:command:ON:1],<[mosquitto:/Haus/EG/GZ/A_B:command:OFF:0]"} 13 14// Sensoren Raum B 15Switch EG_GZ_B_B "Motion_B" {mqtt="<[mosquitto:/Haus/EG/GZ/B_B:command:ON:1],<[mosquitto:/Haus/EG/GZ/B_B:command:OFF:0]"} 16 17 18 19// Taster 20Switch EG_GZ_T0 "Licht Gaestezimmer" ["Lighting"] {mqtt="<[mosquitto:/Haus/EG/GZ/T0:command:ON:1],<[mosquitto:/Haus/EG/GZ/T0:command:OFF:2]"} 21Switch EG_GZ_T1 "EG_GZ_T1" ["Lighting"] {mqtt="<[mosquitto:/Haus/EG/GZ/T1:command:ON:1],<[mosquitto:/Haus/EG/GZ/T1:command:OFF:2]"} 22Switch EG_GZ_T2 "EG_GZ_T2"["Lighting"] {mqtt="<[mosquitto:/Haus/EG/GZ/T2:command:ON:1],<[mosquitto:/Haus/EG/GZ/T2:command:OFF:2]"} 23Switch EG_GZ_T3 "EG_GZ_T3" {mqtt="<[mosquitto:/Haus/EG/GZ/T3:command:ON:1],<[mosquitto:/Haus/EG/GZ/T3:command:OFF:2]"} 24Switch EG_GZ_T4 "EG_GZ_T4" {mqtt="<[mosquitto:/Haus/EG/GZ/T4:command:ON:1],<[mosquitto:/Haus/EG/GZ/T4:command:OFF:2]"} 25Switch EG_GZ_T5 "EG_GZ_T5" {mqtt="<[mosquitto:/Haus/EG/GZ/T5:command:ON:1],<[mosquitto:/Haus/EG/GZ/T5:command:OFF:2]"} 26 27Switch EG_GZ_T6 "EG_GZ_T6" {mqtt="<[mosquitto:/Haus/EG/GZ/T6:command:ON:1],<[mosquitto:/Haus/EG/GZ/T6:command:OFF:2]"} 28Switch EG_GZ_T7 "EG_GZ_T7" {mqtt="<[mosquitto:/Haus/EG/GZ/T7:command:ON:1],<[mosquitto:/Haus/EG/GZ/T7:command:OFF:2]"} 29Switch EG_GZ_T8 "EG_GZ_T8" {mqtt="<[mosquitto:/Haus/EG/GZ/T8:command:ON:1],<[mosquitto:/Haus/EG/GZ/T8:command:OFF:2]"} 30Switch EG_GZ_T9 "EG_GZ_T9" {mqtt="<[mosquitto:/Haus/EG/GZ/T9:command:ON:1],<[mosquitto:/Haus/EG/GZ/T9:command:OFF:2]"} 31Switch EG_GZ_T10 "EG_GZ_T10" {mqtt="<[mosquitto:/Haus/EG/GZ/T10:command:ON:1],<[mosquitto:/Haus/EG/GZ/T10:command:OFF:2]"} 32Switch EG_GZ_T11 "EG_GZ_T11" {mqtt="<[mosquitto:/Haus/EG/GZ/T11:command:ON:1],<[mosquitto:/Haus/EG/GZ/T11:command:OFF:2]"} 33Switch EG_GZ_T12 "EG_GZ_T2" {mqtt="<[mosquitto:/Haus/EG/GZ/T12:command:ON:1],<[mosquitto:/Haus/EG/GZ/T12:command:OFF:2]"} 34Switch EG_GZ_T13 "EG_GZ_T13" {mqtt="<[mosquitto:/Haus/EG/GZ/T13:command:ON:1],<[mosquitto:/Haus/EG/GZ/T13:command:OFF:2]"} 35Switch EG_GZ_T14 "EG_GZ_T14" {mqtt="<[mosquitto:/Haus/EG/GZ/T13:command:ON:1],<[mosquitto:/Haus/EG/GZ/T14:command:OFF:2]"} 36Switch EG_GZ_T15 "EG_GZ_T15" {mqtt="<[mosquitto:/Haus/EG/GZ/T15:command:ON:1],<[mosquitto:/Haus/EG/GZ/T15:command:OFF:2]"} 37// Aktoren 38Switch EG_GZ_R9 " Gest Room Lighting" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R9:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R9:command:OFF:0]"} 39Switch EG_GZ_R10 "Guest Room Socket" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R10:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R10:command:OFF:0]"} 40Switch EG_GZ_R11 "Guest Room tbd1 " <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R11:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R11:command:OFF:0]"} 41Switch EG_GZ_R12 "Guest Room tbd2" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R12:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R12:command:OFF:0]"} 42Switch EG_GZ_R13 "Guest Room tbd3" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R13:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R13:command:OFF:0]"} 43Switch EG_GZ_R14 "Guest Room tbd4" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R14:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R14:command:OFF:0]"} 44Switch EG_GZ_R15 "Guest Room tbd5" <light> (gGuestRoom) ["Lighting"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/R15:command:ON:1],>[mosquitto:/Haus/EG/WZ_Kue/R15:command:OFF:0]"} 45Switch EG_GZ_R16 "Guest Room Heating" {mqtt=">[mosquitto:/Haus/EG/GZ/R16:command:ON:1],>[mosquitto:/Haus/EG/GZ/R16:command:OFF:0]"} 46 47 48Rollershutter EG_GZ_Rolladen "Guest Room Blind" <rollershutter> (gScatterEG_S1) ["Switchable"] {mqtt=">[mosquitto:/Haus/EG/WZ_Kue/F1:command:UP:2],>[mosquitto:/Haus/EG/WZ_Kue/F1:command:DOWN:1],>[mosquitto:/Haus/EG/WZ_Kue/F1:command:STOP:0],>[mosquitto:/Haus/EG/WZ_Kue/F1:command:MOVE:3]"} 49Switch Roll_Hoch "Hoch" ["Lighting"] 50Switch Roll_Runter "Runter" ["Lighting"] 51 52 53 54// W_O : 55// W_K : kipp 56Contact EG_GZ_W_O "WinContactOpen" {mqtt="<[mosquitto:/Haus/EG/GZ/A_W1:state:OPEN:1],<[mosquitto:/Haus/EG/GZ/A_W1:state:CLOSED:0]"} 57Contact EG_GZ_W_K "WinContactTilted" {mqtt="<[mosquitto:/Haus/EG/GZ/A_W2:state:OPEN:1],<[mosquitto:/Haus/EG/GZ/A_W2:state:CLOSED:0]"} 58
Rules to control shutter by Alexa
json
Some rules to extend shutter control via alexa
1rule "Roll_Up_Alexa" 2when 3 Item Roll_Hoch received command ON 4then 5 sendCommand(EG_GZ_T2, ON) 6 7end 8 9 10rule "Roll_Up_Alexa" 11when 12 Item Roll_Hoch received command OFF 13then 14 sendCommand(EG_GZ_T2, OFF) 15 16end 17 18rule "Roll_Down_Alexa" 19when 20 Item Roll_Runter received command ON 21then 22 sendCommand(EG_GZ_T4, ON) 23 24end 25 26 27rule "Roll_Down_Alexa" 28when 29 Item Roll_Runter received command OFF 30then 31 sendCommand(EG_GZ_T4, OFF) 32 33end
Mainboard Sketch
arduino
A example sketch to control two 6times buttons and a few sensors used two sensor boards.
11#define EthernetAvail 22#define DhtAvail 33#define I2cAvail 44#define Wire1Avail 55 66 77#ifdef EthernetAvail // 88 #include <Ethernet2.h> 99 #include <PubSubClient.h> // http://knolleary.net/arduino-client-for-mqtt/ 1010#endif 1111 1212// callback funtion 1313void callback(char* topic, byte* payload, unsigned int length); 1414 1515#ifdef EthernetAvail 1616 // MAC Adresse des Ethernet Shields 1717 byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xDD, 0xFF }; // ENTER MAC ADDRESS OF YOUR ARDUINO BOARD HERE 1818 //the IP address for the shield: 1919 byte ip[] = { 192,168,0,52 }; // ENTER STATIC IP ADDRESS OF YOUR ARDUINO BOARD HERE 2020 // IP des MQTT Servers 2121 byte server[] = { 192, 168, 0, 35}; // ENTER IP ADDRESS OF OPENHABIAN Raspberry Pi HERE 2222#endif 2323 2424int SENS_UPDATE_CNT =5000; // update 'time' counter for slow update e.g. temperature, humidity 2525#define TASTER_WAIT 100 // wait counter between keystrokes 2626 // the counter is used to debounce the button 2727 // but the implementation allows to use one button pressed twice or n times to extend the 2828 // functionality of the buttons 2929 // in the case that the extended version shall be used change the counter value to e.g. 800 3030#define V_MAJOR 1 // SW Major Version (one digit allowed) 3131#define V_MINOR 1 // SW Minor Version (one digit allowed) 3232 3333bool subs=0; // some temp variables 3434bool get_vers=0; // global variable to communicate between callback funtion and main function 3535bool get_update_time=0; // global variable to communicate between callback funtion and main function 3636int counter = 0; // counter to update slow sensor information 3737 3838#include <Arduino.h> 3939 4040//#ifdef IrAvail // -------- IR ------- IR not supported in that sketch because of lack of storage 4141//#include <IRremote.h> 4242//int RECV_PIN = 8; 4343//IRrecv irrecv(RECV_PIN); 4444//decode_results results; 4545//int ledPin = A0; 4646//int toggle, lock; 4747//#endif // --------------- IR 4848 4949// ------------------ Pin definition of RJ45 jack of sensor X1 of Mainboard 5050// Room A 5151#define A_DHTPIN 12 5252int awin1 = A5; 5353int awin2 = A4; 5454int abeweg = A3; 5555const uint8_t ONE_WIRE_PIN = A2; 5656 5757// ------------------ Pin definition of RJ45 jack of sensor X2 of Mainboard 5858// room b 5959#define B_DHTPIN 5 6060const uint8_t ONE_WIRE_PIN2 = 6; 6161int bbeweg = 7; 6262int bwin2 = 9; 6363int bwin1 = 13; 6464 6565// used by ethernet board itself 6666// Pin 10 - for Eth Interface 6767// Pin 4 - for SS signal of SD card 6868 6969// as motion detect the HC-SR501 is used 7070 7171// motion detector room A (RJ 45 jack X1) 7272bool abeweg_active; 7373int counter_abwege; 7474 7575// motion detector room B (RJ 45 jack X2) 7676bool bbeweg_active; 7777int counter_bbwege; 7878 7979// initial values of motion detection 8080bool val_beweg[2] = {0, 0}; 8181bool val_beweg_d[2] = {0, 0}; 8282 8383// contacts ( to control e.g. window status ) room A 8484// two contacts can be used e.g. open, tilt 8585bool val_a_win[2] = {0, 0}; 8686bool val_a_win_d[2] = {0, 0}; 8787 8888// contacts ( to control e.g. window status ) room B 8989// two contacts can be used e.g. open, tilt 9090bool val_b_win[2] = {0, 0}; 9191bool val_b_win_d[2] = {0, 0}; 9292 9393// DHT22 9494#ifdef DhtAvail 9595 #include "DHT.h" 9696 #define DHTTYPE DHT22 //DHT11, DHT21, DHT22 9797 // DHT ROOM A active 9898 DHT a_dht(A_DHTPIN, DHTTYPE); 9999 // DHT ROOM B active 100100 //DHT b_dht(B_DHTPIN, DHTTYPE); // DHT22 on Sensord board for Room B is not used 101101#endif 102102 103103 104104// 1 WIRE for DS2438 105105#ifdef Wire1Avail 106106 #include <OneWire.h> 107107 #include <Arduino.h> 108108 #include <DS2438.h> 109109 // define Address of DS2438 mounted on sensor board of Room A 110110 uint8_t DS2438_address[] = { 0x26, 0x15, 0x70, 0x0F, 0x01, 0x00, 0x00, 0x8D }; // INSERT ADDR OF DS2438 OF USED SENSOR BOARD 111111 OneWire a_ow(ONE_WIRE_PIN); 112112 DS2438 a_ds2438(&a_ow, DS2438_address); 113113 // define Address of DS2438 mounted on sensor board of Room B 114114 //uint8_t DS2438_address2[] = { 0x26, 0x2c, 0x50, 0x27, 0x01, 0x00, 0x00, 0x62 }; // delete comment if a DS2438 is used for room B 115115 //OneWire b_ow(ONE_WIRE_PIN2); // delete comment if a DS2438 is used for room B 116116 //DS2438 b_ds2438(&b_ow, DS2438_address2); // delete comment if a DS2438 is used for room B 117117#endif 118118 119119// I2C - Port Expander 120120#ifdef I2cAvail 121121 #include "Wire.h" 122122 #include <Adafruit_MCP23017.h> 123123 // Raum A / Raum B gemeinsam 124124 Adafruit_MCP23017 mcp1; // Create MCP 1 125125 Adafruit_MCP23017 mcp2; // Create MCP 2 126126 // temporary variables for inputs 127127 int val[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // current value of input 128128 int val_d[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // last value (to detect falling edge) 129129 int delaycounter[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Delaycounter for each input ( to detect a second or third buttonstroke) 130130 int tastencounter[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Number on buttonstrokes 131131#endif 132132 133133// DS2438 brightness 134134float lux; 135135// Variablen for temperature 136136float tempC = 0; 137137// temp variabled to convert different data to string 138138char tempBuffer[100]; 139139char measBuffer[100]; 140140char valBuffer[100]; 141141int button_flag=100; 142142 143143 144144#ifdef EthernetAvail 145145 // Callback function of MQTT. It is called when a subscribed topic is received. 146146 // =========================================================== 147147 void callback(char* topic, byte* payload, unsigned int length) { 148148 // counter 149149 unsigned int i = 0; 150150 // temp variable to convert the message to a string 151151 char message_buff[40]; // maximum message length (including \\0 termination) 152152 153153 // message to char array, \\0 ended 154154 for (i = 0; i < length; i++) { 155155 message_buff[i] = payload[i]; 156156 } 157157 message_buff[i] = '\\0'; // termination 158158 159159 #ifdef Debug_MQTT 160160 String msgString = String(message_buff); // convert message to sting to output it on serial port 161161 Serial.println("Payload: " + msgString); 162162 Serial.println(int(payload[0])-48); 163163 #endif 164164 165165 if (strcmp(topic, "/Haus/EG/GZ/gVers") == 0) // getVersion topic received 166166 { 167167 #ifdef Debug_MQTT 168168 Serial.println("Topic R1 gefunden"); 169169 Serial.println("PAyload :" + *payload); 170170 #endif 171171 get_vers=1; // set get version flag to send version in main function 172172 } 173173 174174 if (strcmp(topic, "/Haus/EG/GZ/gUpdateTime") == 0) // get Update Time topic received 175175 { 176176 #ifdef Debug_MQTT 177177 Serial.println("Topic R1 gefunden"); 178178 Serial.println("PAyload :" + *payload); 179179 #endif 180180 get_update_time=1; // set get update time flag to send version in main function 181181 } 182182 183183 if (strcmp(topic, "/Haus/EG/GZ/sUpdateTime") == 0) // set Update Time topic received 184184 { 185185 #ifdef Debug_MQTT 186186 Serial.println("Topic R1 gefunden"); 187187 Serial.println("PAyload :" + *payload); 188188 #endif 189189 SENS_UPDATE_CNT = atoi(message_buff); // change update time of slow sensor data 190190 // e.g. to reduce network traffic 191191 counter=0; // reset counter to avoid faulty combinations 192192 } 193193 194194 // the relais are serially connected 195195 // you find a diagram here http://www.loxwiki.eu/display/LOX/Koppelrelais+und+Verriegelung 196196 if (strcmp(topic, "/Haus/EG/GZ/F1") == 0) // F1 topic received , to controll roller shutter 197197 { 198198 switch (int(payload[0]) - 48) { 199199 case 0: // stop shutter 200200 #ifdef I2cAvail 201201 // delay(500); 202202 mcp2.digitalWrite(7, 0); // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER 203203 delay(500); // delay is needed to avoid switching direction too fast 204204 mcp2.digitalWrite(6, 0); // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER // || 205205 #endif // +24V---||---6 206206 break; // || 207207 case 1: // move direction one // (12, n.c) 208208 #ifdef I2cAvail //wago 788-304 / ------------- 209209 //delay(500); // K1 (12, n.c) (11) / 210210 mcp2.digitalWrite(6, 0); // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER / ----------------/ -------------\phase 1 , e.g. up 211211 delay(500); // (11) / K2 (14,n.o) |Motor 212212 mcp2.digitalWrite(7, 1); // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER L --------/ ---------------------------------/ phase 2 , e.g. down 213213 #endif // (14,n.o) 214214 break; // || 215215 case 2: // move direction two +24V---||---7 216216 #ifdef I2cAvail // || 217217 //delay(500); 218218 mcp2.digitalWrite(7, 0); // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER 219219 delay(500); 220220 mcp2.digitalWrite(6, 1); // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER 221221 #endif 222222 break; 223223 default: 224224 // if nothing else matches, do the default 225225 // default is optional 226226 break; 227227 } 228228 } 229229 230230 if (strcmp(topic, "/Haus/EG/GZ/R9") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 231231 { 232232 if (int(payload[0]) - 48 == 1) // check if payload message is 1 233233 { 234234 #ifdef I2cAvail 235235 mcp2.digitalWrite(8, 1); // CHANGE PIN NUMER OF RELAIS USED FOR RELAIS 236236 #endif 237237 } 238238 else { // switch relais off in all other cases 239239 #ifdef I2cAvail 240240 mcp2.digitalWrite(8,0); // switch relais off in all other cases 241241 #endif 242242 } 243243 } 244244 245245 if (strcmp(topic, "/Haus/EG/GZ/R10") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 246246 { 247247 if (int(payload[0]) - 48 == 1) // check if payload message is 1 248248 { 249249 #ifdef I2cAvail 250250 mcp2.digitalWrite(9, 1); // switch relais off in all other cases 251251 #endif 252252 } 253253 else { // switch relais off in all other cases 254254 #ifdef I2cAvail 255255 mcp2.digitalWrite(9,0); // switch relais off in all other cases 256256 #endif 257257 } 258258 } 259259 260260 if (strcmp(topic, "/Haus/EG/GZ/R11") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 261261 { 262262 if (int(payload[0]) - 48 == 1) 263263 { 264264 #ifdef I2cAvail 265265 mcp2.digitalWrite(10, 1); 266266 #endif 267267 } 268268 else { 269269 #ifdef I2cAvail 270270 mcp2.digitalWrite(10,0); 271271 #endif 272272 } 273273 } 274274 275275 276276 if (strcmp(topic, "/Haus/EG/GZ/R12") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 277277 { 278278 if (int(payload[0]) - 48 == 1) 279279 { 280280 #ifdef I2cAvail 281281 mcp2.digitalWrite(11, 1); 282282 #endif 283283 } 284284 else { 285285 #ifdef I2cAvail 286286 mcp2.digitalWrite(11,0); 287287 #endif 288288 } 289289 } 290290 291291 if (strcmp(topic, "/Haus/EG/GZ/R13") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 292292 { 293293 if (int(payload[0]) - 48 == 1) 294294 { 295295 #ifdef I2cAvail 296296 mcp2.digitalWrite(12, 1); 297297 #endif 298298 } 299299 else { 300300 #ifdef I2cAvail 301301 mcp2.digitalWrite(12,0); 302302 #endif 303303 } 304304 } 305305 306306 if (strcmp(topic, "/Haus/EG/GZ/R14") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 307307 { 308308 if (int(payload[0]) - 48 == 1) 309309 { 310310 #ifdef I2cAvail 311311 mcp2.digitalWrite(13, 1); 312312 #endif 313313 } 314314 else { 315315 #ifdef I2cAvail 316316 mcp2.digitalWrite(13,0); 317317 #endif 318318 } 319319 } 320320 321321 if (strcmp(topic, "/Haus/EG/GZ/R15") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 322322 { 323323 if (int(payload[0]) - 48 == 1) 324324 { 325325 #ifdef I2cAvail 326326 mcp2.digitalWrite(14, 1); 327327 #endif 328328 } 329329 else { 330330 #ifdef I2cAvail 331331 mcp2.digitalWrite(14,0); 332332 #endif 333333 } 334334 } 335335 336336 if (strcmp(topic, "/Haus/EG/GZ/R16") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 337337 { 338338 if (int(payload[0]) - 48 == 1) 339339 { 340340 #ifdef I2cAvail 341341 mcp2.digitalWrite(15, 1); 342342 #endif 343343 } 344344 else { 345345 #ifdef I2cAvail 346346 mcp2.digitalWrite(15,0); 347347 #endif 348348 } 349349 } 350350 } 351351#endif 352352 353353 354354 355355#ifdef EthernetAvail 356356 // Ethernet Client to communicate as mqtt client 357357 EthernetClient ethClient; 358358 // Server - Variable of byte type mit address of server (openhabian) 359359 // 1883 - standard tcp port for mqtt communication 360360 // name of callback - function called when a mqqt topic is received 361361 // ethClient - ethernet client used for mqtt communication 362362 PubSubClient mqttClient(server, 1883, callback, ethClient); 363363#endif 364364 365365// temp data to save converted version data as char 366366char vers_char[20]; 367367char update_char[20]; 368368 369369// reconnect function to subscribe topics 370370void reconnect() 371371{ 372372 373373#ifdef EthernetAvail 374374 // start mqtt connection if it is not open 375375 if (!mqttClient.connected()) { 376376 subs=mqttClient.connect("EG_WZ_Kue", "openhabian", "YOURPW"); // ENTER YOUR PW HERE 377377 #ifdef Debug_MQTT 378378 Serial.print("con success : "); 379379 Serial.println(subs); 380380 #endif 381381 // subscribe to topics 382382 subs=mqttClient.subscribe("/Haus/EG/GZ/R9"); 383383 #ifdef Debug_MQTT 384384 Serial.print("subs success : "); 385385 Serial.print(subs); 386386 #endif 387387 subs=mqttClient.subscribe("/Haus/EG/GZ/R10"); 388388 #ifdef Debug_MQTT 389389 Serial.print("subs success : "); 390390 Serial.print(subs); 391391 #endif 392392 subs=mqttClient.subscribe("/Haus/EG/GZ/R11"); 393393 #ifdef Debug_MQTT 394394 Serial.print("subs success : "); 395395 Serial.print(subs); 396396 #endif 397397 subs=mqttClient.subscribe("/Haus/EG/GZ/R12"); 398398 #ifdef Debug_MQTT 399399 Serial.print("subs success : "); 400400 Serial.print(subs); 401401 #endif 402402 subs=mqttClient.subscribe("/Haus/EG/GZ/R13"); 403403 #ifdef Debug_MQTT 404404 Serial.print("subs success : "); 405405 Serial.print(subs); 406406 #endif 407407 subs=mqttClient.subscribe("/Haus/EG/GZ/R14"); 408408 #ifdef Debug_MQTT 409409 Serial.print("subs success : "); 410410 Serial.print(subs); 411411 #endif 412412 subs=mqttClient.subscribe("/Haus/EG/GZ/R15"); 413413 #ifdef Debug_MQTT 414414 Serial.print("subs success : "); 415415 Serial.print(subs); 416416 #endif 417417 subs=mqttClient.subscribe("/Haus/EG/GZ/R16"); 418418 #ifdef Debug_MQTT 419419 Serial.print("subs success : "); 420420 Serial.print(subs); 421421 #endif 422422 subs=mqttClient.subscribe("/Haus/EG/GZ/F1"); 423423 #ifdef Debug_MQTT 424424 Serial.print("subs success : "); 425425 Serial.print(subs); 426426 #endif 427427 subs=mqttClient.subscribe("/Haus/EG/GZ/gVers"); 428428 #ifdef Debug_MQTT 429429 Serial.print("subs success : "); 430430 Serial.print(subs); 431431 #endif 432432 subs=mqttClient.subscribe("/Haus/EG/GZ/gUpdateTime"); 433433 #ifdef Debug_MQTT 434434 Serial.print("subs success : "); 435435 Serial.print(subs); 436436 #endif 437437 subs=mqttClient.subscribe("/Haus/EG/GZ/sUpdateTime"); 438438 #ifdef Debug_MQTT 439439 Serial.print("subs success : "); 440440 Serial.print(subs); 441441 #endif 442442 443443 } 444444#endif 445445} 446446 447447 448448// setup function 449449void setup() 450450{ 451451 // Init ethernet 452452 #ifdef EthernetAvail 453453 Ethernet.begin(mac); 454454 #endif 455455 // Init ds2438 if used 456456 #ifdef Wire1Avail 457457 a_ds2438.begin(); 458458 //b_ds2438.begin(); -- not used in this sketch 459459 #endif 460460 461461 // init pins as inputs 462462 pinMode(awin1, INPUT); 463463 digitalWrite(awin1, HIGH); // activate pull up 464464 pinMode(awin2, INPUT); 465465 digitalWrite(awin2, HIGH); // activate pull up 466466 pinMode(bwin1, INPUT); 467467 digitalWrite(bwin1, HIGH); // activate pull up 468468 pinMode(bwin2, INPUT); 469469 digitalWrite(bwin2, HIGH); // activate pull up 470470 pinMode(abeweg, INPUT); 471471 digitalWrite(abeweg, HIGH); // activate pull up 472472 pinMode(bbeweg, INPUT); 473473 474474 // init DHT22 if used 475475 #ifdef DhtAvail 476476 a_dht.begin(); 477477 //b_dht.begin(); // nut used in this sketch 478478 # endif 479479 480480// init mcp1 as input for button 481481#ifdef I2cAvail 482482 mcp1.begin(0); // Addr 0 (=Jumper settings on Mainboard) 483483 mcp1.pinMode(0, INPUT); // Define GPA0 on MCP1 as input 484484 mcp1.pullUp(0, HIGH); 485485 mcp1.pinMode(1, INPUT); // Define GPA1 on MCP1 as input 486486 mcp1.pullUp(1, HIGH); 487487 mcp1.pinMode(2, INPUT); // Define GPA2 on MCP1 as input 488488 mcp1.pullUp(2, HIGH); 489489 mcp1.pinMode(3, INPUT); // Define GPA3 on MCP1 as input 490490 mcp1.pullUp(3, HIGH); 491491 mcp1.pinMode(4, INPUT); // Define GPA4 on MCP1 as input 492492 mcp1.pullUp(4, HIGH); 493493 mcp1.pinMode(5, INPUT); // Define GPA5 on MCP1 as input 494494 mcp1.pullUp(5, HIGH); 495495 mcp1.pinMode(6, INPUT); // Define GPA6 on MCP1 as input // CHANGE THIS SETTING IF YOU LIKE TO USE THE PIN ON THE MAINBOARD AS INPUT (locally on mainboard) 496496 mcp1.pullUp(6, HIGH); 497497 mcp1.pinMode(7, OUTPUT); // Define GPA6 on MCP1 as Output // CHANGE THIS SETTING IF YOU LIKE TO USE THE PIN ON THE MAINBOARD AS INPUT (locally on mainboard) 498498 499499 mcp1.pinMode(8, INPUT); // Define GPB0 on MCP1 as Output 500500 mcp1.pullUp(8, HIGH); 501501 mcp1.pinMode(9, INPUT); // Define GPB0 on MCP1 as Output 502502 mcp1.pullUp(9, HIGH); 503503 mcp1.pinMode(10, INPUT); // Define GPB0 on MCP1 as Output 504504 mcp1.pullUp(10, HIGH); 505505 mcp1.pinMode(11, INPUT); // Define GPB0 on MCP1 as Output 506506 mcp1.pullUp(11, HIGH); 507507 mcp1.pinMode(12, INPUT); // Define GPB0 on MCP1 as Output 508508 mcp1.pullUp(12, HIGH); 509509 mcp1.pinMode(13, INPUT); // Define GPB0 on MCP1 as Output 510510 mcp1.pullUp(13, HIGH); 511511 mcp1.pinMode(14, INPUT); // Define GPB0 on MCP1 as Output // CHANGE THIS SETTING IF YOU LIKE TO USE THE PIN ON THE MAINBOARD AS INPUT (locally on piggy back board) 512512 mcp1.pullUp(14, HIGH); 513513 mcp1.pinMode(15, OUTPUT); // Define GPB0 on MCP1 as Output // CHANGE THIS SETTING IF YOU LIKE TO USE THE PIN ON THE MAINBOARD AS INPUT (locally on piggy back board) 514514 515515 // init mcp as outpus for relais 516516 mcp2.begin(1); // Addr 1 ( jumer setting on mainboard) 517517 mcp2.pinMode(0, OUTPUT); // Define GPA0 on mcp2 as input 518518 mcp2.pinMode(1, OUTPUT); // Define GPA1 on mcp2 as OUTPUT 519519 mcp2.pinMode(2, OUTPUT); // Define GPA2 on mcp2 as OUTPUT 520520 mcp2.pinMode(3, OUTPUT); // Define GPA3 on mcp2 as OUTPUT 521521 mcp2.pinMode(4, OUTPUT); // Define GPA4 on mcp2 as OUTPUT 522522 mcp2.pinMode(5, OUTPUT); // Define GPA5 on mcp2 as OUTPUT 523523 mcp2.pinMode(6, OUTPUT); // Define GPA6 on mcp2 as OUTPUT 524524 mcp2.pinMode(7, OUTPUT); // Define GPA7 on mcp2 as OUTPUT 525525 mcp2.pinMode(8, OUTPUT); // Define GPB0 on mcp2 as OUTPUT 526526 mcp2.pinMode(9, OUTPUT); // Define GPB1 on mcp2 as OUTPUT 527527 mcp2.pinMode(10, OUTPUT); // Define GPB2 on mcp2 as OUTPUT 528528 mcp2.pinMode(11, OUTPUT); // Define GPB3 on mcp2 as OUTPUT 529529 mcp2.pinMode(12, OUTPUT); // Define GPB4 on mcp2 as OUTPUT 530530 mcp2.pinMode(13, OUTPUT); // Define GPB5 on mcp2 as OUTPUT 531531 mcp2.pinMode(14, OUTPUT); // Define GPB6 on mcp2 as OUTPUT 532532 mcp2.pinMode(15, OUTPUT); // Define GPB7 on mcp2 as OUTPUT 533533#endif 534534 535535// connect to mqtt server and subscribe to topics 536536void reconnect(); 537537 538538} 539539 540540void loop() 541541{ 542542 543543 // publis version if version flag was set 544544 // you can use this message to check if the connection/board is available 545545 if (get_vers ==1) // get version flag was set in callback function 546546 { 547547 // convert versions integer to const char * 548548 itoa(V_MAJOR,vers_char,10); // Major version ( only one digit allowed) 549549 vers_char[1]='_'; // add _ as second character 550550 itoa(V_MINOR,&vers_char[2],10); // add V_MINOR to character array 551551 #ifdef EthernetAvail 552552 mqttClient.publish("/Haus/EG/GZ/Vers",vers_char); // publish version char 553553 #endif 554554 get_vers = 0; // reset get version flag 555555 } 556556 557557 if (get_update_time ==1) // get update time flag was set in callback function 558558 { 559559 // convert SENS_UPDATE_CNT integer to const char * 560560 itoa(SENS_UPDATE_CNT,update_char,10); 561561 #ifdef EthernetAvail 562562 mqttClient.publish("/Haus/EG/GZ/UpdateTime",update_char); // publish SENS_UPDATE_CNT 563563 #endif 564564 Serial.println("gesendet"); 565565 get_update_time = 0; // reset flag 566566 update_char[0] ='\\0'; 567567 } 568568 569569 // Check Contacts Room A 570570 // topic is send when status changes 571571 val_a_win[0] = digitalRead(awin1); // read contact 0 Room A 572572 if (val_a_win[0] != val_a_win_d[0] && val_a_win_d[0] == 1 ) // detect falling edge 573573 { 574574 #ifdef EthernetAvail 575575 mqttClient.publish("/Haus/EG/GZ/A_W1","0"); // transmit 0 576576 #endif 577577 } 578578 else 579579 { 580580 if (val_a_win[0] != val_a_win_d[0] && val_a_win_d[0] == 0) // detect rising edge 581581 { 582582 #ifdef EthernetAvail 583583 mqttClient.publish("/Haus/EG/GZ/A_W1","1"); // transmit 1 584584 #endif 585585 } 586586 } 587587 val_a_win_d[0] = val_a_win[0]; // save current value to detect rising or falling edge 588588 589589 val_a_win[1] = digitalRead(awin2); // read contact 1 room A 590590 if (val_a_win[1] != val_a_win_d[1] && val_a_win_d[1] == 1 ) // detect falling edge 591591 { 592592 #ifdef EthernetAvail 593593 mqttClient.publish("/Haus/EG/GZ/A_W2","0"); // transmit 0 594594 #endif 595595 } 596596 else 597597 { 598598 if (val_a_win[1] != val_a_win_d[1] && val_a_win_d[1] == 0) // detect rising edge 599599 { 600600 #ifdef EthernetAvail 601601 mqttClient.publish("/Haus/EG/GZ/A_W2","1"); // transmit 1 602602 #endif 603603 } 604604 } 605605 val_a_win_d[1] = val_a_win[1]; // save current value to detect rising or falling edge 606606 607607 608608 // Check Contacts Room B 609609 // topic is send when status changes 610610 val_b_win[0] = digitalRead(bwin1); // read status contact 0 room B 611611 if (val_b_win[0] != val_b_win_d[0] && val_b_win_d[0] == 1 ) // falling edge 612612 { 613613 #ifdef EthernetAvail 614614 mqttClient.publish("/Haus/EG/GZ/B_W1","0"); // transmit 0 615615 #endif 616616 } 617617 else 618618 { 619619 if (val_b_win[0] != val_b_win_d[0] && val_b_win_d[0] == 0) // detect rising edge 620620 { 621621 #ifdef EthernetAvail 622622 mqttClient.publish("/Haus/EG/GZ/B_W1","1"); // transmit 1 623623 #endif 624624 } 625625 } 626626 val_b_win_d[0] = val_b_win[0]; // save current value to detect rising or falling edge 627627 628628 val_b_win[1] = digitalRead(bwin2); // read status contact 1 room B 629629 if (val_b_win[1] != val_b_win_d[1] && val_b_win_d[1] == 1 ) // falling edge 630630 { 631631 #ifdef EthernetAvail 632632 mqttClient.publish("/Haus/EG/GZ/B_W2","0"); // transmit 0 633633 #endif 634634 } 635635 else 636636 { 637637 if (val_b_win[1] != val_b_win_d[1] && val_b_win_d[1] == 0) // rising edge 638638 { 639639 #ifdef EthernetAvail 640640 mqttClient.publish("/Haus/EG/GZ/B_W2","1"); // transmit 1 641641 #endif 642642 } 643643 } 644644 val_b_win_d[1] = val_b_win[1]; // save current value to detect rising or falling edge 645645 646646 647647 648648 // motion detect HC-SR501 room A 649649 val_beweg[0] = digitalRead(abeweg); // read status of motion detect 650650 //Serial.println(val_beweg[0]); 651651 if (val_beweg[0] != val_beweg_d[0] && val_beweg_d[0] == 1 ) // falling edge detected 652652 { 653653 // send update that motion is off 654654 abeweg_active = 0; // save that motion detect is off 655655 #ifdef EthernetAvail 656656 mqttClient.publish("/Haus/EG/GZ/A_B","0"); // transmit 0 657657 #endif 658658 } 659659 else 660660 { 661661 if (val_beweg[0] != val_beweg_d[0] && val_beweg_d[0] == 0) // rising edge 662662 { 663663 abeweg_active = 1; // save that motion detect is active 664664 #ifdef EthernetAvail 665665 mqttClient.publish("/Haus/EG/GZ/A_B","1"); // transmit 1 666666 #endif 667667 } 668668 else // no edged detected 669669 { 670670 if (abeweg_active == 1) // motion was detected , send update after 3000 tics 671671 { 672672 counter_abwege++; // increment counter 673673 if (counter_abwege == 3000) 674674 { 675675 // transmit 1 again to reset a follow up counter counter (in openhab rules) 676676 counter_abwege = 0; // reset counter to re-transmit the motion detect active message 677677 #ifdef EthernetAvail 678678 mqttClient.publish("/Haus/EG/GZ/A_B","1"); // transmit 1 , motion is still active after 3000 tics count 679679 #endif 680680 } 681681 } 682682 } 683683 } 684684 val_beweg_d[0] = val_beweg[0]; // save current value to detect rising or falling edge 685685 686686 687687 688688 // motion detect HC-SR501 room B 689689 val_beweg[1] = digitalRead(bbeweg); // read status 690690 if (val_beweg[1] != val_beweg_d[1] && val_beweg_d[1] == 1 ) // falling edge detected 691691 { 692692 // motion is off , send 0 693693 bbeweg_active = 0; // store that motion is off 694694 #ifdef EthernetAvail 695695 mqttClient.publish("/Haus/EG/GZ/B_B","0"); // transmit 0 696696 #endif 697697 } 698698 else 699699 { 700700 if (val_beweg[1] != val_beweg_d[1] && val_beweg_d[1] == 0) // detect rising edge 701701 { 702702 bbeweg_active = 1; // store motion detected 703703 #ifdef EthernetAvail 704704 mqttClient.publish("/Haus/EG/GZ/B_B","1"); // transmit 1 705705 #endif 706706 } 707707 else // no edged detected 708708 { 709709 if (bbeweg_active == 1) // motion still active, trtansmit update after 3000 tics 710710 { 711711 counter_bbwege++; 712712 if (counter_bbwege == 3000) 713713 { 714714 // transmit 1 again to reset a follow up counter counter (in openhab rules) 715715 counter_bbwege = 0; // reset counter to re-transmit the motion detect active message 716716 #ifdef EthernetAvail 717717 mqttClient.publish("/Haus/EG/GZ/B_B","1"); // transmit 1 , motion is still active after 3000 tics count 718718 #endif 719719 } 720720 } 721721 } 722722 } 723723 val_beweg_d[1] = val_beweg[1]; // save current value to detect rising or falling edge 724724 725725 mcp1.digitalWrite(15, digitalRead(abeweg)); // debug ouput for motion detected on pinout of maintenance 726726 727727 // buttons 728728 // one for loop to check all 16 buttons 729729 for (int button_nr=0;button_nr<16;button_nr++) 730730 { 731731 if ((button_flag==100) || (button_flag==button_nr)) // if button_flag ==100 no buttonstroke detected 732732 // if a button was pressed the button_flag contains the counter value how often the button was pressed 733733 // all other buttons are deactivated while a sequence of one button is counted 734734 { 735735 val [button_nr]= mcp1.digitalRead(button_nr); // read each input ( pull up is active) 736736 // val_d : old value, Typically 1 , because of pull-up 737737 // if state changes and earlier states was one a keystroke is detected 738 // 739738 if (val[button_nr]!= val_d[button_nr]&& val_d[button_nr]== 1) // falling edge 740739 { 741740 tastencounter[button_nr]++; // pressed button detected 742741 delaycounter [button_nr]= 0; // reset delay counter to wait for a second push within that time 743742 button_flag= button_nr; // save number of pressed button to deactivate other buttons 744743 } 745744 val_d [button_nr]= val[button_nr]; // store current status to detect edge 746745 747746 // reset button counter to ensure that button counter is always in the range of 0..3 748747 // a button topic message will be 0..3 ( each button can be uses for three different commands 749748 if (tastencounter [button_nr]> 3) 750749 { 751750 tastencounter [button_nr]= 0; 752751 button_flag = 100; // reset to detect new keytroke 753752 } 754753 755754 // Delaycounter 756755 // tastcounter>0 : button was presed at least one time 757756 // wait defined number of counts for a second or third buttonstroke 758757 if (tastencounter [button_nr]> 0 && delaycounter [button_nr]< TASTER_WAIT) // button is pressed at least one time 759758 { 760759 delaycounter[button_nr]++; 761760 } 762761 763762 // delay counter is active by at least one buttonstroke 764763 // either the button is presses a second time and the counter is reset 765764 // or the defined max value was reached - its ready to publish the message 766765 if (delaycounter[button_nr]>= TASTER_WAIT) 767766 { 768767 delaycounter [button_nr]= 0; // reset counter 769768 // build message string depeoding on the number ob buttonstrokes during the defined max delay time 770769 switch (tastencounter[button_nr]) { 771770 // not reached anytime 772771 // case 0: 773772 // valBuffer [button_nr]= '1'; 774773 // valBuffer[1] = '\\0'; 775774 // mqttClient.publish("/openHAB/Taster1", valBuffer); 776775 // break; 777776 case 1: 778777 valBuffer [0]= '1'; 779778 valBuffer[1] = '\\0'; 780779 break; 781780 case 2: 782781 valBuffer [0]= '2'; 783782 valBuffer[1] = '\\0'; 784783 break; 785784 case 3: 786785 valBuffer [0]= '3'; 787786 valBuffer[1] = '\\0'; 788787 break; 789788 default: 790789 // if nothing else matches, do the default 791790 // default is optional 792791 break; 793792 } 794793 // publish message 795794 #ifdef EthernetAvail 796795 // generate topic string depending on the presses button number 797796 switch (button_nr) { 798797 // I use the buttons 8..15 in the room b 799798 // the topic is different for that button numbers 800799 case 8 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 801800 measBuffer[14]='8'; // CHANGE INDEX NUMBER DEPENDING ON THE LENGTH OF YOUR TOPIC STRING ABOVE 802801 measBuffer[15]='\\0'; // CHANGE TOPIC STRING HERE 803802 break; 804803 case 9 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 805804 measBuffer[14]='9'; 806805 measBuffer[15]='\\0'; 807806 break; 808807 case 10 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 809808 measBuffer[14]='1'; // two digits button number,CHANGE INDEX NUMBER DEPENDING ON THE LENGTH OF YOUR TOPIC STRING ABOVE 810809 measBuffer[15]='0'; // two digits button number, CHANGE INDEX NUMBER DEPENDING ON THE LENGTH OF YOUR TOPIC STRING ABOVE 811810 measBuffer[16]='\\0'; // CHANGE INDEX NUMBER DEPENDING ON THE LENGTH OF YOUR TOPIC STRING ABOVE 812811 break; 813812 case 11 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 814813 measBuffer[14]='1'; 815814 measBuffer[15]='1'; 816815 measBuffer[16]='\\0'; 817816 break; 818817 case 12 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 819818 measBuffer[14]='1'; 820819 measBuffer[15]='2'; 821820 measBuffer[16]='\\0'; 822821 break; 823822 case 13 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 824823 measBuffer[14]='1'; 825824 measBuffer[15]='3'; 826825 measBuffer[16]='\\0'; 827826 break; 828827 case 14 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 829828 measBuffer[14]='1'; 830829 measBuffer[15]='4'; 831830 measBuffer[16]='\\0'; 832831 break; 833832 case 15 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 834833 measBuffer[14]='1'; 835834 measBuffer[15]='5'; 836835 measBuffer[16]='\\0'; 837836 break; 838837 // button 0...7 has another topic and can be generated easily 839838 default: strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 840839 measBuffer[15]=char(button_nr+48); // add button number as chat to topic 841840 measBuffer[16]='\\0'; // end topic string with \\0 842841 break; 843842 } 844843 mqttClient.publish(measBuffer , valBuffer); // transmit topic and value , 845844 #endif 846845 847846 tastencounter [button_nr]= 0; // reset number of button strokes 848847 button_flag = 100; // reset to off state == 100 849848 } 850849 } 851850 } 852851 853852 // rare events updated when counter == SENS_UPDATE_CNT 854853 // the SENS_UPDATE_CNT refers not to a specified time - it depends on the time to execute the complete code above 855854 if (counter == SENS_UPDATE_CNT) 856855 { 857856 #ifdef Wire1Avail 858857 a_ds2438.update(); 859858 if (a_ds2438.isError()) { 860859 // would be good to publish a error topic here ? 861860 } else { 862861 // Temp 863862 tempC = a_ds2438.getTemperature(); // get Temperature of DS2438 864863 mqttClient.publish("/Haus/EG/GZ/DS_Temp", dtostrf(tempC, 1, 2, tempBuffer)); // publish temperature topic , CHANGE TOPIC NAME IF YOU WANT 865864 // brightness, volt channel A 866865 tempC = a_ds2438.getVoltage(DS2438_CHA); // get channel A voltage, variable tempC is used to save memory 867866 lux = tempC * 0.2441 * (1000000.0 / 390.0); // convert volt to lux 868867 mqttClient.publish("/Haus/EG/GZ/V1", dtostrf(tempC, 1, 2, tempBuffer)); // convert and publish topic 869868 // Voltage 2, not used in my case 870869 tempC = a_ds2438.getVoltage(DS2438_CHB); // Channel B get voltage 871870 mqttClient.publish("/Haus/EG/GZ/V2", dtostrf(tempC, 1, 2, tempBuffer)); // convert and publish topic 872871 } 873872#endif 874873 875874/* // DS 2438 is not used in my case 876875 //------------- get information of DS 2834 room B ----------------------------- / 877876#ifdef Wire1Avail 878877 b_ds2438.update(); 879878 if (b_ds2438.isError()) { 880879 // would be good to publish a error topic here ? 881880 } else { 882881 // Temp 883882 tempC = b_ds2438.getTemperature(); // get Temperature of DS2438 884883 mqttClient.publish("/Haus/EG/WZ/DS_Temp", dtostrf(tempC, 1, 2, tempBuffer)); // publish temperature topic, CHANGE TOPIC NAME IF YOU WANT 885884 // brightness, volt channel A 886885 tempC = b_ds2438.getVoltage(DS2438_CHA); // get channel A voltage, variable tempC is used to save memory 887886 lux = tempC * 0.2441 * (1000000.0 / 390.0); // convert volt to lux 888887 mqttClient.publish("/Haus/EG/WZ/V1", dtostrf(tempC, 1, 2, tempBuffer)); // convert tempC and publish, CHANGE TOPIC NAME IF YOU WANT 889888 // Voltage 2 890889 tempC = b_ds2438.getVoltage(DS2438_CHB); // Channel B get voltage 891890 mqttClient.publish("/Haus/EG/WZ/V2", dtostrf(tempC, 1, 2, tempBuffer)); // convert and publish topic 892891 } 893892#endif 894893*/ 895894 896895#ifdef DhtAvail 897896 // ---------------------------------------------------------- get DHT22 room A 898897 float h = a_dht.readHumidity(); // get humidity 899898 float t = a_dht.readTemperature(); // get temperature 900899 if (isnan(t) || isnan(h)) { 901900 #ifdef EthernetAvail 902901 // publish both values, anotuer topic could be send aditionally in error case 903902 mqttClient.publish("/Haus/EG/GZ/A_DHT_Temp", dtostrf(t, 1, 2, tempBuffer)); // convert and publish 904903 mqttClient.publish("/Haus/EG/GZ/A_DHT_Humi1", dtostrf(h, 1, 2, tempBuffer)); // convert and publish 905904 #endif 906905 } else { 907906 #ifdef EthernetAvail 908907 mqttClient.publish("/Haus/EG/GZ/A_DHT_Temp", dtostrf(t, 1, 2, tempBuffer)); // convert and publish 909908 mqttClient.publish("/Haus/EG/GZ/A_DHT_Humi1", dtostrf(h, 1, 2, tempBuffer)); // convert and publish 910909 #endif 911910 } 912911 // DHT22 room B , not used this case 913912 /* 914913 h = b_dht.readHumidity(); // get humidity 915914 t = b_dht.readTemperature(); // get temperature 916915 if (isnan(t) || isnan(h)) { 917916 #ifdef EthernetAvail 918917 // publish both values, anotuer topic could be send aditionally in error case 919918 mqttClient.publish("/Haus/EG/WZ/B_DHT_Temp", dtostrf(t, 1, 2, tempBuffer)); // convert and publish 920919 mqttClient.publish("/Haus/EG/WZ/B_DHT_Humi1", dtostrf(h, 1, 2, tempBuffer)); // convert and publish 921920 #endif 922921 } else { 923922 #ifdef EthernetAvail 924923 mqttClient.publish("/Haus/EG/WZ/B_DHT_Temp", dtostrf(t, 1, 2, tempBuffer)); // convert and publish 925924 mqttClient.publish("/Haus/EG/WZ/B_DHT_Humi1", dtostrf(h, 1, 2, tempBuffer)); // convert and publish 926925 #endif 927926 } 928927 */ 929928#endif 930929 931930 counter = 0; // reset counter to restart slow actions counter 932931 933932 reconnect(); // check if the mqtt client is still conected and reconnect if it is not 934933 935934 } 936935 937936 counter++; // increment counter 938937 #ifdef EthernetAvail 939938 mqttClient.loop(); 940939 #endif 941940 942941} 943
OpenHAB2 items File for rgb strip
json
Use this item File to generate a Color Item "Farbe" controlled by Alexa. Add a String item to generate a mqtt topic message.
1Color fWIFI_RGB_1 "Farbe" <slider> ["Lighting"] 2String WIFI_RGB_1_RGB (WIFI_RGB_1) {mqtt=">[mosquitto:/Haus/WZ/RGB:command:*:default]"}
Rules to control shutter by Alexa
json
Some rules to extend shutter control via alexa
1rule "Roll_Up_Alexa" 2when 3 Item Roll_Hoch received command ON 4then 5 sendCommand(EG_GZ_T2, ON) 6 7end 8 9 10rule "Roll_Up_Alexa" 11when 12 Item Roll_Hoch received command OFF 13then 14 sendCommand(EG_GZ_T2, OFF) 15 16end 17 18rule "Roll_Down_Alexa" 19when 20 Item Roll_Runter received command ON 21then 22 sendCommand(EG_GZ_T4, ON) 23 24end 25 26 27rule "Roll_Down_Alexa" 28when 29 Item Roll_Runter received command OFF 30then 31 sendCommand(EG_GZ_T4, OFF) 32 33end
Sketch for Arduino Ethernet + RGB Shiled as MQTT Client control RGB strip by Alexa
arduino
Use Ardunio Ethernet and RGB Shield to control rgb strip by Alexa ( respectively openhab2) aditionally you need - Openhab2 Items File - Openhab2 Rules File
1#define EthernetAvail 2#define Debug_MQTT 3 4 5#ifdef EthernetAvail // -------- 6 #include <Ethernet2.h> 7 #include <PubSubClient.h> // http://knolleary.net/arduino-client-for-mqtt/ 8#endif 9 10#include <SPI.h> 11#include <String.h> 12 13 14void callback(char* topic, byte* payload, unsigned int length); 15 16 17#ifdef EthernetAvail 18// MAC Adresse des Ethernet Shields 19byte mac[] = {0x90,0xA2,0xDA,0x10,0xe4,0x15 }; 20//the IP address for the shield: 21byte ip[] = { 192,168,0,230 }; 22// IP des MQTT Servers 23byte server[] = { 192, 168, 0, 30}; 24#endif 25 26int SENS_UPDATE_CNT =5000; 27#define TASTER_WAIT 100 28#define V_MAJOR 1 // nur eine ziffer erlaubt 29#define V_MINOR 1 30 31bool subs=0; 32bool get_vers=0; 33bool get_update_time=0; 34 35// ----------- General Variables 36int counter = 0; 37#include <Arduino.h> 38 39///////// Main Information for Controller Class 40boolean gotAMessage = false; // whether or not you got a message from the client yet 41String text=""; 42 43uint8_t IntRed; // 0..255 44uint8_t IntGreen; // 0..255 45uint8_t IntBlue; // 0..255 46 47// ********************* RGB 48// for RGB control 49int PinGreen =5; // LED connected to digital pin 5 50int PinBlue =6; // LED connected to digital pin 6 51int PinRed =3; // LED connected to digital pin 3 52 53/* undefine this if you don't want blinking LED for diagnosis */ 54#define LED_PIN 13 55#define SER_BAUD 9600 56 57int val = 0; 58int Counter = 0; 59 60#ifdef EthernetAvail 61 // =========================================================== 62 // MQTT Callback Funktion 63 // =========================================================== 64 void callback(char* topic, byte* payload, unsigned int length) { 65 // 66 String StringRGB=""; 67 unsigned int i = 0; 68 // temp variable to convert message to string 69 char message_buff[40]; 70 #ifdef Debug_MQTT 71 Serial.println("Message arrived: topic: " + String(topic)); 72 Serial.println("Length: " + String(length, DEC)); 73 #endif 74 75 // copy payload to char "\\0" ended 76 for (i = 0; i < length; i++) { 77 message_buff[i] = payload[i]; 78 } 79 message_buff[i] = '\\0'; 80 81 // convert message to string 82 String msgString = String(message_buff); 83 #ifdef Debug_MQTT 84 Serial.println("Payload: " + msgString); 85 #endif 86 87 // parse string for rgb values , seperated by ; 88 for (size_t i=0;i<msgString.length();i++) // start wirth first character 89 { 90 Serial.println("substring "); 91 Serial.println(msgString.substring(i,i+1)); 92 if ((msgString.substring(i,i+1)== ";") ) // check if seperater character reachred 93 { 94 Serial.println("String RGB " + StringRGB); 95 // komma is reached, copy data to int refering to color counter, e.g. Counter =0 red ; counter =1 green 96 if (Counter==0) 97 { 98 #ifdef Debug_MQTT 99 Serial.println("Counter Red"); 100 Serial.println(Counter); 101 Serial.println(StringRGB); 102 #endif 103 IntRed=StringRGB.toInt(); 104 StringRGB=""; 105 Counter++; 106 } 107 else 108 { 109 if (Counter==1) 110 { 111 #ifdef Debug_MQTT 112 Serial.println("Counter Green"); 113 Serial.println(Counter); 114 Serial.println(StringRGB); 115 #endif 116 IntGreen=StringRGB.toInt(); 117 StringRGB=""; 118 Counter++; 119 } 120 else 121 { 122 if (Counter==2) 123 { 124 #ifdef Debug_MQTT 125 Serial.println("Counter Blue"); 126 Serial.println(Counter); 127 Serial.println(StringRGB); 128 #endif 129 IntBlue=StringRGB.toInt(); 130 StringRGB=""; 131 Counter++; 132 } 133 } 134 } 135 } 136 else // add char to StringRGB if not == ";" 137 { 138 StringRGB+=msgString.substring(i,i+1); 139 } 140 } // end for loop 141 msgString="\\0"; 142 StringRGB="\\0"; 143 setRGBVal(); 144 Counter=0; 145} // End Callback function 146#endif 147 148 149#ifdef EthernetAvail 150 151// Ethernet Client as MQTT Clients 152EthernetClient ethClient; 153PubSubClient mqttClient(server, 1883, callback, ethClient); 154#endif 155 156 157 158void reconnect() 159{ 160 #ifdef EthernetAvail 161 // connect to mosquitto server 162 if (!mqttClient.connected()) { 163 subs=mqttClient.connect("RGB", "INSERT_MOSQUITTO_PW_HERE", "INSERT_MOSQUITTO_PW_HERE"); // adapt your mosquitto settings here 164 // subscribe to topic 165 subs=mqttClient.subscribe("/Haus/WZ/RGB"); 166 } 167 #endif 168} 169 170void setup() 171{ 172 // use serial if to putput received messages in debug mode 173 #ifdef Debug_MQTT 174 Serial.begin(9600); 175 Serial.println("starting"); 176 #endif 177 // Init Ethernets 178 #ifdef EthernetAvail 179 Ethernet.begin(mac); 180 #endif 181 182 #ifdef LED_PIN 183 pinMode(LED_PIN, OUTPUT); 184 #endif 185 186 /*#ifdef EthernetAvail 187 if (Ethernet.begin(mac) == 0) { 188 #ifdef Serial.println("Failed to configure Ethernet using DHCP \Static IP will be used"); #endif 189 // initialize the ethernet device not using DHCP: 190 Ethernet.begin(mac, ip, gateway, subnet); 191 #endif */ 192 193 // connect to mosquitto 194 reconnect(); 195} 196 197 198 199 200void loop() 201{ 202 # wait to receive topic 203 #ifdef EthernetAvail 204 mqttClient.loop(); 205 #endif 206 207} // end loop 208 209 210// function to update rgb output 211void setRGBVal() 212 { 213 analogWrite(PinRed, IntRed); 214 analogWrite(PinGreen, IntGreen); 215 analogWrite(PinBlue, IntBlue); 216 Serial.println("Rot: " + String(IntRed,DEC)); 217 Serial.println("Grn: " + String(IntGreen,DEC)); 218 Serial.println("Blau: " + String(IntBlue,DEC)); 219 } 220 221 222
Openhab2 Rules File for RGB
json
Openhab2 rules to generate a mqtt message on WIFI_RGB_1_RGB item
1import org.openhab.core.library.types.* 2 3var HSBType hsbValue 4var 5 int redValue 6var int greenValue 7var int blueValue 8var String RGBvalues 9 10 11rule "Set RGB 1 value" 12 when 13 Item fWIFI_RGB_1 changed 14 then 15 16 hsbValue = fWIFI_RGB_1.state as HSBType 17 18 redValue = (hsbValue.red.intValue*2.55).intValue 19 20 greenValue = (hsbValue.green.intValue*2.55).intValue 21 blueValue = (hsbValue.blue.intValue*2.55).intValue 22 23 24 25 RGBvalues= redValue.toString + ";" + greenValue.toString + ";" + 26 blueValue.toString + ";" 27 sendCommand( WIFI_RGB_1_RGB, RGBvalues ) 28 29 30 logInfo( "fWIFI_RGB_1", RGBvalues ) 31 end
Mainboard Sketch
arduino
A example sketch to control two 6times buttons and a few sensors used two sensor boards.
11#define EthernetAvail 22#define DhtAvail 33#define I2cAvail 44#define 5 Wire1Avail 65 76 87#ifdef EthernetAvail // 98 #include <Ethernet2.h> 109 11 #include <PubSubClient.h> // http://knolleary.net/arduino-client-for-mqtt/ 1210#endif 1311 1412// 15 callback funtion 1613void callback(char* topic, byte* payload, unsigned int length); 1714 1815#ifdef 19 EthernetAvail 2016 // MAC Adresse des Ethernet Shields 2117 byte mac[] = { 22 0xAA, 0xBB, 0xCC, 0xDD, 0xDD, 0xFF }; // ENTER MAC ADDRESS OF YOUR ARDUINO BOARD 23 HERE 2418 //the IP address for the shield: 2519 byte ip[] = { 192,168,0,52 }; 26 // ENTER STATIC IP ADDRESS OF YOUR ARDUINO BOARD HERE 2720 28 // IP des MQTT Servers 2921 byte server[] = { 192, 168, 0, 35}; // 30 ENTER IP ADDRESS OF OPENHABIAN Raspberry Pi HERE 3122#endif 3223 3324int SENS_UPDATE_CNT 34 =5000; // update 'time' counter for slow update e.g. 35 temperature, humidity 3625#define TASTER_WAIT 100 // 37 wait counter between keystrokes 3826 // 39 the counter is used to debounce the button 4027 // 41 but the implementation allows to use one button pressed twice or n times to extend 42 the 4328 // functionality 44 of the buttons 4529 // 46 in the case that the extended version shall be used change the counter value to 47 e.g. 800 4830#define V_MAJOR 49 1 // SW Major Version (one digit allowed) 5031#define V_MINOR 1 // SW Minor 51 Version (one digit allowed) 5232 5333bool subs=0; // some temp variables 54 5534bool get_vers=0; // global variable to communicate between callback funtion 56 and main function 5735bool get_update_time=0; // global variable to communicate 58 between callback funtion and main function 5936int counter = 0; // counter 60 to update slow sensor information 6137 6238#include <Arduino.h> 6339 6440//#ifdef 65 IrAvail // -------- IR ------- IR not supported in that sketch because of lack 66 of storage 6741//#include <IRremote.h> 6842//int RECV_PIN = 8; 6943//IRrecv 70 irrecv(RECV_PIN); 7144//decode_results results; 7245//int ledPin = A0; 7346//int 74 toggle, lock; 7547//#endif // --------------- IR 7648 7749// ------------------ 78 Pin definition of RJ45 jack of sensor X1 of Mainboard 7950// Room A 8051#define 81 A_DHTPIN 12 8252int awin1 = A5; 8353int awin2 = A4; 8454int abeweg = A3; 8555const 86 uint8_t ONE_WIRE_PIN = A2; 8756 8857// ------------------ Pin definition of RJ45 89 jack of sensor X2 of Mainboard 9058// room b 9159#define B_DHTPIN 5 9260const 93 uint8_t ONE_WIRE_PIN2 = 6; 9461int bbeweg = 7; 9562int bwin2 = 9; 9663int bwin1 97 = 13; 9864 9965// used by ethernet board itself 10066// Pin 10 - for Eth Interface 10167// 102 Pin 4 - for SS signal of SD card 10368 10469// as motion detect the HC-SR501 105 is used 10670 10771// motion detector room A (RJ 45 jack X1) 10872bool abeweg_active; 10973int 110 counter_abwege; 11174 11275// motion detector room B (RJ 45 jack X2) 11376bool bbeweg_active; 11477int 115 counter_bbwege; 11678 11779// initial values of motion detection 11880bool val_beweg[2] 119 = {0, 0}; 12081bool val_beweg_d[2] = {0, 0}; 12182 12283// contacts ( to control 123 e.g. window status ) room A 12484// two contacts can be used e.g. open, tilt 12585bool 126 val_a_win[2] = {0, 0}; 12786bool val_a_win_d[2] = {0, 0}; 12887 12988// contacts 130 ( to control e.g. window status ) room B 13189// two contacts can be used e.g. open, 132 tilt 13390bool val_b_win[2] = {0, 0}; 13491bool val_b_win_d[2] = {0, 0}; 13592 13693// 137 DHT22 13894#ifdef DhtAvail 13995 #include "DHT.h" 14096 #define DHTTYPE DHT22 141 //DHT11, DHT21, DHT22 14297 // DHT ROOM A active 14398 DHT a_dht(A_DHTPIN, DHTTYPE); 14499 145 // DHT ROOM B active 146100 //DHT b_dht(B_DHTPIN, DHTTYPE); // DHT22 on 147 Sensord board for Room B is not used 148101#endif 149102 150103 151104// 1 WIRE 152 for DS2438 153105#ifdef Wire1Avail 154106 #include <OneWire.h> 155107 #include 156 <Arduino.h> 157108 #include <DS2438.h> 158109 // define Address of DS2438 mounted 159 on sensor board of Room A 160110 uint8_t DS2438_address[] = { 0x26, 0x15, 0x70, 161 0x0F, 0x01, 0x00, 0x00, 0x8D }; // INSERT ADDR OF DS2438 OF USED SENSOR BOARD 162111 163 OneWire a_ow(ONE_WIRE_PIN); 164112 DS2438 a_ds2438(&a_ow, DS2438_address); 165113 166 // define Address of DS2438 mounted on sensor board of Room B 167114 //uint8_t 168 DS2438_address2[] = { 0x26, 0x2c, 0x50, 0x27, 0x01, 0x00, 0x00, 0x62 }; // delete 169 comment if a DS2438 is used for room B 170115 //OneWire b_ow(ONE_WIRE_PIN2); // 171 delete comment if a DS2438 is used for room B 172116 //DS2438 b_ds2438(&b_ow, DS2438_address2); 173 // delete comment if a DS2438 is used 174 for room B 175117#endif 176118 177119// I2C - Port Expander 178120#ifdef I2cAvail 179121 180 #include "Wire.h" 181122 #include <Adafruit_MCP23017.h> 182123 // Raum A / 183 Raum B gemeinsam 184124 Adafruit_MCP23017 mcp1; // Create MCP 1 185125 Adafruit_MCP23017 186 mcp2; // Create MCP 2 187126 // temporary variables for inputs 188127 int val[16] 189 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // current value of input 190 191128 int val_d[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // last 192 value (to detect falling edge) 193129 int delaycounter[16] = {0, 0, 0, 0, 0, 0, 194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Delaycounter for each input ( to detect a second 195 or third buttonstroke) 196130 int tastencounter[16] = {0, 0, 0, 0, 0, 0, 0, 0, 197 0, 0, 0, 0, 0, 0, 0, 0}; // Number on buttonstrokes 198131#endif 199132 200133// 201 DS2438 brightness 202134float lux; 203135// Variablen for temperature 204136float 205 tempC = 0; 206137// temp variabled to convert different data to string 207138char 208 tempBuffer[100]; 209139char measBuffer[100]; 210140char valBuffer[100]; 211141int 212 button_flag=100; 213142 214143 215144#ifdef EthernetAvail 216145 // Callback function 217 of MQTT. It is called when a subscribed topic is received. 218146 // =========================================================== 219147 220 void callback(char* topic, byte* payload, unsigned int length) { 221148 // 222 counter 223149 unsigned int i = 0; 224150 // temp variable to convert the 225 message to a string 226151 char message_buff[40]; // maximum message length 227 (including \\0 termination) 228152 229153 // message to char array, \\0 ended 230154 231 for (i = 0; i < length; i++) { 232155 message_buff[i] = payload[i]; 233156 234 } 235157 message_buff[i] = '\\0'; // termination 236158 237159 #ifdef 238 Debug_MQTT 239160 String msgString = String(message_buff); // convert message 240 to sting to output it on serial port 241161 Serial.println("Payload: " + 242 msgString); 243162 Serial.println(int(payload[0])-48); 244163 #endif 245164 246 247165 if (strcmp(topic, "/Haus/EG/GZ/gVers") == 0) // getVersion topic received 248166 249 { 250167 #ifdef Debug_MQTT 251168 Serial.println("Topic R1 gefunden"); 252169 253 Serial.println("PAyload :" + *payload); 254170 #endif 255171 get_vers=1; 256 // set get version flag to send version in main function 257172 } 258173 259174 260 if (strcmp(topic, "/Haus/EG/GZ/gUpdateTime") == 0) // get Update Time topic 261 received 262175 { 263176 #ifdef Debug_MQTT 264177 Serial.println("Topic 265 R1 gefunden"); 266178 Serial.println("PAyload :" + *payload); 267179 268 #endif 269180 get_update_time=1; // set get update time flag to send 270 version in main function 271181 } 272182 273183 if (strcmp(topic, "/Haus/EG/GZ/sUpdateTime") 274 == 0) // set Update Time topic received 275184 { 276185 #ifdef Debug_MQTT 277186 278 Serial.println("Topic R1 gefunden"); 279187 Serial.println("PAyload 280 :" + *payload); 281188 #endif 282189 SENS_UPDATE_CNT = atoi(message_buff); 283 // change update time of slow sensor data 284190 // 285 e.g. to reduce network traffic 286191 counter=0; // reset counter to avoid 287 faulty combinations 288192 } 289193 290194 // the relais are serially connected 291 292195 // you find a diagram here http://www.loxwiki.eu/display/LOX/Koppelrelais+und+Verriegelung 293 294196 if (strcmp(topic, "/Haus/EG/GZ/F1") == 0) // F1 topic received 295 , to controll roller shutter 296197 { 297198 switch (int(payload[0]) - 48) 298 { 299199 case 0: // stop shutter 300200 #ifdef I2cAvail 301201 // 302 delay(500); 303202 mcp2.digitalWrite(7, 0); // CHANGE PIN NUMER OF RELAIS 304 USED FOR SHUTTER 305203 delay(500); // delay is needed 306 to avoid switching direction too fast 307204 mcp2.digitalWrite(6, 0); 308 // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER // || 309205 310 #endif // 311 +24V---||---6 312206 break; // 313 || 314207 case 1: // move direction one // 315 (12, n.c) 316208 #ifdef I2cAvail //wago 317 788-304 / ------------- 318209 //delay(500); // 319 K1 (12, n.c) (11) 320 / 321210 mcp2.digitalWrite(6, 0); // CHANGE PIN NUMER OF RELAIS USED 322 FOR SHUTTER / ----------------/ -------------\phase 1 , e.g. 323 up 324211 delay(500); // 325 (11) / K2 326 (14,n.o) |Motor 327212 mcp2.digitalWrite(7, 1); // 328 CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER L --------/ ---------------------------------/ 329 phase 2 , e.g. down 330213 #endif // (14,n.o) 331 332214 break; // || 333 334215 case 2: // move direction two +24V---||---7 335 336216 #ifdef I2cAvail // || 337217 338 //delay(500); 339218 mcp2.digitalWrite(7, 0); // CHANGE PIN 340 NUMER OF RELAIS USED FOR SHUTTER 341219 delay(500); 342220 mcp2.digitalWrite(6, 343 1); // CHANGE PIN NUMER OF RELAIS USED FOR SHUTTER 344221 #endif 345222 346 break; 347223 default: 348224 // if nothing else matches, 349 do the default 350225 // default is optional 351226 break; 352227 353 } 354228 } 355229 356230 if (strcmp(topic, "/Haus/EG/GZ/R9") == 357 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 358231 { 359232 if 360 (int(payload[0]) - 48 == 1) // check if payload message is 1 361233 { 362234 363 #ifdef I2cAvail 364235 mcp2.digitalWrite(8, 1); // CHANGE PIN 365 NUMER OF RELAIS USED FOR RELAIS 366236 #endif 367237 } 368238 else 369 { // switch relais off in all other cases 370239 #ifdef 371 I2cAvail 372240 mcp2.digitalWrite(8,0); // switch relais off in all other 373 cases 374241 #endif 375242 } 376243 } 377244 378245 if (strcmp(topic, 379 "/Haus/EG/GZ/R10") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 380246 381 { 382247 if (int(payload[0]) - 48 == 1) // check if payload message is 383 1 384248 { 385249 #ifdef I2cAvail 386250 mcp2.digitalWrite(9, 387 1); // switch relais off in all other cases 388251 #endif 389252 } 390253 391 else { // switch relais off in all other cases 392254 #ifdef 393 I2cAvail 394255 mcp2.digitalWrite(9,0); // switch relais off in all other 395 cases 396256 #endif 397257 } 398258 } 399259 400260 if (strcmp(topic, 401 "/Haus/EG/GZ/R11") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 402261 403 { 404262 if (int(payload[0]) - 48 == 1) 405263 { 406264 #ifdef 407 I2cAvail 408265 mcp2.digitalWrite(10, 1); 409266 #endif 410267 } 411268 412 else { 413269 #ifdef I2cAvail 414270 mcp2.digitalWrite(10,0); 415271 416 #endif 417272 } 418273 } 419274 420275 421276 if (strcmp(topic, 422 "/Haus/EG/GZ/R12") == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 423277 424 { 425278 if (int(payload[0]) - 48 == 1) 426279 { 427280 #ifdef 428 I2cAvail 429281 mcp2.digitalWrite(11, 1); 430282 #endif 431283 } 432284 433 else { 434285 #ifdef I2cAvail 435286 mcp2.digitalWrite(11,0); 436287 437 #endif 438288 } 439289 } 440290 441291 if (strcmp(topic, "/Haus/EG/GZ/R13") 442 == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 443292 { 444293 if 445 (int(payload[0]) - 48 == 1) 446294 { 447295 #ifdef I2cAvail 448296 mcp2.digitalWrite(12, 449 1); 450297 #endif 451298 } 452299 else { 453300 #ifdef I2cAvail 454301 455 mcp2.digitalWrite(12,0); 456302 #endif 457303 } 458304 } 459305 460306 461 if (strcmp(topic, "/Haus/EG/GZ/R14") == 0) // CHANGE TOPIC NAME USED FOR RELAIS 462 (E.G. LIGHT) 463307 { 464308 if (int(payload[0]) - 48 == 1) 465309 { 466310 467 #ifdef I2cAvail 468311 mcp2.digitalWrite(13, 1); 469312 #endif 470313 471 } 472314 else { 473315 #ifdef I2cAvail 474316 mcp2.digitalWrite(13,0); 475317 476 #endif 477318 } 478319 } 479320 480321 if (strcmp(topic, "/Haus/EG/GZ/R15") 481 == 0) // CHANGE TOPIC NAME USED FOR RELAIS (E.G. LIGHT) 482322 { 483323 if 484 (int(payload[0]) - 48 == 1) 485324 { 486325 #ifdef I2cAvail 487326 mcp2.digitalWrite(14, 488 1); 489327 #endif 490328 } 491329 else { 492330 #ifdef I2cAvail 493331 494 mcp2.digitalWrite(14,0); 495332 #endif 496333 } 497334 } 498335 499336 500 if (strcmp(topic, "/Haus/EG/GZ/R16") == 0) // CHANGE TOPIC NAME USED FOR RELAIS 501 (E.G. LIGHT) 502337 { 503338 if (int(payload[0]) - 48 == 1) 504339 { 505340 506 #ifdef I2cAvail 507341 mcp2.digitalWrite(15, 1); 508342 #endif 509343 510 } 511344 else { 512345 #ifdef I2cAvail 513346 mcp2.digitalWrite(15,0); 514347 515 #endif 516348 } 517349 } 518350 } 519351#endif 520352 521353 522354 523355#ifdef 524 EthernetAvail 525356 // Ethernet Client to communicate as mqtt client 526357 EthernetClient 527 ethClient; 528358 // Server - Variable of byte type mit address of server (openhabian) 529359 530 // 1883 - standard tcp port for mqtt communication 531360 // name of callback 532 - function called when a mqqt topic is received 533361 // ethClient - ethernet 534 client used for mqtt communication 535362 PubSubClient mqttClient(server, 1883, 536 callback, ethClient); 537363#endif 538364 539365// temp data to save converted version 540 data as char 541366char vers_char[20]; 542367char update_char[20]; 543368 544369// 545 reconnect function to subscribe topics 546370void reconnect() 547371{ 548372 549373#ifdef 550 EthernetAvail 551374 // start mqtt connection if it is not open 552375 if 553 (!mqttClient.connected()) { 554376 subs=mqttClient.connect("EG_WZ_Kue", "openhabian", 555 "YOURPW"); // ENTER YOUR PW HERE 556377 #ifdef Debug_MQTT 557378 Serial.print("con 558 success : "); 559379 Serial.println(subs); 560380 #endif 561381 // 562 subscribe to topics 563382 subs=mqttClient.subscribe("/Haus/EG/GZ/R9"); 564383 565 #ifdef Debug_MQTT 566384 Serial.print("subs success : "); 567385 568 Serial.print(subs); 569386 #endif 570387 subs=mqttClient.subscribe("/Haus/EG/GZ/R10"); 571388 572 #ifdef Debug_MQTT 573389 Serial.print("subs success : "); 574390 575 Serial.print(subs); 576391 #endif 577392 subs=mqttClient.subscribe("/Haus/EG/GZ/R11"); 578393 579 #ifdef Debug_MQTT 580394 Serial.print("subs success : "); 581395 582 Serial.print(subs); 583396 #endif 584397 subs=mqttClient.subscribe("/Haus/EG/GZ/R12"); 585398 586 #ifdef Debug_MQTT 587399 Serial.print("subs success : "); 588400 589 Serial.print(subs); 590401 #endif 591402 subs=mqttClient.subscribe("/Haus/EG/GZ/R13"); 592403 593 #ifdef Debug_MQTT 594404 Serial.print("subs success : "); 595405 596 Serial.print(subs); 597406 #endif 598407 subs=mqttClient.subscribe("/Haus/EG/GZ/R14"); 599408 600 #ifdef Debug_MQTT 601409 Serial.print("subs success : "); 602410 603 Serial.print(subs); 604411 #endif 605412 subs=mqttClient.subscribe("/Haus/EG/GZ/R15"); 606413 607 #ifdef Debug_MQTT 608414 Serial.print("subs success : "); 609415 610 Serial.print(subs); 611416 #endif 612417 subs=mqttClient.subscribe("/Haus/EG/GZ/R16"); 613418 614 #ifdef Debug_MQTT 615419 Serial.print("subs success : "); 616420 617 Serial.print(subs); 618421 #endif 619422 subs=mqttClient.subscribe("/Haus/EG/GZ/F1"); 620423 621 #ifdef Debug_MQTT 622424 Serial.print("subs success : "); 623425 624 Serial.print(subs); 625426 #endif 626427 subs=mqttClient.subscribe("/Haus/EG/GZ/gVers"); 627428 628 #ifdef Debug_MQTT 629429 Serial.print("subs success : "); 630430 631 Serial.print(subs); 632431 #endif 633432 subs=mqttClient.subscribe("/Haus/EG/GZ/gUpdateTime"); 634433 635 #ifdef Debug_MQTT 636434 Serial.print("subs success : "); 637435 638 Serial.print(subs); 639436 #endif 640437 subs=mqttClient.subscribe("/Haus/EG/GZ/sUpdateTime"); 641438 642 #ifdef Debug_MQTT 643439 Serial.print("subs success : "); 644440 645 Serial.print(subs); 646441 #endif 647442 648443 } 649444#endif 650 651445} 652446 653447 654448// setup function 655449void setup() 656450{ 657451 658 // Init ethernet 659452 #ifdef EthernetAvail 660453 Ethernet.begin(mac); 661454 662 #endif 663455 // Init ds2438 if used 664456 #ifdef Wire1Avail 665457 a_ds2438.begin(); 666458 667 //b_ds2438.begin(); -- not used in this sketch 668459 #endif 669460 670461 671 // init pins as inputs 672462 pinMode(awin1, INPUT); 673463 digitalWrite(awin1, 674 HIGH); // activate pull up 675464 pinMode(awin2, INPUT); 676465 digitalWrite(awin2, 677 HIGH); // activate pull up 678466 pinMode(bwin1, INPUT); 679467 digitalWrite(bwin1, 680 HIGH); // activate pull up 681468 pinMode(bwin2, INPUT); 682469 digitalWrite(bwin2, 683 HIGH); // activate pull up 684470 pinMode(abeweg, INPUT); 685471 digitalWrite(abeweg, 686 HIGH); // activate pull up 687472 pinMode(bbeweg, INPUT); 688473 689474 // init 690 DHT22 if used 691475 #ifdef DhtAvail 692476 a_dht.begin(); 693477 //b_dht.begin(); 694 // nut used in this sketch 695478 # endif 696479 697480// init mcp1 as input 698 for button 699481#ifdef I2cAvail 700482 mcp1.begin(0); // Addr 0 (=Jumper settings 701 on Mainboard) 702483 mcp1.pinMode(0, INPUT); // Define GPA0 on MCP1 as input 703484 704 mcp1.pullUp(0, HIGH); 705485 mcp1.pinMode(1, INPUT); // Define GPA1 on MCP1 as 706 input 707486 mcp1.pullUp(1, HIGH); 708487 mcp1.pinMode(2, INPUT); // Define GPA2 709 on MCP1 as input 710488 mcp1.pullUp(2, HIGH); 711489 mcp1.pinMode(3, INPUT); // 712 Define GPA3 on MCP1 as input 713490 mcp1.pullUp(3, HIGH); 714491 mcp1.pinMode(4, 715 INPUT); // Define GPA4 on MCP1 as input 716492 mcp1.pullUp(4, HIGH); 717493 mcp1.pinMode(5, 718 INPUT); // Define GPA5 on MCP1 as input 719494 mcp1.pullUp(5, HIGH); 720495 mcp1.pinMode(6, 721 INPUT); // Define GPA6 on MCP1 as input // CHANGE THIS SETTING IF YOU LIKE TO 722 USE THE PIN ON THE MAINBOARD AS INPUT (locally on mainboard) 723496 mcp1.pullUp(6, 724 HIGH); 725497 mcp1.pinMode(7, OUTPUT); // Define GPA6 on MCP1 as Output // CHANGE 726 THIS SETTING IF YOU LIKE TO USE THE PIN ON THE MAINBOARD AS INPUT (locally on mainboard) 727 728498 729499 mcp1.pinMode(8, INPUT); // Define GPB0 on MCP1 as Output 730500 731 mcp1.pullUp(8, HIGH); 732501 mcp1.pinMode(9, INPUT); // Define GPB0 on MCP1 as 733 Output 734502 mcp1.pullUp(9, HIGH); 735503 mcp1.pinMode(10, INPUT); // Define 736 GPB0 on MCP1 as Output 737504 mcp1.pullUp(10, HIGH); 738505 mcp1.pinMode(11, INPUT); 739 // Define GPB0 on MCP1 as Output 740506 mcp1.pullUp(11, HIGH); 741507 mcp1.pinMode(12, 742 INPUT); // Define GPB0 on MCP1 as Output 743508 mcp1.pullUp(12, HIGH); 744509 mcp1.pinMode(13, 745 INPUT); // Define GPB0 on MCP1 as Output 746510 mcp1.pullUp(13, HIGH); 747511 mcp1.pinMode(14, 748 INPUT); // Define GPB0 on MCP1 as Output // CHANGE THIS SETTING IF YOU LIKE TO 749 USE THE PIN ON THE MAINBOARD AS INPUT (locally on piggy back board) 750512 mcp1.pullUp(14, 751 HIGH); 752513 mcp1.pinMode(15, OUTPUT); // Define GPB0 on MCP1 as Output // CHANGE 753 THIS SETTING IF YOU LIKE TO USE THE PIN ON THE MAINBOARD AS INPUT (locally on piggy 754 back board) 755514 756515 // init mcp as outpus for relais 757516 mcp2.begin(1); 758 // Addr 1 ( jumer setting on mainboard) 759517 mcp2.pinMode(0, OUTPUT); // Define 760 GPA0 on mcp2 as input 761518 mcp2.pinMode(1, OUTPUT); // Define GPA1 on mcp2 as 762 OUTPUT 763519 mcp2.pinMode(2, OUTPUT); // Define GPA2 on mcp2 as OUTPUT 764520 765 mcp2.pinMode(3, OUTPUT); // Define GPA3 on mcp2 as OUTPUT 766521 mcp2.pinMode(4, 767 OUTPUT); // Define GPA4 on mcp2 as OUTPUT 768522 mcp2.pinMode(5, OUTPUT); // Define 769 GPA5 on mcp2 as OUTPUT 770523 mcp2.pinMode(6, OUTPUT); // Define GPA6 on mcp2 as 771 OUTPUT 772524 mcp2.pinMode(7, OUTPUT); // Define GPA7 on mcp2 as OUTPUT 773525 774 mcp2.pinMode(8, OUTPUT); // Define GPB0 on mcp2 as OUTPUT 775526 mcp2.pinMode(9, 776 OUTPUT); // Define GPB1 on mcp2 as OUTPUT 777527 mcp2.pinMode(10, OUTPUT); // Define 778 GPB2 on mcp2 as OUTPUT 779528 mcp2.pinMode(11, OUTPUT); // Define GPB3 on mcp2 780 as OUTPUT 781529 mcp2.pinMode(12, OUTPUT); // Define GPB4 on mcp2 as OUTPUT 782530 783 mcp2.pinMode(13, OUTPUT); // Define GPB5 on mcp2 as OUTPUT 784531 mcp2.pinMode(14, 785 OUTPUT); // Define GPB6 on mcp2 as OUTPUT 786532 mcp2.pinMode(15, OUTPUT); // Define 787 GPB7 on mcp2 as OUTPUT 788533#endif 789534 790535// connect to mqtt server and subscribe 791 to topics 792536void reconnect(); 793537 794538} 795539 796540void loop() 797541{ 798542 799 800543 // publis version if version flag was set 801544 // you can use this 802 message to check if the connection/board is available 803545 if (get_vers ==1) 804 // get version flag was set in callback function 805546 { 806547 // convert 807 versions integer to const char * 808548 itoa(V_MAJOR,vers_char,10); // Major 809 version ( only one digit allowed) 810549 vers_char[1]='_'; // add 811 _ as second character 812550 itoa(V_MINOR,&vers_char[2],10); // add V_MINOR 813 to character array 814551 #ifdef EthernetAvail 815552 mqttClient.publish("/Haus/EG/GZ/Vers",vers_char); 816 // publish version char 817553 #endif 818554 get_vers = 0; // reset 819 get version flag 820555 } 821556 822557 if (get_update_time ==1) // get update 823 time flag was set in callback function 824558 { 825559 // convert SENS_UPDATE_CNT 826 integer to const char * 827560 itoa(SENS_UPDATE_CNT,update_char,10); 828561 829 #ifdef EthernetAvail 830562 mqttClient.publish("/Haus/EG/GZ/UpdateTime",update_char); 831 // publish SENS_UPDATE_CNT 832563 #endif 833564 Serial.println("gesendet"); 834565 835 get_update_time = 0; // reset flag 836566 update_char[0] ='\\0'; 837567 838 } 839568 840569 // Check Contacts Room A 841570 // topic is send when status 842 changes 843571 val_a_win[0] = digitalRead(awin1); // read contact 0 Room A 844572 845 if (val_a_win[0] != val_a_win_d[0] && val_a_win_d[0] == 1 ) // detect falling 846 edge 847573 { 848574 #ifdef EthernetAvail 849575 mqttClient.publish("/Haus/EG/GZ/A_W1","0"); 850 // transmit 0 851576 #endif 852577 } 853578 else 854579 { 855580 if 856 (val_a_win[0] != val_a_win_d[0] && val_a_win_d[0] == 0) // detect rising edge 857581 858 { 859582 #ifdef EthernetAvail 860583 mqttClient.publish("/Haus/EG/GZ/A_W1","1"); 861 // transmit 1 862584 #endif 863585 } 864586 } 865587 val_a_win_d[0] 866 = val_a_win[0]; // save current value to detect rising or falling edge 867588 868589 869 val_a_win[1] = digitalRead(awin2); // read contact 1 room A 870590 if (val_a_win[1] 871 != val_a_win_d[1] && val_a_win_d[1] == 1 ) // detect falling edge 872591 { 873592 874 #ifdef EthernetAvail 875593 mqttClient.publish("/Haus/EG/GZ/A_W2","0"); 876 // transmit 0 877594 #endif 878595 } 879596 else 880597 { 881598 if 882 (val_a_win[1] != val_a_win_d[1] && val_a_win_d[1] == 0) // detect rising edge 883599 884 { 885600 #ifdef EthernetAvail 886601 mqttClient.publish("/Haus/EG/GZ/A_W2","1"); 887 // transmit 1 888602 #endif 889603 } 890604 } 891605 val_a_win_d[1] 892 = val_a_win[1]; // save current value to detect rising or falling edge 893606 894607 895608 896 // Check Contacts Room B 897609 // topic is send when status changes 898610 899 val_b_win[0] = digitalRead(bwin1); // read status contact 0 room B 900611 if 901 (val_b_win[0] != val_b_win_d[0] && val_b_win_d[0] == 1 ) // falling edge 902612 903 { 904613 #ifdef EthernetAvail 905614 mqttClient.publish("/Haus/EG/GZ/B_W1","0"); 906 // transmit 0 907615 #endif 908616 } 909617 else 910618 { 911619 if (val_b_win[0] 912 != val_b_win_d[0] && val_b_win_d[0] == 0) // detect rising edge 913620 { 914621 915 #ifdef EthernetAvail 916622 mqttClient.publish("/Haus/EG/GZ/B_W1","1"); 917 // transmit 1 918623 #endif 919624 } 920625 } 921626 val_b_win_d[0] 922 = val_b_win[0]; // save current value to detect rising or falling edge 923627 924628 925 val_b_win[1] = digitalRead(bwin2); // read status contact 1 room B 926629 if 927 (val_b_win[1] != val_b_win_d[1] && val_b_win_d[1] == 1 ) // falling edge 928630 929 { 930631 #ifdef EthernetAvail 931632 mqttClient.publish("/Haus/EG/GZ/B_W2","0"); 932 // transmit 0 933633 #endif 934634 } 935635 else 936636 { 937637 if 938 (val_b_win[1] != val_b_win_d[1] && val_b_win_d[1] == 0) // rising edge 939638 { 940639 941 #ifdef EthernetAvail 942640 mqttClient.publish("/Haus/EG/GZ/B_W2","1"); 943 // transmit 1 944641 #endif 945642 } 946643 } 947644 val_b_win_d[1] 948 = val_b_win[1]; // save current value to detect rising or falling edge 949645 950646 951647 952648 953 // motion detect HC-SR501 room A 954649 val_beweg[0] = digitalRead(abeweg); 955 // read status of motion detect 956650 //Serial.println(val_beweg[0]); 957651 958 if (val_beweg[0] != val_beweg_d[0] && val_beweg_d[0] == 1 ) // falling edge 959 detected 960652 { 961653 // send update that motion is off 962654 abeweg_active 963 = 0; // save that motion detect is off 964655 #ifdef EthernetAvail 965656 mqttClient.publish("/Haus/EG/GZ/A_B","0"); 966 // transmit 0 967657 #endif 968658 } 969659 else 970660 { 971661 if (val_beweg[0] 972 != val_beweg_d[0] && val_beweg_d[0] == 0) // rising edge 973662 { 974663 abeweg_active 975 = 1; // save that motion detect is active 976664 #ifdef EthernetAvail 977665 978 mqttClient.publish("/Haus/EG/GZ/A_B","1"); // transmit 1 979666 980 #endif 981667 } 982668 else // no edged detected 983669 { 984670 985 if (abeweg_active == 1) // motion was detected , send update after 3000 tics 986671 987 { 988672 counter_abwege++; // increment counter 989673 if 990 (counter_abwege == 3000) 991674 { 992675 // transmit 1 again to 993 reset a follow up counter counter (in openhab rules) 994676 counter_abwege 995 = 0; // reset counter to re-transmit the motion detect active message 996677 #ifdef 997 EthernetAvail 998678 mqttClient.publish("/Haus/EG/GZ/A_B","1"); // 999 transmit 1 , motion is still active after 3000 tics count 1000679 #endif 1001680 1002 } 1003681 } 1004682 } 1005683 } 1006684 val_beweg_d[0] = val_beweg[0]; 1007 // save current value to detect rising or falling edge 1008685 1009686 1010687 1011688 1012 // motion detect HC-SR501 room B 1013689 val_beweg[1] = digitalRead(bbeweg); 1014 // read status 1015690 if (val_beweg[1] != val_beweg_d[1] && val_beweg_d[1] == 1016 1 ) // falling edge detected 1017691 { 1018692 // motion is off , send 0 1019693 1020 bbeweg_active = 0; // store that motion is off 1021694 #ifdef EthernetAvail 1022695 1023 mqttClient.publish("/Haus/EG/GZ/B_B","0"); // transmit 0 1024696 #endif 1025697 1026 } 1027698 else 1028699 { 1029700 if (val_beweg[1] != val_beweg_d[1] && val_beweg_d[1] 1030 == 0) // detect rising edge 1031701 { 1032702 bbeweg_active = 1; // store 1033 motion detected 1034703 #ifdef EthernetAvail 1035704 mqttClient.publish("/Haus/EG/GZ/B_B","1"); 1036 // transmit 1 1037705 #endif 1038706 } 1039707 else // no edged 1040 detected 1041708 { 1042709 if (bbeweg_active == 1) // motion still active, 1043 trtansmit update after 3000 tics 1044710 { 1045711 counter_bbwege++; 1046712 1047 if (counter_bbwege == 3000) 1048713 { 1049714 // transmit 1050 1 again to reset a follow up counter counter (in openhab rules) 1051715 counter_bbwege 1052 = 0; // reset counter to re-transmit the motion detect active message 1053716 #ifdef 1054 EthernetAvail 1055717 mqttClient.publish("/Haus/EG/GZ/B_B","1"); // 1056 transmit 1 , motion is still active after 3000 tics count 1057718 #endif 1058719 1059 } 1060720 } 1061721 } 1062722 } 1063723 val_beweg_d[1] = val_beweg[1]; 1064 // save current value to detect rising or falling edge 1065724 1066725 mcp1.digitalWrite(15, 1067 digitalRead(abeweg)); // debug ouput for motion detected on pinout of maintenance 1068 1069726 1070727 // buttons 1071728 // one for loop to check all 16 buttons 1072729 1073 for (int button_nr=0;button_nr<16;button_nr++) 1074730 { 1075731 if ((button_flag==100) 1076 || (button_flag==button_nr)) // if button_flag ==100 no buttonstroke detected 1077 1078732 // if a button 1079 was pressed the button_flag contains the counter value how often the button was 1080 pressed 1081733 // all 1082 other buttons are deactivated while a sequence of one button is counted 1083734 { 1084735 1085 val [button_nr]= mcp1.digitalRead(button_nr); // read each input ( pull 1086 up is active) 1087736 // val_d : old value, Typically 1 , because of pull-up 1088737 1089 // if state changes and earlier states was one a keystroke is detected 1090 1091 // 1092738 if (val[button_nr]!= val_d[button_nr]&& val_d[button_nr]== 1) 1093 // falling edge 1094739 { 1095740 tastencounter[button_nr]++; // pressed 1096 button detected 1097741 delaycounter [button_nr]= 0; // reset delay counter 1098 to wait for a second push within that time 1099742 button_flag= button_nr; 1100 // save number of pressed button to deactivate other buttons 1101743 } 1102744 1103 val_d [button_nr]= val[button_nr]; // store current status to detect edge 1104745 1105 1106746 // reset button counter to ensure that button counter is always in 1107 the range of 0..3 1108747 // a button topic message will be 0..3 ( each button 1109 can be uses for three different commands 1110748 if (tastencounter [button_nr]> 1111 3) 1112749 { 1113750 tastencounter [button_nr]= 0; 1114751 button_flag 1115 = 100; // reset to detect new keytroke 1116752 } 1117753 1118754 // Delaycounter 1119755 1120 // tastcounter>0 : button was presed at least one time 1121756 // wait defined 1122 number of counts for a second or third buttonstroke 1123757 if (tastencounter 1124 [button_nr]> 0 && delaycounter [button_nr]< TASTER_WAIT) // button is pressed at 1125 least one time 1126758 { 1127759 delaycounter[button_nr]++; 1128760 } 1129761 1130 1131762 // delay counter is active by at least one buttonstroke 1132763 // 1133 either the button is presses a second time and the counter is reset 1134764 // 1135 or the defined max value was reached - its ready to publish the message 1136765 1137 if (delaycounter[button_nr]>= TASTER_WAIT) 1138766 { 1139767 delaycounter 1140 [button_nr]= 0; // reset counter 1141768 // build message string depeoding 1142 on the number ob buttonstrokes during the defined max delay time 1143769 switch 1144 (tastencounter[button_nr]) { 1145770 // not reached anytime 1146771 // 1147 case 0: 1148772 // valBuffer [button_nr]= '1'; 1149773 // 1150 valBuffer[1] = '\\0'; 1151774 // mqttClient.publish("/openHAB/Taster1", 1152 valBuffer); 1153775 // break; 1154776 case 1: 1155777 valBuffer 1156 [0]= '1'; 1157778 valBuffer[1] = '\\0'; 1158779 break; 1159780 case 1160 2: 1161781 valBuffer [0]= '2'; 1162782 valBuffer[1] = '\\0'; 1163783 1164 break; 1165784 case 3: 1166785 valBuffer [0]= '3'; 1167786 1168 valBuffer[1] = '\\0'; 1169787 break; 1170788 default: 1171789 1172 // if nothing else matches, do the default 1173790 // default 1174 is optional 1175791 break; 1176792 } 1177793 // publish message 1178 1179794 #ifdef EthernetAvail 1180795 // generate topic string 1181 depending on the presses button number 1182796 switch (button_nr) { 1183 1184797 // I use the buttons 8..15 in the room b 1185798 // 1186 the topic is different for that button numbers 1187799 case 8 : 1188 strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC 1189 STRING HERE 1190800 measBuffer[14]='8'; // 1191 CHANGE INDEX NUMBER DEPENDING ON THE LENGTH OF YOUR TOPIC STRING ABOVE 1192801 measBuffer[15]='\\0'; 1193 // CHANGE TOPIC STRING HERE 1194802 break; 1195803 1196 case 9 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic 1197 sting [0..13] CHANGE TOPIC STRING HERE 1198804 measBuffer[14]='9'; 1199805 1200 measBuffer[15]='\\0'; 1201806 break; 1202807 1203 case 10 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting 1204 [0..13] CHANGE TOPIC STRING HERE 1205808 measBuffer[14]='1'; 1206 // two digits button number,CHANGE INDEX NUMBER DEPENDING 1207 ON THE LENGTH OF YOUR TOPIC STRING ABOVE 1208809 measBuffer[15]='0'; 1209 // two digits button number, CHANGE INDEX NUMBER DEPENDING 1210 ON THE LENGTH OF YOUR TOPIC STRING ABOVE 1211810 measBuffer[16]='\\0'; 1212 // CHANGE INDEX NUMBER DEPENDING ON THE LENGTH OF YOUR TOPIC 1213 STRING ABOVE 1214811 break; 1215812 case 11 1216 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC 1217 STRING HERE 1218813 measBuffer[14]='1'; 1219814 measBuffer[15]='1'; 1220815 1221 measBuffer[16]='\\0'; 1222816 break; 1223 1224817 case 12 : strcpy(measBuffer, "/Haus/EG/GZ/T"); 1225 // topic sting [0..13] CHANGE TOPIC STRING HERE 1226818 measBuffer[14]='1'; 1227819 1228 measBuffer[15]='2'; 1229820 measBuffer[16]='\\0'; 1230821 1231 break; 1232822 case 13 : strcpy(measBuffer, 1233 "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC STRING HERE 1234823 1235 measBuffer[14]='1'; 1236824 measBuffer[15]='3'; 1237825 1238 measBuffer[16]='\\0'; 1239826 break; 1240827 1241 case 14 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting 1242 [0..13] CHANGE TOPIC STRING HERE 1243828 measBuffer[14]='1'; 1244829 1245 measBuffer[15]='4'; 1246830 measBuffer[16]='\\0'; 1247831 1248 break; 1249832 case 1250 15 : strcpy(measBuffer, "/Haus/EG/GZ/T"); // topic sting [0..13] CHANGE TOPIC 1251 STRING HERE 1252833 measBuffer[14]='1'; 1253834 measBuffer[15]='5'; 1254835 1255 measBuffer[16]='\\0'; 1256836 break; 1257 1258837 // button 0...7 has another topic and can be generated 1259 easily 1260838 default: strcpy(measBuffer, "/Haus/EG/GZ/T"); // 1261 topic sting [0..13] CHANGE TOPIC STRING HERE 1262839 measBuffer[15]=char(button_nr+48); 1263 // add button number as chat to topic 1264840 measBuffer[16]='\\0'; 1265 // end topic string with \\0 1266841 break; 1267842 1268 } 1269843 mqttClient.publish(measBuffer , valBuffer); // 1270 transmit topic and value , 1271844 #endif 1272845 1273846 tastencounter 1274 [button_nr]= 0; // reset number of button strokes 1275847 button_flag = 100; 1276 // reset to off state == 100 1277848 } 1278849 } 1279850 } 1280851 1281852 // 1282 rare events updated when counter == SENS_UPDATE_CNT 1283853 // the SENS_UPDATE_CNT 1284 refers not to a specified time - it depends on the time to execute the complete 1285 code above 1286854 if (counter == SENS_UPDATE_CNT) 1287855 { 1288856 #ifdef Wire1Avail 1289857 1290 a_ds2438.update(); 1291858 if (a_ds2438.isError()) { 1292859 // 1293 would be good to publish a error topic here ? 1294860 } else { 1295861 // 1296 Temp 1297862 tempC = a_ds2438.getTemperature(); // get Temperature of DS2438 1298 1299863 mqttClient.publish("/Haus/EG/GZ/DS_Temp", dtostrf(tempC, 1, 2, 1300 tempBuffer)); // publish temperature topic , CHANGE TOPIC NAME IF YOU WANT 1301864 1302 // brightness, volt channel A 1303865 tempC = a_ds2438.getVoltage(DS2438_CHA); 1304 // get channel A voltage, variable tempC is used to save memory 1305866 lux 1306 = tempC * 0.2441 * (1000000.0 / 390.0); // convert volt to lux 1307867 mqttClient.publish("/Haus/EG/GZ/V1", 1308 dtostrf(tempC, 1, 2, tempBuffer)); // convert and publish topic 1309868 // 1310 Voltage 2, not used in my case 1311869 tempC = a_ds2438.getVoltage(DS2438_CHB); 1312 // Channel B get voltage 1313870 mqttClient.publish("/Haus/EG/GZ/V2", dtostrf(tempC, 1314 1, 2, tempBuffer)); // convert and publish topic 1315871 } 1316872#endif 1317873 1318874/* 1319 // DS 2438 is not used in my case 1320875 //------------- get information of 1321 DS 2834 room B ----------------------------- / 1322876#ifdef Wire1Avail 1323877 1324 b_ds2438.update(); 1325878 if (b_ds2438.isError()) { 1326879 // would 1327 be good to publish a error topic here ? 1328880 } else { 1329881 // Temp 1330 1331882 tempC = b_ds2438.getTemperature(); // get Temperature of DS2438 1332883 1333 mqttClient.publish("/Haus/EG/WZ/DS_Temp", dtostrf(tempC, 1, 2, tempBuffer)); 1334 // publish temperature topic, CHANGE TOPIC NAME IF YOU WANT 1335884 // brightness, 1336 volt channel A 1337885 tempC = b_ds2438.getVoltage(DS2438_CHA); // get channel 1338 A voltage, variable tempC is used to save memory 1339886 lux = tempC * 0.2441 1340 * (1000000.0 / 390.0); // convert volt to lux 1341887 mqttClient.publish("/Haus/EG/WZ/V1", 1342 dtostrf(tempC, 1, 2, tempBuffer)); // convert tempC and publish, CHANGE TOPIC NAME 1343 IF YOU WANT 1344888 // Voltage 2 1345889 tempC = b_ds2438.getVoltage(DS2438_CHB); 1346 // Channel B get voltage 1347890 mqttClient.publish("/Haus/EG/WZ/V2", dtostrf(tempC, 1348 1, 2, tempBuffer)); // convert and publish topic 1349891 } 1350892#endif 1351893*/ 1352894 1353895#ifdef 1354 DhtAvail 1355896 // ---------------------------------------------------------- 1356 get DHT22 room A 1357897 float h = a_dht.readHumidity(); // get humidity 1358898 1359 float t = a_dht.readTemperature(); // get temperature 1360899 if (isnan(t) 1361 || isnan(h)) { 1362900 #ifdef EthernetAvail 1363901 // publish both 1364 values, anotuer topic could be send aditionally in error case 1365902 mqttClient.publish("/Haus/EG/GZ/A_DHT_Temp", 1366 dtostrf(t, 1, 2, tempBuffer)); // convert and publish 1367903 mqttClient.publish("/Haus/EG/GZ/A_DHT_Humi1", 1368 dtostrf(h, 1, 2, tempBuffer)); // convert and publish 1369904 #endif 1370905 1371 } else { 1372906 #ifdef EthernetAvail 1373907 mqttClient.publish("/Haus/EG/GZ/A_DHT_Temp", 1374 dtostrf(t, 1, 2, tempBuffer)); // convert and publish 1375908 mqttClient.publish("/Haus/EG/GZ/A_DHT_Humi1", 1376 dtostrf(h, 1, 2, tempBuffer)); // convert and publish 1377909 #endif 1378910 1379 } 1380911 // DHT22 room B , not used this case 1381912 /* 1382913 h = 1383 b_dht.readHumidity(); // get humidity 1384914 t = b_dht.readTemperature(); 1385 // get temperature 1386915 if (isnan(t) || isnan(h)) { 1387916 #ifdef EthernetAvail 1388917 1389 // publish both values, anotuer topic could be send aditionally in error 1390 case 1391918 mqttClient.publish("/Haus/EG/WZ/B_DHT_Temp", dtostrf(t, 1, 1392 2, tempBuffer)); // convert and publish 1393919 mqttClient.publish("/Haus/EG/WZ/B_DHT_Humi1", 1394 dtostrf(h, 1, 2, tempBuffer)); // convert and publish 1395920 #endif 1396921 1397 } else { 1398922 #ifdef EthernetAvail 1399923 mqttClient.publish("/Haus/EG/WZ/B_DHT_Temp", 1400 dtostrf(t, 1, 2, tempBuffer)); // convert and publish 1401924 mqttClient.publish("/Haus/EG/WZ/B_DHT_Humi1", 1402 dtostrf(h, 1, 2, tempBuffer)); // convert and publish 1403925 #endif 1404926 1405 } 1406927 */ 1407928#endif 1408929 1409930 counter = 0; // reset counter 1410 to restart slow actions counter 1411931 1412932 reconnect(); // check if the mqtt 1413 client is still conected and reconnect if it is not 1414933 1415934 } 1416935 1417936 1418 counter++; // increment counter 1419937 #ifdef EthernetAvail 1420938 1421 mqttClient.loop(); 1422939 #endif 1423940 1424941} 1425
Openhab2 rules for Standard Room
json
some rules to control light and blinds by pushbutton and/or hanpanel standard room widget.
1var Shutter_state = 0 2 3// each time a button is used it poublish 4 a ON value on its topic 5// The status of the Relais is read to toggle it by 6 each keystroke 7//// 8rule "EG_GZ_Licht_Toggle_per_ON" 9when 10 Item 11 EG_GZ_T0 received command ON 12then 13 logInfo("eg_gz", "eg_gz licht change") 14 if 15 (EG_GZ_R6.state == ON) { // Relais status is on 16 sendCommand(EG_GZ_R6, 17 OFF) // switch Relais off 18 sendCommand(EG_GZ_T0, OFF) // reset button 19 to off 20 logDebug("eg_gz", "eg_gz light off") 21 logInfo("eg_gz", 22 "eg_gz light off") 23 } 24 else 25 { 26 if (EG_GZ_R6.state == OFF) 27 { // Relais is off 28 sendCommand(EG_GZ_R6, ON) // relais on 29 logDebug("eg_gz", 30 "eg_gz light on") 31 logInfo("eg_gz", "eg_gz light on") 32 } 33 else 34 { 35 sendCommand(EG_GZ_R6, ON) // relais status not initialised 36 logDebug("eg_gz", 37 "light on") 38 logInfo("eg_gz", "light on") 39 40 } 41 } 42end 43 44 45// 46 47rule "EG_GZ_Lighting_off" 48when 49 Item EG_GZ_T0 received command OFF 50then 51 logInfo("eg_gz", 52 "eg_gz licht cahnged") 53 sendCommand(EG_GZ_R6, OFF) 54 logDebug("eg_gz", 55 "eg_gz light off") 56 logInfo("eg_gz", "eg_gz light off") 57end 58 59 60rule 61 "EG_GZ_Blinds_Up" 62when 63 Item EG_GZ_T2 received command ON 64then 65 logInfo("eg_gz", 66 "eg_gz blind up or stop") 67 logInfo("eg_gz",receivedCommand.toString) 68 logInfo("eg_gz",EG_GZ_Rolladen.state.toString) 69 70 if (Shutter_state == 1 ) // shutter is moving 71 { 72 Shutter_state 73 = 0; // save new status stop 74 sendCommand(EG_GZ_Rolladen, STOP) // stop 75 blind item 76 sendCommand(EG_GZ_T4, OFF) // reset button status 77 sendCommand(EG_GZ_T2, 78 OFF) // reset button status 79 logInfo("eg_gz","Rolladen changed 80 from moving to stop") 81 } 82 else // state is stop 83 { 84 Shutter_state 85 = 1; // new status is up 86 sendCommand(EG_GZ_Rolladen, UP) // start moving 87 up 88 logInfo("eg_gz","Rolladen changed to up") 89 } 90end 91 92 93rule 94 "EG_GZ_Rolladen_down" 95when 96 Item EG_GZ_T4 received command ON 97then 98 logInfo("eg_gz", 99 "eg_gz Rollo down or stop") 100 logInfo("eg_gz",receivedCommand.toString) 101 logInfo("eg_gz",EG_GZ_Rolladen.state.toString) 102 103 if (Shutter_state == 2 ) // currently moving down 104 { 105 Shutter_state 106 = 0; // new status is stop 107 sendCommand(EG_GZ_T4, OFF) // reset button status 108 sendCommand(EG_GZ_T2, 109 OFF) // reset button status 110 sendCommand(EG_GZ_Rolladen, STOP) // stop blind 111 item 112 logInfo("eg_gz","Rolladen changed from moving to stop") 113 } 114 else 115 // state is stop 116 { 117 Shutter_state = 2; // new status is moding down 118 sendCommand(EG_GZ_Rolladen, 119 DOWN) // start moving down 120 logInfo("eg_gz","Rolladen changed to down") 121 122 } 123end 124 125// these rules are needed because openhab panel transmits 126 on and of values when button is pushed 127 128rule "EG_GZ_Rolladen_off_button_up" 129when 130 Item 131 EG_GZ_T2 received command OFF 132then 133 Shutter_state = 0; 134 sendCommand(EG_GZ_Rolladen, 135 STOP) 136 logInfo("eg_gz","Rolladen changed from moving to stop") 137end 138 139rule 140 "EG_GZ_Rolladen_off_button_down" 141when 142 Item EG_GZ_T4 received command OFF 143then 144 Shutter_state 145 = 0; 146 sendCommand(EG_GZ_Rolladen, STOP) 147 logInfo("eg_gz","Rolladen 148 changed from moving to stop") 149end 150 151
Comments
Only logged in users can leave comments