Web server to control GPIO on a Raspberry Pi with websockets (see description for important notes)

Поделиться
HTML-код
  • Опубликовано: 22 янв 2021
  • Control your Raspberry Pi's GPIO pins on any modern web browser using websockets and Node JS
    Code at:
    github.com/StevesRandomProjec...
    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. See below to see what versions of node worked for me. 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.
    I did everything from an ssh connection from my Linux laptop to the Pi so I could film this video. But for those of you that use Windows, you will probably want to attach a keyboard, mouse, and display to your Pi and just do everything local on the Pi instead. Windows doesn't support the X-Windows System so you will probably find that geany does not work via an ssh connection.
    One thing I have notice lately however is that another version of Node.JS is already installed on the full desktop version of Raspberry Pi OS with recommended software. I now recommend using the Desktop version without recommended software. As far as I can tell, installing another version of Node JS on top of it doesn't cause any issues other than you may have to reboot the Pi before the new version of node is active. So after you install node, check the version. If it reports a different version of node or npm than what you installed, reboot the pi before running the npm init command or installing the npm packages.
    For those of you who prefer to program in Python or another language, you may want to check out my follow up video • Web server with WebSoc... that discusses how to interface Node JS to Python and other languages/devices.
    Others have reported that this project will not work on a Raspberry Pi 2. I myself own a Raspberry Pi model 3B, Model 3B+, Model 4B, Model 400, and Zero2W and have found this demo project works on all of them.
    While I have gotten this project to work on a Raspberry Pi Zero W (arm 6l) using Node v11, this version of node hasn't been supported for years -- so with the latest versions of Raspberry Pi OS, node no longer works. You could revert to the same versions of raspberry Pi OS that I specified in the video (Pi OS 2020-12-02-raspios-buster-armhf-full) and then it will work. But it will not have any security updates so use at your own risk.
    As of August 2022, Node JS v16.16.0 with npm v8.11.0 works fine for me.
    As of August 7, 2021, the latest version Node (v14.17.4) and npm (v6.14.14) works on the Raspberry Pi 4. So you no longer need to use Node v12.x. However, if you are using a Raspberry Pi Zero W, you will have to use an even older version of Node and install a few more packages. See updated documentation on my github for special instructions if you are using a Zero W.
    Update November 27, 2021. The demo project still works with the new bullseye version of Raspberry Pi OS dated Oct 30, 2021. I recommend using the desktop version without recommend software as the full version has a different version of node JS preinstalled. . I have tested node.js v16.13.0 and found it does NOT work. Recommend you now use node v14.17.4 as I know it works. Be sure to reboot the pi after installing node but before running "npm init". For now, stick with 32 bit OS -- This project will probably not work with the 64 bit versions of Raspberry Pi OS.
    Update December 26, 2021. I've tested Node JS v16.13.1 with npm v8.1.2 and confirm that both the Raspberry Pi model 3B-Plus and model 4B works with this version of node. Works either with Raspberry Pi Buster or Bullseye OS's. You may consider using this newer version of Node as v12.x is getting quite old now.
    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".

Комментарии • 185

  • @ThePhildrage
    @ThePhildrage 3 года назад +11

    absolutely superb. Exactly what i was looking for, well explained and documented. Thanks so much, this will enable me to build my camper van control panel!

  • @jnine3807
    @jnine3807 2 года назад +2

    this is thousands than the course i found it in the other utube channel. Thanks a lot!!

  • @cwdaniels4
    @cwdaniels4 3 года назад +2

    got to 25minutes and my Pi 3 with single Relay was working Beautifully. Thank you.

  • @andlykwan
    @andlykwan 3 года назад

    the end result at the beginning definitely made it better, thanks

    • @highfidelityinc
      @highfidelityinc  3 года назад

      Yes, it is something I wish more videos would do instead of waiting to the end to see the final result.

  • @Christophereneumann
    @Christophereneumann 3 года назад +1

    I've been forcefully bashing my head against inanimate hard objects for three months now trying to figure out what you so thoroughly explained here. Thank you so much!

    • @highfidelityinc
      @highfidelityinc  3 года назад +1

      Yes it took me many months to figure out too. There are so many different web technologies out there, with many of them being obsolete, that it is very difficult to figure out what is relative and what is not. Glad to hear you found the video useful.

    • @Christophereneumann
      @Christophereneumann 3 года назад

      @@highfidelityinc I got so frustrated, I was just trying to connect one pi to another via bluetooth then wifi ad hoc then I stumbled across your video. There were WEEKS of falling down rabbit holes head first.
      Do you have a cashapp? I want to buy you a beer sir. And if you have time, I'd love to get your thoughts on something.

    • @highfidelityinc
      @highfidelityinc  3 года назад

      Thanks for the offer but really, you don't need to do anything. I am in a middle of a bunch of work related projects right now and don't have much free time right now.

  • @ArielFawkwes
    @ArielFawkwes Год назад

    Never been more excited for an hour long video in my life

  • @teodor9176
    @teodor9176 2 года назад +1

    Nice video. I hope to see more soon. Well explained and easy to follow! Cheers

  • @pranavguruprasad1834
    @pranavguruprasad1834 3 года назад +5

    Thank you, well explained. Most tutorials skip essential steps. For example, running the node web server. I once again appreciate your efforts.

    • @highfidelityinc
      @highfidelityinc  3 года назад

      Glad you found it helpful. Thanks for the feedback.

  • @jsmythib
    @jsmythib Год назад

    Silver banded resistors. Family heirloom? :) Great content. Exactly what people need to 'hello world' this type of project. So many possibilities come into view.

  • @AlfOfAllTrades
    @AlfOfAllTrades 2 года назад

    I have Apache2 (as part of LAMP) installed on my pi, so port 80 was taken, creating error messages. Changed the port in webserver.js to 8080 and then everything worked fine. Of course, instead of actually watching the video all the way through, I went on the interwebs and searched, read and figured out how to change the port that way. It only took a few hours, instead of the 20 minutes it would have by finishing this video :D
    Great informative video and fun stuff to play around with. Thanks!

    • @highfidelityinc
      @highfidelityinc  2 года назад +1

      Yep, if people would just learn to follow the instructions, they will find that it works. It is always better to start with a clean install when trying to run the demo project. Good to hear you figured out the problem.

    • @AlfOfAllTrades
      @AlfOfAllTrades 2 года назад

      @@highfidelityinc I live by the old saying "When everything else fails, read the manual..." Some times it takes far more time, but occasionally you learn something you otherwise wouldn't have :) I think I will remember now, where to find the port setting...

  • @zachsandvik1867
    @zachsandvik1867 3 года назад

    Hi Steve. You are my solution! ;-) you described exactly what I needed. Top notch. :-)

  • @prasenjitmaji3388
    @prasenjitmaji3388 3 года назад

    Thank you for explaining in detail :)

  • @azelbane87
    @azelbane87 Год назад

    Thank you so much 4 this! MAGNIFICENT!!!MUCH NEEDED!! EXCELLENT 👌🏻👍🏽👌🏻

  • @kevinpankhurst1
    @kevinpankhurst1 3 года назад

    That was great, thank you for doing it.

    • @highfidelityinc
      @highfidelityinc  3 года назад

      Thanks for the feedback. Good to hear my efforts to make this video were not in vain.

  • @icedmuons
    @icedmuons 2 года назад +1

    This is fantastic, works brilliantly so far with my 2x8 relay boards connected to the Pi's GPIOs. I'm trying to expand this into a larger home hobby board gaming project, but it seems the webserver doesn't have good support for CGI. Would love to pick your brain... as I haven't done any webdesign or javascripting since 1999.

    • @highfidelityinc
      @highfidelityinc  2 года назад

      Oh Boy, What you are asking (CGI) is something I don't know. And by no means am I a Node JS expert. But after a lot of persistence, I finally figured out how to do a basic web server that incorporated web sockets. I made the video because I figure others were were trying to do the same thing.

  • @robvanmoorst3179
    @robvanmoorst3179 2 года назад

    Good presentation.

  • @kpm25
    @kpm25 Год назад

    Thanks a lot.. I'll give it a go..

  • @siribvcm9653
    @siribvcm9653 3 года назад

    Thanks for the video sir. Because this i can solve my orangepi project

  • @harishbetrabet
    @harishbetrabet 2 года назад

    Nice video, well explained.

  • @oscarpantoja7705
    @oscarpantoja7705 2 года назад

    Really good video! thnaks

  • @miguelcirc
    @miguelcirc 2 года назад

    Thank you! Great explanation! I’m going to make similar project and its very useful. Instead off control only by buttons or slides, I want to activate relays connects on Raspberry sending orders from another Raspberry or Arduino client.

  • @marwanalsouki9185
    @marwanalsouki9185 2 года назад

    Great presentation thank you

  • @technopy6659
    @technopy6659 3 года назад +1

    I just saw the multiple switches and multiple web tabs in 2:00 and i just said: WOOW!

  • @jayweaver5574
    @jayweaver5574 Год назад

    Ok I found it, In plane sight. Silly me.
    Thanks for the help.

  • @robbals3173
    @robbals3173 3 года назад +1

    Hi Steve, first of all, great presentation!!! Thx for this working/useable project. Now, I now you're busy...but could you help (or point me in the right direction) to get a function in your code to start one of these leds at a certain time?

    • @highfidelityinc
      @highfidelityinc  3 года назад +1

      Hi Rob, Thanks for the head up. Without doing some research, I don't have a good answer for your question. But you might want to subscribe to my channel. I am working on a follow up video on how to set up a communication link to another program such as Python. With that video, you can use your preferred programming language to implement your project yet retain node JS to handle just the web interface.

  • @smangasimphiwe5852
    @smangasimphiwe5852 9 месяцев назад

    Thanks Steve 🤗 very insightful video! quick question, Any clue on how I can do the same using java springboot framework?

    • @highfidelityinc
      @highfidelityinc  9 месяцев назад

      Sorry. I am not familiar with java springboot framework. Perhaps someone else who reads this can answer your question.

  • @nearlyphantom
    @nearlyphantom 2 года назад +1

    very helpful tutorial. i just need to combine two gpio pins. can u tell me what to edit in the code to do that?

  • @havbaba
    @havbaba 2 года назад

    In webserver.js function(handler) change:
    return res.end(content,'utf'); //display 404 on error
    to:
    return res.end(content,'utf8'); //display 404 on error
    This solves the error 404 error ;)

    • @highfidelityinc
      @highfidelityinc  2 года назад +1

      Good catch. I have updated the code to correct the error. I should note that it looks like the error will only occur if you try to access a web page that does not exist. Thanks for finding this error.

    • @havbaba
      @havbaba 2 года назад +1

      Indeed, that's the case.
      Otherwise a very good and helpfull video!
      Love it and learned a lot about this in just 1 video!

  • @jorisdenengenieur1239
    @jorisdenengenieur1239 3 года назад

    Excellent video Steve. Can you create this without Java and just with Python? I'm not familiar with Java :-/

    • @highfidelityinc
      @highfidelityinc  3 года назад

      More power to you if you want to try to do it in Python. As I mentioned in the video/text, I tried several other webservers (Apache2/NGINX) first but had issues with getting websockets to work properly. Node JS (which is javascript) was the only one I could get to work. No matter what, you will have to learn javascript because that is programming language for the web browser. So while you might be able to implement the web server side in Python, you will still have to use JavaScript on the web browser side.

  • @bramrecourt7623
    @bramrecourt7623 2 года назад

    @stevesprojects, thanks great video! I have some problems running it on raspberry 2 b, or only working on a 4?

    • @highfidelityinc
      @highfidelityinc  2 года назад +1

      Others have reported the earlier versions of the Pi do not work. So you will probably have to upgrade your Pi.
      I have tested the project on the Raspberry Pi models 4B, 3B+, 3B, and Zero W and it will work on those models. The first three use an arm7l processor which is still supported. The Zero W however uses an arm 6l processor which is not supported by newer versions of Node JS.

  • @rossridler4870
    @rossridler4870 2 года назад +1

    very nice video, it was very informative and well presented.
    I do have a question for you... is there a reason for the order in which you listed the GPIO ports (26, 20, 21, 16)?
    and also is there a way to get the buttons to slide back to the "off" position when the server disconnects or shuts down?

    • @highfidelityinc
      @highfidelityinc  2 года назад

      No particular reason why I chose GPIO ports 26, 20. 21, and 16 and in that order other than they are all on one end of the Pi's 40 pin header and were easy to locate and connect. Right off hand I don't know a way to have the buttons to slide back to off when the server disconnects. However, one possible way to do it is write a client side JavaScript procedure to ping the server every few seconds. If the server fails to respond after so many pings, use that same JavaScript procedure to turn all the switches off. Another way is to write a client javascript procedure to refresh the page every so many seconds -- the issue though is if the server is offline, your web browser with come back with a message that the page cannot be found rather than keeping the web page open.

    • @rossridler4870
      @rossridler4870 2 года назад

      I appreciate your response. I was truly wondering about your choice and sequence of the GPIO pins, I do see that the location of the pins make your choice a good one. The first switch does turn off but the other 3 switches don't and that may be because the exit command completes too quick. I have tried to use a script to have the exit command pause so the switches have the time to shut off with no success. Maybe you could figure out something, I would be grateful for the help.

  • @MonteStudios
    @MonteStudios 7 месяцев назад

    December 2023 - Thanks for this video and the example files..
    I tried all of this yesterday on a Raspberry Pi Model 3B and it all worked. I tried installing the latest version on NODE.JS but I could not get NODE.JS or NPM to correctly install then return their version numbers. I don't know if this is the problem you were encountering.
    I was able to make it work with NODE.JS version: " node-v18.19.0-linux-armv7l.tar.gz " so I'm happy with that.
    I'm planning on using a high (above port 1024) port number so I may not need the capability that " libcap2-bin " provides.
    I then played with / tweaked the stylesheet a bit and again the framework you provided was very helpful.
    Now to expand on all of this since I want digital inputs in addition to the digital outputs, so I'm going to try working on adding that next.
    Thanks again.

    • @highfidelityinc
      @highfidelityinc  7 месяцев назад

      Glad it worked out for you. Yes, sometimes the latest version of Node has a bug and you have to revert to an earlier version. Usually happens when they first release a new major version.

  • @alexandredambrun9598
    @alexandredambrun9598 2 года назад

    Nice tutorial ! Is it possible to send a video from a computer to the raspberry pi with this websocket/Node JS interface ? (with some code modifications)

    • @highfidelityinc
      @highfidelityinc  2 года назад +1

      I am sure it can be done but it would most certainly require you to write some type of custom program or use some off the shelf program for your computer (assuming your computer is not a raspberry pi). The most obvious way to stream video from your computer to a node JS server on your pi is to set up your computer to stream video when a browser to connects to it. Then use the html tag in your node JS webserver index.html file to link your computer video to the Pi's web page. That is what I did on the followup video ruclips.net/video/YvsOUkTUsxg/видео.html . In that case I used motioneyeOS software on another Pi and then made a link to it in the index.html file on this followup video. You just need to replace that link with the link to your computer's web server.

  • @miguelcabral5757
    @miguelcabral5757 Год назад

    HI Steves, thank you, well explained. Great project i do that and works fine. But I need one think for you, I need to put that on startup because i dont have monitor to see what happen, do you have something our do you now something to do that? Thanks

    • @highfidelityinc
      @highfidelityinc  Год назад

      Good to hear you found the video useful. In terms of your question, I personally prefer to use System-D to start programs automatically. I just downloaded a new updated document to my github that explains how to add a System-D process to start node JS automatically. See the last section of the document at github.com/StevesRandomProjects/PiGpioWebServer/blob/main/GPIO%20Webserver%20for%20Raspberry%20Pi%202023-07-15.pdf

    • @miguelcabral5757
      @miguelcabral5757 11 месяцев назад

      @@highfidelityinc works fine, thanks. I make same code upgrades, activated a bottom sequencing.

  • @ashkanradnia1456
    @ashkanradnia1456 3 года назад +1

    Thanks for the video it is very comprehensive and useful!
    Do you know how this can be deployed to Heroku or glitch to control a raspberry pi via a remote server? I would appreciate it if you direct me to some sources to figure out the remote server part.

    • @highfidelityinc
      @highfidelityinc  3 года назад

      Sorry, I know nothing about Keroku or glitch.

    • @ThienTran-df2gs
      @ThienTran-df2gs 3 года назад

      create a nodejs server using socketio and deploy that to heroku then you can run a socketio client script in the raspberry receiving messages sent from the server and do whatever you want with raspberry

  • @FreClimbs
    @FreClimbs Год назад

    Hey Steve, thanks for the tutorial! I was wondering. Will this also work over 4G/5G or only when on the same Wifi network?

    • @highfidelityinc
      @highfidelityinc  Год назад

      The 4G services that I have seen or used have all been behind some type of firewall or nat. 5G is probably the same. To access remotely, you will probably have to set up a remote server that has a public IP and then create a link between your 4/5G device and the remote server. One way to do that is to set up an OpenVPN server on your remote server and an OpenVPN client on the devices that is on your 4/5G network. Essentially you will be creating your own cloud service.

    • @FreClimbs
      @FreClimbs Год назад

      @@highfidelityinc i think "web rtc" could be a solution.

    • @highfidelityinc
      @highfidelityinc  Год назад

      Sorry for the late response -- been busy at work. I haven't used "web rtc" but just quick look at it appears it is some type of cloud service. So yes if you want to use it, go for it. For myself, I wouldn't consider it because it is just too easy to set up my own service. Every cloud service I have used has eventually failed -- be it a change in terms and condition, a change in the protocol, a data breach, going out of business, or etc.

  • @artunzincir9235
    @artunzincir9235 2 года назад +1

    Thank you for great presentation, i want build more pins to control on pi i was tried but cant reach the solution.Would you shot the video another available time about this?:)

    • @highfidelityinc
      @highfidelityinc  2 года назад +1

      Your welcome! I'll consider making future videos when time permits.

  • @b3nsh4
    @b3nsh4 3 года назад

    Nice video! I would prefer python/flask web framework over nodejs and map different endpoints as different functions /turnofLed1 /turnonLed2 and so.. What do you think?

    • @highfidelityinc
      @highfidelityinc  3 года назад +2

      By all means, if you want to do it in python/flask, go for it. The programming language I program in mainly is "C". I know very little if nothing about Python so python/flash was not a good choice for me. By sticking with Node JS, I just had to learn Javascript as I had to learn that anyway for client side. The main purpose of the video was to give others a working example using websocket in an html5 environment. I started this project with absolutely no knowledge of web development. So when I finally figured out a working solution (which turned out to be node JS), I decided to share it with others. Good luck with doing it in python.

    • @b3nsh4
      @b3nsh4 3 года назад

      @@highfidelityinc I really appreciate what you just showed and it's amazing. But tho I use flask, there won't be websockets. And I don't think I can use flask and socketio in this specific case. But this will be something I'll do in near time. Thank you again!

    • @highfidelityinc
      @highfidelityinc  3 года назад

      You might want to check out my new video at ruclips.net/video/YvsOUkTUsxg/видео.html . It describes how to set up a communication link to a Python program.

  • @MrBartPawlowski
    @MrBartPawlowski 4 месяца назад

    hey Steve, nice video! . One question, it this gone work on the PI itself in the browser? So if I connect the screen to the pi and run browser in kiosk mode it will give me gui ?

    • @highfidelityinc
      @highfidelityinc  4 месяца назад

      You most certainly can connect a screen to the pi and run in kiosk mode while node is running in the background on the same pi. For the web address you type in your web browser, you would use the loopback address which would be either localhost or 127.0.0.1

    • @MrBartPawlowski
      @MrBartPawlowski 4 месяца назад

      Thanks for replying, much appreciated!

  • @fatchickencatcher
    @fatchickencatcher 3 года назад

    Really appreciated. Works and is a fantastic base for my home automation. Thanks
    Would you know how to add an MCP3008 ADC to this setup? I am at a loss. Every attempt, up to 30, fails.

    • @highfidelityinc
      @highfidelityinc  3 года назад

      I never have tried using the MCP3008 but a quick google search for "mcp3008 node js raspberry pi" shows numerous hits. There is apparently a NPM package available for the MCP3008. This one looks promising: mikaelleven.wordpress.com/2015/12/13/getting-started-with-spi-analog-input-in-nodejs-using-raspberry-pi-and-mcp3008/ . Alternatively, if you prefer to program in your favorite programming language, you could do all the MCP3008 stuff in python, C, etc and then create a simple UDP, TCP, or SSH link link between the program. There is also methods of sharing data between different programs more directly although I haven't attempted that myself yet. But I have done a simple UDP link to another device (e.g. a Crestron home automation processor) and used the Pi for the html5 front end for the Crestron processor.

    • @fatchickencatcher
      @fatchickencatcher 3 года назад

      @@highfidelityinc Thanks Steve, yes I have used that page but it seems too old. I think Node has advanced and the examples do not work. It is the same for all the other pages that I have found. I will investigate your other suggestions. Node rpio claims that it works but I am not skilled enough to sort it out.

    • @highfidelityinc
      @highfidelityinc  3 года назад

      You might want to check out my new video at ruclips.net/video/YvsOUkTUsxg/видео.html . It describes how to set up a communication link to a Python program. So you could do your MCP3008 in Python (or your preferred programming language) and then send that to the Node Web server.

  • @marcoramos7386
    @marcoramos7386 3 года назад

    Greate video. It is possible to access it from another network?

    • @highfidelityinc
      @highfidelityinc  3 года назад

      By default, the Pi can be access from any subnet unless you specifically set up the firewall rules in the Pi to do otherwise. So to answer your question, yes you can access the Pi from a distant device as long as there is a routeable path between the two. But one thing I would not do is open a port on your router and forward it directly to the Pi -- you will very quickly find hackers all over the world hacking into your Pi. The easiest safest way to access your Pi from a remote location is to buy a router with an OpenVPN server and access the Pi via an OpenVPN tunnel -- but that in itself is another whole topic.

  • @danielpalfi2000
    @danielpalfi2000 2 года назад

    Great video with all the files! Can you write down how can I add extra ports to the program? I have already copied the relevant part with the free port number in index.html; main.js; webserver.js but doesn't react.

    • @highfidelityinc
      @highfidelityinc  2 года назад

      Okay I was curious myself if you could run two node JS servers at the same time. And yes it does work. But instead of trying to combine both webservers into one webserver.js file, create a separate a second node JS server (e.g. webserver.js and webserver2.js) and start them both. Just make sure to use a different port for webserver2.js. I have webserver.js running on port 80 and webserver2.js running on port81. Both are running the same gpio example program except I changed the port number in webserver2.js to port 81. However, this gpio example program does not check if the state of the GPIO pin when changes from an external program (e.g. the other webserver). So when you change the state of the GPIO from webserver.js, it does not reflect that changed in webserver2.js. But both can and do change the state of the GPIO pin. You would have to add some add code if you want to keep the GPIO in sync in both programs -- I haven't tried that yet so you will have to figure this aspect on your own.

  • @czerjaknorbert4610
    @czerjaknorbert4610 Год назад

    Hi! This video is wonderful ,it has a nice presentation and a very good documentation ,you did a very nice job explaining everything. I have a question , I'm trying to automate this site (to auto start the server , with the pi powers on ), I tried creating a service .... long story short it's not working() , I tried rc.local it's not working either, I'm stuck at this banal problem for like 3 days . How would you do this ? I'm still trying with services but i don't even know if it's possible ,I'm not very experienced with Linux , but I'm not new either.

    • @highfidelityinc
      @highfidelityinc  Год назад

      Glad you found the video useful.
      In terms of your program not auto starting, my guess you are starting node before some service is available (such as the network adapter), you don't have your working directory pointed to the ~/webserver/gpio directory, or you are using a user other than pi when you start the node program.
      My prefer method of auto starting a program is SystemD. Sparkfun has excellent tutorial on auto starting a program. SystemD is one of the methods they discuss.
      learn.sparkfun.com/tutorials/how-to-run-a-raspberry-pi-program-on-startup/all
      The main advantages of using SystemD are:
      1) You can have system D automatically restart the program if it crashes.
      2) You can specify to start the program only after certain processes, such at the network adapter, are available.
      3) You can use systemD to check the status of the process and it will give you some error information if the process fails.
      Here is a .service file that I find works for me to auto start node.
      [Unit]
      Description=webserver service
      After=network.target
      [Service]
      ExecStart=node /home/pi/webserver/gpio/webserver.js
      WorkingDirectory=/home/pi/webserver/gpio
      StandardOutput=inherit
      StandardError=inherit
      Restart=always
      User=pi
      [Install]
      WantedBy=multi-user.target
      You might want to look at the "Installing Kaffeine software" section my video "Raspberry Pi OTA ATSC Tuner/DVR using Kaffeine and using minidlna to stream video to a Roku player". I used SystemD to start Kaffeine because I could delay starting the program until the hard drive was ready.

  • @edyyrb
    @edyyrb 2 года назад

    cuando corroe node -v me manda "illegal instruction " y no puedo continuar

  • @jayweaver5574
    @jayweaver5574 Год назад

    Hey? Where is the build/install instructions you mentioned? I don’t see them any where in the Git download?

    • @highfidelityinc
      @highfidelityinc  Год назад

      It is right there at the top of the video description and I just confirmed it still works. Here is again:
      github.com/StevesRandomProjects/PiGpioWebServer

  • @jean-pierrecattoor8067
    @jean-pierrecattoor8067 2 года назад

    Hello, good tuto that works fine. I would like to start the server at the boot. I can't find how to do it. Can you help me? Thanks in advance

    • @highfidelityinc
      @highfidelityinc  2 года назад

      There are many ways that you can start a program automatically. Sparkfun.com has a great write up on the different ways to start a program automatically at learn.sparkfun.com/tutorials/how-to-run-a-raspberry-pi-program-on-startup/all . My preferred method is via a SystemD process. If you are interested in using SystemD, you might look at the video I posted a few months ago on a Raspberry Pi OTA ATSC DVR. In that video I discuss how to start Kaffeine and MiniDNLA automatically using SystemD.

  • @michaelhinterndorfer4942
    @michaelhinterndorfer4942 2 месяца назад

    Can i send other numbers then 1 or 0, bc i want to control a rgb stripe with a slider

  • @amalamara7506
    @amalamara7506 2 года назад

    hi thanks for the video but i have a question how can i get the data from my raspberry pi and send them to my website using websockets?(ps my website is hosted on my pc and it's angular js)

    • @highfidelityinc
      @highfidelityinc  2 года назад

      Sorry, I know very little about angular other than it is a framework developed by Google. And using websockets to communicate to another web server is way beyond the scope of this video. However, you may want to watch my follow up video where I have a demo program that uses a simple UDP connection to talk to another program.

    • @amalamara7506
      @amalamara7506 2 года назад

      @@highfidelityinc yes I'll watch it thank you❤

  • @llony89
    @llony89 3 года назад

    hola agoto todo el proceso tal como lo comenta pero me marca error cuando ejecuto node webserver.js que puedo hacer para solucionar la diferencia es que yo inatale node v 11.15.0 por el procesador que tengo que es el armv6
    fs.js:119
    throw err;
    ^
    Error: EBUSY: resource busy or locked, write
    at Object.writeSync (fs.js:589:3)
    at Object.writeFileSync (fs.js:1220:26)
    at exportGpio (/home/pi/webserver/node_modules/onoff/onoff.js:18:8)
    at new Gpio (/home/pi/webserver/node_modules/onoff/onoff.js:172:36)
    at Object. (/home/pi/webserver/gpio/webserver.js:10:13)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
    espero y puedas apoyar a identificar mi error o que me falto

    • @highfidelityinc
      @highfidelityinc  3 года назад

      Sounds like you are trying to run this project on an Arm6 processor instead of a Arm7 and you are using an older version of node. If this is a Raspberry Pi Zero W, see updated documentations on my Github that has special instructions for the Pi Zero W. Also note that this project uses html5 technology (e.g websockets) -- older obsolete operating systems such as Windows XP do not support html5. Suggest you use the same hardware and software as I used and use a modern web browser. You have the best chance of success if you do exactly as what I did. After you get that working, you can experiment with other hardware and software.

  • @GoldenEye0006
    @GoldenEye0006 2 года назад

    Hi, have got some trouble with mine Raspberry Pi 1B. Does this program support the old Pi ? "Error: EBUSY: resource busy or locked, write" this error i get.

    • @highfidelityinc
      @highfidelityinc  2 года назад

      I doubt if Node JS support the Raspberry Pi 1B as it is very old. Your only hope is to see what arm processor it uses as I describe in the video and then see if node JS has an older version that supports that arm processor. But even then, I doubt that socket IO even existed when that Pi was made. So you are probably SOL.

    • @GoldenEye0006
      @GoldenEye0006 2 года назад

      @@highfidelityinc it's the armv6l , normaly thats the same like Pi Zero, so i did follow the extra notes for the Pi zero.

    • @GoldenEye0006
      @GoldenEye0006 2 года назад

      I've did the exact same on Pi3+ and it's worked form the first time. So yeah the 1B is not a good solution.

  • @smatinader3715
    @smatinader3715 3 года назад

    hello i am trying to use this to control some LEDs and iw ould like to know to i have to be logged on the same network wifi or i can control it from anywhere. 192.168.18.105 is it local or can i do this from a distant device not logged it to the same wifi ?

    • @highfidelityinc
      @highfidelityinc  3 года назад +1

      By default, the Pi can be access from any subnet unless you specifically set up the firewall rules in the Pi to do otherwise. So to answer your question, yes you can access the Pi from a distant device as long as there is a routeable path between the two. But one thing I would not do is open a port on your router and forward it directly to the Pi -- you will very quickly find hackers all over the world hacking into your Pi. The easiest safest way to access your Pi from a remote location is to buy a router with an OpenVPN server and access the Pi via an OpenVPN tunnel -- but that in itself is another whole topic.

    • @smatinader3715
      @smatinader3715 3 года назад

      @@highfidelityinc thank you for your reply

  • @jesusitoavg7305
    @jesusitoavg7305 Год назад

    You know, how can I implement a humedity sensor?
    And, can I add a database??

    • @highfidelityinc
      @highfidelityinc  Год назад

      For a humidity sensor, I would use an off the shelf sensor that interfaces easily with the Pi. Search for humidity sensors on Mouser or DigiKey. I am using a Mouser 403-SHT40-AD1B-R2 temperature/humidity sensor on another project. It has an I2C interface that should work in the Pi's GPIO I2C pins. However this sensor is tiny. You will have to make a custom PCB and then mount the sensor using a hot plate, reflow oven, or hot air station. There is a I2C module in npm although I have not used it yet -- so I don't know if it works. Or you could use a Python I2C module and send that data to node (see my follow up video). For a database, something like MariaDB SQL database should work. But details on how to do both of these is way more involve that what I can describe in just a few lines. Good luck.

  • @meghanarprakash7862
    @meghanarprakash7862 Год назад

    Does anybody know how to use node js in aarch64

  • @anthonyhattar2135
    @anthonyhattar2135 2 года назад

    currently only one pin is working (GPIO 26) I cannot get the others to work. Any advice?

    • @highfidelityinc
      @highfidelityinc  2 года назад

      Hard to say. If you are using a HAT, it may be interfering with some of the GPIO pins or you may have another process that is using the same pin. Those types of things have bite me in the past.

  • @guytas
    @guytas 3 месяца назад

    Hi, very nice projet. Can you tell me if that could be the way to go for me? I need a local web server (no internet access) that would constantly refresh a page that would show my total energy use of my house. I already wrote the python software that shows the energy on the pi itself but I wish to convert it to web server.

    • @highfidelityinc
      @highfidelityinc  3 месяца назад +1

      I have a follow up video the explains one method of interfacing a Python program to a node JS web interface.
      ruclips.net/video/YvsOUkTUsxg/видео.html
      This video is running two programs. The first is a python program that controls the GPIO and the second is a Node JS program to serve up the web pages. I cover how the interface between the two programs works. It also demo's other types of web interfaces such as gauges and text boxes although it is up to you to write the python code to control them.

    • @guytas
      @guytas 3 месяца назад

      ​@@highfidelityinc thanks.

  • @andredepaula8993
    @andredepaula8993 2 года назад

    Hi Steve. I got the error showed bellow when runing "node webserver.js" on RPi. Any tip of what could be happing?

    • @highfidelityinc
      @highfidelityinc  2 года назад

      Hi Andre. I need a little more information to answer your question. In particular, what version of the RPI are you using (Module 4B, 3B, zero W, ?. Which version of Raspberry Pi OS are you using? Which version of Node JS and NPM?

    • @andredepaula8993
      @andredepaula8993 2 года назад

      Hi @@highfidelityinc Tks for your reply. I am using a "Raspberry Pi 4 Model B Rev 1.4" w/ 4GB, running "Raspbian GNU/Linux 11 (bullseye)", Node: v12.20.1 and NPM: '6.14.10'. Best regards, André

    • @highfidelityinc
      @highfidelityinc  2 года назад

      Hi Andre,
      I was curious myself if the latest Bullseye version of Raspberry Pi OS still worked with the GPIO example project. So I just installed the 2021-10-30-raspios-bullseye-armhf-full OS on my Raspberry Pi 4 Model B Rev 1.1
      4GB model and then followed the procedure in my documentation to install Node v12.20.1 (be sure to get the arm7l version if that is what the "uname -m" command reports), NPM v6.14.10, install the onoff and socket.io module, ran the setcap command, downloaded the demo code from my github and then ran the project. And GPIO demo ran the first time with no errors. So yes, the demo project still works with Bullseye.
      I should note however that the newer versions of Raspberry Pi OS full desktop versions already has another version node already installed. So after you install node and check the version with "node -v" and "npm -v", if it does not report the versions you just installed, it is very important to reboot the Pi before running the "npm init" command and ensure the version reported is correct before proceeding.
      Of course you should always start with a clean install and follow my instructions to the letter. Back when I made this video, the latest versions on node.js did not work. But that is no longer that case. Back in August 2021, I tested the demo project with Node v14.17.4 and npm v6.14.14 and they worked too. So it is no longer necessary to use node v12.20.1. I have not tested node v16 yet.
      I guess there could be something different with your revision of the Pi 4. That is unlikely, but, I would have to buy a new Pi 4 to confirm that. Also, all bets are off if you are trying to use the 64 bit version of Raspberry Pi OS.
      Good luck,
      Steve

  • @a.cetinyuce4122
    @a.cetinyuce4122 3 месяца назад

    Hello, how can we make this file xx run automatically every time node webserver.js is opened?

    • @highfidelityinc
      @highfidelityinc  3 месяца назад

      I am not sure exactly what you are asking. If if you are asking how to start the node webserver.js automatically, refer to the last section of the pdf documentation that is included in my github link at the top of the video description.

    • @a.cetinyuce4122
      @a.cetinyuce4122 3 месяца назад

      @@highfidelityinc I'm sorry I didn't see it. Thanks

  • @mrryan9407
    @mrryan9407 2 года назад

    For Some reason this isn't working after following you to point. Please advise.

    • @highfidelityinc
      @highfidelityinc  2 года назад

      Sorry, impossible to make suggestions for such a broad question. You need to leave a little more details. Like what version of Pi you are using, version of Raspberry Pi OS, version of node JS, and any error messages that occur during the install. Some versions of Raspberry pi OS are now coming with an older version of Node JS preinstall. So after you install node JS and you check the version, if it is not the same as you just installed, reboot the Pi to activate new version node JS before installing the npm packages.

  • @jayweaver5574
    @jayweaver5574 Год назад

    i fixed my problem. was something wrong with my user account setup.

  • @MAXI-cp7lk
    @MAXI-cp7lk 3 года назад

    Can this be used for controlling DC motors on a RPI 4 robot

    • @highfidelityinc
      @highfidelityinc  3 года назад +1

      Sure, I see no reason why you couldn't use it for motor control. Of course you will probably need some type of motor driver to go between the GPIO pin and the motor.

    • @MAXI-cp7lk
      @MAXI-cp7lk 3 года назад

      @@highfidelityinc Thank you
      But, did I miss something?
      The GUI of your buttons. Was it done in Android Studio?

    • @MAXI-cp7lk
      @MAXI-cp7lk 3 года назад

      Oh my! I watch it all again and found the HTML code wonderful.

  • @ChylleSuu
    @ChylleSuu 6 месяцев назад

    Does this only work while on the same network?

    • @highfidelityinc
      @highfidelityinc  6 месяцев назад

      This project will work across subnets. So the web interface will work on other networks as long as the network equipment doesn't have any firewall rules to block that traffic.

  • @SA-oj3bo
    @SA-oj3bo 2 года назад

    Anyone knows about a tutorial explaining how to do this with python instead of nodeJS? Thx.

    • @highfidelityinc
      @highfidelityinc  2 года назад

      While I have played around with some Python web servers, I haven't seen any examples that demo what I have done in this project with node JS. I am not even sure if it is possible to do web sockets in Python.

    • @SA-oj3bo
      @SA-oj3bo 2 года назад

      ​@@highfidelityinc Hi Steve, I am trying now.. websockets exist in python but build on top of asyncio, so I first needed to understand asyncio which was new for me. Then have to run an extra server to serve the webpage first before the websockets server can be used. I can not find any example... I will try to run the low level or flask webserver and webscoket server in different threads.

    • @highfidelityinc
      @highfidelityinc  2 года назад

      By all means, if you want to do it in Python only, go for it. Hope you are successful.

  • @lastnamewhite
    @lastnamewhite Год назад

    can't get it to work. started getting errors at 'setcap' instruction ... i've never got a websocket program to work so no big surprise here...thanks anyways... you did a great job on the project... i just cant get it to work on my setup

    • @highfidelityinc
      @highfidelityinc  Год назад +1

      To confirm that this project still works, I just tested this demo project and found it still works with the latest software and hardware. I am using the following:
      1) Raspberry Pi Model 4B
      2) 32 bit Raspberry Pi OS version "2022-04-04-raspios-bullseye-armhf.img.xz" (Desktop WITHOUT recommended software).
      3) Node JS v16.16.0 with npm v8.11.0 (do not updated to a newer version of npm when you run 'npm init'")
      I suggest you use the same hardware and software as above, start with a clean install and do not install anything else, and follow the instructions very carefully. Also be sure to look at the video description as I post updates on what works and what does not there.
      Good luck,
      Steve

    • @lastnamewhite
      @lastnamewhite Год назад

      @@highfidelityinc i've got a Raspberry Pi Model 3B+ maybe that's the difference. I've tried other tutorials but always run into a snag. w3schools has one very similar to yours... I get to the last page and it stops working... havent figured out why.

    • @highfidelityinc
      @highfidelityinc  Год назад

      The Raspberry Pi Model 3B+ should be fine. Any Pi with an arm7l processor will probably work. I have both a Model 3B and 3B+ and never had issues with running node JS on them. Do NOT use the raspberry Pi OS desktop with recommended software -- Starting about a year ago, that version comes with another version of Node JS that will mess up your setup.
      All the setcap command does is allow node to use port 80 without running node as a root user. Be sure to use sudo with netcap command. You don't need to use the setcap command if you use any port over 1024 for the webserver port.

    • @lastnamewhite
      @lastnamewhite Год назад

      @@highfidelityinc thanks. I'm goin to be trying again. I followed the pdf but i guess i'm missing something. I really dont mind using 8000 or whatever port number for now, just like to get it working then figure out the details and changes

    • @highfidelityinc
      @highfidelityinc  Год назад +1

      If I had to make a guess, I would say "setcap" command did not work for you because you did not install node in the proper directory. That could be because you tried to install Node via the "apt" package manager instead of downloading from Node JS from node.js.org. I made that mistake the first time I tried to use node. When you use "apt", it will put node in different directories. I could never get node to work from the "apt" package manager. Do not deviate from the instructions other then using a newer version of node and using the raspberry pi desktop without recommended software.

  • @rajatchaudhary5060
    @rajatchaudhary5060 3 года назад

    Awesome content Sir,
    Want some guidance from you on this project.
    Can I have your email?

    • @highfidelityinc
      @highfidelityinc  3 года назад

      Good you hear you like it. I am extremely busy at work right now so I am not available for consultation right now.

  • @kaddelack
    @kaddelack 11 месяцев назад

    If you use Tailscale you can control this from anywhere in the world. You can also put a domain name to log straight in to the control page, all for free.
    I want to ask where I can download your code? I did similar project but your implementation is much better then mine :)
    Please share if you want to!

    • @highfidelityinc
      @highfidelityinc  11 месяцев назад

      Look at the video description. The link to the code is near the top of the description.
      There are many ways to do remote access. I myself have an OpenVPN server built into my router. No cloud service required so I never have to worry about the cloud server going away someday.

  • @jayweaver5574
    @jayweaver5574 Год назад

    hey your video is out of focus here on my tablet

    • @highfidelityinc
      @highfidelityinc  Год назад

      Looks fine on my computer monitor. Your tablet probably does not have enough resolution or its internet speed is too slow. Watch on a 1080p monitor and ensure you are streaming in 1080P mode. It should be clear then.

  • @DanielleR-h5s
    @DanielleR-h5s 16 дней назад

    I tried to follow this tutorial and it has not be successful for me, I am not sure what is wrong but when trying to run the node webserver.js command i get a node:fs:2346
    return binding.writeFileUtf8(
    ^
    Error: EINVAL: invalid argument, write
    message and I have tried to fix the code with no success. Any suggestions?

  • @franciscojaviernunez8191
    @franciscojaviernunez8191 3 месяца назад

    Hi, Thanks for yours videos. I tried to implement your solucion in a Raspberry pi 3B, and i have problem when i execute you webserver.js.Do you know where is the problem:
    node:fs:2342
    return binding.writeFileUtf8(
    ^
    Error: EINVAL: invalid argument, write
    at Object.writeFileSync (node:fs:2342:20)
    at exportGpio (/home/losvolaos/webserver/node_modules/onoff/onoff.js:18:8)
    at new Gpio (/home/losvolaos/webserver/node_modules/onoff/onoff.js:172:36)
    at Object. (/home/losvolaos/webserver/gpio/webserver.js:7:13)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49 {
    errno: -22,
    code: 'EINVAL',
    syscall: 'write'
    }
    Node.js v21.7.2

    • @highfidelityinc
      @highfidelityinc  3 месяца назад

      Difficult so say why it is not working for you. I just installed the ARM 64 bit version on an Raspberry Pi 4 running Raspberry Pi 64 bit OS back a week or two ago and didn't have issues. But the most common reason for the project not working is are:
      1) A binding error is usually caused by some other service already running on you Pi that is already using port 80. Be sure to start with a clean installation to ensure no other web servers are running on your pi.
      2) You tried to install node via the apt packet manager. Be sure to read the video description and the pdf on my github and following the instructions exactly.
      3) The latest version of node may have a bug in it. Installing the previous major release will usually work when that happens. In particular, I would try v20.12.1(LTS).
      4) I know the 32 bit version of Node JS has work on the Pi3B running 32 bit OS in the past. However I have not tried it with the 64 bit version so I cannot say for sure the 64 version works with the Pi3B

    • @franciscojaviernunez8191
      @franciscojaviernunez8191 3 месяца назад

      @@highfidelityinc Thank you very much for the answer, I have a 64-bit Raspberry pi 3B. Is the latest version of your project on github or how can I access it? It seems that the problem occurs when accessing the onoff program. If you would like to help me I would love to have your help.
      Thank you so much!!

    • @highfidelityinc
      @highfidelityinc  3 месяца назад

      @@franciscojaviernunez8191
      Sorry, I have not idea why you cannot get it to run other than what I already mentioned. Of course if you are using a 64 bit OS, you need to use the 64 bit arm version of Node. It could be that the Pi3B just will not run the 64 bit version of node. Like I suggested, start with a clean install of the lite or basic desktop 32 bit OS and install absolutely no other software. Use long term support version (LTS) of node and do not update Node if it says there is a update -- many times that will break node. If that doesn't work, it could be a case like the original raspberry Pi zero W being too old to run the latest version of node. In which case you will have to use a very old version of Raspberry Pi OS and a very old version of Node.

    • @leebaker4263
      @leebaker4263 24 дня назад

      @@franciscojaviernunez8191 Did you ever get this working? I am also getting the same error on both my Pi 2B and Pi 4B, have tried multiple versions of nodejs and different OS (32 and 64 bit versions)

    • @leebaker4263
      @leebaker4263 23 дня назад

      I managed to get it working on a legacy OS, it looks like something in the latest OS has changed pin numbers and onoff no longer works