To set this up, you will need:
- RaspberryPi
- Adaptor/SDcard with micro SD card (64 GB preferably)
- Acomputer
- RaspberryPi power supply charger/ 5V micro-USB
- HDMIcord/composite video cable
- Monitor/TVthat supports HDMI or composite video
- Keyboard/mouse
- Optional:Ethernet cable (not required for set up, but helpful)
**Why do we need a Raspberry Pi? Your Raspberry Pi acts as a computer and we’re essentially making one to start the project.**
First, take your SD card and plug it into your SD slot on your computer.
Second, we need to download Raspbian/NOOBS. Raspbian has programs such as Python, Scratch, Wolfram and is great for beginner coders.
You can find Raspbian at this link: https://www.raspberrypi.org/downloads/raspbian/
Link to Noobs: https://www.raspberrypi.org/downloads/noobs/
*You want NOOBS version2.4.5, not NOOBS LITE
You should download the file that fits best with your computer. If done incorrectly, you may need to format the SD card (erasing the information) by downloading an SD card formatter and redownloading Raspbian, or try to use a different computer, if possible, that may be more compatible with the program. NOOBS is a large fileso it may take time to complete.
Third: Once NOOBS is downloaded and you have Raspbian, drag and drop the file to your SD card and it should automatically save.
Fourth, safely remove the SD card by properly ejecting the card.
……. Now that we have the programs that we need, take the micro SD card from the Adaptor, and insert the card in the SD card slot under your Raspberry Pi….
Step 1: Part 2: Set up Raspberry PiFirst, plug in your USB Keyboard/USB mouse into the USB ports on your raspberry pi.
Second, turn on your monitor/TV set and set the input to HDMI or component (depending on which HDMI port you’re using on your monitor.
Third, connect the other end of the HDMI cable to the Raspberry Pi.
Fourth, connect an Ethernet cable to your router or you can use Wi-Fi and configure the Wi-Fi by using network and internet settings on the Pi.
…. Then, connect the other side of the cable to your Raspberry Pi.
Fifth, connect the power supply to the Raspberry Pi, plug the power supply to a power outlet to turn the Raspberry Pi on/ boot up the Pi and a red light will turn on to let you know that you have connected everything.
Step 1: Part 3: Set up Raspberry Pi
We will now get Raspbian set up on your Raspberry Pi by configuring the Pi.
Start by using Raspbian operating System. Raspbian the easiest to use for beginners and several programs use it. If you want to use a different operating system, you can reconfigure your raspberry pi to whichever system you choose.
When you successfullyc ompleted Step 1: Parts 1-3, a start screen should appear on your monitor/TV.
First, Select Raspbian and click install.
Second, A warning window will pop up. Click Yes.The pop-up is just a confirmation pop up.
Third, wait for the install to finish.
…Then, Raspbian will automatically begin to boot.
Step 1: Part 4: Set up Raspberry PiWe have reached the final step of configuring the Pi!
Once the boot process has completed, the Raspbian home screen will appear. Here we are setting the location, date and time.
First, click menu in the upper left corner. The icon should be a raspberry.
Second, select Preferences
Third, select Raspberry Pi Configuration
Fourth, a configuration window will appear. Click on the tab that says Localisation.
…you’ll then,
ClickSet Locale to set your location,
ClickSet Timezone to your local time zone,
ClickSet Keyboard to your keyboard language.
Lastly, the reconfiguration requires you to reboot your Raspberry Pi. When the rebootwindow appears, click Yes to continue.
Finally, you should be all set to start!
Step 2: Part 1: Set up Losant &Install DependenciesGo to:
Click Sign Up in the right-hand corner, create an account and Log in.
You should do this step, Step 2: Part 1 on your Raspberry Pi.
Install Dependencies to Raspberry Pi
Python is already pre-installed on your Raspberry Pi (in the upper left-hand corner of your Pi under programming. To make things run fasterand smoother, we are going to use Python for this project and use index.py from the GitHub site:
https://github.com/Losant/example-raspberry-pi-python
If you are not a Python user, here are two other alternatives:
First, we need to install the Python package manager: pip. (If you have pip already, you can skip this command)
Open your commandprompt on your computer. Also known as, your terminal. If you can’t find the command prompt, go to your homescreen’s search bar and type in cmd.
Then, in the commandprompt, type:
$ sudo apt-get install python-pip
Second, we need to connect to Losant. To connect, we need to install the Losant Python library which is also found on GitHub. We also need to install gpiozero, a library that gives us an interface to GPIO devices with Raspberry Pi (which is extremely important).
$ sudo pip install losant-mqtt gpiozero
Assuming nothing was changed from Step 1, for Step 2 you will need:
- Raspberry Pi
- Red LED light(s) (just in case one burned out or didn’t work
- Male/Female wires
- Male/male wires
- Breadboard
- Resistors
First, attach the LED and the button to the RaspberryPi. The wiring is very important! You will need your GPIO Pin chart to make sure you connect the right pieces together.
**make sure you don’t connect power to ground or your Raspberry Pi will catch on fire**
Follow these pictures exactly to make sure everything is wired right. Depending on your Pi, you may need to refer to your GPIO Pin chart to make sure you don’t connect power to ground. If you get stuck and your light does not work, switch the LED around because electricity works through positives and negatives. You will notice that one side of the LED pin is longer than the other.
If you look at the source code, index.py, the LED is attached to GPIO pin 23, and the button is attached to GPIO 21:
First, Go into your Losant account by loggingin.
Second, under Applications, go to the tab Devices and click: +Add Device.
Tofill out the overview:
- Give the device a name
- The device type: Standalone
§Device attributes:
- Datatype: Boolean
- Name:button
**This is because it will report the state when the button is pressed. The button has an on/off value (true/false) which is why our data type is set to boolean**
Finally, create an access key and make sure it’s copied and saved somewhere safe and where you can get a hold of it easily.
Step 2: Part 4: CodeNow we need to write a way for the device to communicate with Losant, give the device instructions and listen for device commands.
First, open your file explorer and create a new file called: index.py.
Second, in this file, include this code:
- Import json
- From gpiozero import LED, Button # Import GPIO library:https://gpiozero.readthedocs.io/en/stable/
- From time import sleep
- From losant mqtt import Device # Import Losant library:https://github.com/Losant/losant-mqtt-python
led_gpio = 23
button_gpio = 21
led = LED(led_gpio)
button =Button(button_gpio, pull_up=False)
# Construct Losantdevice
device = Device("my-device-id", "my-app-access-key", "my-app-access-secret")
def on_command(device, command):
print(command["name"] + " commandreceived.")
# Listen for the gpioControl. This name configured in Losant
if command["name"] == "toggle":
# toggle the LED
led.toggle()
def sendDeviceState():
print("Sending Device State")
device.send_state({"button": True})
# Listen for commands.
device.add_event_observer("command", on_command)
print("Listening for device commands")
button.when_pressed =sendDeviceState # Send device state when button is pressed
# Connect to Losant and leave the connection open
device.connect(blocking=True)
Where it says:
Device=device ("my-device-id", "my-app-access-key", "my-app-access-secret"),
You already have these values from when you created your device.
- Go to your device
- Click on the device you already created
- Copy the device ID and paste it in "my-device-id",
- Grab where you saved your access key and accesssecret
- Paste those in where you see "my-app-access-key", "my-app-access-secret"
- Open your command prompt and now type in:
$python index.py
Now let’s create a workflow!
Step 2: Part 5: Creating a Workflow/Toggle LEDIn Losant, a workflow allows you to use triggers to create actions on your device. The following picture shows, whenever the button is pressed, a SMS message is sent to your phone.
Now let’s toggle the LED.
First, we need to add a virtual button to a device command. Don’t change/delete what you have already created, just add the virtual button to the device command and place it near your already created workflow.
The device command should be connected to your device when you click on the device command button.
Test: Open the command prompt and type
$ python index.py
The screen should show:
- listening for device commands
Press the button on the bread board and you should get:
- Sending device state
**If you get an error, check your wiring and redo step 2: Part 5: Test**
Now, Whenever the virtual button on the workflow is clicked, it will send a command with the name "toggle" to the connected Raspberry Pi. We are now able to control our LED virtually!
Hurray!
Second, scroll down to your triggers and add a Webhook.
Third, connect the webhook at the same place as the virtual Button on the device command.
- click the webhook
- give the webhook a name
- copy the link provided
Your workflow should now look like this:
This is the webhook URL you need for IFTTT.
Step3: Part 1: Setting up IFTTTHang in there, we’re almost finished!
We are now making an IFTTT account (if this, then that). In this app, we are able to connect to Gmail’s API, grab messages from our Gmail account, grab the first message in our inox and deploy. The best thing about this app, is that this app does all of this for you!
- First, go to: https://ifttt.com/discover and create a new account.
- Second, go to My Applets
- Third, click: New Applet
- Fourth, a screen that says “if+this then that” should pop up. Hover your mouse over +this and click.
- Fifth, choose your service as Gmail
- Sixth, click “Any new email in inbox”
- Seventh, the screen should now look like:
then ✚that
Click ✚that
- §Eighth, type in webhooks
Once you click webhooks and you click to make a web request to access a URL (here we need to access the URL we created in our workflow in Losant.
- change method to post
- paste URL from workflow
- (if you didnʻt copy the URL, go back into your workflow on Losant and click on webhook that you add earlier. Then scroll down to copy the URL.
Congratulations! You have finished the tutorial.
Now send yourself an email and watch the LED light turn on every time you get a new email!
It worked!
If it didn’t work go back and retrace your steps.
Demo Video:
Comments