Starting IoT with Raspberry Pi

This is the first in a series of posts where I get a Raspberry Pi connected to the Internet, and able to respond to commands from a web page.  I also want to get information off the Pi, so I can use the Pi to monitor things.  I am new to Raspberry Pi and the Python programming language, but have experience with programming and electronics.  If this works out well, I'd really like to do something with making an Arduino Uno talk to the Internet.

First up I set up the simple circuit I'm planning on starting with.  Raspberry Pis don't have any analogue inputs, so I decided to use a switch for input and to turn on and off an LED as an output. The green wire connects the switch to IO port #4 on the Pi, while the blue wire connects the LED to port #5.  There's a 100Ω resistor to prevent too much current going through the LED and I put a 10kΩ resistor on the switch to pull the voltage down when the switch is turned off.

The simple circuit
I booted up my Pi with the latest Raspian operation system installed - called Jessie after the Toy Story character.  Jessie comes with Python installed, and a handy shell called Idle.  Idle is a REPL, or Read Eval Print Loop so it can be used to try out commands straight away, without saving your programme.  I started Idle as root by typing in sudo idle3 &, and ran:


import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(5, gpio.OUT)
while True:
    gpio.output(5, True)
    time.sleep(1)
    gpio.output(5, False)
    time.sleep(1)

This code flashes the LED attached to GPIO #5 on and off every second.  Now for the input.  I wrote a loop that would print out the I/O value to the REPL console.  The code is:

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(4, gpio.IN)

while True:
    print(gpio.input(4))
    time.sleep(1)

This code prints the switch's value every second, 1 when the switch is on and 0 when it's turned off.   Next blog I'll connect the Pi to the Internet.

Comments

Popular posts from this blog

Feature Toggles on a .Net Core API

Setup a Kubernetes Cluster on Azure

Automatically Logging PowerShell in to Azure