Hi everyone!
Noob here. I created a minimalistic Phoenix project (LiveView included) were I can switch On and Off a virtual switch (for controlling lights on/off) and I can interact with it in “real time” from a web browser.
This is deployed on Nerves, not sure if this is relevant, however what I want to achieve is to send a command to the physical switch via the Web interface.
I managed to send manual commands to the switch already with HTTPoison, but I want to use Phoenix as interface.
The switches are from the brand Sonoff (MiniR2 ref. below) and are connected to the net via wireless. They offer a REST API to change their status and query them.
Now, I saw some similar answers and it was suggested to use PubSub solution, but honestly I don’t understand how this could fit in this case. I cannot subscribe to the switches because they do not publish anything, unless it is requested (pulled from them).
The switches cannot subscribe to Phoenix, because that is not available on their REST API interface.
My quickest solution would be to instantiate one HTTPoison per each switch when the page is loaded and then send them a command and hook it to the event that handles the click event (LiveView) on the controller. However, I would like to get some proposals from the experts.
I prepared a nice image describing the deployment but then I found out I cannot attach it. ( will just add the PlantUml for it in case you are curious.
MiniR2 API ref:
Image:
@startuml
skinparam BackgroundColor transparent
skinparam roundCorner 10
card Home {
card "Nerves on RBP3" {
node "Phoenix" as ph
}
card "IoT environment" {
card "Sonoff 1" as sn1
card "Sonoff 2" as sn2
}
}
cloud Internet as inter
card “Client browser” as pc
sn1 <… ph
sn2 <… ph
pc → inter
inter → ph
@enduml