Raspberry Pi Setup for IoT Lab
Raspberry Pi Setup for IoT Lab
(IT605PC)
o Connect to your WiFi network by selecting its name, entering the password, and
clicking Next.
o Click Next let the wizard check for updates to Raspbian and install them (this might take a
little while).
o Click on File, then choose Save, and then click on Desktop and save the file as .
o Click the icon with red crosses in the top right-hand corner of the screen, and select your
network from the drop-down menu. You may need to ask an adult which network you
should choose.
o When your Pi is connected to the internet, you will see a wireless LAN symbol instead of
the red crosses.
Password — set the password of the pi user (it is a good idea to change the password
from the factory default ‘raspberry’)
Boot — select to show the Desktop or CLI (command line interface) when your
Raspberry Pi starts
Auto Login — enabling this option will make the Raspberry Pi automatically log in
whenever it starts
This tab allows you to change your Raspberry Pi settings to be specific to a country or
location.
Locale — set the language, country, and character set used by your Raspberry Pi
Before moving to connect your Raspberry Pi to your laptop display, you need an SD card
with the OS preinstalled, or install Raspbian on a blank SD card. You will find lots of blogs
and tutorials on preparing an SD card for the Raspberry Pi. If you are a beginner, you can
simply click hereand know more about this. This will show how to install the OS for the
Raspberry Pi. You can also buy SD cards with the Raspbian and NOOBs operating systems
preinstalled. I would suggest you install the latest full Raspbian OS image from the official
Raspberry Pi website as it is having VNC Server in the OS package.
After setting up your SD Card, insert it into the Raspberry Pi. Next, connect your power
adapter to the Raspberry Pi to power it. Also, connect your Raspberry Pi to the laptop via an
Ethernet cable and connect a keyboard and mouse to it.
Note: You need screen and a mouse after booting a new OS into Pi for the first time as by
default, the SSH and VNC are disabled in Pi. Without SSH disabled, we cannot enable the
PuTTY Configuration.
Click on Properties (shown below), then go to Sharing and click on “Allow other network
users to connect”. Make sure that the networking connection is changed to the connection of
the Raspberry Pi. In my case, it is Ethernet:
Download VNC Client and install it. Now, download the VNC Viewerand install it on the
laptop.
Open the VNC Server and the VNC Viewer now.
In the VNC Viewer, click on File > New Connection.
Enter IP Address and in Options > Picture Quality, select High.
GPIO Pins :
As shown in above figure, there are 40output pins for the PI. But when you look at the second
figure, you can see not all 40 pin out can be programmed to our use. These are only 26 GPIO
pins which can be programmed. These pins go from GPIO2 to GPIO27.
These 26 GPIO pins can be programmed as per need. Some of these pins also perform
some special functions, we will discuss about that later. With special GPIO put aside, we
have 17 GPIO remaining (Light green Cirl).
Each of these 17 GPIO pins can deliver a maximum of 15mA current. And the sum of
currents from all GPIO cannot exceed 50mA. So we can draw a maximum of 3mA in average
Components Required
Here we are using Raspberry Pi 2 Model B with Raspbian Jessie OS. All the basic
Hardware and Software requirements are previously discussed, you can look it up in
the Raspberry Pi Introduction, other than that we need:
Connecting pins
220Ω or 1KΩresistor
LED
Bread Board
Circuit Diagram:
1. On the desktop, go the Start Menu and choose for the PYTHON 3, as shown in figure
below.
2. After that, PYHON will run and you will see a window as shown in below figure.
If the program has no errors in it, you will see a “>>>”, which means the program is executed
successfully. By this time you should see the LED blinking three times. If there were any
errors in the program, the execution tells to correct it. Once the error is corrected execute the
program again.
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
The eZ430 Chronos development kit from Texas Instruments represents great value for
money and provides a wristwatch with a wireless-enabled microcontroller, accelerometers
and temperature and barometric pressure sensors, and a USB programmer and RF access
point. In this post I take a look at what it takes to get it up and running with a Raspberry Pi.
The Chronos RF access point simply presents itself as a serial port to the operating system
and drivers are included in Linux, and so any heavy lifting in enabling communications
between the watch and host has already been done for us.
With the access point plugged into the Raspberry Pi USB we just need to install a few
dependencies in order to run the TI supplied demonstration software and a simple example
Chronos Control Center is a GUI tool that provides a selection of applications which
demonstrate the capabilities of the eZ430 Chronos. The Linux version of the software must
have been developed with x86 architecture in mind as it's provided as a binary installer rather
than a tar archive. However, since it's Tcl/Tk based it should run on just about any
platform/architecture for which this software is available. It's trivial to repackage it so that it's
not architecture-specific, and this just requires access to an Intel/AMD Linux machine on
which to run the following commands:
$ unzip slac388a.zip
$ ./Chronos-Setup
Obviously if you installed the software to a location other than the default as part of the
second step, you will need to use that location for the second argument in the third step. The
ccc.tgz archive can then be copied to the Raspberry Pi and unpacked to a suitable location.
Control Center software running, with the access point enabled and the watch set
to ACC mode and with RF enabled. Real-time data from the watch accelerometers is
displayed, and by selecting Mouse On it's also possible to use the watch to control the
Raspberry Pi mouse pointer through gesture. As can be seen the Control Center provides a
number of other simple applications that can be selected via the tabs at the top.
It should be possible to write host-based applications for the Chronos in just about any
language that provides access to serial devices. When using the Python language this is
achieved via the pySerial library, and with a reasonably short script it's possible to
Note that if you do wish to make use of the linked script you will need to change the line that
configures the serial port parameters to read:
ser = serial.Serial('/dev/ttyACM0',115200,timeout=1)
Conclusion
Together the Chronos eZ430 and Raspberry Pi opens up all sorts of exciting possibilities,
where data can be sourced from the watch sensors or the Internet, processed and pushed in
either direction. With the relatively powerful processing capabilities of the Raspberry Pi
being made use of, and its hardware capabilities further extended via the GPIO port. As such
it would seem like a winning combination for low cost experimentation with wearable and
ubiquitous computing. And with a little enhanced support from within the Python language, it
is easy to see how the Chronos could become an incredibly fun accessory to Raspberry Pi-
based learning in schools.
Experiment - 5
ON/OFF Control Based On Light Intensity
Using the light sensors, monitor the surrounding light intensity & automatically turn
ON/OFF the high intensity LED's by taking some pre-defined threshold light intensity
value.
Measure the intensity of light in a room using a single photocell and a capacitor connected to
the raspberry pi with a bit of code in python.
What is Photocell?
The Photocell is a light sensor in which the resistance varies according to the intensity of
light. The resistance reduces when it is in brighter surroundings. We have to set up a
threshold value for the measurements of the intensity because it cannot give the precise
measurements. If the measurements are below the threshold then it is dark, else it is bright.
Role of a Capacitor
A Capacitor is an electrical component that can store electrical energy temporarily. It is
measured in Farads which is characterized by capacitance. The capacitor consists of 2
conductors that can hold the electric charge and when it is fully charged the capacitor starts
discharging. This kind of alternative behavior is used to generate AC.
Circuit:
We need to measure the resistance of the photoresistor. The Raspberry pi acts as the battery
whereas the GPIO pin 1 provides 3.3 V to the photoresistor. Make the GPIO pin 12 as the
bidirectional pin ( input and output pin). When the capacitor is charging it will take some
time to reach a voltage that registers as high. GPIO pin 6 is grounded which is connected
to the negative side of the capacitor (short end). Check how long it takes for the input pin to
become high and use the result to calculate the resistance of the photocell.
Code
#measuring the light intensity using a photocell
import RPi.GPIO as GPIO,time,os #import the libraries
DEBUG=1
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
def RCtime(RCpin): # function start
reading=0
GPIO.setup(RCpin,GPIO.OUT)
GPIO.output(RCpin,GPIO.LOW)
time.sleep(2) # time to discharge capacitor
GPIO.setup(RCpin,GPIO.IN)
while (GPIO.input(RCpin) == GPIO.LOW):
# the loop will run till the capacitor is charged
reading += 1
# measuring time which in turn is measuring resistance
return reading
# function
Output
1. With light:
2. Without light:
Material Required
Arduino UNO
10 Segment LED Bar Graph
LCD (16*2)
Potentiometer-10k
Resistor (100ohm-10;330ohm)
Battery (to be tested)
Connecting wires
12v adapter for Arduino
The LED bar graph comes in industrial standard size with a low power consumption. The bar is
categorized for luminous intensity. The product itself remains within RoHS compliant version. It
has a forward voltage of up to 2.6v. The power dissipation per segment is 65mW. The operating
temperature of the LED bar graph is -40℃ to 80 ℃. There are many application for the LED bar
graph like Audio equipment, Instrument panels, and Digital readout display.
Pin Diagram
Pin Configuration
CODE:
#include <LiquidCrystal.h>
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // an array of pin numbers to which LEDs are attached
int pinCount = 10; // the number of pins (i.e. the length of the array)
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
lcd.begin(16, 2); //// set up the LCD's number of columns and rows:
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
}
void LED_function(int stage)
{
for (int j=2; j<=11; j++)
{
digitalWrite(j,LOW);
}
for (int i=1, l=2; i<=stage; i++,l++)
{
digitalWrite(l,HIGH);
//delay(30);
}
}
void loop()
{
// Conversion formula for voltage
analogValue = analogRead (A4);
Serial.println(analogValue);
delay (1000);
input_voltage = (analogValue * 5.0) / 1024.0;
lcd.setCursor(0, 1);
lcd.print("Voltage= ");
lcd.print(input_voltage);
Serial.println(input_voltage);
delay(100);
#include <LiquidCrystal.h>
long randNumber;
int Led = 13; //define LED port
int Shock = 2; //define shock port
int val;//define digital variable val
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12 );
byte customChar[] = {
B00000,
B00000,
B11111,
B11001,
B10101,
B10011,
lcd.begin(16, 2);
lcd.createChar(0, customChar);
lcd.home();
pinMode(Led, OUTPUT); //define LED as a output port
randomSeed(analogRead(0));
pinMode(Shock, INPUT); //define shock sensor as a output port
lcd.write(byte( 0));
lcd.print("Digital dice");
lcd.write(byte( 0));
delay(1000);
}
void loop()
{
val = digitalRead(Shock); //read the value of the digital interface 3 assigned to val
if (val == LOW) //when the shock sensor have signal do the following
{
lcd.clear();
lcd.print("Rolling dice...");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
randNumber = random(1,7);
lcd.print("Dice 1 = ");
lcd.print(randNumber);
lcd.setCursor(0, 1);
randNumber = random(1,7);
delay(150);
}
Keeping up to date with the latest news is tough and sometimes we need a little help. RSS
feeds provide a great way to quickly digest lots of news quickly. Sure you could visit an RSS
feed or have an RSS reader on your computer, but what if you could have a simple, dedicated
device that only shows the headlines?
Here’s a Raspberry Pi project that will use Python code to read an RSS feed, the Tom’s
Hardware feed for example, and display the top five headlines on an LCD screen.
1. Install Python libraries to use LCD screens and work with RSS feeds by entering the
following commands:
4. Launch Thonny. You can find it on the start menu under Programming.
5. In a new file import libraries of Python code to use the LCD screen, control the pace of
the project, read the RSS feed and finally manipulate text into chunks.
6. Create an object, called “tom” which will store the RSS feed data from Tom’s
Hardware.
tom = feedparser.parse("https://www.tomshardware.com/uk/feeds/all")
7. Create a connection to the LCD and then pause the code for 1 second.
lcd = LCD()
sleep(1)
8. Use a for loop to repeat code five times. If you want more than 5 headlines, change the (5)
to a higher number.
for i in range(5):
9. Print the entries from the Tom’s Hardware RSS feed. The value of i is incremented
each time the for loop goes round, to a maximum of five.
print(tom['entries'][i]['title'])
10. Create an object called split and use that to save 16 character chunks of the RSS feed.
The chunk size is set by the 16 character screen size of the LCD.
split = textwrap.wrap(text, 16)
11. Create an object called split and use that to save 16 character chunks of the RSS feed.
The chunk size is set by the 16 character screen size of the LCD.
split = textwrap.wrap(text, 16)
12. Print “Tom’s Hardware” (or the name of your news source) to the first line of the
LCD screen.
lcd.text("Tom's Hardware", 1)
13. Create another for loop to print the contents of the split object to the LCD screen.
for i in range(len(split)):
14. Add a one second pause before clearing the LCD screen.
sleep(1)
lcd.clear()
Code:
from rpi_lcd import LCD
from time import sleep
import feedparser
import textwrap
tom = feedparser.parse("https://www.tomshardware.com/uk/feeds/all")
lcd = LCD()
sleep(5)
for i in range(5):
print(tom['entries'][i]['title'])
text = tom['entries'][i]['title']
split = textwrap.wrap(text, 16)
lcd.text("Tom's Hardware", 1)
for i in range(len(split)):
lcd.text(split[i], 2)
sleep(0.5)
sleep(1)
lcd.clear()
Porting Openwrt
To the Device Attempt to use the device while connecting to a wifi network using a USB
dongle and at the same time providing a wireless access point to the dongle.
A Raspberry Pi within an Ethernet network can be used as a wireless access point, creating a
secondary network. The resulting new wireless network is entirely managed by the Raspberry
Pi.
If you wish to extend an existing Ethernet network to wireless clients, consider instead setting
up a bridged access point.
A routed wireless access point can be created using the inbuilt wireless features of the
Raspberry Pi 4, Raspberry Pi 3 or Raspberry Pi Zero W, or by using a suitable USB wireless
dongle that supports access point mode. It is possible that some USB dongles may need slight
changes to their settings. If you are having trouble with a USB wireless dongle, please check
the forums.
This documentation was tested on a Raspberry Pi 3B running a fresh installation of Raspberry
Pi OS Buster.
Before we start
Have a wireless client (laptop, smartphone, ...) ready to test your new access point.
Enable the wireless access point service and set it to start when your Raspberry Pi boots:
In order to provide network management services (DNS, DHCP) to wireless clients, the
Finally, install netfilter-persistent and its plugin iptables-persistent . This utilty helps by
saving firewall rules and restoring them when the Raspberry Pi boots:
The Raspberry Pi runs a DHCP server for the wireless network; this requires static IP
configuration for the wireless interface ( wlan0 ) in the Raspberry Pi. The Raspberry Pi also
acts as the router on the wireless network, and as is customary, we will give it the first IP
To configure the static IP address, edit the configuration file for dhcpcd with:
interface wlan0
static ip_address=192.168.4.1/24
nohook wpa_supplicant
# https://www.raspberrypi.org/documentation/configuration/wireless/access-point-routed.md
# Enable IPv4 routing
net.ipv4.ip_forward=1
Enabling routing will allow hosts from network 192.168.4.0/24 to reach the LAN and the
main router towards the internet. In order to allow traffic between clients on this foreign
wireless network and the internet without changing the configuration of the main router, the
Raspberry Pi can substitute the IP address of wireless clients with its own IP address on the
LAN using a "masquerade" firewall rule.
The main router will see all outgoing traffic from wireless clients as coming from the
Raspberry Pi, allowing communication with the internet.
The Raspberry Pi will receive all incoming traffic, substitute the IP addresses back,
and forward traffic to the original wireless client.
This process is configured by adding a single firewall rule in the Raspberry Pi:
Now save the current firewall rules for IPv4 (including the rule above) and IPv6 to be loaded
Filtering rules are saved to the directory /etc/iptables/ . If in the future you change the
configuration of your firewall, make sure to save the configuration before rebooting.
Configure the DHCP and DNS services for the wireless network
The DHCP and DNS services are provided by dnsmasq . The default configuration file
serves as a template for all possible configuration options, whereas we only need a few. It is
easier to start from an empty file.
Rename the default configuration file and edit a new one:
The Raspberry Pi will deliver IP addresses between 192.168.4.2 and 192.168.4.20 , with a
lease time of 24 hours, to wireless DHCP clients. You should be able to reach the Raspberry
There are many more options for dnsmasq ; see the default configuration file
allowing applications to be configured with a two-letter "WiFi country code", e.g. US for a
This setting will be automatically restored at boot time. We will define an appropriate
country code in the access point software configuration, next.
Add the information below to the configuration file. This configuration assumes we are using
password AardvarkBadgerHedgehog . Note that the name and password should not have
quotes around them. The passphrase should be between 8 and 64 characters in length.
country_code=GB
interface=wlan0
ssid=NameOfNetwork
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=AardvarkBadgerHedgehog
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Note the line country_code=GB : it configures the computer to use the correct wireless
frequencies in the United Kingdom. Adapt this line and specify the two-letter ISO code of
your country. See Wikipedia for a list of two-letter ISO 3166-1 country codes.
To use the 5 GHz band, you can change the operations mode
Note that when changing the hw_mode , you may need to also change the channel -
Once your Raspberry Pi has restarted, search for wireless networks with your wireless client.
The network SSID you specified in file /etc/hostapd/hostapd.conf should now be present,
wireless client as follows, assuming the pi account is present: ssh [email protected] or ssh
If your wireless client has access to your Raspberry Pi (and the internet, if you set up
routing), congratulations on setting up your new access point!
By default, Apache puts a test HTML file in the web folder. This default web page is served
Pi's IP address is) from another computer on the network. To find the Pi's IP address,
type hostname -I at the command line (or read more about finding your IP address).
Browse to the default web page either on the Pi or from another computer on the network and
you should see the following:
This default web page is just an HTML file on the filesystem. It is located
at /var/www/html/index.html .
Navigate to this directory in a terminal window and have a look at what's inside:
cd /var/www/html
ls -al
total 12
drwxr-xr-x 2 root root 4096 Jan 8 01:29 .
drwxr-xr-x 12 root root 4096 Jan 8 01:28 ..
-rw-r--r-- 1 root root 177 Jan 8 01:29 index.html
owned by the root user (as is the enclosing folder). In order to edit the file, you need to
change its ownership to your own username. Change the owner of the file (the
You can now try editing this file and then refreshing the browser to see the web page change.
Your own website
If you know HTML you can put your own HTML files and other assets in this directory and
serve them as a website on your local network.
To allow your Apache server to process PHP files, you'll need to install the latest version of
PHP and the PHP module for Apache. Type the following command to install these:
sudo rm index.html
Now save and refresh your browser. You should see "hello world". This is not dynamic but
still served by PHP. Try something dynamic:
If you are not using the default pi user account, you need to add your username to
the video group, otherwise you will see 'permission denied' errors.
To check that the user has been added to the group correctly, use
the groups command.
Basic usage
Enter the command fswebcam followed by a filename and a picture will be
fswebcam image.jpg
Note the small default resolution used, and the presence of a banner showing the
timestamp.
Specify resolution
The webcam used in this example has a resolution of 1280 x 720 so to specify the
Picture now taken at the full resolution of the webcam, with the banner present.
Specify no banner
Now add the --no-banner flag:
Some webcams are more reliable than others, but this sort of issue may occur with
poor quality webcams. If the problem persists, ensure your system is up to date.
Also try other webcams, but you'll get the best performance from the Raspberry
Pi camera module.
mkdir webcam
To create a script, open up your editor of choice and write the following example
code:
#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
fswebcam -r 1280x720 --no-banner /home/pi/webcam/$DATE.jpg
This script will take a picture and name the file with a timestamp. Say we saved it
as webcam.sh , we would first make the file executable:
chmod +x webcam.sh
./webcam.sh
Which would run the commands in the file and give the usual output:
crontab -e
This will either ask which editor you would like to use, or open in your default editor. Once
you have the file open in an editor, add the following line to schedule taking a picture every
minute (referring to the Bash script from above):
* * * * * /home/pi/webcam.sh 2>&1
Ensure your script does not save each picture taken with the same filename. This
will overwrite the picture each time.