Setting up Raspberry Pi Controlled LED in Minutes

ABHISHEK KUMAR
3 min readNov 13, 2018

--

Step 1 — Burning the Raspbian image to the SD Card is the first thing to do. For this you can use the following

1. Win32 Disk Imager for windows

2. This Tutorial for linux and mac

Step 2 — -Once the SD card is ready with the operating system, insert it to Raspberry Pi and boot. And also connect the Pi to your modem via Ethernet, (WiFi can also be used)

Now you can ssh into RPi with the IP address of Pi which you can find from model status page.

ssh pi@ipaddress

Your default username for Pi will be pi and the password will be raspberry. if the authentication is successful it’ll take you to your Pi’s console.

Step 3 — -Now it’s time for setting up the libraries. For this tutorial you need a set of library to be installed which can be done with following commands

sudo apt-get install python-pip
sudo pip install flask

Above commands will install pip and flask in your Pi. pip is a package manager for python and flask is a python based micro web framework.

Step 4 — Wiring the board will the next major task. Here we’re using the pin 7 for positive and negative terminal is grounded to ground pin of the Raspberry Pi. The wiring will be like the following diagram.

for more details about the pin configuration refer this website : https://pinout.xyz/

Pin Configuration for LED

Step 5 — — The main task now is to create a simple web app with flask which handles the request and control the LED power supply.

First create a simple python file with following command

sudo nano simple_iot.py

And here is the code for achieving that. Copy paste it to the file.

from flask import Flask, render_template
from flask.ext.responses import json_response, xml_response, auto_response
import datetime
import RPi.GPIO as GPIO
app = Flask(__name__)

@app.route(“/toggle”, methods=[‘POST’])
def togle():
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
state = not GPIO.input(7)
GPIO.output(7,state) ## Turn on GPIO pin 7
return json_response({“status”: state}, status_code=201)

if __name__ == “__main__”:
app.run(host=’0.0.0.0', port=80, debug=True)

Now time to run the server. You can run the server simply like a normal python app

sudo python simple_iot.py

Now open up your rest client like Postman or Advance Rest Client and raise a POST request to

http://pi_ipaddress/toggle

Something like below. If everything works fine you’ll find status of the light returned and the light will toggle its state.

A screenshot from Postman

This is a quick run through of Setting Raspberry Pi with WebServer to control LED.

github link https://github.com/abhibvp003/RaspberryPi.git

--

--

ABHISHEK KUMAR

DevOps/Cloud | 2x AWS Certified | 1x Terraform Certified | 1x CKAD Certified | Gitlab