Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for reading temperature/humidity from DHT11 and DHT22, bump v1.4 #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ software/adc
software/blink
software/blink_ws2812
software/button
software/dht11
software/debugConsole
software/fade_ws2812
software/hardwarePWM
Expand All @@ -19,3 +20,6 @@ software/rgb_cycle_ws2812
software/servo
software/softPWM
software/spi_LTC1448

firmware/main.hex
firmware/main.elf
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

For more details: <http://littlewire.cc>

Current version is: **v1.3**
Current version is: **v1.4**

Previous version releases can be found at: <https://github.com/littlewire/Little-Wire/releases>

Expand Down
124 changes: 122 additions & 2 deletions firmware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#define delayMicroseconds(value) _delay_us(value);
#define sbi(register,bit) (register|=(1<<bit))
#define cbi(register,bit) (register&=~(1<<bit))
const uint8_t LITTLE_WIRE_VERSION = 0x13;
const uint8_t LITTLE_WIRE_VERSION = 0x14;
enum
{
// Generic requests
Expand Down Expand Up @@ -827,6 +827,13 @@ uchar usbFunctionSetup(uchar data[8])

return 0;
}
if( req == 56) /* dht read */
{
rxBuffer[0]=data[2];
jobState=18;
return 0;
}

#if 0
if ((req & 0xF0) == 0xD0) /* pic24f send bytes */
{
Expand Down Expand Up @@ -1063,6 +1070,113 @@ unsigned char I2C_Read( unsigned char ack )

return res;
}

#define DHT11 0
#define DHT22 1

#define DHT_TIMEOUT 255
#define DHT_DELAY 10
#define DHT_LEN 4

#define DHT_SUCCESS 0
#define DHT_TIMEOUT_ERR 128
#define DHT_CHECKSUM_ERR 255

uint8_t DHT_Read(unsigned char type) {
uint8_t timeout = 0;
uint8_t len = 0;
uint8_t bit_index = 7;
uint8_t byte_index = 0;

uint16_t f;

pinMode(B,DATA_PIN,OUTPUT);

sendBuffer[0] = 0; sendBuffer[1] = 0; sendBuffer[2] = 0; sendBuffer[3] = 0; sendBuffer[4] = 0;

ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
digitalWrite(B,DATA_PIN, LOW);
_delay_ms(18);
digitalWrite(B,DATA_PIN, HIGH);
_delay_us(40);

pinMode(B, DATA_PIN, INPUT);
internalPullup(B, DATA_PIN, DISABLE);

timeout = DHT_TIMEOUT;
while(!digitalRead(B, DATA_PIN)) { // Wait for 1
if (timeout-- == 0) {
return DHT_TIMEOUT_ERR;
}
}

timeout = DHT_TIMEOUT;
while(digitalRead(B, DATA_PIN)) { // Wait for 0
_delay_us(1);
if (timeout-- == 0) {
return DHT_TIMEOUT_ERR;
}
}


for (uint8_t i = 0; i < 40; i++) {
timeout = DHT_TIMEOUT;
while(!digitalRead(B, DATA_PIN)) { // Wait for 1
if (timeout-- == 0) {
return DHT_TIMEOUT_ERR;
}
}

len = 0;
timeout = DHT_TIMEOUT;
while(digitalRead(B, DATA_PIN)) { // Wait for 0
_delay_us(DHT_DELAY);
if (len++ == DHT_TIMEOUT) {
return DHT_TIMEOUT_ERR;
}
}

if (len >= DHT_LEN) {
sendBuffer[byte_index] |= (1 << bit_index);
}

if (bit_index == 0) {
bit_index = 7;
byte_index++;
} else {
bit_index--;
}

}
}

uint16_t checksum = sendBuffer[0] + sendBuffer[1] + sendBuffer[2] + sendBuffer[3];
if (sendBuffer[4] != (checksum & 0x00FF)) {
return DHT_CHECKSUM_ERR;
}

if (type == DHT11) {
// Expand DHT11 values to DHT22 format
f = sendBuffer[0];
f *= 10;
sendBuffer[0] = 0x00FF & f;
sendBuffer[1] = 0x00FF & (f >> 8);

f = sendBuffer[2];
f *= 10;
sendBuffer[2] = 0x00FF & f;
sendBuffer[3] = 0x00FF & (f >> 8);
} else if (type == DHT22) {
f = sendBuffer[1];
sendBuffer[1] = sendBuffer[0];
sendBuffer[0] = f;

f = sendBuffer[3];
sendBuffer[3] = sendBuffer[2];
sendBuffer[2] = f;
}
return 0;
}
// ----------------------------------------------------------------------------


Expand Down Expand Up @@ -1382,7 +1496,13 @@ int main(void) {
}
jobState=0;
break;

case 18: /* dht11/dht22 read */
sendBuffer[4] = DHT_Read(rxBuffer[0]); // error msg
sendBuffer[8] = 5;

jobState=0;
break;

default:
jobState=0;
break;
Expand Down
1 change: 1 addition & 0 deletions software/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CFLAGS = $(USBFLAGS) $(LIBS) -I$(INCLUDE) -O -g $(OSFLAG)
LWLIBS = littleWire littleWire_util littleWire_servo opendevice
EXAMPLES = adc blink blink_ws2812 rgb_cycle_ws2812 fade_ws2812 button servo i2c_blinkM
EXAMPLES += spi_LTC1448 onewire softPWM hardwarePWM debugConsole lwbuttond i2c_nunchuck
EXAMPLES += dht11

.PHONY: clean library docs

Expand Down
56 changes: 56 additions & 0 deletions software/examples/dht11.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Updated for the new firmware: July 2012
by ihsan Kehribar <[email protected]>

Created: December 2011
by ihsan Kehribar <[email protected]>
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "littleWire.h"
#include "littleWire_util.h"

unsigned char version;

dht_reading val;

int main(int argc, char **argv)
{
littleWire *lw = NULL;

lw = littleWire_connect();

if(lw == NULL){
printf("> Little Wire could not be found!\n");
exit(EXIT_FAILURE);
}

version = readFirmwareVersion(lw);
printf("> Little Wire firmware version: %d.%d\n",((version & 0xF0)>>4),(version&0x0F));
if(version<0x14)
{
printf("> This example requires the new 1.4 version firmware. Please update soon.\n");
return 0;
}

while(1)
{
val = dht_read(lw, DHT11);
if (!val.error) {
printf("humidity: %f, temp %f\n", (float)val.humid/10.0, (float)val.temp/10.0);
} else {
printf("Error Reading sensor!");
}

if(lwStatus<0)
{
printf("> lwStatus: %d\n",lwStatus);
printf("> Connection error!\n");
return 0;
}

delay(5000);
}
}
12 changes: 12 additions & 0 deletions software/library/littleWire.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,19 @@ void ws2812_preload(littleWire* lwHandle, unsigned char r,unsigned char g,unsign
{
lwStatus=usb_control_msg(lwHandle, 0xC0, 54, (g<<8) | 0x20, (b<<8) | r, rxBuffer, 8, USB_TIMEOUT);
}

dht_reading dht_read(littleWire* lwHandle, unsigned char type)
{
lwStatus=usb_control_msg(lwHandle, 0xC0, 56, type, 0, rxBuffer, 8, USB_TIMEOUT);

delay(100);

lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);

return *(dht_reading*) (void*) rxBuffer;
}


int customMessage(littleWire* lwHandle,unsigned char* receiveBuffer,unsigned char command,unsigned char d1,unsigned char d2, unsigned char d3, unsigned char d4)
{
int i;
Expand Down
11 changes: 11 additions & 0 deletions software/library/littleWire.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "opendevice.h" // common code moved to separate module
#include "littleWire_util.h"
#include <stdio.h>
#include <stdint.h>

#define VENDOR_ID 0x1781
#define PRODUCT_ID 0x0c9f
Expand Down Expand Up @@ -93,6 +94,15 @@
#define MOSI_PIN PIN4
#define RESET_PIN PIN3

#define DHT11 0
#define DHT22 1

typedef struct dht_reading {
uint16_t humid;
uint16_t temp;
uint8_t error;
} dht_reading;

extern unsigned char rxBuffer[RX_BUFFER_SIZE]; /* This has to be unsigned for the data's sake */
extern unsigned char ROM_NO[8];
extern int lwStatus;
Expand Down Expand Up @@ -557,6 +567,7 @@ void ws2812_preload(littleWire* lwHandle, unsigned char r,unsigned char g,unsign

/*! @} */

dht_reading dht_read(littleWire* lwHandle, unsigned char type);

/**
* @mainpage Introduction
Expand Down