From cc467b9cf30709e3a7b5290ebeb4cbd268a28c78 Mon Sep 17 00:00:00 2001 From: FluffyCube9343 Date: Fri, 2 Dec 2022 23:11:57 -0500 Subject: [PATCH] old comms bad (low retention) -> this works as PoC --- Comms-Rewrite/bouncer/bouncer.ino | 27 +++++++++++++++++++ Comms-Rewrite/toA.py | 45 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Comms-Rewrite/bouncer/bouncer.ino create mode 100644 Comms-Rewrite/toA.py diff --git a/Comms-Rewrite/bouncer/bouncer.ino b/Comms-Rewrite/bouncer/bouncer.ino new file mode 100644 index 0000000..4bf81a8 --- /dev/null +++ b/Comms-Rewrite/bouncer/bouncer.ino @@ -0,0 +1,27 @@ +/* +Program: Receive Strings From Raspberry Pi +File: receive_string_from_raspberrypi.ino +Description: Receive strings from a Raspberry Pi +Author: Addison Sears-Collins +Website: https://automaticaddison.com +Date: July 5, 2020 +*/ +int i=0; +void setup(){ + + // Set the baud rate + Serial.begin(9600); + +} + +void loop(){ + + if(Serial.available() > 0) { + String data = Serial.readStringUntil('\n'); + if(data=="Data"){ + Serial.print("Hi Raspberry Pi! You sent me: "); + Serial.println(String(i)); + i+=1;} + } + +} diff --git a/Comms-Rewrite/toA.py b/Comms-Rewrite/toA.py new file mode 100644 index 0000000..1df8b78 --- /dev/null +++ b/Comms-Rewrite/toA.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +############################################################################### +# Program: Send Strings to an Arduino From a Raspberry Pi +# File: send_strings_to_arduino.py +# Description: This program runs on a Raspberry Pi. It sends strings +# to Arduino. It also receives the string it sent +# and prints it to the screen. This provides bi-directional (2-way) communication +# between Arduino and Raspberry Pi. +# Author: Addison Sears-Collins +# Website: https://automaticaddison.com +# Date: July 5, 2020 +############################################################################### + +import serial # Module needed for serial communication +import time # Module needed to add delays in the code + +# Set the port name and the baud rate. This baud rate should match the +# baud rate set on the Arduino. +# Timeout parameter makes sure that program doesn't get stuck if data isn't +# being received. After 1 second, the function will return with whatever data +# it has. The readline() function will only wait 1 second for a complete line +# of input. +ser = serial.Serial('/dev/ttyACM1', 9600, timeout=1) + +# Get rid of garbage/incomplete data +ser.flush() + +# Infinite loop +i = 0 +while (1): + i+=1 + send_string = ("Data\n") + + # Send the string. Make sure you encode it before you send it to the Arduino. + ser.write(send_string.encode('utf-8')) + + # Do nothing for 500 milliseconds (0.5 seconds) + time.sleep(0.02) + + # Receive data from the Arduino + receive_string = ser.readline().decode('utf-8').rstrip() + + # Print the data received from Arduino to the terminal + print(receive_string) \ No newline at end of file