7 Replies Latest reply on Apr 14, 2015 5:06 PM by CMata_Intel

    Communicate b/w Arduino & Node.js on Linux Side

    Mouhyemen

      Aim: Collect sensor's reading from Arduino and send it over to Node.js on the Linux side.

       

      I read a very good blog on Efficient communication between Arduino* and Linux native processes. However, I am not sure if it has to be done in that manner only. Even the Arduino sketch seemed a little complex in the blog. I guess I am either looking for something simpler or b/w Arduino -> Node.js exclusively instead of relying on C++.

       

      I have a functioning sketch.ino that gives me readings and I just need to send these data-readings to a node.js script. Any advices/tips/blogs/reading-materials will be helpful.

       

      Regards,

      Jim

        • 1. Re: Communicate b/w Arduino & Node.js on Linux Side
          AshishD

          You can run a Pub/Sub client in Arduino using MQTT (ex. Arduino Client for MQTT «  knolleary and then have another one on the NodeJS side (mqtt) receiving the data that doing something with it.

           

           

          Or if you want you could skip the Arduino side and use MRAA & UPM and use the NodeJS bindings.

          • 2. Re: Communicate b/w Arduino & Node.js on Linux Side
            Mouhyemen

            Hey AshishD,

             

            I am not really sure what you meant by that. I went over the links you gave me (thanks!) but I have no clue what the API is doing. Do you have any sample code or examples that I can follow? By the way, the arduino code I will be using is the i2cdev-lib by Jeff Rowberg to get orientation readings from MPU6050 sensor. The lib was a fabulous job and I won't be able to re-code it on MRAA.

             

            Someone suggested to me that I should do IPC b/w Arduino & node.js script but I have never done inter-process comm. before or any server/client based coding which I am guessing is your approach?

             

            How do I proceed?

             

            This is how I envision to do it.

            1. Upload the Arduino sketch

            2. Transfer it internally to Node.js (no clue how it's done)

            3. Use the data in Node.js

             

            Earlier I had done it through serial comm. b/w Arduino & Raspberry Pi but this time I don't think the serial comm. is possible.

            • 3. Re: Communicate b/w Arduino & Node.js on Linux Side
              Mouhyemen

              Hey AshishD,

               

              I found this sample code and I had some questions regarding this.

              /*
              Basic MQTT example with Authentication
                - connects to an MQTT server, providing username
                and password
                - publishes "hello world" to the topic "outTopic"
                - subscribes to the topic "inTopic"
              */
              #include <SPI.h>
              #include <Ethernet.h>
              #include <PubSubClient.h>
              // Update these with values suitable for your network.
              byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
              byte server[] = { 172, 16, 0, 2 };
              byte ip[] = { 172, 16, 0, 100 };
              void callback(char* topic, byte* payload, unsigned int length) {
                // handle message arrived
              }
              EthernetClient ethClient;
              PubSubClient client(server, 1883, callback, ethClient);
              void setup()
              {
                Ethernet.begin(mac, ip);
                if (client.connect("arduinoClient", "testuser", "testpass")) {
                client.publish("outTopic","hello world");
                client.subscribe("inTopic");
                }
              }
              void loop()
              {
                client.loop();
              }

               

              Is this the code that should be uploaded onto Arduino merged with my own sketch?

              The client.publish() function will pass the data on to node.js ?

              What would the variables server, ip be? I understand mac will be the mac address of the Edison but what about server/ip?

               

               

              Am I even thinking along the right track?

              • 4. Re: Communicate b/w Arduino & Node.js on Linux Side
                AshishD

                Mouhyemen,

                 

                Sorry I was not clear in my previous post, but yes the snippet of code is what I was thinking. The client.publish() will be sending the data out and Node.js will be subscribed to the same topic as the Arduino side. I should have mentioned that knowledge of MQTT will be required, but I promise MQTT is very easy to use. I will try to code an example when I can.


                As for the variables in the example. Server is referring to the MQTT server running, IP address of the board. Note: instead of using <Ethernet.h> you should probably be using <Wifi.h> since Edison has no ethernet port.


                ps. I believe it is possible to have Arduino send data using Serial1 and read the values on the Linux side via /dev/ttyMFD1.


                Ashish

                • 5. Re: Communicate b/w Arduino & Node.js on Linux Side
                  CMata_Intel

                  Hi Mouhyemen;

                   

                  Did you try with AshishD's suggestion? You can also try with something like writing in files and then with JS start reading the file. Something like:

                  void setup() {
                       int value = 0;  //this will be your variable
                       String S1 = "echo ";
                       S1 += value;
                       S1 += " > /sketch/test.txt";
                       system(S1.buffer);  
                  }
                  void loop() {
                  }
                  

                   

                  I think it will give you an idea on how to start.

                   

                  Regards;

                  • 6. Re: Communicate b/w Arduino & Node.js on Linux Side
                    Mouhyemen

                    Hey CMata_Intel,

                     

                    Thanks a lot. I am trying both AshishD & your solution but I am having trouble uploading the code. This is the error I am getting while uploading even a simple blink LED example.

                     

                    Sketch uses 76,207 bytes (0%) of program storage space. Maximum is 10,000,000 bytes.

                    #!/bin/sh

                    starting download script

                     

                     

                    Args to shell: C:\Intel\arduino-1.6.0+Intel\hardware\tools\x86\bin C:\Users\MOUHYE~1\AppData\Local\Temp\build5903910267688727999.tmp/Blink.cpp.elf COM67

                    # clupload script to invoke lsz

                    COM PORT 67

                    # Copyright (C) 2014 Intel Corporation

                    Converted COM Port COM67 to tty port /dev/ttyS66

                    #

                    Sending Command String to move to download if not already in download mode

                    # This library is free software; you can redistribute it and/or

                    # modify it under the terms of the GNU Lesser General Public

                    # License as published by the Free Software Foundation; either

                    # version 2.1 of the License, or (at your option) any later version.

                    #

                    # This library is distributed in the hope that it will be useful,

                    # but WITHOUT ANY WARRANTY; without even the implied warranty of

                    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

                    # Lesser General Public License for more details.

                    #

                    # You should have received a copy of the GNU Lesser General Public

                    # License along with this library; if not, write to the Free Software

                    # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

                    #

                     

                     

                    echo "starting download script"

                    Deleting existing sketch on target

                    echo "Args to shell:" $*

                     

                     

                    # ARG 1: Path to lsz executable.

                    # ARG 2: Elf File to download

                    # ARG 3: COM port to use.

                     

                     

                    #path contains \ need to change all to /

                    path_to_exe=$1

                    fixed_path=${path_to_exe//\\/\/}

                     

                     

                    #COM ports are not always setup to be addressed via COM for redirect.

                    #/dev/ttySx are present. Howwever, COMy -> /dev/ttySx where x = y - 1

                     

                     

                    com_port_arg=$3

                    com_port_id=${com_port_arg/COM/}

                    echo "COM PORT" $com_port_id

                    tty_port_id=/dev/ttyS$((com_port_id-1))

                    echo "Converted COM Port" $com_port_arg "to tty port" $tty_port_id

                     

                     

                    echo "Sending Command String to move to download if not already in download mode"

                    echo "~sketch downloadEdison" > $tty_port_id

                    C:\Intel\arduino-1.6.0+Intel/hardware/intel/i686/tools/izmir/clupload_win.sh: line 42: /dev/ttyS66: Read-only file system

                     

                     

                    #Move the existing sketch on target.

                    echo "Deleting existing sketch on target"

                    "$fixed_path/lsz.exe" --escape -c "mv -f /sketch/sketch.elf /sketch/sketch.elf.old" <> $tty_port_id 1>&0

                    C:\Intel\arduino-1.6.0+Intel/hardware/intel/i686/tools/izmir/clupload_win.sh: line 46: /dev/ttyS66: Read-only file system

                    # Execute the target download command

                     

                     

                    #Download the file.

                    host_file_name=$2

                    "$fixed_path/lsz.exe" --escape --binary --overwrite $host_file_name <> $tty_port_id 1>&0

                    C:\Intel\arduino-1.6.0+Intel/hardware/intel/i686/tools/izmir/clupload_win.sh: line 51: /dev/ttyS66: Read-only file system

                     

                     

                    #mv the downloaded file to /sketch/sketch.elf

                    target_download_name="${host_file_name##*/}"

                    Moving downloaded file to /sketch/sketch.elf on target

                    echo "Moving downloaded file to /sketch/sketch.elf on target"

                    "$fixed_path/lsz.exe" --escape -c "mv $target_download_name /sketch/sketch.elf; chmod +x /sketch/sketch.elf" <> $tty_port_id 1>&0

                    C:\Intel\arduino-1.6.0+Intel/hardware/intel/i686/tools/izmir/clupload_win.sh: line 56: /dev/ttyS66: Read-only file system

                     

                    I have gone over multiple discussions in this forum but haven't come across anyone facing this problem.

                     

                    I learned that I need to terminate bash.exe & lsz.exe from a discussion. Should I do this on my Edison's command line interface?

                    • 7. Re: Communicate b/w Arduino & Node.js on Linux Side
                      CMata_Intel

                      Hi Mouhyemen

                       

                      Do you still have problems when you upload sketches? I checked your other thread (Upload Fail: Intel Edison Arduino Breakout Board) but you haven't update it either.

                      Have you tried the suggestions above?

                       

                      Regards;

                      CMata