Hauptprogramm
Dependencies: ILI9340_Driver_Lib PM2_Libary Lib_DFPlayerMini
Feuchtigkeitssensor/Chirp.cpp@27:bbcd157dcd63, 2021-05-02 (annotated)
- Committer:
- ackerden
- Date:
- Sun May 02 19:50:46 2021 +0000
- Revision:
- 27:bbcd157dcd63
- Child:
- 29:91df2c5fb297
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ackerden | 27:bbcd157dcd63 | 1 | #include "mbed.h" |
ackerden | 27:bbcd157dcd63 | 2 | #include "Chirp.h" |
ackerden | 27:bbcd157dcd63 | 3 | |
ackerden | 27:bbcd157dcd63 | 4 | Chirp chirp(D14, D15); |
ackerden | 27:bbcd157dcd63 | 5 | |
ackerden | 27:bbcd157dcd63 | 6 | //Chirp Docs are at https://github.com/Miceuz/i2c-moisture-sensor and https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/ |
ackerden | 27:bbcd157dcd63 | 7 | void getResults(); |
ackerden | 27:bbcd157dcd63 | 8 | Chirp::Chirp(PinName sda, PinName scl, char slave_adr) |
ackerden | 27:bbcd157dcd63 | 9 | :i2c_p(new I2C(sda, scl)), i2c(*i2c_p), address(slave_adr) |
ackerden | 27:bbcd157dcd63 | 10 | { |
ackerden | 27:bbcd157dcd63 | 11 | i2c.frequency(10000); |
ackerden | 27:bbcd157dcd63 | 12 | |
ackerden | 27:bbcd157dcd63 | 13 | } |
ackerden | 27:bbcd157dcd63 | 14 | |
ackerden | 27:bbcd157dcd63 | 15 | float Chirp::getCapacitance(){ |
ackerden | 27:bbcd157dcd63 | 16 | i2c.frequency(10000); |
ackerden | 27:bbcd157dcd63 | 17 | short capacitance = 0; |
ackerden | 27:bbcd157dcd63 | 18 | char i2cData[2]= {0}; |
ackerden | 27:bbcd157dcd63 | 19 | |
ackerden | 27:bbcd157dcd63 | 20 | char dataReg = 1; //Capacitance is stored in 2 bytes at address 0 |
ackerden | 27:bbcd157dcd63 | 21 | i2c.write(address, &dataReg, 1); |
ackerden | 27:bbcd157dcd63 | 22 | int success = i2c.read(address, i2cData, 2); |
ackerden | 27:bbcd157dcd63 | 23 | |
ackerden | 27:bbcd157dcd63 | 24 | |
ackerden | 27:bbcd157dcd63 | 25 | if(success != 0) |
ackerden | 27:bbcd157dcd63 | 26 | return -1000000; |
ackerden | 27:bbcd157dcd63 | 27 | else |
ackerden | 27:bbcd157dcd63 | 28 | { |
ackerden | 27:bbcd157dcd63 | 29 | //I2C bytes are in different byte order, write into short bytes 0 and 1 in reverse order! |
ackerden | 27:bbcd157dcd63 | 30 | *((char*)&capacitance) = i2cData[1]; |
ackerden | 27:bbcd157dcd63 | 31 | *(((char*)(&capacitance)) + 1) = i2cData[0]; |
ackerden | 27:bbcd157dcd63 | 32 | return capacitance; |
ackerden | 27:bbcd157dcd63 | 33 | }} |
ackerden | 27:bbcd157dcd63 | 34 | |
ackerden | 27:bbcd157dcd63 | 35 | float Chirp::getTemperature(){ |
ackerden | 27:bbcd157dcd63 | 36 | i2c.frequency(10000); |
ackerden | 27:bbcd157dcd63 | 37 | short temperature = 0; |
ackerden | 27:bbcd157dcd63 | 38 | char i2cData[2]= {0}; |
ackerden | 27:bbcd157dcd63 | 39 | |
ackerden | 27:bbcd157dcd63 | 40 | |
ackerden | 27:bbcd157dcd63 | 41 | char dataReg = 4; //Temperature is stored in 2 bytes at address 5 |
ackerden | 27:bbcd157dcd63 | 42 | i2c.write(address, &dataReg, 1); |
ackerden | 27:bbcd157dcd63 | 43 | int success = i2c.read(address, i2cData, 2); |
ackerden | 27:bbcd157dcd63 | 44 | printf("%i\n\n", success); |
ackerden | 27:bbcd157dcd63 | 45 | |
ackerden | 27:bbcd157dcd63 | 46 | |
ackerden | 27:bbcd157dcd63 | 47 | if(success != 0) |
ackerden | 27:bbcd157dcd63 | 48 | return -1000000; |
ackerden | 27:bbcd157dcd63 | 49 | else |
ackerden | 27:bbcd157dcd63 | 50 | { |
ackerden | 27:bbcd157dcd63 | 51 | *((char*)&temperature) = i2cData[1]; |
ackerden | 27:bbcd157dcd63 | 52 | *(((char*)(&temperature)) + 1) = i2cData[0]; |
ackerden | 27:bbcd157dcd63 | 53 | return (float) temperature / 10.0; |
ackerden | 27:bbcd157dcd63 | 54 | } } |
ackerden | 27:bbcd157dcd63 | 55 | |
ackerden | 27:bbcd157dcd63 | 56 | |
ackerden | 27:bbcd157dcd63 | 57 | void getResults(){ |
ackerden | 27:bbcd157dcd63 | 58 | printf("test\n\n"); |
ackerden | 27:bbcd157dcd63 | 59 | printf("Capacitance = %.2f\n", chirp.getCapacitance()); |
ackerden | 27:bbcd157dcd63 | 60 | printf("Temperatur = %.2f\n", chirp.getTemperature()); |
ackerden | 27:bbcd157dcd63 | 61 | } |