Reputation: 23
for a school project I have to read data from 2 sensors on an Arduino (Sodaq Mbili) board. The sensors I use are TPHv2 (temperature, pressure, humidity) and a Grove Light Sensor. I want to read the temperature, humidity and light intensity. I use the following code for this:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(bme.readTemperature());
Serial.println(bme.readHumidity());
int sensorValue = analogRead(SENSOR_PIN);
Serial.println(sensorValue);
delay(3000);
}
This gives me the following output every 3 seconds:
21.23
25.65
256
I then connect the Arduino to my Raspberry Pi 2 through USB. I want to get the data in variables so that I can put it in a Json format and send it to an Azure Event Hub. I now have this code in Python on the raspberry (I found this online):
import serial
ser = serial.Serial('/dev/ttyUSB0',9600)
s = [0]
while True:
s[0] = ser.readline()
print s
My output then gives, every 3 seconds:
['22.46\r\n']
['37.93\r\n']
['643\r\n']
My question now is, how do I get these 3 values in 3 different variables? I tried to put them in the same array (I want something like this: [22.46,37.93,643] ) but that didn't work.
Does anyone have suggestions? Thanks in advance!
Upvotes: 0
Views: 8707
Reputation: 489
Well, if you don't want to format the code in the arduino like jabujavi said, you could do something like this:
import serial
ser = serial.Serial('/dev/ttyUSB0',9600)
s = []
while True:
data = ser.readline() #read data from serial
if data: #if there is data, append it to s
s.append(data)
if len(s) == 3: #when s is 3 elements long, (all data has been retrieved)
print s #print out s
s = [] #and then reset s to start over.
Upvotes: 1