cordova app to scan local network for zeroconf devices of "_http." and "_workstation." type.
this approach doesn't require to access the individual api enpoints for every possible node in the subnet.
offers to re-scan after 6 seconds.
$ git clone [this repo]
$ cd [this repo]
$ cordova run android
- app should run on connected android device
$ cordova plugin add cordova-plugin-zeroconf
- zeroconf scan not always succesful. rescans sometimes required to see every device.
- some networks will have lots of different devices listed. Would be useful to filter on some known
txtRecord
value?.. - for more help setting up your system to test cordova apps follow this readme
to simulate a device on the network run this command on the dev laptop to see it shown in the scan results list:
$ avahi-publish-service "Dev Laptop" _http._tcp. 80 v=10.2.0 platform=emoncms path=/emoncms
you can change the above value for
path
to the "real" path (if different).
If you're vs code to live-preview your code in a browser, here are 6 different example responses from the zeroconf.watch()
function you can use as an example api responses:-
{"action":"added","service":{"domain":"local.","type":"_http._tcp.","name":"smartplug5339","port":0,"hostname":"smartplug5339._http._tcp.local.","ipv4Addresses":[],"ipv6Addresses":[],"txtRecord":{"smartplug5339._http._tcp.local.":"true"}}}
{"action":"resolved","service":{"domain":"local.","type":"_http._tcp.","name":"smartplug5339","port":80,"hostname":"","ipv4Addresses":["192.168.1.78"],"ipv6Addresses":[],"txtRecord":{}}}
{"action":"added","service":{"domain":"local.","type":"_http._tcp.","name":"openevse-8970","port":0,"hostname":"openevse-8970._http._tcp.local.","ipv4Addresses":[],"ipv6Addresses":[],"txtRecord":{"openevse-8970._http._tcp.local.":"true"}}}
{"action":"resolved","service":{"domain":"local.","type":"_http._tcp.","name":"openevse-8970","port":80,"hostname":"","ipv4Addresses":["192.168.1.81"],"ipv6Addresses":[],"txtRecord":{}}}
{"action":"resolved","service":{"domain":"local.","type":"_workstation._tcp.","name":"emonpi [b8:27:b8:27:b8:27]","port":9,"hostname":"","ipv4Addresses":["192.168.1.186"],"ipv6Addresses":["fe80::b03c:3354:7105:7555"],"txtRecord":{}}}
{"action":"added","service":{"domain":"local.","type":"_workstation._tcp.","name":"emonpi [b8:27:b8:27:b8:27]","port":0,"hostname":"emonpi [b8:27:b8:27:b8:27]._workstation._tcp.local.","ipv4Addresses":[],"ipv6Addresses":[],"txtRecord":{"emonpi [b8:27:b8:27:b8:27]._workstation._tcp.local.":"true"}}}
you could use the
avahi-discover
terminal command to see your actual results and change the above examples to match
if avahi-publish-service
not installed run:
$ sudo apt install avahi-utils