Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5745

Networking and servers • Re: Raspberry as local website

$
0
0
Hi I have some sensors I want to address sensors via i2c and make the data visible in a browser. The site will only be used in a local website. So that the user in the network can type in an ip adress and will be presented with the data.
First question will a rapberry zero 2 w be capable to handle that. It's a really basic website with 9 data values
Second how can I do it that my Raspberry shows up as a website in my network?

For a really low-effort, bare-bones approach you could do it with a few lines of Python.

Python as installed in RasPiOS has a ready-to-run webserver template.

Code:

python -m http.server
By default it will give access to the files in the directory from which it was started.

So you could visit a URL like http://localhost:8000/sensor_stats.txt and it might return a webpage with the content of a simple text file, like this -

Code:

Last sensor update at 2024-08-23 18:18:55.448791 Sensor #0: 11 Sensor #1: 15 Sensor #2: 7 Sensor #3: 48 Sensor #4: 54 Sensor #5: 46 Sensor #6: 25 Sensor #7: 17 Sensor #8: 20 
And that text file could be generated/modified on a regular basis by another trivial Python script -

Code:

from datetime import datetimefrom random import randintresults = "sensor_stats.txt"with open(results, 'w') as file:    file.write(f'Last sensor update at {datetime.now()} \n')    for _i in range(9):         file.write(f'Sensor #{_i}: {randint(0, 64)} \n')
After re-running that the webpage has an updated timestamp and set of results -

Code:

Last sensor update at 2024-08-23 18:27:46.492728 Sensor #0: 20 Sensor #1: 8 Sensor #2: 16 Sensor #3: 48 Sensor #4: 4 Sensor #5: 48 Sensor #6: 9 Sensor #7: 43 Sensor #8: 7 
In your case you might insert code to read the live sensors rather than generating random test data.

Not the least bit sophisticated, but maybe it would do the job...

Statistics: Posted by B.Goode — Fri Aug 23, 2024 5:32 pm



Viewing all articles
Browse latest Browse all 5745

Trending Articles