Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
AgriTech
Mobile Autonomous Cart with guided Vision for Agriculture
Commits
d2699bf8
Commit
d2699bf8
authored
Jul 17, 2021
by
Tejasri
🎯
Browse files
Merge branch 'master' into 'master'
Arduino - Python Communication See merge request
!10
parents
2616c05d
f6f84845
Changes
5
Hide whitespace changes
Inline
Side-by-side
Arduino - Python Communication/.gitkeep
0 → 100644
View file @
d2699bf8
Arduino - Python Communication/main.py
0 → 100644
View file @
d2699bf8
# 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
:
try
:
incoming_stream
=
myArduino
.
readline
()
#incoming_stream = incoming_stream.decode('utf-8')
incoming_stream
=
incoming_stream
.
strip
()
print
(
incoming_stream
)
except
:
print
(
"keyboard Interrupt"
)
break
Arduino - Python Communication/readme.md
0 → 100644
View file @
d2699bf8
Use the main.py to recieve the data on computer.
Change COM port accordingly.
See arduino code comments for connections.
Arduino - Python Communication/requirements.txt
0 → 100644
View file @
d2699bf8
pyserial==3.5
Arduino - Python Communication/sensorJSON.ino
0 → 100644
View file @
d2699bf8
/*
* 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
);
delayMicroseconds
(
2
);
digitalWrite
(
trigPin
,
HIGH
);
delayMicroseconds
(
10
);
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
;
}
else
{
return
1
;
}
}
void
setup
()
{
//Begin Serial Communication
Serial
.
begin
(
115200
);
}
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.
Serial
.
println
(
jsonString
);
//100ms delay
delay
(
1000
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment