Connecting a serial port to the WEB via Node.js

A previous post (here) documented how to connect a Python script to a serial port on a microcontroller.  This post documents connecting a serial port to the WEB via Node.js (which is a JavaScript runtime).

Using Node.js and a module for node called serialport, it is very easy to connect a PC to a serial port on a microcontroller.   First install Node.js  (and npm which is the node package manager). Then install the serial port package via:

npm install serialport

NODE.JS to Console Javascript code

Save the following code as serial_port_console.js:

and run it via:

node serial_port_console.js

This is a very simple means to communicate with your serial device just from the console. Data received from the serial device is sent to the console and data typed in on the console is sent to the serial device. Note that the serial port on the PC is hard coded as “/dev/ttyUSB0” and the baud rate is 115200 which should be changed by editing the code to match the connected device.

NODE.JS to WEB Javascript code

Alternatively, use this script for connecting a serial port to the WEB via Node.js:

That script also requires an html file to send to the browser:

Save the Node.Js file as serial_port_web.js and the  html  file as index.html. This script also requires three other npm modules (fs for reading files,  express for the WEB interface and socket.io to maintain a WEB socket connection for the serial data flow).

Install them with:  npm install socket.io fs express

Make sure the microcontroller serial device is connected to the PC, then run the script via:

node serial_port_web.js

The serial_port_web.js script will search for the serial port and use what is found  at a baud rate of 115200 unless it is over ridden with optional parameters on the command line:

node serial_port_web.js  /dev/ttyUSB0 9600

The port is hard coded to 8888 but of course can be changed by editing the code.

When the script is running ,  copy/paste this URL in the browser:

http://localhost:8888

and this screen should appear:

Serial Port to WEB via Node.JsThe large top blue box is where the serial data will be displayed (and a time stamp added). The bottom blue box is where data can be typed to send to the serial device. Note that when Enter is pressed, Enter (or as it is called the Carriage Return or Newline character) is sent to the serial device.  Pressing the Send button (sends the typed in characters without sending Enter.) Depending on what (code) is running on the serial device, sometimes the Enter is needed and sometimes not.

There is a lot more code in the WEB example, than in the console example, however, the WEB example has more features and presents the data in a ‘prettier’ way. Using the WEB example also allows a remote connection to the serial device if the host computer is connected to the internet.

An STM32F103C8T6 based MIDI Controller for MIDI2LR (Updated)

In a previous post (here) ,  a project was introduced for those of you who use Adobe Lightroom. There is a way to use a MIDI hardware ‘box’ to control various Lightroom actions. I am told that once you use the hardware controls you will never want to go back to the software ones.  A Lightroom plugin called MIDI2LR  interfaces various MIDI ‘boxes’ to  Lightroom. The MIDI box sends MIDI Control/Change commands to MIDI2LR which then changes them into Lightroom actions. More info on MID2LR can be found HERE.

This post continues the project by adding multiple rotary encoders (in addition to the buttons). Here is how things are wired (download the Fritzing.org file for this graphic here):

Here is how it  actually looks all wired together:

In this project (so far) there are two STM32F103C8 microcontrollers. One contains the MIDI control code and interfaces via USB to the PC. It also controls the OLED screen and buttons.  Another one controls the five rotary encoders. A previous post (here) documents the STM32F103C8T6 and interfacing with rotary encoders.

Also shown in the picture above are how the STM32F103C8 microcontrollers are programmed. The one on the left has an FTDI serial-to-usb connector and the one on the right has an ST-Link programmer connected.

The two STM32F103C8 microcontrollers are connected together via I2C (two wires).  To add more rotary encoders, just add another STM32F103C8T6 and up to  five encoders.

In the above picture, at the top left, is a simple I2C circuit which was introduced in a previous post here. Using that circuit, multiple groups of rotary encoders can be added (each additional STM32F103C8T6 can control up to 5 more encoders).

Here is the code (using the Arduino IDE) to load into the STM32F103C8T6 controlling the USB connection, buttons and OLED:

Note: The above code is an enhanced version from the previous post (here) were this project was only using buttons.

Here is the code to load into the STM32F103C8T6 which controls the rotary encoders:

Note: those two sketches will need to be updated if more encoders are added.

Once the code is sent/loaded into the two microcontrollers, just plug in the USB-micro cable (shown in the picture on the right) to your Windows 10 PC and it will be added as an audio device (no drivers are needed as it is automatically recognized). Install the MIDI2LR plugin and while in Lightroom you can test out the button and rotary encoder functions and customize the functionality.

Next up for this project is a custom case,  cleaning up the wiring and adding more encoders! Stay tuned!

Multiple Rotary Encoders on an STM32F103C8T6

Rotary encoders are useful for many things.  Here is what they look like (with and without the button):

Rotary EncoderRotary Encoder with Button

They have 5 connections (Power, Ground, Data, Clock and Switch). The button can be turned (continuously in any direction) and it can also be pushed down .

 

 

 

 

The switch connection monitors the button push whereas the data and clock connections are used to monitor the direction and turn of the button.  There are many tutorials on how these ‘encoders’ work, for more information check this link.

Multiple encoders are needed for the MIDI2LR  project that was posted here. In order to test multiple encoders , this sketch called ‘Multiple Rotary Encoders on an STM32F103C8T6’ was created:

The pins are set up in an array so that the code can quickly ‘scan’ through them and not miss any state changes.

Notice that the switch (button press) pins are set up as INPUT_PULLUP.  If they go ‘LOW’ then the button was pushed.

Here is what the wiring looks like:

STM32F103C8T6 Multiple Rotary Encoders

Get the fritzing.org code for that wiring graphic here (so it can be modified if needed).

Powering your electronics projects with an ATX power supply

Working with different electronics projects,  and especially microcontrollers , requires different voltages (usually 3.3,  5 and 12 Volts).  It is very fitting that the power supply used in most desktop computers (an ATX power supply) can provide exactly these voltages.

There are a number of  projects  (found via a Google search) that turn an  ATX power supply into a bench power supply.  Usually those projects highly modify the  power supply itself  and don’t break out all of the specific voltages it supplies.

Powering your electronics projects with an ATX power supply is actually pretty easy without modifying anything.  The ‘secret’ is to plug the ATX power supply connector to an ATX breakout board.  There are several types of these (boards) adapters. Pictured here are two  different ones:

PC Bench Power Supply Adaptor

ATX Breakout Board

 

 

 

 

These breakout boards split off ground, 3.3, 5 and 12 volts. They have a power on/off switch that also turns on or off the power supply.  The binding posts (or screw in connectors) can be wired to  terminal blocks which allow more connections if needed.

Terminal Block

To monitor voltage and amps used by your electronics, add a DSN-VC288 vols/amp measuring module or two. One can monitor the 5 volt circuit and one can monitor the 12 volt circuit.  Here is how the DSN-VC288 is wired:

DSN-VC288

Here is what it looks like when all the pieces are wired together:

 

Bench Power SupplyBench Power Supply

STM32F103C8T6 Pin Tester to check your soldering

How good are you at soldering?   Here is the STM32F103C8T6  microcontroller board that I use  and have to solder all of the pins.

STM32F103C8T6 Front View

Sometimes when I solder pins to the  board, the connection looks OK and even tests OK with continuity on a multimeter.  However, the solder connections even though they pass those two tests sometimes don’t fully connect the pin to the board.  More information about common soldering problems can be found  here.

I came up with a way to better test those connections. It is an Arduino sketch that tests all of the pins to make sure they work when connected to an LED.

Here is the STM32F103C8T6 pin tester to check your soldering sketch:

The sketch uses an array called ‘pins’ with a list of all the STM32F103C8T6 pin names. The setup code sets all the pins to output and the main loop just toggles the pins with a 100 millisecond delay. This makes sure that the LED will toggle ON/OFF if the connection is working properly.

Make sure to use the STM32F103C8T6  Arduino Core from here as it supports using all the pins  as output and testing with an LED.

Once you load it into the STM32F103C8T6, then  just make sure it has power and ground and connect an LED to each pin in succession and make sure it lights up. If it doesn’t, then you know it is a bad connection. This is also a way to test any connection wires that need testing. Sometimes they are ‘bad’ too, due to defects in manufacturing or just usage.

STM32F103C8T6 Pin Tester

5 Way Navigation Button connected to an Arduino or STM32F103C8T6

A 5 way navigation button actually has 7 different navigational settings. It has a lot of functionality in a small package.  The ‘button’ on it can be moved/pushed in 4 directions ( labeled up, down, left, right)  or pressed down. There are also two other switched buttons labeled ‘Set’ and ‘Reset’ for a total of 7 functions. Quite handy!

Here is what it looks like:

5 Way Navigation Button

COM is wired to ground. Each of the other navigation pins are wired to a pin on the STM32F103C8T6 or Arduino . Here is what the wiring looks like when it is wired to an STM32F103C8T6.

STM32F103C8T6 with a 5 Way Navigation Button Here is the Arduino IDE code for the STM32F103C8T6. When the button is moved or pressed it will light the LED and display the action on the serial console. Note that the input pin is initially configured as INPUT_PULLUP which makes the pin HIGH. When the button is pushed/moved it will make the  corresponding pin go to ground (LOW).

Here is the code for an Arduino Uno using Digital Pins (2,3,4,5,6,7,8):

Here is a top and side view of the button so you can see it looks somewhat like a joystick.

5 Way Navigation Button5 Way Navigation Button

PIR Sensor and Relay Switch to turn on a Light

Ever wonder how a motion detector is used to turn on a light? Take a look at this graphic (made with Fritzing). It shows the circuitry that uses an STM32F103C8T6 microcontroller to control a PIR sensor and relay switch to turn on a Light.

PIR Sensor and Relay Switch to turn on a Light

Get the Fritzing source code for the above image HERE (in case you would like to modify it).  Shown below is the code for the STM32F103C8T6 microcontroller to be used in the Arduino IDE.

When motion is detected by the sensor, the relay is turned on which completes the mains circuit. Then, what ever is connected to the relay (like a light) is turned on. The motion detection is also shown by turning on the led and logging to the serial port.

To use this code in a practical situation, remove the serial port messages and change the length of time the relay switch is on.

An STM32F103C8T6 based MIDI Controller for MIDI2LR

For those of you who use Adobe Lightroom,  there is a way to use a MIDI hardware ‘box’ to control various Lightroom actions. I am told that once you use the hardware controls you will never want to go back to the software ones. There is a Lightroom plugin called MIDI2LR which interfaces various MIDI ‘boxes’ to  Lightroom. The MIDI box sends MIDI Control/Change commands to MIDI2LR which then changes them into Lightroom actions. More info on MID2LR can be found HERE.

There are several commercial MIDI controllers which do work with MID2LR, however, features and functionality differ. Since the whole idea for using the hardware controls is ease of use and productivity, I would think that Lightroom enthusiasts would want a means of creating a custom hardware solution.

It is actually relatively easy to create an STM32F103C8T6 based MIDI Controller for MIDI2LR. As a proof of concept, I built one on a breadboard to demonstrate what can be done. Here is the design layout:

An STM32F103C8T6 based MIDI Controller for MIDI2LRDownload the Fritzing diagram source code HERE.  And here is what it looks like all wired together:

An STM32F103C8T6 based MIDI Controller for MIDI2LR

The two buttons next to the display are used to control menu selections. The menu is used to set whether the buttons send MIDI Notes or Change/Control commands and also is used to select up to 5 channels.  Therefore with the 8 buttons  x ( 5 channels) x 2 (Note vs. CC commands), there is a possibility of  80 different commands to send to MIDI2LR.  The 5 channels is arbitrary and of course changeable in the code so possibilities are only limited by practicality. When a button is pressed, the MIDI CC/Note is sent to MIDI2LR and displayed on the OLED screen.

The code below was developed and sent to the STM32F103C8T6 via the Arduino IDE:

The Arduino IDE was used to develop and program the STM32F103C8T6. The IDE was also customized to use the Arduino Core for STM32 MCUs. Also note the use of the Adafruit graphics libraries to control the OLED display.

Once the code is sent, just plug in the STM32F103C8T6 via an USB-micro cable to your Windows 10 PC and it will be added as an audio device (no drivers are needed as it is automatically recognized). Install the MIDI2LR plugin and while in Lightroom you can test out the button functions and customize the functionality.

You might say that buttons are OK but dials (rotary encoders) are what is missing.  You are right, that is what is being planned for in the next step of this project. Stay tuned!

WebRTC Phone Calls via Asterisk

Are you ready for another off topic article on WebRTC?  This one is titled WebRTC Phone Calls via Asterisk. I have written about Asterisk before (HERE) and that article did have something to do with microcontrollers  😎  Asterisk is an open source full featured phone system (PBX). In my last post about WebRTC, I showed how to do video/audio sharing via WebRTC. This article shows how to turn your Web browser into a (tele)phone (using the PC audio devices and WebRTC Javascript code). With the code provided you can make and answer calls through your Asterisk system via the Web.

Of course you need an Asterisk system up and running for testing this. There are many helpful tutorials on the internet to help with that. The Asterisk wiki also has very good documentation on installing and customizing Asterisk.

The article to customize Asterisk for WebRTC is HERE. Basically, there are three configuration files that need changed to make WebRTC Phone Calls via Asterisk.  Usually these files (httpd.conf, extensions.conf, sip.conf) are found in the /etc/asterisk directory after installation .

For httpd.conf, you will need to select a port for both TLS and HTTP.  You will also need a valid SSL certificate. I am using one from Letsencrypt.org. In the example below I am using port 7070 for HTTP and 7079 for TLS.

For sip.conf , you need to specify the parameters for the phone (I am using extension 5099):

Note that WebRTC will be using the websocket protocol for communications to the Asterisk server.

In extensions.conf is where you specify the context and dialing plan so that the WebRTC enabled phone can make and receive calls:

Note: The dialing plan is where security is very important because it specifies who the  ‘phone/user’ is allowed to dial  and also what is allowed to call it.

Once Asterisk has been configured, the WebRTC code can be  accessed to try a call.  To implement the SIP and WebRTC protocols I have chosen to use the JSSIP Javascript library code (HERE).

Download  the JSSIP library and  place it (jssip.min.js) in the same Web directory as the two other files (index.html and sip.js) show below.

Here is the index.html page to enable the phone:

Here is the main Javascript code (sip.js):

Make sure to change the domain name and port to match what is configured in Asterisk. Then navigate to the index.html page and you should see this page:

WebRTC Phone Calls via Asterisk

 

 

 

 

 

Then put in the phone number and password  for the WebRTC phone and click Register. If all goes well, the Register button will change to ‘Registered’. Your WebRTC phone is now ready to answer or make calls.

Put in an extension to call and click the Call button. The Call button will change to match the state of the call. Any incoming calls will automatically be answered and the Call button will be updated to the state of the call. Note: WebRTC (the browser) will ask permission to use the microphone when the fist call is made or answered.

Note that diagnostic messages are logged to the web console. Un-commenting the JsSIP debug.enable in sip.js will also show a lot of interesting debug information for those curious as to what is happening behind the scenes.

WebRTC Screen/Video Sharing

As many readers of this site can tell, I am a bit off topic with this article. None the less, I think those same readers will find it useful. The topic is WebRTC Screen/Video Sharing and shows how to communicate with another person (peer to peer) via video and audio using a Web browser.  This can be very useful to share a ‘screen’ or video and provide remote assistance / technical support.  Complete code and documentation is also provided to try it out (or view the documentation to see the features and if they are of interest).

As working from home becomes more prevalent these days, there is a greater need for this type of service. There are popular software products which offer some of these services, however,  perhaps you are like me and want to try/code these things yourself.  I get the features I want and I control  the (sometimes overlooked) security aspects of the service.

For those not acquainted with WebRTC, it is a means of communications between two peers. (RTC is an abbreviation for Real Time Communications) WebRTC allows for secure sharing of audio and video directly between those two peers, browser to browser (usually without anything in between).

For example, If someone has a problem/question on their computer (perhaps about the Arduino IDE) and you want to show them how something is done, get on the WebRTC Screen/Video sharing page with them. Then share  a screen and ‘walk’ them through the process in question.

WebRTC uses an API built into most modern day browsers.  Technically there are several protocols involved in WebRTC (signalling, ICE, STUN and/or TURN). For more specific information on WebRTC, see the WebRTC.org site.

Specifically, the Web application that I am showing in this article, uses the WebRTC API to allow two users to securely communicate via audio/video over a peer to peer connection in real time.  Each of the two ‘peers’ using this Web app would need to have a video device (Web camera), microphone and speakers or headphones.

Features for this Web app include audio, video and/or screen sharing, screen capture, video capture/replay/download, chat messages, file transfer and playing a local video to stream it to the remote peer.

Technically I am not using a ‘Web’ server (like Apache or Nginx) to serve the Web page. I have coded a Node.js server script (in Javascript) which functions as a Web server in that it handles the browser HTTPS Web page request and serves back the Web page.

Here is the code for  the Node Server:

Note: The script (and WebRTC) requires an SSL certificate to ensure secure communications. Make sure to replace the lines (with key: and cert: with valid certificate references). Note: The port serving the Web page is 8080 which can be changed to any port (not conflicting with other ports in use on the server).

The modules required for the node server can be installed via npm:

npm i ws fs  https

To start the node server, use the command:

node server.js

To access the WebRTC app, in addition to the node server, a STUN and/or TURN server is also required.  For the peers to communicate, they need to know how to contact each other and therefor need to know each others’ IP address.  The STUN or (TURN) server provides this info.  The TURN server is needed if one or both of the peers is behind a NAT. As mentioned earlier, for more specifics on how WebRTC works check the the  documentation on the  WebRTC.org site. There are public STUN servers available so no need to run your own.

However, if a TURN server is need, the coturn open source TURN server is the one I recommend.

The main functioning of this app is via the webrtc.js javascript code. There are two lines  in the webrtc.js script which must be edited and changed to replace the urls for the STUN/TURN server references.

Also, if the port is changed in server.js, make sure to change it in the webrtc.js source to match also.  Once the node server is running and the changes are made to the webrtc.js script for the STUN/TURN server references, the WebRTC app is ready to go.

Using the Firefox or Chrome browser, navigate to its page:

https://yourdomain.com:8080/webrtcvideo

Replacing  yourdomain.com (and optionally the port) with the actual domain name running the node server script.

The browser will ask if it is OK to share your video and audio device, once approved, the Web Page will show up:

WebRTC Screen/Video Sharing

 

Click the ‘Show Functions’ / ‘Hide Functions’  button to show or hide all of the extra features available for this app. View or download the documentation (HERE) for an explanation of the features.

The complete source code ( a ZIP file which includes the documentation) , can be downloaded (HERE).

Load more