Technology

First IoT Project with Raspberry Pi

What you’ll need:

  1. SD Card
  2. Wireless keyboard and mouse
  3. Raspberry Pi 3B+
  4. Power supply
  5. HDMI cable

What to do:

  1. Format the SD Card using SD Memory Card Formatter available here
  2. Download NOOBS from here (the file is >1.5 GB so this will take some time)
  3. Extract NOOBS files from the .zip and copy them to the SD card
  4. Eject the SD card from your PC and insert into the RPi
  5. Connect the RPi to a monitor, keyboard/mouse, and lastly a power supply
  6. When prompted, install Raspbian (this will take awhile)
  7. When complete, the RPi will automatically restart and take you to the desktop
  8. First, enable WiFi (it is disabled by default). Go to Preferences -> Raspberry Pi Configuration -> Localisation -> Set Locale and then Set WiFi Country and choose your country. You will be prompted to reboot after setting the WiFi Country.
  9. Open the terminal and use the following command to install an Apache server: sudo apt-get install apache2 -y
  10. Get the RPi’s IP address sing the command: hostname -i (Note: an IP like 127.0.0.1 will run in the local browser only. To access the page from other devices on the same network, use the 192 IP address address, which can be found using the sudo ifconfig command)
  11. Open the browser and enter the IP address in the command line and verify the Apache server is running
  12. Install the php processor using the command: sudo app-get install php7.0 libapache2-mod-php7.0 -y
  13. Delete the default index.html in /var/www/html and create a new file index.php
  14. Insert the following code into index.php and save the file:
<?php
if (isset($_POST['on']))
{
    exec("sudo killall python");
    exec("sudo python /var/www/ledON.py");
}
else if (isset($_POST['off']))
{
    exec("sudo killall python");
    exec("sudo python /var/www/ledOFF.py");
}
else if (isset($_POST['blink']))
{
    exec("sudo python /var/www/ledBLINK.py");
}
?>
<html>
<body>
  <div id="container">

      <h1>Led ON</h1>
      <h1>Led OFF</h1>
      <h1>Led BLINK</h1>

  </div>
</body>
</html>

Next you’ll create the three python files (ledON.py, ledOFF.py, and ledBLINK.py):
ledON.py

import time, RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.setwarnings(False)
GPIO.output(12, True)
time.sleep(1)

ledOFF.py

import time, RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.setwarnings(False)
GPIO.output(12, False)
time.sleep(1)

ledBLINK.py

import time, RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.setwarnings(False)
while True:
    GPIO.output(12, False)
    time.sleep(0.2)
    GPIO.output(12, True)
    time.sleep(0.2)

Last step is to update the sudoers file. Open it with the command sudo nano /etc/sudoers and add to the bottom of the file the line: www-data ALL=(ALL) NOPASSWD:ALL

Reboot your Pi (e.g. sudo reboot) and desk using a local browser and from another device on the same network.

Source

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s