Tuesday, 6 November 2018

Arduino - V

In this short post on Arduino, we introduce the time element into the serial output. In the previous posts on Arduino, we have used LEDs and interacted with the serial monitor. More often than not, sensors are hooked to such boards and data is read live from these sensors. But, any sensor data makes sense only when the sensor data is captured along with the time. For all the work in this post, we will use the same Arduino Uno that we have used in previous environments.

There are multiple ways we can add the time element into sensor data that is read off Arduino including adding separate hardware, etc. While we may explore various such possibilities in the near future, we now focus on how to add the time element in the simplest possible manner. We do so by importing a new library called Time. Navigate as Tools --> Manage Libraries... as shown below:
















In the Library Manager window that comes up, search for Time as shown below and click on Install button against the latest version 1.5.0. In our case, this library has already been installed. So the Install button is grayed out:
















Once the library is installed, click on Close button. Then, paste below code:


#include <TimeLib.h>

void setup()  {
  Serial.begin(9600);
  setTime(1541502061);        //The argument to setTime must be Unix time 
  Serial.print("The hour in 24 hour format: ");
  Serial.println(hour());            // the hour in 24 hour format
  Serial.print("The hour in 12 hour format: ");
  Serial.println(hourFormat12());    // the hour in 12 hour format
  Serial.print("The minute in 60 minute format: ");
  Serial.println(minute());          // the minute in 60 minute format
  Serial.print("The second in 60 second format: ");
  Serial.println(second());          // the second in 60 second format
  Serial.print("The day of month: ");
  Serial.println(day());             // the day of month
  Serial.print("Day of the week starting with Sunday as 1: ");
  Serial.println(weekday());         // day of the week starting with Sunday as 1
  Serial.print("The month of year with January as 1: ");
  Serial.println(month());           // the month of year with January as 1
  Serial.print("The full four digit year: ");
  Serial.println(year());            // the full four digit year
  Serial.print("Returns 1 if time now is AM else 0: ");
  Serial.println(isAM());            // returns true if time now is AM
  Serial.print("Returns 1 if time now is PM else 0: ");
  Serial.println(isPM());            // returns true if time now is PM
  Serial.print("Returns the current time as seconds since January 1 1970: ");
  Serial.println(now());             // returns the current time as seconds since January 1 1970
}

void loop() {
}

To see the date elements in the serial output, we need to set Unix time as parameter in setTime. It is the number of seconds since 00:00:00 1st January 1970 as is indicated in the last line of above code. Upload the above code to Arduino Uno. The output in serial monitor is shown below:



















We will revisit the time part in later posts. This concludes the post

Wednesday, 24 October 2018

Raspberry Pi - I

In the next post on IOT, we look at Raspberry Pi, a  low cost, credit-card sized computer. We will be using Raspberry Pi 3 for all the work in this post. The hardware specification and other details can be found here. There is a detailed process for setup and installation of OS and how to make it ready for use. We are bypassing all this as these are available on the net. In our case, we are using Raspbian. The aim of this post is to replicate the traffic signal that we have shown in our first post on IOT.

The code in Python is shown below:















The circuit is shown below:







































We can run the above code as shown below on terminal:



We can see that the red and green LEDs light up alternately:






































Another code to achieve the same effect is shown below:















We can run the above code as shown below:



This concludes the post on Raspberry Pi

Friday, 12 October 2018

Arduino - IV

In the fourth post on Arduino, we see how Python can communicate with Arduino via Serial Port. For the all the work in this post, we will be using the same Arduino Uno and Arduino IDE version 1.8.7. Python version 3.7.0 is used. We will need pyserial library version 3.4 also. Details are shown below:

F:\>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

pyserial details are here. At the time of this line being written 3.4 version is the latest

The Arduino program is shown below:

int data;
int red = 10;

void setup() {
  Serial.begin(9600); //initialize serial COM at 9600 baudrate
  pinMode(red, OUTPUT); //make the LED pin (10) as output
  digitalWrite (red, LOW);
  Serial.println("Message from Arduino setup()");
  Serial.println("Enter 1 for firing up LED");
  Serial.println("Enter 0 for turning off LED");
  Serial.println("Any other number to exit Python program");
}

void loop() {
  
  while (Serial.available()){
    data = Serial.read();
  }
  if (data == '1'){
    digitalWrite (red, HIGH);
  }
  if (data == '0'){
    digitalWrite (red, LOW);
  }
  
}

The above program is uploaded to Arduino board:

























The circuit is shown below:




















The python program used to send input is shown below:

import serial #Serial imported for Serial communication
import time #Required to use delay functions

ArduinoSerialPort = serial.Serial('COM4',9600)

time.sleep(2)

print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())

while True:
  var = input() #get input from user
  print("You entered ", var) #print the input for confirmation
  if ((var != '0') and (var != '1')):
    print("Exiting program")
    break
  elif (var == '1'): #if the value is 1
    ArduinoSerialPort.write(b'1') #send 1 to Arduino
    print("LED is turned ON")
    time.sleep(1)

  elif (var == '0'): #if the value is 0
    ArduinoSerialPort.write(b'0') #send 0 to Arduino
    print("LED is turned OFF")

    time.sleep(1)

If the user enters 1 in Python Shell, LED is turned on. Then, if 0 is entered, LED is turned off. If the user enter any other number, then, the program exits

Open above program in IDLE as shown below:

 























To run above program, click on Run Module under Run menu as shown below:

























This will open the Python Shell as shown below and wait for user input:

























Enter 1. This will light up the LED as shown below:












































Entering 0 will turn of the LED as shown below:












































Entering 5 will exit the program as shown below:

























Thus, we have seen how Python communicates with Arduino using Serial Port using pyserial. This concludes the post on Python interaction with Arduino