Web server with WebSockets for other programs/devices (e.g. Python) on Raspberry Pi using Node JS
HTML-код
- Опубликовано: 9 фев 2025
- This is a follow up video to my "Web server to control GPIO on a Raspberry Pi with websockets" video. It demos using a UDP communication link in NodeJS to talk to other programs (e.g. Python) and devices. It also demos using a embedded video camera link, analog sliders, and text input/output in the browser.
Video on how to install Node JS at:
• Web server to control ...
Code and documentation at
github.com/Ste...
IMPORTANT NOTES: Do not install node.js via the "apt" package manager. This project will not work if you don't install node manually by downloading from node.js.org instead. Following the instructions exactly except for:
1) Use the 32bit Raspberry Pi OS Desktop WITHOUT recommend software. The full desktop version of OS now includes a version of node preinstalled that will not work with this project. When I originally made this video, the full desktop version did not include node preinstalled.
2) Use a newer version of node. As of August 2022, Node JS v16.16.0 with npm v8.11.0 works fine for me. For the most part, the newest version of node work although occasionally you may need to revert to an earlier package if bugs are introduced. See my previous video description for more information if needed.
If when running
python3 txrx.py
If you get an error" ModuleNotFoundError: No module named 'RPi'", run the following command first:
sudo apt install python3-rpi.gpio
Then rerun
python3 txrx.py
If when running
python3 txrx.py
you get and error "OSError: [Errno 98] Address already in use", the issue is a previous time you ran rxtx.py is still running in the background. The easiest thing to do is just reboot the pi. Or you could run htop and kill all the "python3 txrx.py" processes that are running.
Thank you Steve! You have just saved my 3rd year project
Glad to help!
This guy is awesome, sure to achieve all goals of his life
When I started with python I thought it would be more simple to do such things than in other languages. Now it seems NodeJS is the winner :-)
Yes, sometimes we have to broaden our tool set to accomplish a particular task. I originally wanted to make a web server in "C" as "C" is what I had the most experience with. But I very quickly realize that would take a lot of effort. I then went through several other off the shelf web development platforms before settling on Node JS as the best option.
Well done Steve, I have tried to do this but did not succeed. Thanks very much. Based on your last websocket video I have installed a pi in my RV connected via wifi to my home system. As I wanted numerous analog inputs I was able to pair it with arduino uno. The node module johnny-five along with arduino firmata is wonderful. If you have not looked into it then I encourage you to do so. Had I been able to use python I may not have used the arduino but in hindsight, multiple ADC's connected to a pi is very cumbersome and slow. Again, thanks for you clear explanation.
Good to hear you found a solution. Yes the Pi is not very well suited for reading analog input. I normally use a Microchip Pic processor for these types of applications but the arduino uno is very capable too.
@@highfidelityinc That would be a good video to make, how to use a Microchip Pic with the Pi. I would like to know how and maybe others too. Seems like the next step.
Most of the Microchip projects I have done are stand alone. But I am planning on a project that combines a Raspberry Pi Zero combined with a Pic processor to monitor my septic tank pump for run time, current draw, ground fault current, and alarm monitoring. That way I know well in advance when things start going haywire instead of waiting for the s*** to back up. Of course all I have to do is find the time to do it.
Hello, how can we take the value from a DHT 11 connected to PI3 and display it in an text next to the buttons?
More videos, please 😍
Eventual I do plan to make some more videos on projects I am working on. Unfortunately, my work commitments limit how much free time I have to devote to making videos.
I have the demo running with the latest Node version 16.15 on Pi4b running Bullseye and it all works fine. My question is, I need to add a physical switch (bridging pins 12 and Gnd that will activate pin 21. So basically have a switch on the website on my screen in my van that activates a light and a switch in my cab that will activate the same light (and show up on the screen it is active. Is this possible. I would save me installing another pi and screen on my dash just to activate the light
Thanks for all your work and for sharing
Hi John.
You may want to look at www.npmjs.com/package/onoff . It has example code on how to trigger on a GPIO input and then then use that input to turn an LED on and off on another output. This would be a pure node JS solution and it is event driven so no need to constantly poll the GPIO input.
I am sure there is a way to do it in Python too, but right off hand, I don't have an example I can point you too. Good luck.
@@highfidelityinc THanks for the reply and suggestion Steve. Really appreciate it
Very useful with me, thanks.
Glad it was helpful!
I am having an odd problem, the switches are not switching and just acting like momentary buttons, and sends the commands "GPIO26T":1 and then 0. Any ideas? Using node ver 16.17.1, maybe I need to downgrade?
I haven't seen the particular symptom you are describing. Every 6 months are so, I do test the demo with the latest versions of Raspberry Pi OS and node JS and post what works and does not on the first video I made on GPIO control from a pure Node JS implementation. So you may want to look at the show notes in that video description. Here is an excerpt from that video description:
Update August 15, 2022. I've test Node JS v16.16.0 with npm v8.11.0 and confirmed that the Raspberry Pi Model 4B works with this version of node. I used Raspberry Pi OS 32-bit Desktop (WITHOUT recommend software) "2022-04-04-raspios-bullseye-armhf.img.xz".
You might want to try those versions instead. The next time I set up a Pi, I will test the demo with the latest versions of Raspberry Pi OS and node.
Those notes have other information for different versions of Pi. In particular, if you are using the original Raspberry Pi Zero W, you must use a very old version of Raspberry Pi OS and Node JS as specified in the show notes. I have not tested the demo with the 64 bit version of Raspberry Pi OS yet.
But more likely, you have a previous txrx.py still running in the background when you run the txrx.py python program. You can search if you have multiple txrx.py processes running using htop and then kill it from there. However the easiest thing to do is just reboot the pi as that will kill orphan processes.
I might note that the Python txrx.py program ignores the {"GPIO26T":0}. If the python program is receiving a single {"GPIO26T":1 } command for each press of the toggle button, it would be kind of hard for the button to act as a momentary button. However, if you are getting two {"GPIO26T":1} for each press, well yes that would cause the LED to come on and then back off again. It is only the {"GPIO26M":0} signal that should cause the LED to go off when the button is released.
@@highfidelityinc trying to investigate it further, I found the function is working correctly, its the switch in the html that doesn't always go blue and back. If i click, hold, and drag it I can sometimes get it to work right.
Love the web browser interface, I was wonder if you could help out to use this system with IFTTT, so I can control the relay separately, Thank you
Sorry I know nothing about IFTTT. It appears to be some type of cloud service. I myself just you an OpenVPN server built into my router if I need to access my Pi remotely.
THis isa great option. I used the previous version to control the lights and water pump in my campervan via a 7inch touchscreen or my phone from a pi. But I had to start the server manually each time I started to the pi. this python version looks interesting. can either of the versions be started automatically at startup without having to to it manually
Great work by the way
Yes you can have the program start automatically. There are many ways for doing that: systemD, rc.local, crontab, autostart, and etc. I personally prefer using systemD because I have much better control over how and when the program starts and can have it automatically restart if it crashes. Plus you can get the status of the process or why it failed pretty easily from SystemD. A good overview of different methods is at learn.sparkfun.com/tutorials/how-to-run-a-raspberry-pi-program-on-startup/all
Or you might check out my video about a Raspberry Pi OTA ATSC Tuner/DVR. In that video, I have an example of using systemD to start the kaffiene and miniDLNA programs.
Good luck
I love your video. but can you go one step further, end for example record analog data from a light sensor in your adc converter and display it derectly on the web server? is it possible?
Eventually to do plan to make a project using a Raspberry Pi Zero with analog inputs. However it will be a while before it is ready as it is in the very early planning stages. I am sure you are well aware that the Raspberry does not have a ADC built in. So some additional hardware will be required. In your case, I would look for an off the shelf ADC that will directly interface with the Pi's USB or GPIO pins -- in particular the UART, I2C, or SPI interface.
hey Steve. do you have a video showing how to install netcat and get it running? I'd like to replicate this project exactly as you have presented it but as you know from my attempt on the last project, im weak on Linux. Are you running netcat on your laptop or on your pi? thanks again
In your case, it would better to run netcat on your Raspberry PI instead since netcat is install by default on the Raspberry Pi. You can even do it through an ssh connection if you prefer -- no X-Window support is required for netcat. However, if you look at the documentation, you want to execute following command instead if you are running netcat on the same Pi has your node server.
netcat -u 127.0.0.1 3000 -p 3001
Make sure you have started the webserver first with
node ~/webserver/webgui/webserver.js
Also make sure the Python3 txrx.py example program is not running at the same time since both cannot use the same port at the same time.
The main purpose of this example was to give you a framework where you can use Node as a front end webserver for another program such as Python. So I include text boxes, different types of buttons, and sliders in the node webserver side to demo the interface. The python program only implements the 4 GPIO LED interfaces as was in the first video.
Good Luck,
Steve
@@highfidelityinc I really like the idea of using Python and Node together. I have a lot of Python code that communicates though serial or bluetooth etc. would be great to move some of that over to websockets. thanks again for working with me.
Good to hear you got it working.
Hello. In this project, you have used another Raspberry Pi to stream video to the webpage. But, can the same Raspberry Pi running the NodeJS server stream video from a USB webcam? If it can, please suggest the frameworks that can be used to do so. Thanks!
The Pi that is streaming the video is running MotionEyeOS. Unfortunately, it does not have a package manager package so it is not possible install node JS. Steaming video is quite taxing on the Pi. So even if you could run node JS along with MotionEyeOS on the same pi, it likely would overwhelm the Pi. There is probably a way to do what you want using raspivid. But you will likely have to use something like ffmpeg to convert it to a form that HTML5 can accept. But again, it is likely to be taxing on the Pi. Sorry, I currently do not know what the exact process would be to do that.
how/where is js calling the python functions to run?
Communication between the webserver.js node program and the txrx.py python program is done by a UDP loopback socket connections between the two programs.