📜 ⬆️ ⬇️

Automation with convenient Pebble, stable Noolite and affordable esp8266

Since the last article (the link has passed almost a year. And this year, I rethought some things, the Internet and it turned out something like IoT :) → Internet of things).

I will try to briefly present the new portion of accumulated knowledge, describe where I aspire and what I want to achieve → I ask for reading.

Noolite F v2.0


In the title, I slipped the words: pebble & noolite. Let's go to them!
I still control the noolite with my pebble, garage and gate. About noolite already written a lot of useful information, the search shows new articles.

In addition to the blocks of the first generation of the noolite system, I had new ones: the power block SLF-1-200 (nooLite-F) with feedback ( and encryption of the new generation ).
')
image

power unit SB-1-150

image

and updated usb adapter MTRF-64 USB.

image

The whole world in my house on the system noolite.

More recently, I had a first-generation noolite USB adapter for 16 devices, and now for 64 channels (MTRF-64 USB), and also for new-generation devices with feedback (noolite F) in my “system home cramp”. Notechnology is moving in the right direction, creating a new modern device.

There are 2 most important options for new devices: feedback and the principle of identification of devices by addresses: ID. Now you don’t need to write to channel 1 of devices and control the channel. Now you can access a specific device in one channel by its ID and sending it an execution command.

Documentation for new generation units is available on the manufacturer’s website.

Everything works via serial port using python code.
# -*- coding: utf-8 -*- #!/usr/bin/env python import serial import time class NooLiteCommand: def __init__(self, ch, cmd, mode=0, ctr=0, res=0, fmt=0, d0=0, d1=0, d2=0, d3=0, id0=0, id1=0, id2=0, id3=0): self.st = 171 self.mode = mode self.ctr = ctr self.res = res self.ch = ch self.cmd = cmd self.fmt = fmt self.d0 = d0 self.d1 = d1 self.d2 = d2 self.d3 = d3 self.id0 = id0 self.id1 = id1 self.id2 = id2 self.id3 = id3 self.sp = 172 @property def crc(self): crc = sum([ self.st, self.mode, self.ctr, self.res, self.ch, self.cmd, self.fmt, self.d0, self.d1, self.d2, self.d3, self.id0, self.id1, self.id2, self.id3, ]) return crc if crc < 256 else divmod(crc, 256)[1] def to_bytes(self): return bytearray([ self.st, self.mode, self.ctr, self.res, self.ch, self.cmd, self.fmt, self.d0, self.d1, self.d2, self.d3, self.id0, self.id1, self.id2, self.id3, self.crc, self.sp ]) class NooliteSerial: def __init__(self, tty_name): self.tty = self._get_tty(tty_name) def on(self, ch): self.send_command(ch, 2, 2, 0) pass def off(self, ch): self.send_command(ch, 0, 2, 0) pass def status(self, ch): m = self.send_command(ch, 128, 2, 0) pass def send_command(self, ch, cmd, mode=0, ctr=0, res=0, fmt=0, d0=0, d1=0, d2=0, d3=0, id0=0, id1=0, id2=0, id3=0): command = NooLiteCommand(ch, cmd, mode, ctr, res, fmt, d0, d1, d2, d3, id0, id1, id2, id3) self.tty.write(command.to_bytes()) while True: bytes_response = list(self.tty.read(117)) if bytes_response: all_responses.append(bytes_response) if bytes_response[3] == 0: break else: break return all_responses @staticmethod def _get_tty(tty_name): serial_port = serial.Serial(tty_name, timeout=0.1) if not serial_port.is_open: serial_port.open() serial_port.flushInput() serial_port.flushOutput() return serial_port noo_serial = NooliteSerial('/dev/ttyUSB0') #ch, cmd, mode, ctr #noo_serial.send_command(1, 15, 2, 0) #    1 #noo_serial.send_command(0, 4, 2, 0) #switch #noo_serial.send_command(0, 2, 2, 0) #turn on #noo_serial.send_command(0, 3, 2, 0) #   #noo_serial.send_command(0, 0, 2, 0) #turn off #noo_serial.send_command(0, 15, 2, 0) #  #noo_serial.send_command(0, 128, 2, 0,0,1) #CMD_Read_State + fmt = 1 #noo_serial.on(0) #noo_serial.status(0) #noo_serial.off(0) #noo_serial.off(0) #noo_serial.status(0) #noo_serial.send_command(ch=0,ctr=8, cmd=4, id0=0,id1=0,id2=48,id3=114) #switch noolite ID 0.0.48.114 


Using the right functions at the right time, we can manage noolite blocks and get status from new ones. Since I have 99% of these are old blocks, it remains for me to use the old scheme of recording 1 block in 1 channel, taking into account the fact that the usb adapter supports both old and new blocks (thanks to the developers for compatibility without tambourine dances). On the fly, you can manage and receive the status of new blocks and manage old ones.

Until recently, there was not a single pushbutton switch in the house. I had to add light to the garage (I just got there to put things in order: D), and there was also a switch in the backlight of the makeup mirror. Knowing that there is a power unit SB-1-150 , I set the switch to normal and connected the unit. The backlight mirror made of white plate, 3w 4000 K light bulbs, white light. SB-1-150 is unique in that it can be placed in a wall switch box (switch) in the context of an existing circuit and connecting a push button switch to the unit itself.

Exactly what it takes to make up my girls :)
image

Pebble


When I started to get involved in automation, one of the main factors was: competent and convenient control.

It seems to be convenient to control from a smartphone, but ... and it is not convenient, to load an application every time, where small uncomfortable buttons, left advertising or until you reach the button ... all desire disappears. Voice control disappears, as it is also inconvenient and strange :) - let it remain in the plots of science fiction films.

image

And I solved the problem differently - through a wristwatch, which CONSTANTLY with me, do not occupy space and combine all the conveniences, mobility and speed of control. In general - Pebble is alive! But the most important thing is that, for hours, you can still write applications — your own applications, for managing your automation systems. This is generally a “bomb”.


The tragedy of closing pebble.com companies (Fitbit bought all pebble.com for patents) did not affect their performance and performance. Ios, android has recently been updated - the clock is untied from pebble cloud services (in case of termination of support) and authorization.

Visually, nothing has changed, but the service is still live: cloudpebble.net - WTF? This is a very useful thing. If you can write code on JS (and there is no desire to spend a lot of time on C ++) - please, welcome on board :) - cloud pebble allows you to quickly “add” the application. Well, that's how I did. Of course, JS performance should not be compared with C ++ (this is sacred), be patient. Yes!

Pebble cloud JS code for working with API
 var UI = require('ui'); var ajax = require('ajax'); var noolite = [ ['','http://your-home-server-ip-address:1183/mqtt/gate/slidegate', 'images/gate.png'], ['','http://your-home-server-ip-address:1183/mqtt/gate/garage', 'images/door.png'], ['!','http://your-home-server-ip-address:1183/noolite/switch/103', 'images/light.png'], [' ','http://your-home-server-ip-address:1183/noolite/switch/100', 'images/system2.png'], ['1  ','', 'images/light.png'], ['2  ','', 'images/light.png'], [' 1','http://your-home-server-ip-address:1183/noolite/switch/101', 'images/light.png'], [' 2','http://your-home-server-ip-address:1183/noolite/switch/102', 'images/light.png'], ['','', 'images/water.png'], ['','http://your-home-server-ip-address:1183/admin/system/1', 'images/system1.png'], ]; var water = [ [' ','http://your-home-server-ip-address:1183/admin/poliv/1', 'images/water.png'], [' ','http://your-home-server-ip-address:1183/admin/poliv/2', 'images/water.png'], [' 1 ','http://your-home-server-ip-address:1183/noolite/switch/20', 'images/water.png'], [' 2 ','http://your-home-server-ip-address:1183/noolite/switch/21', 'images/water.png'], [' 3 ','http://your-home-server-ip-address:1183/noolite/switch/22', 'images/water.png'], [' 4 ','http://your-home-server-ip-address:1183/noolite/switch/23', 'images/water.png'], [' 5 ','http://your-home-server-ip-address:1183/admin/poliv/switch/5', 'images/water.png'], [' 6 ','http://your-home-server-ip-address:1183/admin/poliv/switch/6', 'images/water.png'], [' 7 ','http://your-home-server-ip-address:1183/admin/poliv/switch/7', 'images/water.png'], [' 8 ','http://your-home-server-ip-address:1183/admin/poliv/switch/8', 'images/water.png'], [' 9 ','http://your-home-server-ip-address:1183/admin/poliv/switch/9', 'images/water.png'], ]; var light1 = [ [' ','http://your-home-server-ip-address:1183/noolite/switch/6', 'images/light.png'], [' ','http://your-home-server-ip-address:1183/noolite/switch/0', 'images/light.png'], ['  ','http://your-home-server-ip-address:1183/noolite/switch/1', 'images/light.png'], ['  ','http://your-home-server-ip-address:1183/noolite/switch/7', 'images/light.png'], ['  ','http://your-home-server-ip-address:1183/noolite/switch/8', 'images/light.png'], [' 1 ','http://your-home-server-ip-address:1183/noolite/switch/2', 'images/light.png'], [' 1 ','http://your-home-server-ip-address:1183/noolite/switch/3', 'images/light.png'], [' 1 ','http://your-home-server-ip-address:1183/noolite/switch/4', 'images/light.png'], [' ','http://your-home-server-ip-address:1183/noolite/switch/5', 'images/light.png'], [' ','http://your-home-server-ip-address:1183/noolite/switch/9', 'images/light.png'], ]; var light2 = [ [' ','http://your-home-server-ip-address:1183/noolite/switch/15', 'images/light.png'], [' ','http://your-home-server-ip-address:1183/noolite/switch/16', 'images/light.png'], [' 2 ','http://your-home-server-ip-address:1183/noolite/switch/10', 'images/light.png'], [' 2 ','http://your-home-server-ip-address:1183/noolite/switch/11', 'images/light.png'], [' 2 ','http://your-home-server-ip-address:1183/noolite/switch/12', 'images/light.png'], [' ','http://your-home-server-ip-address:1183/noolite/switch/13', 'images/light.png'], ['  ','http://your-home-server-ip-address:1183/noolite/switch/14', 'images/light.png'], ]; var menu = new UI.Menu({ sections: [{ items: [{ title: '', subtitle: '' }] }] }); var menu1 = new UI.Menu({ sections: [{ items: [{ title: '', subtitle: '' }] }] }); var menu2 = new UI.Menu({ sections: [{ items: [{ title: '', subtitle: '' }] }] }); var menu3 = new UI.Menu({ sections: [{ items: [{ title: '', subtitle: '' }] }] }); var items = []; for (var i=0; i<noolite.length; i++) { items[i] = { title: noolite[i][0], subtitle: '', icon: noolite[i][2] }; } var items1 = []; for (var i=0; i<light1.length; i++) { items1[i] = { title: light1[i][0], subtitle: '', icon: light1[i][2] }; } var items2 = []; for (var i=0; i<light2.length; i++) { items2[i] = { title: light2[i][0], subtitle: '', icon: light2[i][2] }; } var items3 = []; for (var i=0; i<water.length; i++) { items3[i] = { title: water[i][0], subtitle: '', icon: water[i][2] }; } menu.items(0, items); menu1.items(0, items1); menu2.items(0, items2); menu3.items(0, items3); menu.show(); menu.on('select', function(e) { if (e.itemIndex == 4) { menu1.show(); } else if (e.itemIndex == 5) { menu2.show(); } else if (e.itemIndex ==8) { menu3.show(); } else { var url = noolite[e.itemIndex][1]; console.log(url); ajax({ url: url, method: 'get' }, function(data) { console.log('switched OK'); }, function(error) { // Failure! console.log('error'); } ); } }); menu1.on('select', function(e) { var url = light1[e.itemIndex][1]; console.log(url); ajax({ url: url, method: 'get' }, function(data) { console.log('switched OK'); }, function(error) { // Failure! console.log('error'); } ); }); menu2.on('select', function(e) { var url = light2[e.itemIndex][1]; console.log(url); ajax({ url: url, method: 'get' }, function(data) { console.log('switched OK'); }, function(error) { // Failure! console.log('error'); } ); }); menu3.on('select', function(e) { var url = water[e.itemIndex][1]; ajax({ url: url, method: 'get' }, function(data) { console.log('switched OK'); }, function(error) { // Failure! console.log('error'); } ); }); 


The code is written, compiled directly on the cloud Pebble website and through the phone, downloading the compiled application can be installed on the watch - in seconds. Everything is so simple that any young automator can do it;)

It’s a pity ... it’s a pity that Pebble has ceased to exist and for the current moment I don’t see an alternative. Pebble Time will always be in our hearts!

MQTT


Last year I met such a wonderful protocol as MQTT (History: The first version of the protocol was developed by Dr. Andy Stanford-Clarke (IBM) and Arlen Nipper (Arcom) in 1999 and published under a royalty free license. The MQTT 3.1.1 specification was standardized by the OASIS consortium in 2014. Reference ).

There is a lot of information about how the protocol works and what kind of clients there are ... and was very disappointed that there are no real clients for mobile applications, only on android.

Understanding that the lack of a good mobile application is a very big problem (hole) in the field of IoT (many people speak and do very little ....) - I decided to create a client for android, ios, wp with my team, at work. so that it meets all the criteria and is comfortable. I’ll write about the client separately a little later, as well as how we made friends with ESP8266 + MTRF64 (Noolite) and prepared the Nodemcu firmware. It will be great, convenient, cheap and beautiful! Follow the news of the application here .

image

Now you can control Noolite lighting without any home servers and usb adapters for $ 50-100!

General scheme of home automation


The following scheme still exists:

Video surveillance: xeoma video surveillance [ip camera]
Server on the system unit: nginx + gunicorn + python + mqtt broker
Management: Pebble watch + MQTT client
Control modules (system nodes): ESP8266 + optocouplers \ sensors ds18b20, dht11 [22]

Due to the fact that I saw a lot of conveniences in the MQTT protocol, starting from the subscription architecture and instant communication with end IoT devices, ending with the fact that MQTT messages can fly through the router, without “forwarding” all kinds of ports and dancing with a tambourine. Conveniently! Fun! Fervently :)

All this led me to decentralization of the system, that is, the departure from one “home wormwood” and the transition to many so-called nodes and the use of the cloud MQTT broker (there are free MQTT brokers, for example: mqtt.ximxim.com (on the website login and pass for access )) - since esp8266 solve all problems, and it in turn works through wifi using the MQTT protocol.

Given that MQTT Buddy will provide script services, I won’t think at all that I need to write some home scripts, I’ll just create them in a cloud solution and this will be a real IoT and it works! From words to deeds!

ESP8266


When I became acquainted with this development board, I was immediately bribed by the fact that the board is small, there is support for the LUA programming language in Nodemcu. What you need + MQTT module is. In automation, simple and reliable solutions are the foundation. Cost from $ 2 to $ 5 for a fee (for 5 models immediately with a usb adapter on board) allows you to quickly deploy subsystems.

WiFi connection is fast, there are a lot of possibilities For example: control of the gate \ garage \ watering \ lighting. My 30% lawn irrigation works from the Noolite system (dry contact blocks), the remaining 70% of the esp8266 development board is controlled via LUA using the MQTT protocol.

Sample code on LUA for esp8266, getting ip and invoking MQTT work file
 --load credentials dofile("credentials.lua") function init(name, pass) wifi.setmode(wifi.STATION) wifi.sta.config(name, pass) wifi.sta.connect() tmr.alarm(0, 1000, 1, function() if wifi.sta.getip()== nil then print("IP unavaiable, Waiting...") else tmr.stop(0) print("Config done, IP is "..wifi.sta.getip()) print("mac : "..wifi.sta.getmac()) dofile("mqtt.lua") end end) end print("START!") init(SSID,PASSWORD) 


- mqtt.lua file

 local door = 7 -- gpio13 local window = 6 -- gpio12 local cooler = 5 --gpio14 local led = 4 --GPIO2 board led! LOW == turn ON local light1 = 8 --gpio 15 local light2 = 1 --gpio 5 local light3 = 2 -- gpio 4 function register_myself() m:subscribe("mqtt_buddy/#",0,function(conn) print ("subscribed to Xim mqtt.ximxim.com server") end) end m = mqtt.Client("MQTT_BUDDY_SHOW_ROOM", 120, MQTT_USER, MQTT_PASS) m:on("connect", function(client) print ("connected to Xim mqtt.ximxim.com server") end) m:on("offline", function(client) reconnect_mqtt() end) m:on("message", function(client, topic, data) --print(topic.." data:"..data) if topic == "mqtt_buddy/window" and data == "1" then print "open-close window..." gpio.write(window, gpio.HIGH) gpio.write(led, gpio.LOW) tmr.alarm(1, 1000, tmr.ALARM_SINGLE, function() gpio.write(window, gpio.LOW) gpio.write(led, gpio.HIGH) print "command to close\open window is sent!" end) elseif topic == "mqtt_buddy/door" and data == "1" then print "open-close door..." gpio.write(door, gpio.HIGH) gpio.write(led, gpio.LOW) tmr.alarm(2, 1000, tmr.ALARM_SINGLE, function() gpio.write(door, gpio.LOW) gpio.write(led, gpio.HIGH) print "command to close\open door is sent!" end) elseif topic == "mqtt_buddy/cooler" then if data == "1" then print "switch on fan..." gpio.write(cooler, gpio.HIGH) gpio.write(led, gpio.LOW) else print "switch off fan..." gpio.write(cooler, gpio.LOW) gpio.write(led, gpio.HIGH) end elseif topic == "mqtt_buddy/light1" then if data == "1" then print "switch light1 ON..." gpio.write(light1, gpio.HIGH) gpio.write(led, gpio.LOW) else print "switch light1 off..." gpio.write(light1, gpio.LOW) gpio.write(led, gpio.HIGH) end elseif topic == "mqtt_buddy/light2" then if data == "1" then print "switch light2 ON..." gpio.write(light2, gpio.HIGH) gpio.write(led, gpio.LOW) else print "switch light2 off..." gpio.write(light2, gpio.LOW) gpio.write(led, gpio.HIGH) end elseif topic == "mqtt_buddy/light3" then if data == "1" then print "switch light3 ON..." gpio.write(light3, gpio.HIGH) gpio.write(led, gpio.LOW) else print "switch light3 off..." gpio.write(light3, gpio.LOW) gpio.write(led, gpio.HIGH) end end end) m:connect(MQTT_SERVER, MQTT_SERVER_PORT, 0, function(conn) register_myself() end) 


Problem


Addressing a huge community of thinking people , I wanted to touch on an extremely important topic, or rather the problem I want to solve, but I don’t have enough time for deeper research / knowledge.

Essence: during decentralization, there remains video surveillance, which somehow DEMANDS having a host machine for (minimum) transferring source images either to a surveillance server (for example, ivideon) or generally processing video on a home machine, like xeoma (although they also have a cloud) one way or another, it is necessary to “FLIP” video streams, pictures on final computing powers (cloud solutions) - or vice versa - streaming is simple.

There are thoughts to get source images from the camera (there is also a problem here, not every camera has a URL for receiving a picture from the camera and it’s not at all clear how others will recognize this URL ...) and forward to the MQTT channel where binary data is supported ( there is a picture can be easily transferred via mqtt protocol in binary form).

Perhaps someone has already tried to implement such things = ESP8266 + IP web cam?
Call out pliz (bogdanovich.alex [@] gmail.com). I will be very grateful!

At implementation of probros, the home server question will disappear. What for? Because he eats electricity and the furnace heats me :)

image

All positive day and good mood!

Source: https://habr.com/ru/post/403273/


All Articles