For my project I am trying to send over an array of three values from a CSV to Python and from there to Arduino.
In this code I send over one value to Arduino and then back to Python to verify if it is working. But In Python I am not receiving anything back. Am I sending over the values from Python to Arduino in the right data type? Maybe some other things I am overlooking?
Python code:
import serial
import struct
import time
import csv
csv_file = open('/home/ko/Downloads/Buzzard_600_2020-04_all_data_with_wind_only flight-for-python.csv')
csv_reader = csv.reader(csv_file, delimiter=';')
i=0
temp = 0
arduino = serial.Serial('/dev/ttyACM0', 115200, timeout=.1)
# let it initialize
time.sleep(2)
def conversion(k):
kfloat = float(k)
kint = int(kfloat)
return kint
# bird, date_time, longitude, latitude, altitude, pressure temperature satellites_used gps_fixtime positiondop h_accuracy v_accuracy x_speed y_speed z_speed speed_accuracy location userflag speed_3d speed_2d direction altitude_agl wind_direction (degrees) wind_speed (km/h)
for row in csv_reader:
bird, datetime, longitude, latitude, altitude, pressure, temp, satellites, gpsfixtime, positiondop, h_accuracy, v_accuracy, x_speed, y_speed, z_speed, speed_accuracy, location, userflag, speed_3d, speed_2d, direction, altitude_agl, wind_direction, wind_speed, no = row
tempint = conversion(temp)
altitudeint = conversion(altitude)
wind_directionint = conversion(wind_direction)
wind_speedint = conversion(wind_speed)
arduino.write(struct.pack('>BBB', tempint,wind_speedint,altitudeint))
#arduino.write(bytes(altitude, 'utf-8'))
#data = arduino.readline()
print("---------------------")
#print(i)
print("temp_int=")
#print(tempint)
#print("altitude_map=")
#print(altitudemap)
#print("wind_speed_map=")
#print(wind_speedmap)
#arduino.write(bytes(tempmap))
#arduino.write(tempint)
#arduino.write (tempint)
#time.sleep(1)
data = arduino.readline()
time.sleep(1)
#print (tempint)
print (data)
time.sleep(1)
i = i + 1
time.sleep(3)
Arduino code:
#define USE_SERIAL Serial
int incoming[3];
int outVal1 = 0;
int outVal2 = 0;
int outVal3 = 0;
void setup() {
USE_SERIAL.begin(115200);
Serial.setTimeout(1);
}
void loop()
{
while (!Serial.available() >= 3) {
//fill array
for (int i = 0; i < 3; i++) {
incoming[i] = Serial.readString(). toInt ();
outVal1 = incoming[0];
//outVal2 = incoming[1];
//outVal3 = incoming[2];
Serial.print (outVal1);
delay (1000);
}
}
}