jonnyd42
Posts: 3
Joined: Sun Aug 18, 2019 9:40 pm

Using Pi 3 as IoT server w/ web app interface

Sun Aug 18, 2019 10:16 pm

I'd like to set up my raspberry pi 3 to act as an IoT management hub for a thermostat and some other home automation things I'm making with arduinos. I'm connecting the arduinos to the Pi via BLE. I'd like to be able to access the pi via a web portal so that I can issue commands and view graphs.

For the thermostat example, I want to be able to turn on my AC, view usage graphs, and current indoor temperature from a web portal even when I'm not home. A few questions:

-What's the best way to set up the Pi as an accessible server that hosts the webpage on my network?
-If I have the server running on a thread, how should I take control inputs like "turn on AC" and send the data over BLE? Should I maintain a bluetooth daemon? What's the best way to do a cross thread function call then?

User avatar
topguy
Posts: 5672
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Using Pi 3 as IoT server w/ web app interface

Mon Aug 19, 2019 6:45 am

-What's the best way to set up the Pi as an accessible server that hosts the webpage on my network?
That may depend on what programming languiage you would prefer to write this in.

Also I would suggest using a database to collect readings from the sensors. So you have one program purely dedicated to reading the sensors and filling the database. The sensors should just proadcast their readings on BLE at defined intervals so that program only need to listen.

Then the web-service is mainly responsible for reading the database and presenting the tables. I do belive the two programs can talk to the BLuez stack independantly so one can listen while the other can send commands, if that is not the case then you could use MQTT for example to send instructions from one to the other.

There are definitly more than one way to design such a setup.

jonnyd42
Posts: 3
Joined: Sun Aug 18, 2019 9:40 pm

Re: Using Pi 3 as IoT server w/ web app interface

Tue Aug 20, 2019 6:13 am

I'd prefer to use C++ if possible, but happy to use Python as well!

blimpyway
Posts: 148
Joined: Mon Mar 19, 2018 1:18 pm

Re: Using Pi 3 as IoT server w/ web app interface

Tue Aug 20, 2019 4:06 pm

jonnyd42 wrote:
Sun Aug 18, 2019 10:16 pm
I'd like to set up my raspberry pi 3 to act as an IoT management hub for a thermostat and some other home automation things I'm making with arduinos. I'm connecting the arduinos to the Pi via BLE. I'd like to be able to access the pi via a web portal so that I can issue commands and view graphs.
That's what node-red was made for. If you are unaware of it look at some examples on youtube. Then install and test it on any pc.
For the thermostat example, I want to be able to turn on my AC, view usage graphs, and current indoor temperature from a web portal even when I'm not home. A few questions:

-What's the best way to set up the Pi as an accessible server that hosts the webpage on my network?
What do yo mean by this? You just do a normall raspbian installation configure its network, install whatever packages are needed for your project then write the code.
-If I have the server running on a thread, how should I take control inputs like "turn on AC" and send the data over BLE? Should I maintain a bluetooth daemon? What's the best way to do a cross thread function call then?
It depends. Node-red is basically a javascript web server quite intuitive to program from a browser. And it has a whole lot of libraries easy to search and install,
If you find a bluetooth library that actually works with your sensors you just install and use it in node-red environment.
Or maybe your ble sensors open a virtual serial pipe over ble, sending lines with text messages in which case you have to open those serial ports as any input file from node.red and read values from pipes. And send text to send messages back to ble sensors if they need such messages/commands.

If neither of the above doesn't work then you look for whatever Pi libraries are able to communicate with your sensors and do your own scripts which run separately as bridges between node.red and your sensors, forwarding messages between them.

Sending messages to node.red from an external script/process is quite straightforward, but I won't enter too much details here. It can be done in many ways like tcp/udp/http/mqtt/database it is made to talk with whatever protocols are available out there.

https://nodered.org/

daphatty1
Posts: 1
Joined: Tue Aug 20, 2019 3:46 am

Re: Using Pi 3 as IoT server w/ web app interface

Tue Aug 20, 2019 4:50 pm

Surprised Node-Red was recommended before Home Assistant. Home Assistant does exactly what the OP desires, has pre-existing custom components that can be leveraged right away, and if by some chance those components don't quite fit the bill, you can write your own.

Andyroo
Posts: 4141
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Using Pi 3 as IoT server w/ web app interface

Tue Aug 20, 2019 5:16 pm

daphatty1 wrote:
Tue Aug 20, 2019 4:50 pm
Surprised Node-Red was recommended before Home Assistant.
...
All depends on who gets here first and what they are using or know. That’s the fun of user based forums - so many ways to achieve the same end :lol:
Need Pi spray - these things are breeding in my house...

jonnyd42
Posts: 3
Joined: Sun Aug 18, 2019 9:40 pm

Re: Using Pi 3 as IoT server w/ web app interface

Wed Aug 21, 2019 3:01 am

Yeah honestly I want to do it all myself since I think it would be a really cool learning experience. Have you seen any similar types of projects done from the ground up by someone that I could reference?

dmikester1
Posts: 13
Joined: Fri Jul 25, 2014 1:19 pm

Re: Using Pi 3 as IoT server w/ web app interface

Fri Aug 23, 2019 6:40 pm

OpenHAB is another great option of a HA system that will run on a pi. I hope to try it out someday once I get some more time.

Andyroo
Posts: 4141
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Using Pi 3 as IoT server w/ web app interface

Fri Aug 23, 2019 6:57 pm

Given the OP want a DIY solution (why not - more fun) then:

Installing a web server - I use Apache and documented a Buster install here

Calling your programs from the web page can be done from php with the exec command. A couple of things to note:
1) Web scripts run under the www-data user not the Pi user
2) Path config is very limited so you need to check external commands and programs can be reached
3) Look through documentation on the sudoers file to control / limit access to system commands

As for connection - I would rather use Ethernet or at worse WiFi - you could then use MQTT to pass messages back and forth between machines.

Sorry I cannot help on electrics - they can kill / burn house down etc so make sure you know what you are doing or employ a contractor that does (not a general / white van electrician).
Need Pi spray - these things are breeding in my house...

Return to “Networking and servers”