IoT : Raspberry-Pi/Arduino with Node-Red

After interfacing Arduino with Raspbeery-Pi with the help of Python and Firmata now I am ready to explore the possibility of all IoT stuffs. To know how to interface with Arduino+Raspberry-Pi+Python see my previous blog:  Let us RAP…

Node-Red

Node-red Installation steps:

In my raspberry-Pi3, I am using Raspbian OS, in which Node-red is pre-installed.

But as per Node-red guide it is recommended to install the latest version. For upgrade or fresh installation of Node-Red, refer below guide:

Node-Red Installation

After successful installation try out with LED blink example.

Then few more stuffs need to install before getting on board.

  1.  Install  Node-Red DashBoard
  2. Install Arduino Node-Red
  3. Install Node-Red GPIO

Please make sure you have installed Wiring-Pi and Pi-Firmata in your raspberry Pi .

Watch this youtube video to see the example for controlling Arduino from Node-Red:

Arduino Node-Red Youtube

Below You Tube video shows the power of node-red !!

Bluetooth-based Android Controller for Arduino

I was looking for a quick way to connect my arduino with android via Bluetooth to control stuffs easily. The reason for selecting Bluetooth over wi-fi is, Bluetooth pairing done once and multiple Bluetooth devices you can connect simultaneously.

ArduDroid: a Simple 2-Way Bluetooth-based Android Controller for Arduino

INTRODUCTION

After doing little google search came across ARDUDROID (formerly Andruino) is a simple Android app to help you control the pins of your Arduino Uno (or clone) from your Android phone wirelessly. It’s both an Android app and an Arduino program. ArduDroid employs a simple Android user interface to 1) control Arduino Uno’s digital and PWM pins 2) send text commands to Arduino 3) and receive data from Arduino over Bluetooth serial using the ever popular and really cheap (less than $10 from ebay) HC-05 Bluetooth over serial module. This app has been tested and designed for the HC-05 Serial Bluetooth module.
 How to use ArduDroid :
You can use ArduDroid to send commands to Arduino to control a relay to turn electric gadgets on/off, control a robot servo, increase/decrease speed of a motor and reverse direction, dim a light, speed up a fan, and so on. Furthermore, you can read Arduino pins and sensors then transmit the data back to your android phone.

Below code snippet must be in Arduino before starting the ArduDroid app.

/*
 PROJECT: ArduDroid
 PROGRAMMER: Hazim Bitar (techbitar at gmail dot com)
 DATE: Oct 31, 2013
 FILE: ardudroid.ino
 LICENSE: Public domain
*/

#define START_CMD_CHAR '*'
#define END_CMD_CHAR '#'
#define DIV_CMD_CHAR '|'
#define CMD_DIGITALWRITE 10
#define CMD_ANALOGWRITE 11
#define CMD_TEXT 12
#define CMD_READ_ARDUDROID 13
#define MAX_COMMAND 20  // max command number code. used for error checking.
#define MIN_COMMAND 10  // minimum command number code. used for error checking.
#define IN_STRING_LENGHT 40
#define MAX_ANALOGWRITE 255
#define PIN_HIGH 3
#define PIN_LOW 2

String inText;

void setup() {
  Serial.begin(38400);
  Serial.println("ArduDroid 0.12 Alpha by TechBitar (2013)");
  Serial.flush();
}

void loop()
{
  Serial.flush();
  int ard_command = 0;
  int pin_num = 0;
  int pin_value = 0;

  char get_char = ' ';  //read serial

  // wait for incoming data
  if (Serial.available() return; // if serial empty, return to loop().

  // parse incoming command start flag
  get_char = Serial.read();
  if (get_char != START_CMD_CHAR) return; // if no command start flag, return to loop().

  // parse incoming command type
  ard_command = Serial.parseInt(); // read the command

  // parse incoming pin# and value
  pin_num = Serial.parseInt(); // read the pin
  pin_value = Serial.parseInt();  // read the value

  // 1) GET TEXT COMMAND FROM ARDUDROID
  if (ard_command == CMD_TEXT){
    inText =""; //clears variable for new input
    while (Serial.available())  {
      char c = Serial.read();  //gets one byte from serial buffer
      delay(5);
      if (c == END_CMD_CHAR) { // if we the complete string has been read
        // add your code here
        break;
      }
      else {
        if (c !=  DIV_CMD_CHAR) {
          inText += c;
          delay(5);
        }
      }
    }
  }

  // 2) GET digitalWrite DATA FROM ARDUDROID
  if (ard_command == CMD_DIGITALWRITE){
    if (pin_value == PIN_LOW) pin_value = LOW;
    else if (pin_value == PIN_HIGH) pin_value = HIGH;
    else return; // error in pin value. return.
    set_digitalwrite( pin_num,  pin_value);  // Uncomment this function if you wish to use
    return;  // return from start of loop()
  }

  // 3) GET analogWrite DATA FROM ARDUDROID
  if (ard_command == CMD_ANALOGWRITE) {
    analogWrite(  pin_num, pin_value );
    // add your code here
    return;  // Done. return to loop();
  }

  // 4) SEND DATA TO ARDUDROID
  if (ard_command == CMD_READ_ARDUDROID) {
    // char send_to_android[] = "Place your text here." ;
    // Serial.println(send_to_android);   // Example: Sending text
    Serial.print(" Analog 0 = ");
    Serial.println(analogRead(A0));  // Example: Read and send Analog pin value to Arduino
    return;  // Done. return to loop();
  }
}

// 2a) select the requested pin# for DigitalWrite action
void set_digitalwrite(int pin_num, int pin_value)
{
  switch (pin_num) {
  case 13:
    pinMode(13, OUTPUT);
    digitalWrite(13, pin_value);
    // add your code here
    break;
  case 12:
    pinMode(12, OUTPUT);
    digitalWrite(12, pin_value);
    // add your code here
    break;
  case 11:
    pinMode(11, OUTPUT);
    digitalWrite(11, pin_value);
    // add your code here
    break;
  case 10:
    pinMode(10, OUTPUT);
    digitalWrite(10, pin_value);
    // add your code here
    break;
  case 9:
    pinMode(9, OUTPUT);
    digitalWrite(9, pin_value);
    // add your code here
    break;
  case 8:
    pinMode(8, OUTPUT);
    digitalWrite(8, pin_value);
    // add your code here
    break;
  case 7:
    pinMode(7, OUTPUT);
    digitalWrite(7, pin_value);
    // add your code here
    break;
  case 6:
    pinMode(6, OUTPUT);
    digitalWrite(6, pin_value);
    // add your code here
    break;
  case 5:
    pinMode(5, OUTPUT);
    digitalWrite(5, pin_value);
    // add your code here
    break;
  case 4:
    pinMode(4, OUTPUT);
    digitalWrite(4, pin_value);
    // add your code here
    break;
  case 3:
    pinMode(3, OUTPUT);
    digitalWrite(3, pin_value);
    // add your code here
    break;
  case 2:
    pinMode(2, OUTPUT);
    digitalWrite(2, pin_value);
    // add your code here
    break;
    // default:
    // if nothing else matches, do the default
    // default is optional
  }
}

Reference: http://www.techbitar.com/ardudroid-simple-bluetooth-control-for-arduino-and-android.html

Let us RAP…

Raspberry pi controls Arduino using Python (RAP)

The Raspberry Pi is sometimes seen as competition to micro controllers like the Arduino. However the Raspberry Pi has a different sweet spot and can easily be combined with an Arduino to accomplish a wider range of tasks than otherwise possible. For example the missing Analog inputs.

arduino

Setting up your Arduino for Firmata

Firmata control of the Arduino requires loading an Arduino with the special Firmata sketch. You can download the Arduino software from the Arduino website. After opening the Arduino IDE, follow these steps to install Firmata on your Arduino:
1. Click File->Examples->Firmata->StandardFirmata
2. From the Tools->Board menu, select the type of Arduino you are using.
3. From the Tools->Serial Port menu, choose the USB port to which your Arduino is connected.
4. Click the upload button (it looks like a right arrow, just next to the checkmark) and wait for your sketch to upload. A message in the bottom black window will indicate success or failure
5. Once the Firmata sketch is loaded on your Arduino, you can test it out with the Firmata Test Program. (http://www.firmata.org/wiki/Main_Page)

Controlling your Arduino from Python

Next, your Raspberry Pi must be setup with the python firmata libraries. Run the following commands:

sudo apt-get install python-pip python-serial
sudo pip install pyfirmata

Use a USB cable to connect the Arduino with the Raspberry Pi (remember to use the big USB Standard A connector and not the smaller Micro B power connector). You can now find the USB name of the Arduino by running ‘ls -lrt /dev/tty*’. On my Raspberry Pi, it was listed as /dev/ttyUSB0. Remember this value for later.
Connecting to an Arduino
To control an Arduino from a Python script on your Raspberry Pi, you must first import the Arduino and util classes from the pyfirmata module. Then, create an object using the USB address you found in the previous step

>>> from pyfirmata import Arduino, util
>>> board = Arduino('/dev/ttyUSB0')

Controlling Arduino GPIO
The Arduino’s digital input and output ports can be controlled using the board.digital[] list. Calling write() can set the pin values high or low (1 and 0 respectively). The read() method returns the current value of the pin.

>>> board.digital[2].write(1)
>>> print board.digital[2].read()

If you’d like to use a pin repeatedly, its cumbersome to keep referring to it as board.digital[2]. Instead, you can get a reference to a pin with the board.get_pin() function. To this function, you pass a string of the format “[a|d]:[pin#]:[i:o:p:s]”. It is split by colons into three sections. The first section determines if the pin will be used in analog or digital mode. The second section is the number of the pin you would like to use. The third section selects the pin mode between input, output, pwm, and servo. The returned pin can be assigned to a variable and then later used to call read() and write() methods.

>>> pin2 = board.getpin('d:2:o')
>>> pin2.write(1)

Controlling Analog Pins
To read the value on an analog pin, you have to first turn on the analog value reporting on that pin. However, this continuously sends the analog value from the Arduino to the Raspberry Pi. If not continuously read, this will clog up the serial connection and prevent the rest of your script from running properly. To read the values, it is helpful to use an iterator thread.

>>> it = util.Iterator(board)
>>> it.start()
>>> board.analog[0].enable_reporting()
>>> board.analog[0].read()
>>> it.start()

To turn off the reporting of analog values, call disable_reporting() on the pin object

Sample code

Read LM35 temperature  from AI0 pin and store in CSV

# Python27
import csv
import pyfirmata
import time
from time import sleep


port = '/dev/cu.usbmodemfa1331' #'COM3' for Windows
board = pyfirmata.Arduino(port)
#pin =[0]
it = pyfirmata.util.Iterator(board)
it.start()
a0 = board.get_pin('a:0:i')
#a0.enable_reporting()
with open('SensorDataStore.csv', 'w') as f:
    w = csv.writer(f)
    w.writerow(["Number","Temperature"])
    i = 0
    while i < 25:
        Temperature = a0.read()
        if (Temperature != None):
            Temperature = Temperature*100 # to read value in decimal
        sleep(1)
        i += 1
        row = [i, Temperature]
        w.writerow(row)
        print (Temperature)
    print ("Done. CSV file is ready!")
board.exit()