Arduino using XBee and other sensors

From COINS Research Group
Jump to: navigation, search

Tutorial Overview


Simple program for Arduino (UNO) board using Xbees and other sensors to send/read data to/from an UNO board to another one using different shields with XBees to a running server.


Abdelrahman Mahmoud


This tutorial is done using Linex OS but you can do it with any other OS. For any suggestions/comments about this tutorial, please send an email to /

Make sure you download:

  1. Arduino 1.6.7 version
  1. Download this running Java Server





This tutorial will present how to read data from a "Button sensor" and send the data of the sensor to another arduino board and then send the same data to a Server using a python listener script. First step, you need to run the server. Second step, you need to upload the arduino programs on the boards.Last step, is to run the python script that listens to the serial monitor for the arduino IDE and then sends a UDP packets with this data to the server.

Main Steps

Let's divide this tutorial into three parts:

1-The Arduino programs: using one arduino UNO boards connected to Wireless SD shield and any sensor from the grove starter kit write a simple program that reads the data from the sensor and write it in the serial monitor. Then another UNO board connected to XBee shield.
Note: This tutorial is using the Button sensor as it is the easiest one since this is a beginner tutorial.

2-The Java Server: you will find a 6 videos tutorials to understands how to run the UDP server and to understand the concept of the whole Java Project.

3- The python script: the script is simply listening to the serial monitor from the Arduino IDE and send the data to the server in UDP packets.

The Arduino Program:

For the Transmitter board:

const int pinButton = 3; // select the pin number for the button sensor

void setup() {
 //set the pin mode to work as an input
 pinMode(pinButton, INPUT);

void loop() {
 //Print the data received from the sensor every 2 sec
 Serial.println(" "); //You will see why in the note part below

For the Receiver board:

void setup() {
 // initialize serial communication:

void loop() {
 // see if there's incoming serial data:
 if (Serial.available() > 0) {
 // It may differ how do you consider low and high

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. These are the same values but in ASCII so the easiest way is to know the new values received and then convert this data to the original value/output that you are expecting.


  • For the added space in the transmitter code refer to this link.
  • The way used above for converting the values won't work if you are using big range of numbers you won't be able to memorize the whole values in ASCII so you will have to look for different approach.

The Python Script:

import serial
import socket

from time import sleep

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=None)
UDP_IP = ""
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


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 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

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.