3 ms·
I thought it would be interesting and appropriate to write the high level behavior code for a home automation system in Prolog so I did. It has evolved a lot,
by sprior 5y ago
I thought it would be interesting and appropriate to write the high level behavior code for a home automation system in Prolog so I did. It has evolved a lot, but has been running things in the house for almost 20 years now. It currently sends commands and receives stimuli via MQTT and is running in a Docker container.
- pedrovhb 5y agoI'd love to see this! That exact idea has been tingling through my mind for a while. I did some experiments, but Prolog (and anything logic programming, really) is a bit of a pain to integrate nicely with Python, which is what I'm building my system with. I'm looking to make things declarative as much as possible, but having Prolog manage the automation rules would be next level. I imagine it abstracts away a lot of the boilerplate and unnecessary implementation details.
- sprior 5y agoSee my reply to ojford with a bigger description below.
- sprior 5y agoInstead of trying to directly integrate for example Prolog and Python I set up services in whatever language/hardware and communicate between then using JSON messages transported via MQTT. With this method I've got Docker containers running Java, Python, and Prolog code interoperating with ESP8266 and ESP32 devices I programmed in C++ and anyone can talk to anyone. I also wrote a Java server that interfaces with an Insteon gateway so my little MQTT cluster can respond to devices turning on/off or send a MQTT message to control a device. The pieces don't know or care what language the other pieces are written in. On the Prolog side of things with not that much framework code here is a trivial example: message(['Smarthome','Notify','brain','ping'],_):- publish(['Smarthome', 'Notify', 'brain', 'ping_response']). This code receives a MQTT message with a topic of Smarthome/Notify/brain/ping ignoring the message body and then publishes a return message on topic Smarthome/Notify/brain/ping_response I even have a container that runs nothing but cron with a mqtt client installed that sends mqtt messages at various times of the day and all Prolog has to do is subscribe to those topics and handle those messages.
- OJFord 5y agoThat sounds awesome, is anything about it open-source?
- sprior 5y agoI forked an abandoned implementation of MQTT for SWI-Prolog by olsky, my fork is at https://github.com/sprior/swi-mqtt-pack https://github.com/sprior/swi-mqtt-pack Look in the examples directory for some basic pub/sub code. The Prolog code that runs in my house is pretty specific to my house so I figured the best way to open source things would be as a framework more than an implementation. You can contact me via issues on the github repo and prod me into adding some more advanced examples - I've learned a lot since my last commit on the repo. I've started using the Redis functionality recently added to SWI-Prolog, so my code now responds to MQTT messages and uses state queried from Redis to help determine what actions (implemented by sent MQTT messages) to send out. The beauty is that since I don't do anything that blocks significantly in the Prolog code it is now single threaded - even the MQTT listening. It still responds quickly enough and is MUCH easier to deal with than multi-threaded. An example of what I'm doing is I built a bunch of ESP8266/EESP32 display devices that control neopixels/OLED/LCD displays. When one of those devices boots it sends a MQTT message announcing its location and capabilities (display type, bit depth, dimensions). Prolog receives that message and then stores that info in Redis. So that device info is all dynamic. So then later Prolog might get a notification that something is in the driveway. All by MQTT it requests an image from the appropriate camera, then sends the image off to Sighthound and deepstack image recognition servers. The Sighthound front end sends a message back to Prolog with a description of any vehicle spotted which Prolog then matches against known vehicles. If it determines for example that a Fedex truck is in the driveway then Prolog sends notifications around the house - it queries all the display devices from Redis and then based on the capabilities of each devices creates a JSON formatted MQTT message to send to each announcing the Fedex truck. It then also sends a MQTT message to some Java code that connects with Google and sends a push message to an Android app I wrote that displays the alert on my phone and watch. Before I switched to MQTT I was using Prolog with CORBA as the message transport and back then I also had Visual Basic and MS Agent as part of the system. One night I got bored and a little while later had 3 Peedy the Parrot characters singing Row Your Boat in a round across three different computers coordinated by Prolog. It was actually only a page worth of custom Prolog code for that.