Benjamin Juul
/
mbed_indeklima
afas
main.cpp@0:fb4d631a5994, 2014-10-28 (annotated)
- Committer:
- 201209650
- Date:
- Tue Oct 28 09:02:51 2014 +0000
- Revision:
- 0:fb4d631a5994
bde
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
201209650 | 0:fb4d631a5994 | 1 | #include "mbed.h" |
201209650 | 0:fb4d631a5994 | 2 | #include "RHT03.h" //Include neede to use the RHT03 lib |
201209650 | 0:fb4d631a5994 | 3 | |
201209650 | 0:fb4d631a5994 | 4 | Serial pc(USBTX, USBRX); |
201209650 | 0:fb4d631a5994 | 5 | I2C i2c(p28,p27); //sda=p28. scl=p27 |
201209650 | 0:fb4d631a5994 | 6 | |
201209650 | 0:fb4d631a5994 | 7 | Serial xBee(p9, p10); //xBee pins |
201209650 | 0:fb4d631a5994 | 8 | |
201209650 | 0:fb4d631a5994 | 9 | int done=0; |
201209650 | 0:fb4d631a5994 | 10 | float temp,hum; |
201209650 | 0:fb4d631a5994 | 11 | |
201209650 | 0:fb4d631a5994 | 12 | RHT03 humtemp(p21); //Initalise the RHT03 (change pin number to the pin its connected to) |
201209650 | 0:fb4d631a5994 | 13 | |
201209650 | 0:fb4d631a5994 | 14 | DigitalOut lysdiode(p14); |
201209650 | 0:fb4d631a5994 | 15 | |
201209650 | 0:fb4d631a5994 | 16 | void tickerFunction() { |
201209650 | 0:fb4d631a5994 | 17 | //I2C CO2 |
201209650 | 0:fb4d631a5994 | 18 | const char cmd[4] = {0x22,0x00,0x08,0x2A}; |
201209650 | 0:fb4d631a5994 | 19 | i2c.write(0xD0, cmd, 4); |
201209650 | 0:fb4d631a5994 | 20 | char data[4] = {0x00,0x00,0x00,0x00}; |
201209650 | 0:fb4d631a5994 | 21 | wait_ms(50); |
201209650 | 0:fb4d631a5994 | 22 | i2c.read(0xD1, data, 4); |
201209650 | 0:fb4d631a5994 | 23 | int CO2i2c = (int)data[1]*0x100 + (int)data[2]; |
201209650 | 0:fb4d631a5994 | 24 | |
201209650 | 0:fb4d631a5994 | 25 | |
201209650 | 0:fb4d631a5994 | 26 | |
201209650 | 0:fb4d631a5994 | 27 | //Print data |
201209650 | 0:fb4d631a5994 | 28 | pc.printf("CO2: %d\n", CO2i2c); |
201209650 | 0:fb4d631a5994 | 29 | |
201209650 | 0:fb4d631a5994 | 30 | //Hum & Temp |
201209650 | 0:fb4d631a5994 | 31 | while(!done) { //Loop keeps running until RHT03 is read succesfully |
201209650 | 0:fb4d631a5994 | 32 | wait(2); //Needed to make sure the sensor has time to initalise and so its not polled too quickly |
201209650 | 0:fb4d631a5994 | 33 | if(humtemp.readData() == RHT_ERROR_NONE) done=1; //Request data from the RHT03 |
201209650 | 0:fb4d631a5994 | 34 | } |
201209650 | 0:fb4d631a5994 | 35 | |
201209650 | 0:fb4d631a5994 | 36 | done = 0; |
201209650 | 0:fb4d631a5994 | 37 | temp = humtemp.getTemperatureC(); //Gets the current temperature in centigrade |
201209650 | 0:fb4d631a5994 | 38 | hum = humtemp.getHumidity(); //Gets the current humidity in percentage |
201209650 | 0:fb4d631a5994 | 39 | |
201209650 | 0:fb4d631a5994 | 40 | if (hum > 50 || temp > 23 || CO2i2c > 1000) { |
201209650 | 0:fb4d631a5994 | 41 | lysdiode = 0; |
201209650 | 0:fb4d631a5994 | 42 | } else { |
201209650 | 0:fb4d631a5994 | 43 | lysdiode = 1; |
201209650 | 0:fb4d631a5994 | 44 | } |
201209650 | 0:fb4d631a5994 | 45 | |
201209650 | 0:fb4d631a5994 | 46 | //Print data |
201209650 | 0:fb4d631a5994 | 47 | pc.printf("Hum: %0.2f, Temp: %0.2f\n", hum, temp); |
201209650 | 0:fb4d631a5994 | 48 | xBee.printf("xxxx;indeklima;%0.2f;%0.2f;%i\r\n", hum, temp, CO2i2c); |
201209650 | 0:fb4d631a5994 | 49 | |
201209650 | 0:fb4d631a5994 | 50 | } |
201209650 | 0:fb4d631a5994 | 51 | |
201209650 | 0:fb4d631a5994 | 52 | Ticker ticker; |
201209650 | 0:fb4d631a5994 | 53 | |
201209650 | 0:fb4d631a5994 | 54 | int main() |
201209650 | 0:fb4d631a5994 | 55 | { |
201209650 | 0:fb4d631a5994 | 56 | |
201209650 | 0:fb4d631a5994 | 57 | pc.printf("Hello\n"); |
201209650 | 0:fb4d631a5994 | 58 | |
201209650 | 0:fb4d631a5994 | 59 | i2c.frequency(100000); //mBed default frequency = 100kHz = max frequency of CO2 sensor |
201209650 | 0:fb4d631a5994 | 60 | ticker.attach(&tickerFunction, 5.0); //update every 1 sec |
201209650 | 0:fb4d631a5994 | 61 | |
201209650 | 0:fb4d631a5994 | 62 | while (1) { |
201209650 | 0:fb4d631a5994 | 63 | } |
201209650 | 0:fb4d631a5994 | 64 | } |