I currently have an Arduino Mega 2560. This is hooked up to a DHT-11 sensor which works perfectly.
The code I use for the Arduino:
#include <dht.h>
#define dht_dpin A0 //no ; here. Set equal to channel sensor is on
dht DHT;
void setup(){
Serial.begin(9600);
delay(300);//Let system settle
delay(700);//Wait rest of 1000ms recommended delay before
//accessing sensor
}//end "setup()"
void loop(){
//This is the "heart" of the program.
DHT.read11(dht_dpin);
Serial.print("Hum: ");
Serial.print(DHT.humidity);
Serial.print(" temperature: ");
Serial.println(DHT.temperature);
delay(5000); //no need to access frequently
}// end loop()
Now when I connect my arduino to my pc and launch the Serial Monitor, everything works fine and the value is emitted every 5 seconds. If the problem was with the Arduino I wouldve changed stack exchange site
The problem is that I've now connected my Arduino to my Raspberry Pi 3. I'm reading the output of my USB device with the Python pySerial
.
Python code:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
ser.timeout = 1 #turned off and on depeinding on output
s = [0]
while 1:
try:
s[0] = ser.readline()
if(s[0]):
print[s0]
hum = s[0].split()[1]
temp = s[0].split()[3]
print "hum:" + hum
print "temp" + temp
except Exception, e1:
print "error: " + str(e1)
print ser.readline()
ser.close()
Now when I comment out ser.timeout = 1
I get the following output:
�������������������������������������������������������������������������������������������������������Hum: 47.00 temperature: 24.00
hum:47.00
temp24.00
Hum: 47.00 temperature: 24.00
hum:47.00
temp24.00
Hum: 47.00 temperature: 24.00
hum:47.00
temp24.00
And then it doesn't output anything anymore. The Arduino is still sending data though (as seen by the LED light).
If I add the ser.timeout = 1
I do get output everytime the Arduino sends data but it seems corrupt:
�������������������
error: list index out of range
Hum: 47.00 temperature: 24.00
Hum: 47.00 temperature: 24.00
hum:47.00
temp24.00
Hum: 47.00 temperature: 24.00
hum:47.00
temp24.00
����������������������������������������������
error: list index out of range
����������������������������������������������
error: list index out of range
����������������������������������������������
error: list index out of range
(After the last hum and temp values are shown, the question marks are the only data I receive)
What is causing this and why? How can I resolve this?