{"id":26871271,"url":"https://github.com/codeadamca/arduino-from-nodejs","last_synced_at":"2025-05-07T06:29:27.890Z","repository":{"id":46328124,"uuid":"296498728","full_name":"codeadamca/arduino-from-nodejs","owner":"codeadamca","description":"Using JavaScript, Node.js, and an Arduino to communicate from a browser to an Arduino. ","archived":false,"fork":false,"pushed_at":"2025-01-26T21:39:20.000Z","size":128,"stargazers_count":19,"open_issues_count":1,"forks_count":14,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-31T07:18:53.678Z","etag":null,"topics":["arduino","cplusplus","javascript","nodejs","socket"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeadamca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-18T03:00:22.000Z","updated_at":"2025-03-13T03:22:57.000Z","dependencies_parsed_at":"2024-02-24T19:26:26.146Z","dependency_job_id":"281283bb-1567-452d-a262-b46cd323bd2f","html_url":"https://github.com/codeadamca/arduino-from-nodejs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Farduino-from-nodejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Farduino-from-nodejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Farduino-from-nodejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Farduino-from-nodejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/arduino-from-nodejs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252826496,"owners_count":21810121,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","cplusplus","javascript","nodejs","socket"],"created_at":"2025-03-31T07:18:57.413Z","updated_at":"2025-05-07T06:29:27.873Z","avatar_url":"https://github.com/codeadamca.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Communicating Between a Node.js Server and an Arduino\n\nThis tutorial will walkthrough the process of creating a web interface to control an Arduino. The web interface will include an on and off button that will turn a light on and off on a USB connected Arduino. \n\n## HTML and JavaScript\n\nCreate an HTML file called `index.html`. Add the following code:\n\n```javascript\n\u003c!doctype html\u003e\n\u003chtml\u003e\n   \u003chead\u003e\n\n      \u003ctitle\u003eCommunicating from Node.js to an Arduino\u003c/title\u003e\n      \u003cscript src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'\u003e\u003c/script\u003e\n\n   \u003c/head\u003e\n   \u003cbody\u003e\n\n      \u003ch1\u003eCommunicating from Node.js to an Arduino\u003c/h1\u003e\n\n      \u003cbutton id=\"lightOn\"\u003eTurn Light On\u003c/button\u003e\n      \u003cbutton id=\"lightOff\"\u003eTurn Light Off\u003c/button\u003e\n\n      \u003cscript\u003e\n\n      var socket = io();\n\n      document.getElementById('lightOn').onclick = function() {\n        socket.emit('lights', { \"status\":\"1\" });\n      };\n\n      document.getElementById('lightOff').onclick = function(){               \n        socket.emit('lights', { \"status\":\"0\" });\n      };\n\n      \u003c/script\u003e\n\n   \u003c/body\u003e\n\u003c/html\u003e\n```\n\nThe above code creates a webpage with two buttons. When the buttons are clicked they use JavaScript and Socket.io to send a one or zero message to the Node.js server.\n\n## Node.js Server\n\nBefore we setup the Node.js server we need to know the name of the serialport your Arduino is attached to. You can find the name of your serialport, it will look something like `/dev/tty.wchusbserialfa1410`. On a Mac using the Terminal and entering the following command:\n\n```sh\nls /dev/{tty,cu}.*\n```\n\nOn a PC you can use the command line and the following command:\n\n```sh\nchgport\n```\n\nOn a Windows machine the `chgport` command would result in the following:\n\n```sh\nAUX = \\DosDevices\\COM1\nCOM1 = \\Device\\Serial0\nCOM3 = \\Device\\Serial2\n```\n\nMy Node.js will require the use of `COM3` as the serialport string.\n\nIf you're not sure which one is your Arduino, just disconnet your Arduino and execute the cpommand again and take note of which port is no longer on the list. \n\nOr you can find the name in [Arduino Create](https://create.arduino.cc/editor) in the drop down menu used to select your Arduino.\n\nCreate a file called `app.js` and add the following code:\n\n```javascript\nvar http = require('http');\nvar fs = require('fs');\nvar index = fs.readFileSync( 'index.html');\n\nvar SerialPort = require('serialport');\nconst parsers = SerialPort.parsers;\n\nconst parser = new parsers.Readline({\n    delimiter: '\\r\\n'\n});\n\nvar port = new SerialPort('/dev/tty.wchusbserialfa1410',{ \n    baudRate: 9600,\n    dataBits: 8,\n    parity: 'none',\n    stopBits: 1,\n    flowControl: false\n});\n\nport.pipe(parser);\n\nvar app = http.createServer(function(req, res) {\n    res.writeHead(200, {'Content-Type': 'text/html'});\n    res.end(index);\n});\n\nvar io = require('socket.io').listen(app);\n\nio.on('connection', function(socket) {\n    \n    socket.on('lights',function(data){\n        \n        console.log( data );\n        port.write( data.status );\n    \n    });\n    \n});\n\napp.listen(3000);\n```\n\nThe above code uses Socket.io to listen for a message from the HTML/JavaScript webpage and then simply passes on the message to the connected Arduino. \n\n\u003e [!Note]  \n\u003e Make sure to change the name of the serialport.\n\n## The Arduino\n\nUsing [Arduino Create](https://create.arduino.cc/editor) create the following sketch and upload it to your Arduino. \n\n```cpp\nint lightPin = 2;\n \nvoid setup() \n{ \n  pinMode(lightPin, OUTPUT);\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  \n  if (Serial.available() \u003e 0) {\n    \n    String receivedString = \"\";\n    \n    while (Serial.available() \u003e 0) {\n      receivedString += char(Serial.read ());\n    }\n    \n    Serial.println(receivedString);\n    \n    if(receivedString == \"1\")\n      digitalWrite(lightPin,HIGH);  \n    else\n      digitalWrite(lightPin,LOW);\n    \n  }\n\n}\n```\n\nThe previous code will listen to the serialport for an incoming message. Once a message is received, if the message is a one the light will turn on, if the message is a zero the light will turn off. \n\n\u003e [View the Arduino code on Arduino Create](https://create.arduino.cc/editor/professoradam/af5288bf-00cc-406c-844e-f20485fa2df8/preview)\n\nYou will need to setup the following circuit using your Arduino:\n\n![Tinkercad Circuit](https://raw.githubusercontent.com/codeadamca/arduino-from-nodejs/main/_readme/tinkercad-from-nodejs.png)\n\n\u003e [View the Circuit on Tinkercad](https://www.tinkercad.com/things/h0C03Xahv9R)\n\n## Launch Application\n\n1. Using [Arduino Create](https://create.arduino.cc/editor) upload the sketch to your Arduino.\n2. Using the Terminal start your Node.js app using `node app.js`.\n3. Open up a browser and enter the URL `http://localhost:3000/`.\n4. Using your browser push the on and off buttons and watch your Arduino for a changing light. \n\n\u003e Full tutorial URL:  \n\u003e https://codeadam.ca/learning/arduino-from-nodejs.html\n\n***\n\n## Repo Resources\n\n* [Visual Studio Code](https://code.visualstudio.com/) (or any code editor)\n* [Arduino Create](https://create.arduino.cc/editor) \n* [SerialPort NPM](https://www.npmjs.com/package/serialport)\n* [Socket.io](https://socket.io/)\n\n\u003cbr\u003e\n\u003ca href=\"https://codeadam.ca\"\u003e\n\u003cimg src=\"https://cdn.codeadam.ca/images@1.0.0/codeadam-logo-coloured-horizontal.png\" width=\"200\"\u003e\n\u003c/a\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Farduino-from-nodejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Farduino-from-nodejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Farduino-from-nodejs/lists"}