Ways to control Arduino – Javascript, Python and Internet

Posted by:

Preamble

There is always different ways to achieve the same goal. This is very true in programming, so is in life. Most times we like to think there is only one way – our way.

set up Arch Linux system for node and johnny-five etc.

This is a good tutorial for this subject and Arduino Experimenter’s Guide for NodeJS.

  1. install Arduino IDE for your system
  2. upload Firmata into board

    select-standardfirmata.jpg

  3. install node.js

    sudo pacman -S nodejs
    
  4. install npm

    sudo pacman -S npm
    or update
    npm update npm -g
    
  5. switch on python for default (nmp install –python=python2.7 does NOT work) to install johnny-five

    cd /usr/bin/
    $ sudo ln -sf python2 python
    sudo npm install johnny-five
    sudo ln -sf pytho3 python #switch off python2
    

    Note: not a good practice since it will effect other packages, make sure reset to default after installation.

  6. mkdir for js files and link to johnny5 folder

    ln -s ~/node_modules/ ./node_modules
    
  7. blink.js file

    var j5 = require("johnny-five");
    var board = new j5.Board();
    var LEDPIN = 13;
    board.on("ready", function(){
    var led = new j5.Led(LEDPIN);
    led.strobe();
    });
    
  8. run node blink.js

    node blink.js 
    1430631447456 Device(s) /dev/ttyACM0 
    1430631447475 Connected /dev/ttyACM0 
    1430631450138 Repl Initialized
    

Bridging with Arduino via pySerial, pyFirmata or PyMata

  1. Install pyfirmata and/or PyMata

    sudo pip install pyfirmata
    
  2. with python via pyserial only Only serial strings can be passed with Serial.write and Serial.read. Customized sketch code required for advanced function, very powerful via pyhton GUI (esp PyQt), but required some serious coding from both end (python and Arduino sketch).
  3. with python via pyfirmata /or PyMata and pyerial Firmata needs to be loaded into Arduino. All the coding is from python end with PyFirmata framework. This is a very easy approach to control Arduino. Following code from PyFirmata

    >>> import sh
    >>> pin=13
    >>> port='/dev/ttyACM0'
    >>> board = pyfirmata.Arduino(port)
    >>> board.digital[pin].write(1)
    >>> board.digital[pin].write(0)
    >>> board.digital[pin].read()
    0
    >>> board.digital[pin].write(1)
    >>> board.digital[pin].read()
    1
    >>> board.digital[pin].write(0)
    

Control from the Internet with a web Interface

  1. Using Arduino Ethernet Shield Did not get a chance to try this way, and do not understand why spend $80 for a shield instead of $35 for a Raspberry PI with so much more to offer.
  2. Use Python CGI as gateway to web interface As shown in RLM site , with jQuery you are a true master of your Arduino and peripheral Hardwares. I will post some other project we did later.

    scn_pok_web.png

Firmata_test to quick test your system

  1. Download firmata_test from firmata.org, roll to bootom to get the righ version – 64 bit in my case

    chmod +x firmata_test.64bit
    ./firmata_test.64bit
    
  2. The screenshot

    scr_firmata_test.png

0

Add a Comment