Commit d2699bf8 authored by Tejasri's avatar Tejasri 🎯
Browse files

Merge branch 'master' into 'master'

Arduino - Python Communication

See merge request !10
parents 2616c05d f6f84845
# This code accepts incoming string from the COM Port, Parses the JSON String and converts it into a dictionary.
# The final dictionary object JSONDict is created.
# I have simply printed the dictionary here.
# The code can be modified to do required opeartions on the dictionary.
# - Shrishailya Agashe
import json
import serial
#replace this with the comport of your arduino
comport = 'COM3'
#initialize the serial object
myArduino = serial.Serial(port = comport, baudrate = 115200, timeout=1)
#infinite loop that checks continuously for incoming JSON string
while True:
incoming_stream = myArduino.readline()
#incoming_stream = incoming_stream.decode('utf-8')
incoming_stream = incoming_stream.strip()
print("keyboard Interrupt")
Use the to recieve the data on computer.
Change COM port accordingly.
See arduino code comments for connections.
* This code takes care of the Ultrasonic and PIR motion sensor.
* It calculates the distance and also checks status of the PIR motion sensor.
* It sends a JSON string to the Serial Port
* Here, it is assumed that trigPin = 9, echoPin = 10.
* The PIR input is taken through pin 8
* Change the pins accordingly
* - Shrishailya Agashe
#include <Arduino_JSON.h>
//Function that calculates the distance form Ultra Sonic Sensor in cm.
long getUlrasonicDistance(int trigPin, int echoPin){
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = duration /29 /2;
return cm;
//Function that returns the status of PIR Sensor.
int getPIRStatus(int PIRPin){
pinMode(PIRPin, INPUT);
if(digitalRead(PIRPin) == 0){
return 0;
return 1;
void setup() {
//Begin Serial Communication
void loop() {
//Create object to store items like a python dictionary
JSONVar myObject;
//Set the values as needed.
myObject["US"] = getUlrasonicDistance(9,10);
myObject["PIR"] = getPIRStatus(8);
//Convert the JSON object to a JSON string
String jsonString = JSON.stringify(myObject);
//Send the string through Serial port.
//100ms delay
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment