Reader small image

You're reading from  Internet of Things with Arduino Blueprints

Product typeBook
Published inOct 2015
Publisher
ISBN-139781785285486
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Pradeeka Seneviratne
Pradeeka Seneviratne
author image
Pradeeka Seneviratne

Pradeeka Seneviratne is a software engineer with over 10 years' experience in computer programming and systems design. He is an expert in the development of Arduino- and Raspberry Pi-based embedded systems. Pradeeka is currently a full-time embedded software engineer who works with embedded systems and highly scalable technologies. Previously, he worked as a software engineer for several IT infrastructure and technology servicing companies. He collaborated with the Outernet project as a volunteer hardware and software tester for Lighthouse- and Raspberry Pi-based DIY Outernet receivers based on Ku band satellite frequencies. He is also the author of five books: Internet of Things with Arduino Blueprints [Packt Publishing] IoT: Building Arduino-Based Projects [Packt Publishing] Building Arduino PLCs [Apress] Raspberry Pi 3 Projects for Java Programmers [Packt Publishing] Beginning BBC micro:bit [Apress]
Read more about Pradeeka Seneviratne

Right arrow

Chapter 7. Tweet-a-Light – Twitter-Enabled Electric Light

In Chapter 1, Internet-Controlled PowerSwitch, we learned how to control a PowerSwitch Tail (or any relay) through the Internet by using the Arduino Ethernet library. Now, we will look into how Twitter tweets can be used to control the PowerSwitch Tail.

In this chapter, we will learn:

  • How to install Python on Windows

  • How to install some useful libraries on Python, including pySerial and Tweepy

  • How to create a Twitter account and obtain Twitter API keys

  • How to write a simple Python Script to read Twitter tweets and write data on serial port

  • How to write a simple Arduino sketch to read incoming data from serial port

Hardware and software requirements


To complete this project, you will require the following hardware and software.

Hardware

Software

The software needed for this project is mentioned under each topic so that it will be easier to download and organize without messing things up.

Getting started with Python


Python is an interpreted, object-oriented, and high-level computer programming language with very powerful features that's easy to learn because of its simple syntax. For this project, we can easily write an interface between Twitter and Arduino using the Python script.

Installing Python on Windows

The following steps will explain how to install Python on a Windows computer:

  1. Visit https://www.python.org/.

  2. Click on Downloads | Windows.

    The Python home page

  3. Then, you will navigate to the Python Releases for Windows web page:

    The Python download page

  4. Python can be downloaded from two development branches: legacy and present. The legacy releases are labeled as 2.x.x, and present releases are labeled as 3.x.x. (For reference, the major difference of 2.7.x and 3.0 can be found at http://learntocodewith.me/programming/python/python-2-vs-python-3/). Click on the latest (see the date) Windows x86-64-executable installer to download the executable installer setup file to your local...

Creating a Twitter app and obtaining API keys


To proceed with our project, use the following steps to create a Twitter App and obtain the API keys.

  1. Go to https://apps.twitter.com/ and sign in with your Twitter login credentials (create a new Twitter account if you don't have one). The following page will display on the browser:

    apps.twitter.com, the Application Management start page

  2. Click on the Create New App button. The Create an application page will display:

    Twitter's Create an application page

  3. Fill in the required fields (for the website textbox, just type http://www.example.com as a placeholder), accept the Developer Agreement by clicking on the Yes, I agree checkbox.

  4. After this, click on the Create your Twitter application button.

  5. You will be navigated to the following page:

    The Twitter application settings page

  6. Click on the Keys and Access Tokens tab. Under this tab, you will find Consumer Key (API Key) and Consumer Secret (API Secret). Copy these two keys and paste them in a Notepad file...

Reading the serial data using Arduino


You can read incoming data from the serial port where we wrote data using the Python script in the previous section using Arduino. The following Arduino sketch will read the incoming data from the serial port and turn on the PowerSwitch Tail if it finds 1, and turn off the PowerSwich Tail if it finds 0.

The sample code, B04844_07_01.ino, can be found in the Chapter 7 codes folder, so you can copy and paste it on a new Arduino IDE and upload it to your Arduino UNO board.

Connecting the PowerSwitch Tail with Arduino

Connect the PowerSwitch Tail to your Arduino UNO board, as shown in the following Fritzing diagram. For this project, we will use a 240V AC PowerSwitch Tail:

  1. Using a hook-up wire, connect the Arduino digital pin 5 with the PowerSwitch Tail positive (+ in) connecter.

  2. Using another hook-up wire, connect the Arduino ground pin with the PowerSwitch Tail negative (- in) connector.

  3. Connect a 240V AC light bulb to the LOAD end of the PowerSwitch Tail.

  4. Connect...

Summary


In this chapter, you learned how to use Twitter, a social media platform, to interact with our Arduino UNO board and control its functionalities.

In the next chapter, you will learn how to control devices using Infrared, the Internet, and Arduino.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Internet of Things with Arduino Blueprints
Published in: Oct 2015Publisher: ISBN-13: 9781785285486
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Pradeeka Seneviratne

Pradeeka Seneviratne is a software engineer with over 10 years' experience in computer programming and systems design. He is an expert in the development of Arduino- and Raspberry Pi-based embedded systems. Pradeeka is currently a full-time embedded software engineer who works with embedded systems and highly scalable technologies. Previously, he worked as a software engineer for several IT infrastructure and technology servicing companies. He collaborated with the Outernet project as a volunteer hardware and software tester for Lighthouse- and Raspberry Pi-based DIY Outernet receivers based on Ku band satellite frequencies. He is also the author of five books: Internet of Things with Arduino Blueprints [Packt Publishing] IoT: Building Arduino-Based Projects [Packt Publishing] Building Arduino PLCs [Apress] Raspberry Pi 3 Projects for Java Programmers [Packt Publishing] Beginning BBC micro:bit [Apress]
Read more about Pradeeka Seneviratne