OpenWrt Ubus with ESP-NOW and KASA Smart home devices IOT Network

Here's what i have really been working on... bringing iot to my little gl-inet mifi router. I have successfully created a network of arduino/esp32 based sensors which send there data to there respective masters (esp32's) which pass the data in json form to openwrt ubus. On top of that, I created an event system and the kasa interface library so I can control kasa smart home devices and act on the live data, to for example, turn things on and off as required. smart bulbs can be used as status leds, for visual alerts, they can also be shared among events, i created a algorithm to decide which event is of highest priority and should have control of the led state. ESP-NOW protocol is some what long range, (150-200M) in my use cases without a hiccup, however, distances of over 1km have been achieved(with los and external antennas albeit). And yet, with all that horsepower ... my daughters favourite feature is the dance lights script i wrote, which flashes the 6-10 smart bulbs erratically. My bunnies (lion mane rabbits) have never been so well looked after. My gardens are green and the greenhouse is on point. The pool is clean enough to drink from, and my free time has increase dramatically ... and now I am bored again. So i figured its worth sharing and perhaps we can as a community give everyone a little free time back.

Now, keep in mind, I wrote all of this ...many times over i may add lol, so although it works wonderfully for me, its not super user friendly yet. I work almost solely from the command line so I didn't spend time on fancy ui's and such, strictly performance and functionality. the bells and whistles can be added later.

heres a quick overview of my setup


basically there is 3 components.

The Openwrt Router

  • connects to the ESP32 master unit/s via websocket.
  • ubus processes the real time data and decides whether or not there is an event to handle..too hot, too cold, to wet , to dry , too bright ...what have you. if an event is raised , ubus sends the command to the appropriate kasa smart device, to act upon
    it..turn a heater or ac on, humidifier, turn on the water ...what ever the case. once the event is handled, the device is turned off/on ...

The ESP32 Master/s

  • the masters are the end point for the sensor clusters.
  • In my set-up, every master/s is a ESP32's running as the esp-now master for the peered esp-now
    slaves(sensors),
  • The matser/s also provide the websocket connection for ubus on the router.
  • Each Master can reach each of its senders, and the router can reach each master. So effectively,
    from the router you can reach ..or change the send rate of each/every sender/s individually.

The ESP32 Senders

  • the senders do just that, broadcast the sensor data to their respective masters.
  • As previously mentioned, in esp-now, the senders call also be receivers..handy :slight_smile:

I will post my info later. and some screen shots of it in action.

4 Likes

as promised some logs..

hostle@dani:~/rootED-ubus/src$ sudo ./rootED-ubus&
[1] 6158
hostle@dani:~/rootED-ubus/src$ sudo ubus -v list
'device' @f38f6b4e
	"scan":{"ip":"String","timeout":"Integer","is_query":"Boolean"}
	"del":{"name":"String"}
	"start":{}
	"stop":{"name":"String"}
	"list":{"name":"String"}
'event' @c81f7635
	"add":{"name":"String","board_id":"String","dev_name":"String","led_name":"String","type":"Integer","timeout":"Integer","target":"String","plus":"String","minus":"String"}
	"del":{"name":"String"}
	"list":{"name":"String"}
'stream' @162a527f
	"add":{"name":"String","s_url":"String","board_id":"Integer","timeout":"Integer","verbose":"Boolean"}
	"del":{"name":"String"}
	"list":{"name":"String"}
hostle@dani:~/devo/ubus$ logread -f
Aug 15 21:58:47 dani user.info rootED-ubus[6160]: rootED-ubus-mod v1.00 has added has started
Aug 15 21:58:47 dani user.info rootED-ubus[6160]: connected as fbbf086a
Aug 15 21:59:20 dani authpriv.notice sudo:   hostle : TTY=pts/3 ; PWD=/home/hostle/rootED-ubus/src ; USER=root ; COMMAND=/usr/local/bin/ubus -v list
Aug 15 21:59:20 dani authpriv.info sudo: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
hostle@dani:~$ sudo ubus monitor
-> 0ceb15b3 #00000003         status: {"status":0}
-> f0e8043e #f0e8043e          hello: {}
<- f0e8043e #00000000     add_object: {"objpath":"stream","signature":{"add":{"name":3,"s_url":3,"board_id":5,"timeout":5,"verbose":7},"del":{"name":3},"list":{"name":3}}}
-> f0e8043e #00000000           data: {"objid":-1729047688,"objtype":1798140966}
-> f0e8043e #00000000         status: {"status":0}
<- f0e8043e #00000000     add_object: {"objpath":"event","signature":{"add":{"name":3,"board_id":3,"dev_name":3,"led_name":3,"type":5,"timeout":5,"target":3,"plus":3,"minus":3},"del":{"name":3},"list":{"name":3}}}
-> f0e8043e #00000000           data: {"objid":1932028218,"objtype":-1145090265}
-> f0e8043e #00000000         status: {"status":0}
<- f0e8043e #00000000     add_object: {"objpath":"device","signature":{"scan":{"ip":3,"timeout":5,"is_query":7},"del":{"name":3},"start":{},"stop":{"name":3},"list":{"name":3}}}
-> f0e8043e #00000000           data: {"objid":1682640399,"objtype":-523021937}
-> f0e8043e #00000000         status: {"status":0}
-> 6a08bffb #6a08bffb          hello: {}
<- 6a08bffb #00000000     add_object: {"objpath":"stream","signature":{"add":{"name":3,"s_url":3,"board_id":5,"timeout":5,"verbose":7},"del":{"name":3},"list":{"name":3}}}
-> 6a08bffb #00000000           data: {"objid":371872383,"objtype":330649950}
-> 6a08bffb #00000000         status: {"status":0}
<- 6a08bffb #00000000     add_object: {"objpath":"event","signature":{"add":{"name":3,"board_id":3,"dev_name":3,"led_name":3,"type":5,"timeout":5,"target":3,"plus":3,"minus":3},"del":{"name":3},"list":{"name":3}}}
-> 6a08bffb #00000000           data: {"objid":-937462219,"objtype":1235606538}
-> 6a08bffb #00000000         status: {"status":0}
<- 6a08bffb #00000000     add_object: {"objpath":"device","signature":{"scan":{"ip":3,"timeout":5,"is_query":7},"del":{"name":3},"start":{},"stop":{"name":3},"list":{"name":3}}}
-> 6a08bffb #00000000           data: {"objid":-208704690,"objtype":807457022}
-> 6a08bffb #00000000         status: {"status":0}
-> 671fe070 #671fe070          hello: {}
<- 671fe070 #00000000         lookup: {}
-> 671fe070 #00000000           data: {"objpath":"device","objid":-208704690,"objtype":807457022,"signature":{"scan":{"ip":3,"timeout":5,"is_query":7},"del":{"name":3},"start":{},"stop":{"name":3},"list":{"name":3}}}
-> 671fe070 #00000000           data: {"objpath":"event","objid":-937462219,"objtype":1235606538,"signature":{"add":{"name":3,"board_id":3,"dev_name":3,"led_name":3,"type":5,"timeout":5,"target":3,"plus":3,"minus":3},"del":{"name":3},"list":{"name":3}}}
-> 671fe070 #00000000           data: {"objpath":"stream","objid":371872383,"objtype":330649950,"signature":{"add":{"name":3,"s_url":3,"board_id":5,"timeout":5,"verbose":7},"del":{"name":3},"list":{"name":3}}}
-> 671fe070 #00000000         status: {"status":0}
COMMAND=/usr/local/bin/ubus call device start
Aug 15 22:02:51 dani authpriv.info sudo: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
Aug 15 22:02:52 dani user.info rootED-ubus[6160]: Device[0]: "TP-LINK_Smart Plug_25EC" Type: 2 Type Index: 0
Aug 15 22:02:52 dani user.info rootED-ubus[6160]: Add device: device_0
Aug 15 22:02:52 dani user.info rootED-ubus[6160]: Device[1]: "TP-LINK_Smart Bulb_AC2A" Type: 3 Type Index: 0
Aug 15 22:02:52 dani user.info rootED-ubus[6160]: Add device: device_1
Aug 15 22:02:52 dani user.info rootED-ubus[6160]: Add stream: stream_1
Aug 15 22:02:52 dani user.info RootED[6160]: RootED Sensor Stream[1] has Started
Aug 15 22:02:53 dani user.info RootED[6160]: Temperature Device Name: righty plug
Aug 15 22:02:53 dani user.info RootED[6160]: Temp Status Led Name: TP-LINK_Smart Bulb_AC2A
Aug 15 22:02:54 dani user.info RootED[6160]: Blob Board_id: 1
Aug 15 22:02:54 dani user.info RootED[6160]: RootED Events Server v1.00 has added [ board_1 ] to active events
Aug 15 22:02:54 dani user.info RootED[6160]: Humidity Device Name: lefty plug
Aug 15 22:02:54 dani user.info RootED[6160]: Hum Status Led Name: TP-LINK_Smart Bulb_AC2A
Aug 15 22:02:55 dani user.info RootED[6160]: Add event: event_0
Aug 15 22:02:55 dani user.info RootED[6160]: Add event: event_1
Aug 15 22:02:55 dani user.info RootED[6160]: RootED Events Server v1.00 has added [ board_1 ] to active events
Aug 15 22:02:55 dani authpriv.info sudo: pam_unix(sudo:session): session closed for user root
Aug 15 22:02:59 dani user.info RootED[6160]: Blob Board_id: 1
Aug 15 22:02:59 dani user.info RootED[6160]: New Event -> Type: 2
Aug 15 22:02:59 dani user.info RootED[6160]: Event Bias: 0.12500000
Aug 15 22:02:59 dani user.info RootED[6160]: Event Magnitude: 37.00
Aug 15 22:02:59 dani user.info RootED[6160]: Event Priority: 462.500
Aug 15 22:02:59 dani user.info RootED[6160]: Humidity Device State: 0
Aug 15 22:02:59 dani user.info RootED[6160]: Humidity Led State: 1
Aug 15 22:02:59 dani user.info RootED[6160]: Event Id: 1
Aug 15 22:02:59 dani user.info RootED[6160]: Humidity is Too High !!
Aug 15 22:02:59 dani user.info RootED[6160]: Target Humidity: 37.00 <- 45.00 -> 50.00
Aug 15 22:02:59 dani user.info RootED[6160]: Actual Humidity: 66.29
Aug 15 22:02:59 dani user.info RootED[6160]: *** New High Priority Event: 1 Priority: 462.500 ***
Aug 15 22:02:59 dani user.info RootED[6160]: New Event -> Type: 1
Aug 15 22:02:59 dani user.info RootED[6160]: Event Bias: 0.19999999
Aug 15 22:02:59 dani user.info RootED[6160]: Event Magnitude: 23.00
Aug 15 22:02:59 dani user.info RootED[6160]: Event Priority: 460.000
Aug 15 22:02:59 dani user.info RootED[6160]: Temperature Device State: 1
Aug 15 22:02:59 dani user.info RootED[6160]: Temperature Led State: 1
Aug 15 22:02:59 dani user.info RootED[6160]: Event Id: 0
Aug 15 22:02:59 dani user.info RootED[6160]: Temperature is Too Low !!
Aug 15 22:02:59 dani user.info RootED[6160]: Target Temperature: 23.00 <- 28.00 -> 33.00
Aug 15 22:02:59 dani user.info RootED[6160]: Actual Temperature: 20.60
Aug 15 22:03:04 dani user.info RootED[6160]: Blob Board_id: 1
Aug 15 22:03:04 dani user.info RootED[6160]: New Event -> Type: 1
Aug 15 22:03:04 dani user.info RootED[6160]: Event Bias: 0.19999999
Aug 15 22:03:04 dani user.info RootED[6160]: Event Magnitude: 2.40
Aug 15 22:03:04 dani user.info RootED[6160]: Event Priority: 48.000
Aug 15 22:03:04 dani user.info RootED[6160]: Temperature Device State: 1
Aug 15 22:03:04 dani user.info RootED[6160]: Temperature Led State: 1
Aug 15 22:03:04 dani user.info RootED[6160]: Event Id: 0
Aug 15 22:03:04 dani user.info RootED[6160]: Temperature is Too Low !!
Aug 15 22:03:04 dani user.info RootED[6160]: Target Temperature: 23.00 <- 28.00 -> 33.00
Aug 15 22:03:04 dani user.info RootED[6160]: Actual Temperature: 20.63
Aug 15 22:03:04 dani user.info RootED[6160]: New Event -> Type: 2
Aug 15 22:03:04 dani user.info RootED[6160]: Event Bias: 0.19999999
Aug 15 22:03:04 dani user.info RootED[6160]: Event Magnitude: 16.29
Aug 15 22:03:04 dani user.info RootED[6160]: Event Priority: 325.800
Aug 15 22:03:04 dani user.info RootED[6160]: Humidity Device State: 0
Aug 15 22:03:04 dani user.info RootED[6160]: Humidity Led State: 1
Aug 15 22:03:04 dani user.info RootED[6160]: Event Id: 1
Aug 15 22:03:04 dani user.info RootED[6160]: Humidity is Too High !!
Aug 15 22:03:04 dani user.info RootED[6160]: Target Humidity: 37.00 <- 45.00 -> 50.00
Aug 15 22:03:04 dani user.info RootED[6160]: Actual Humidity: 66.33
Aug 15 22:03:09 dani user.info RootED[6160]: Blob Board_id: 1
Aug 15 22:03:09 dani user.info RootED[6160]: New Event -> Type: 1
Aug 15 22:03:09 dani user.info RootED[6160]: Event Bias: 0.19999999
Aug 15 22:03:09 dani user.info RootED[6160]: Event Magnitude: 2.37
Aug 15 22:03:09 dani user.info RootED[6160]: Event Priority: 47.400
Aug 15 22:03:09 dani user.info RootED[6160]: Temperature Device State: 1
Aug 15 22:03:09 dani user.info RootED[6160]: Temperature Led State: 1
Aug 15 22:03:09 dani user.info RootED[6160]: Event Id: 0
Aug 15 22:03:09 dani user.info RootED[6160]: Temperature is Too Low !!
Aug 15 22:03:09 dani user.info RootED[6160]: Target Temperature: 23.00 <- 28.00 -> 33.00
Aug 15 22:03:09 dani user.info RootED[6160]: Actual Temperature: 20.62
Aug 15 22:03:09 dani user.info RootED[6160]: New Event -> Type: 2
Aug 15 22:03:09 dani user.info RootED[6160]: Event Bias: 0.19999999
Aug 15 22:03:09 dani user.info RootED[6160]: Event Magnitude: 16.33
Aug 15 22:03:09 dani user.info RootED[6160]: Event Priority: 326.600
Aug 15 22:03:10 dani user.info RootED[6160]: Humidity Device State: 0
Aug 15 22:03:10 dani user.info RootED[6160]: Humidity Led State: 1
Aug 15 22:03:10 dani user.info RootED[6160]: Event Id: 1
Aug 15 22:03:10 dani user.info RootED[6160]: Humidity is Too High !!
Aug 15 22:03:10 dani user.info RootED[6160]: Target Humidity: 37.00 <- 45.00 -> 50.00
Aug 15 22:03:10 dani user.info RootED[6160]: Actual Humidity: 66.35
Aug 15 22:03:14 dani user.info RootED[6160]: Blob Board_id: 1
Aug 15 22:03:14 dani user.info RootED[6160]: New Event -> Type: 1
Aug 15 22:03:14 dani user.info RootED[6160]: Event Bias: 0.19999999
Aug 15 22:03:14 dani user.info RootED[6160]: Event Magnitude: 2.38
Aug 15 22:03:14 dani user.info RootED[6160]: Event Priority: 47.600
Aug 15 22:03:15 dani user.info RootED[6160]: Temperature Device State: 1
Aug 15 22:03:15 dani user.info RootED[6160]: Temperature Led State: 1
Aug 15 22:03:15 dani user.info RootED[6160]: Event Id: 0
Aug 15 22:03:15 dani user.info RootED[6160]: Temperature is Too Low !!
Aug 15 22:03:15 dani user.info RootED[6160]: Target Temperature: 23.00 <- 28.00 -> 33.00
Aug 15 22:03:15 dani user.info RootED[6160]: Actual Temperature: 20.60
Aug 15 22:03:15 dani user.info RootED[6160]: New Event -> Type: 2
Aug 15 22:03:15 dani user.info RootED[6160]: Event Bias: 0.19999999
Aug 15 22:03:15 dani user.info RootED[6160]: Event Magnitude: 16.35
Aug 15 22:03:15 dani user.info RootED[6160]: Event Priority: 327.000
Aug 15 22:03:15 dani user.info RootED[6160]: Humidity Device State: 0
Aug 15 22:03:15 dani user.info RootED[6160]: Humidity Led State: 1
Aug 15 22:03:15 dani user.info RootED[6160]: Event Id: 1
Aug 15 22:03:15 dani user.info RootED[6160]: Humidity is Too High !!
Aug 15 22:03:15 dani user.info RootED[6160]: Target Humidity: 37.00 <- 45.00 -> 50.00
Aug 15 22:03:15 dani user.info RootED[6160]: Actual Humidity: 66.31

<- dfbc4162 #00000000         status: {"status":0,"objid":-337692494}
<- 82aab2a9 #00000000         status: {"status":0,"objid":169798604}
<- d2570762 #00000001         invoke: {"objid":1,"method":"send","data":{"id":"board_1","data":{"id":1,"stype":2,"c02":"1976.12","temperature":"20.30","humidity":"66.30","readingId":765}}}
-> dfbc4162 #00000000         invoke: {"objid":-337692494,"method":"board_1","data":{"id":1,"stype":2,"c02":"1976.12","temperature":"20.30","humidity":"66.30","readingId":765}}
-> 82aab2a9 #00000000         invoke: {"objid":169798604,"method":"board_1","data":{"id":1,"stype":2,"c02":"1976.12","temperature":"20.30","humidity":"66.30","readingId":765}}
-> d2570762 #00000001         status: {"status":0}
<- dfbc4162 #00000000         status: {"status":0,"objid":-337692494}
<- 82aab2a9 #00000000         status: {"status":0,"objid":169798604}
<- d2570762 #00000001         invoke: {"objid":1,"method":"send","data":{"id":"board_1","data":{"id":1,"stype":2,"c02":"1977.26","temperature":"20.32","humidity":"66.26","readingId":766}}}
-> dfbc4162 #00000000         invoke: {"objid":-337692494,"method":"board_1","data":{"id":1,"stype":2,"c02":"1977.26","temperature":"20.32","humidity":"66.26","readingId":766}}
-> 82aab2a9 #00000000         invoke: {"objid":169798604,"method":"board_1","data":{"id":1,"stype":2,"c02":"1977.26","temperature":"20.32","humidity":"66.26","readingId":766}}
-> d2570762 #00000001         status: {"status":0}
<- dfbc4162 #00000000         status: {"status":0,"objid":-337692494}
<- 82aab2a9 #00000000         status: {"status":0,"objid":169798604}
<- d2570762 #00000001         invoke: {"objid":1,"method":"send","data":{"id":"board_1","data":{"id":1,"stype":2,"c02":"1976.88","temperature":"20.30","humidity":"66.15","readingId":767}}}
-> dfbc4162 #00000000         invoke: {"objid":-337692494,"method":"board_1","data":{"id":1,"stype":2,"c02":"1976.88","temperature":"20.30","humidity":"66.15","readingId":767}}
-> 82aab2a9 #00000000         invoke: {"objid":169798604,"method":"board_1","data":{"id":1,"stype":2,"c02":"1976.88","temperature":"20.30","humidity":"66.15","readingId":767}}
-> d2570762 #00000001         status: {"status":0}
<- dfbc4162 #00000000         status: {"status":0,"objid":-337692494}
<- 82aab2a9 #00000000         status: {"status":0,"objid":169798604}


hostle@dani:~/rootED-ubus/src$ sudo ubus call device list '{"name":"all"}'
{
	{
		"name": "device_0",
		"sw_ver": "1.0.3 Build 220803 Rel.172301",
		"hw_ver": "3.0",
		"model": "KP400(US)",
					0000000000000000000000000000000000000000
		"deviceId": "0000000000000000000000000000000000000000",
		"oemId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		"hwId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		"rssi": -66,
		"latitude_i": 441817,
		"longitude_i": -816185,
		"alias": "TP-LINK_Smart Plug_25EC",
		"status": "new",
		"mic_type": "IOT.SMARTPLUGSWITCH",
		"feature": "TIM",
		"mac": "XX:XX:XX:XX:XX:XX",
		"updating": 0,
		"led_off": 0,
		"children": [
			{
				"id": "0000000000000000000000000000000000000000",
				"state": 0,
				"alias": "lefty plug",
				"on_time": 0,
				"next_action": {
					"type": -1
				}
			},
			{
				"id": "0000000000000000000000000000000000000000",
				"state": 1,
				"alias": "righty plug",
				"on_time": 2595,
				"next_action": {
					"type": -1
				}
			}
		],
		"child_num": 2,
		"ntc_state": 0,
		"err_code": 0
	},
	{
		"name": "device_1",
		"sw_ver": "1.0.5 Build 230613 Rel.151643",
		"hw_ver": "4.0",
		"model": "KL125(US)",
		"deviceId": "0000000000000000000000000000000000000000",
		"oemId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		"hwId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		"rssi": -49,
		"latitude_i": -1879048193,
		"longitude_i": -1879048193,
		"alias": "TP-LINK_Smart Bulb_AC2A",
		"status": "new",
		"obd_src": "tplink",
		"description": "Smart Wi-Fi LED Bulb with Color Changing",
		"mic_type": "IOT.SMARTBULB",
		"mic_mac": "XXXXXXXXXXXXX",
		"dev_state": "normal",
		"is_factory": false,
		"disco_ver": "1.0",
		"ctrl_protocols": {
			"name": "Linkie",
			"version": "1.0"
		},
		"active_mode": "none",
		"is_dimmable": 1,
		"is_color": 1,
		"is_variable_color_temp": 1,
		"light_state": {
			"on_off": 1,
			"mode": "normal",
			"hue": 120,
			"saturation": 100,
			"color_temp": 0,
			"brightness": 100
		},
		"preferred_state": [
			{
				"index": 0,
				"hue": 120,
				"saturation": 100,
				"color_temp": 0,
				"brightness": 100
			},
			{
				"index": 1,
				"hue": 0,
				"saturation": 100,
				"color_temp": 0,
				"brightness": 100
			},
			{
				"index": 2,
				"hue": 120,
				"saturation": 100,
				"color_temp": 0,
				"brightness": 100
			},
			{
				"index": 3,
				"hue": 240,
				"saturation": 100,
				"color_temp": 0,
				"brightness": 100
			}
		],
		"err_code": 0
	}
}

hostle@dani:~/rootED-ubus/src$ sudo ubus call event list '{"name":"all"}'
{
	{
		"event_name": "event_0",
		"event_id": 0,
		"event_dev": "righty plug",
		"event_dev_state": 1,
		"event_led": "TP-LINK_Smart Bulb_AC2A",
		"cur_read": 20.510000
	},
	{
		"event_name": "event_1",
		"event_id": 1,
		"event_dev": "lefty plug",
		"event_dev_state": 0,
		"event_led": "TP-LINK_Smart Bulb_AC2A",
		"cur_read": 66.350000
	}
}

hostle@dani:~/rootED-ubus/src$ sudo ubus call stream list '{"name":"all"}'
{
	{
		"stream_name": "stream_1",
		"stream_id": 1,
		"s_url": "ws://10.0.10.200:81/",
		"board_id": 1
	}
}

Looks like a great project. I use Home Assistant w/ESPHome so I don't think this would work for me. Plus all my devices are ESP8266s. I did see somewhere recently that some are actually installing Home Assistant on OpenWRT, which is also pretty cool!

ya, I prefer to roll my own. home assistant is far to generic for my use cases. And just for note, the esp8266 works just fine with esp-now, and in my setup the openwrt side of things doesn't care who is sending, it just wants the data in the correct format, who is sending it for the most part is unbeknown to ubus.