first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
89
Software/Cpp/grove_dht_pro/grove_dht_example.cpp
Normal file
89
Software/Cpp/grove_dht_pro/grove_dht_example.cpp
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
//
|
||||
// GrovePi Example for using the Grove Temperature & Humidity Sensor Pro
|
||||
// (http://www.seeedstudio.com/wiki/Grove_-_Temperature_and_Humidity_Sensor_Pro)
|
||||
//
|
||||
// The GrovePi connects the Raspberry Pi and Grove sensors.
|
||||
// You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
|
||||
//
|
||||
// Have a question about this example? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi
|
||||
//
|
||||
/*
|
||||
## License
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
|
||||
Copyright (C) 2017 Dexter Industries
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "grovepi.h"
|
||||
#include "grove_dht_pro.h"
|
||||
|
||||
using GrovePi::DHT;
|
||||
using GrovePi::delay;
|
||||
using GrovePi::I2CError;
|
||||
|
||||
// sudo g++ -Wall grovepi.cpp grove_dht_pro.cpp grove_dht_example.cpp -o grove_dht_example.out -> without grovepicpp package installed
|
||||
// sudo g++ -Wall -lgrovepicpp grove_dht_pro.cpp grove_dht_example.cpp -o grove_dht_example.out -> with grovepicpp package installed
|
||||
|
||||
int main()
|
||||
{
|
||||
int sensorPin = 4; // digital port (D4) to which we have DTH serial sensor connected
|
||||
float temp = 0, humidity = 0; // variables to hold data from the DHT sensor
|
||||
|
||||
// initialize the BLUE module (got from the GrovePi kit)
|
||||
// and use digital port 4 for the DHT sensor
|
||||
DHT dht = DHT(DHT::BLUE_MODULE, sensorPin);
|
||||
|
||||
try
|
||||
{
|
||||
dht.init(); // same as initGrovePi
|
||||
|
||||
// do this indefinitely
|
||||
while(true)
|
||||
{
|
||||
// read the DHT sensor values
|
||||
dht.getSafeData(temp, humidity);
|
||||
|
||||
// and print them on the screen
|
||||
printf("[temp = %.02f C][humidity = %.02f%%]\n", temp, humidity);
|
||||
|
||||
// and wait 100 before the other reading
|
||||
// so we don't overflow the terminal
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
catch(I2CError &error)
|
||||
{
|
||||
// I2C error while reading / writing
|
||||
printf(error.detail());
|
||||
return -1;
|
||||
}
|
||||
catch(std::runtime_error &e)
|
||||
{
|
||||
// catch error on number values
|
||||
// NaN & bad value readings
|
||||
printf(e.what());
|
||||
return -2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
105
Software/Cpp/grove_dht_pro/grove_dht_pro.cpp
Normal file
105
Software/Cpp/grove_dht_pro/grove_dht_pro.cpp
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#include "grove_dht_pro.h"
|
||||
|
||||
using std::runtime_error;
|
||||
using GrovePi::DHT;
|
||||
|
||||
/**
|
||||
* function for connecting to the GrovePi
|
||||
* doesn't matter whether you call it multiple times
|
||||
* just check with the isConnected() function to see if you got a connection
|
||||
*/
|
||||
void DHT::init()
|
||||
{
|
||||
initGrovePi();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns via its parameters the temperature and humidity
|
||||
* this function is NaN-proof
|
||||
* it always gives "accepted" values
|
||||
*
|
||||
* if bad values are read, then it will retry reading them
|
||||
* and check if they are okay for a number of [MAX_RETRIES] times
|
||||
* before throwing a [runtime_error] exception
|
||||
*
|
||||
* @param temp in Celsius degrees
|
||||
* @param humidity in percentage values
|
||||
*/
|
||||
void DHT::getSafeData(float &temp, float &humidity)
|
||||
{
|
||||
int current_retry = 0;
|
||||
this->getUnsafeData(temp, humidity); // read data from GrovePi once
|
||||
|
||||
// while values got are not okay / accepteed
|
||||
while((std::isnan(temp) || std::isnan(humidity) || !this->areGoodReadings(temp, humidity))
|
||||
&& current_retry < this->MAX_RETRIES)
|
||||
{
|
||||
// reread them again
|
||||
current_retry += 1;
|
||||
this->getUnsafeData(temp, humidity);
|
||||
}
|
||||
|
||||
// if even after [MAX_RETRIES] attempts at getting good values
|
||||
// nothing good came, then throw one of the bottom exceptions
|
||||
|
||||
if(std::isnan(temp) || std::isnan(humidity))
|
||||
throw runtime_error("[GroveDHT NaN readings - check analog port]\n");
|
||||
|
||||
if(!DHT::areGoodReadings(temp, humidity))
|
||||
throw runtime_error("[GroveDHT bad readings - check analog port]\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* function for returning via its arguments the temperature & humidity
|
||||
* it's not recommended to use this function since it might throw
|
||||
* some NaN or out-of-interval values
|
||||
*
|
||||
* use it if you come with your own implementation
|
||||
* or if you need it for some debugging
|
||||
*
|
||||
* @param temp in Celsius degrees
|
||||
* @param humidity in percentage values
|
||||
*/
|
||||
void DHT::getUnsafeData(float &temp, float &humidity)
|
||||
{
|
||||
writeBlock(this->DHT_TEMP_CMD, this->pin, this->module_type);
|
||||
readByte();
|
||||
|
||||
uint8_t data_block[33];
|
||||
readBlock(data_block);
|
||||
|
||||
temp = DHT::fourBytesToFloat(data_block + 1);
|
||||
humidity = DHT::fourBytesToFloat(data_block + 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* function for converting 4 unsigned bytes of data
|
||||
* into a single float
|
||||
* @param byte_data array to hold the 4 data sets
|
||||
* @return the float converted data
|
||||
*/
|
||||
const float DHT::fourBytesToFloat(uint8_t *byte_data)
|
||||
{
|
||||
float output;
|
||||
|
||||
// reinterpret_cast guarantees that if you cast a pointer
|
||||
// to a different type, and then reinterpret_cast it back
|
||||
// to the original type, you get the original value.
|
||||
//
|
||||
// as opposed to the static_cast where it only guarantees
|
||||
// the address is preserved
|
||||
//
|
||||
// so we take the 1st address &byte_data[0],
|
||||
// the 5th address &byte_data[4] (the end address)
|
||||
// and translate the output into a unsigned byte type
|
||||
std::copy(reinterpret_cast<const uint8_t*>(&byte_data[0]),
|
||||
reinterpret_cast<const uint8_t*>(&byte_data[4]),
|
||||
reinterpret_cast<uint8_t*>(&output));
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
const bool DHT::areGoodReadings(int temp, int humidity)
|
||||
{
|
||||
return (temp > -100.0 && temp < 150.0 && humidity >= 0.0 && humidity <= 100.0);
|
||||
}
|
||||
52
Software/Cpp/grove_dht_pro/grove_dht_pro.h
Normal file
52
Software/Cpp/grove_dht_pro/grove_dht_pro.h
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#ifndef GROVE_RGB_LCD_H
|
||||
#define GROVE_RGB_LCD_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdint.h>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
|
||||
#include "grovepi.h"
|
||||
|
||||
namespace GrovePi
|
||||
{
|
||||
class DHT
|
||||
{
|
||||
public:
|
||||
|
||||
const static uint8_t BLUE_MODULE = 0;
|
||||
const static uint8_t WHITE_MODULE = 1;
|
||||
|
||||
DHT(const uint8_t _module_type = BLUE_MODULE, const uint8_t _pin = 4)
|
||||
: module_type(_module_type), pin(_pin) {
|
||||
}
|
||||
|
||||
void init();
|
||||
void getSafeData(float &temp, float &humidity);
|
||||
void getUnsafeData(float &temp, float &humidity);
|
||||
|
||||
private:
|
||||
|
||||
uint8_t DEVICE_FILE; // I2C device file
|
||||
const uint8_t module_type;
|
||||
const uint8_t pin;
|
||||
const static uint8_t DHT_TEMP_CMD = 40; // command for reaching DTH sensor on the GrovePi
|
||||
const static int MAX_RETRIES = 3;
|
||||
|
||||
// converts the first 4 bytes of the array
|
||||
// into a float
|
||||
static const float fourBytesToFloat(uint8_t *data);
|
||||
static const bool areGoodReadings(int temp, int humidity);
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue