Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Internet of Things Programming Projects

You're reading from   Internet of Things Programming Projects Build modern IoT solutions with the Raspberry Pi 3 and Python

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781789134803
Length 436 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Colin Dow Colin Dow
Author Profile Icon Colin Dow
Colin Dow
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Installing Raspbian on the Raspberry Pi FREE CHAPTER 2. Writing Python Programs Using Raspberry Pi 3. Using the GPIO to Connect to the Outside World 4. Subscribing to Web Services 5. Controlling a Servo with Python 6. Working with the Servo Control Code to Control an Analog Device 7. Setting Up a Raspberry Pi Web Server 8. Reading Raspberry Pi GPIO Sensor Data Using Python 9. Building a Home Security Dashboard 10. Publishing to Web Services 11. Creating a Doorbell Button Using Bluetooth 12. Enhancing Our IoT Doorbell 13. Introducing the Raspberry Pi Robot Car 14. Controlling the Robot Car Using Python 15. Connecting Sensory Inputs from the Robot Car to the Web 16. Controlling the Robot Car with Web Service Calls 17. Building the JavaScript Client 18. Putting It All Together 19. Assessments 20. Other Books You May Enjoy

Reading robot car sensory data with Python

Although we have covered this before, it's a good idea to familiarize (or re-familiarize) ourselves with the programming of the HC-SR04:

  1. Open up Thonny from Application Menu | Programming | Thonny Python IDE.
  2. Click New to create a new file.
  3. Type the following:
from gpiozero import DistanceSensor
from time import sleep

distance_sensor = DistanceSensor(echo=18, trigger=17)

while True:
print('Distance: ', distance_sensor.distance*100)
sleep(1)
  1. Save the file as distance-sensor-test.py.
  2. Run the code.
  3. Place your hand in front of the distance sensor. You should see the following in the shell (depending on how far your hand is from the distance sensor):
Distance: 5.05452024001
  1. As you move your hand closer to, or farther away from, the distance sensor, the value will change. This code is pretty self explanatory. The distance_sensor...
lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image