Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
Last active December 2, 2018 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ti-nspire/b36c9f0336a72667695f12f5e54289bc to your computer and use it in GitHub Desktop.
Save ti-nspire/b36c9f0336a72667695f12f5e54289bc to your computer and use it in GitHub Desktop.
A library for AM2302/DHT22 connected to the micro:bit on the Mbed platform
#include "MicroBit.h"
#include "AM2302.h"
extern MicroBit uBit;
int AM2302::bits2bytes(int *buf, int pos){
int Pos = (8 * pos) - 7;
int sute = 0;
for(int i=Pos ; i<(Pos+8); i++){
sute |= buf[i] << (Pos+7-i);
}
return sute;
}
void AM2302::begin(){
uBit.io.pin[_pin].setDigitalValue(1);
uBit.io.pin[_pin].setDigitalValue(0);
uBit.sleep(1);
uBit.io.pin[_pin].getDigitalValue();
while(uBit.io.pin[_pin].getDigitalValue() == 1){}
}
void AM2302::getBits(int *buf){
int threshold;
for(int j=0; j<41; j++){
while(uBit.io.pin[_pin].getDigitalValue()==0){}
int counter;
for(counter=1; counter<200; counter++){
if(uBit.io.pin[_pin].getDigitalValue()==0){break;}
}
if(j==0){threshold=counter/2;}
buf[j] = (counter > threshold)? 1 : 0;
}
}
void AM2302::getBytes(int *buf){
int bytes[5];
for(int i=0; i<5; i++){
bytes[i] = bits2bytes(buf, i+1);
}
humidHigh = bytes[0]; humidLow = bytes[1];
tempHigh = bytes[2]; tempLow = bytes[3];
crc = bytes[4];
}
void AM2302::getData(){
int buf[41];
begin();
getBits(buf);
getBytes(buf);
}
double AM2302::getHumid(){
return (double)((humidHigh<<8) | humidLow) / 10.0;
}
double AM2302::getTemp(){
int tempTemp = (tempHigh<<8) | tempLow;
if(tempHigh & 0x80){
tempTemp = -(tempTemp & 0x7fff);
}
return (double)tempTemp / 10.0;
}
int AM2302::getPin(){
return _pin;
}
int AM2302::isOK(){
int sum = humidHigh + humidLow + tempHigh + tempLow;
return (crc == (sum & 0xff));
}
AM2302::AM2302(int pin){_pin = pin;}
AM2302::AM2302(){}
#ifndef AM2302_H
#define AM2302_H
class AM2302{
private:
static int bits2bytes(int *buf, int pos); // extracts and creates a 8-bit value from any position in an array of 1s or 0s.
int _pin;
int humidHigh, humidLow, tempHigh, tempLow, crc;
void begin(); // wakes the AM2302 up.
void getBits(int *buf); // creates a 41-element array of 1s or 0s, including a start-bit,.
void getBytes(int *buf); // stores 5-bytes data into their own variables.
public:
void getData();
double getHumid();
double getTemp();
int getPin();
int isOK();
// declares constructors.
AM2302(int pin);
AM2302();
};
#endif
#include "MicroBit.h"
#include "AM2302.h"
MicroBit uBit;
//////////////////////////////////
// How to use the AM2302 class: //
//////////////////////////////////
int main(void){
uBit.init();
// Instantiate the AM2302 class with any uBit's digital pin specified. In the following example, the P0 was specified.
AM2302 am(0);
/* // To connect more than one sensor:
const int n = 3;
AM2302 am[n];
for(int i=0; i<n; i++){
am[i] = AM2302(i);
}
*/
// The .getData() method will store raw values of humidity, temperature, and CRC into their variables.
// The .getPin() method will return its pin number.
// The .getHumid() method will return its humidity.
// The .getTemp() method will return its temperature.
// Call .isOK() method, and you will determine that the measured values have been correct (ex. 1: OK, 0: NG).
while(1){
am.getData();
printf("Pin %d, Humid %0.1f%%, Temp %0.1fC, isOK? %d\n",
am.getPin(), am.getHumid(), am.getTemp(), am.isOK());
/* // If you have connected multiple sensors:
for(int i=0; i<n; i++){
am[i].getData();
printf("Pin %d, Humid %0.1f%%, Temp %0.1fC, isOK? %d\n",
am[i].getPin(), am[i].getHumid(), am[i].getTemp(), am[i].isOK());
}
*/
uBit.sleep(2500); // Wait for more than 2 seconds.
}
release_fiber();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment