IoT Tutorials

From COINS Research Group
Revision as of 06:02, 22 March 2016 by Abdelrahman (Talk | contribs) (Created page with "==Tutorial Overview== ===Title === Introduction into Arduino and Internet Of Things Applications. === Author=== Abdelrahman Mahmoud ===Note=== For any suggestions/comments ab...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Tutorial Overview

Title

Introduction into Arduino and Internet Of Things Applications.

Author

Abdelrahman Mahmoud

Note

For any suggestions/comments about this tutorial, please send an email to akoubaa@coins-lab.org / a.mahmoud1994@gmail.com

Make sure you download:

  1. Arduino 1.6.7 version
  • Download your OS version whether it is Windows/Linux/IOS
  • Extract the zip file any where you want

Components

Description

This tutorial will present some demo for the Arduino platform, how to read data from sensors, write data to sensors and send data to another arduino board through Xbee wireless communication.

The Blink Program

Go to File -> Examples -> Basics -> Blink

void setup() {
 // initialize digital pin 13 as an output.
 pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
 digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(1000);              // wait for a second
 digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
 delay(1000);              // wait for a second
}

For the Receiver board:


void setup() {
 // initialize serial communication:
 Serial.begin(9600);
}

void loop() {
 // see if there's incoming serial data:
 if (Serial.available() > 0) {
 // It may differ how do you consider low and high
   if(Serial.read()==49)
   Serial.println("High");
   if(Serial.read()==48)
   Serial.println("Low");
 }
}

Description: It is very common when using XBees that you receive different data than what you are expecting. If you printed the received data when you run this code you will find out that the data is actually 48 and 49. The easiest way is to know the new range received and then convert this data to the original range/output that you are expecting. Note: for the added space in the transmitter code refer to this link.

The Python Script:

import serial
import socket

from time import sleep

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=None)
UDP_IP = "127.0.0.1"
UDP_PORT = 5555



print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT


while True:

   data = ser.readline()
   sock = socket.socket(socket.AF_INET, # Internet
                    socket.SOCK_DGRAM) # UDP
   sock.sendto(data, (UDP_IP, UDP_PORT))
   print "Data:", data

   sleep(0.001)

Important Notes

  • Choose the same port number as the server has.
  • Put the "Serial Select" switch in both shields to USB when you are uploading the program to the board then switch it to the other side -it may differ from a shield to another what the other side is called (XBEE, MICRO...etc)-.

Executing step

1.Run the UDPServerDemo.java file in the path src/org/psu/acmchapter/networking/udp/ using any program eclipse/netbeans/JCreator..etc

2. Run this command : sudo chmod 777 /dev/ttyUSB0

To give the permission to the port you are using. Note: make sure you give the permission to the right port.

3.Upload the arduino programs to the arduino boards and make sure you picked the right Board and the right Serial Port.

4. run the python script by typing python filename.py

Note Type this command in a terminal after going to the right path where you keep the script file

5.Open the Server's console window to see the received data.