Book Image

Internet of Things Programming with JavaScript

Book Image

Internet of Things Programming with JavaScript

Overview of this book

The Internet of Things is taking the tech world by storm, and JavaScript is at its helm. This book will get you to grips with this exciting new technology. Where do Node.js, HTML5 and Windows 10 IoT Core come in with JavaScript and IoT? Why Raspberry Pi Zero rather than Arduino? How do you configure and build an IoT network from scratch? All your IoT JavaScript questions are answered in this book.
Table of Contents (15 chapters)
Internet of Things Programming with JavaScript
Credits
About the Author
www.packtpub.com
Customer Feedback
Preface

Controlling the Arduino board from Python


First we need to install the serial library, as this helps to communicate with Arduino via the USB port communication. Type the following command to install the library:

sudo apt-get install python-serial

The following code controls Arduino from Raspberry Pi; you can now either copy the code inside a file called ControlArduinoFromRasp.py, or just get the complete code from the folder for this project.

The following snippet imports the serial library in Python:

import serial 

We define the serial communication:

Arduino_UNO = serial.Serial('/dev/ttyACM0', 9600) 

Print a message to see that the communication is done:

print("Hello From Arduino!") 

While this executes, the user can enter a command:

while True: 
      command = raw_input('Enter the command ') 
      Arduino_UNO.write(command) 

If it's an H it prints the message; in case it is false it displays LED off:

      if command == 'H': 
            print('LED ON') ...