lundi 20 avril 2015

Raspberry Pi Qt program receive data from arduino

I am doing this project with a Qt GUI on raspberry Pi. Now I need to receive data from a DHT11 tem&humidity sensor connected to XinoRF(based on Arduino Uno) and display in my GUI.

The Arduino module and my pi is connected wirelessly and received data could be displayed in the serial monitor in Arduino IDE successfully. But what I really need to achieve is to send a signal from my GUI, say a button is clicked, then the Arduino start to send data back to pi.

However now from the Arduino IDE i called Serial,print(Serial.read()) and found that there is a signal '248' send to the serial port each time I get a reading so I think I am not able to send my signal while there is already one sending from the serial port. I could not find where the '248' signal comes from so I suspect it is sent from the Slice of Radio when it is communicating with the Arduino module. So how can I control the communication from my GUI?

here is the code for Arduino

const int DHpin = 2;

byte dat[4];
void setup()
{
    pinMode(DHpin, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
    DHT11_test();
    show_data();
    delay(500);
}

void show_data(){   

    Serial.print("Humdity: ");
    Serial.print(dat[0], DEC);
    Serial.print(".");
    Serial.print(dat[1], DEC);
    Serial.println("%");

    Serial.print("\tTemperature: ");
    Serial.print(dat[2], DEC);
    Serial.print(".");
    Serial.print(dat[3], DEC);
    Serial.println(" C");

}
byte DHT11_read(){
    byte data = 0;
    for(int i=0; i<8; i++){
        if( digitalRead(DHpin)==LOW ){
        while( digitalRead(DHpin)==LOW );
        delayMicroseconds(30);
            if( digitalRead(DHpin)==HIGH )
                data |= 1<<(7-i);
            while( digitalRead(DHpin)==HIGH );
        }
    }
    return data;
}

void DHT11_test(){
    digitalWrite(DHpin, LOW);
    delay(30);
    digitalWrite(DHpin, HIGH);  
    delayMicroseconds(40);
        pinMode(DHpin, INPUT);
        while( digitalRead(DHpin)==HIGH );
        delayMicroseconds(80);
        while( digitalRead(DHpin)==LOW );
        delayMicroseconds(80);
        for(int i=0; i<4; i++)
            dat[i] = DHT11_read();
        pinMode(DHpin, OUTPUT); 
    digitalWrite(DHpin, HIGH);
}`

Aucun commentaire:

Enregistrer un commentaire