Hi
I have tried searching the internet and Arduino forums for clue but no luck so far.
My setup:
Arduino Uno via USB to a linux VM running on macosx Vmware fusion
Arduino 1.0.5 IDE running on linux
JDK version 1.7.0_25
Python 2.7.4
Problem
I have setup a DHT11 sensor with Arduino board and I am trying to send temperature and humidity readings to a python program running on linux via pyserial library. The Arduino sketch is using EmBencode lib from Jee labs to create a bencode dictionary to write to serial line. (The EmBencode lib is using Serial.write() so that may have some implications)
If I connect the Arduino via USB then I can read data alright via pyserial
If I do not run the python program and just bring up the Arduino IDE serial monitor then also data shows up fine.
However if the Arduino serial monitor is open and then I run python program then the program is missing the data on serial ine. Like instead of
ABCDEF
ABCDEF
ABCDEF
it gets
ABF
ABCD
ABCF
AE
D
ABCDE
etc. The serial monitor also shows garbled data. So there must be some issue when both the python program is running and the serial monitor is open.
I would like to understand why am I running into this problem. As far as I know, 2 programs can read from the serial line at the same time, isnt it? There should be no synchronization issues, as both program are doing read and individually they work fine.
Arduino sketch
/*
DHT11 library
https://github.com/AnnaGerber/ButterflyProject/tree/master/arduino/libraries/DHT11
*/
#include <dht11.h>
#include <EmBencode.h>
#define DHT11PIN 2
void EmBencode::PushChar (char ch) {
Serial.write(ch);
}
dht11 DHT11;
int ldr_val = 0;
char sno[37] = "2a3f74c9-f077-43d8-94e4-900c38dd5ce1" ;
int humidity ;
int temperature;
EmBencode encoder;
void setup() {
Serial.begin(9600);
}
void read_sensors() {
ldr_val = analogRead(3);
delay(500);
int chk = DHT11.read(DHT11PIN);
if(chk == 0 ) {
temperature = DHT11.temperature ;
humidity = DHT11.temperature ;
} else {
temperature = 0 ;
humidity = 0 ;
}
delay(500);
}
void send_dictionary()
{
encoder.startDict();
encoder.push("temperature");
encoder.push(temperature);
encoder.push("humidity");
encoder.push(DHT11.humidity);
encoder.push("ldr");
encoder.push(ldr_val);
encoder.push("serialNumber");
encoder.push(sno);
encoder.endDict();
Serial.println();
delay(1000);
}
void loop()
{
read_sensors();
send_dictionary();
delay(3000);
}
And the python program - relevant portions are
ser = None
port = '/dev/ttyUSB0'
baud = 9600
while True :
try:
ser = serial.Serial(port, baud)
break
except serial.serialutil.SerialException :
#not open yet
time.sleep(1)
# port open
while True:
try:
buffer = ''
while 1:
ch = ser.read(1)
if(ch == '\n'):
process_line(line)
break
else :
buffer = buffer + ch
except serial.serialutil.SerialException as e1:
print "serial error({0}): {1}".format(e1.errno, e1.strerror)
pass
Your help appreciated
Thanks
/rajeev