Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
sensor_dht11_dht22_block/main.c

===== Description =====

This demo shows how to communicate with the DHT11 and DHT22 1-Wire interface chip, using the nz_sensDht.h and nz_sensDht.c driver. It uses non-blocking (asynchronous) functions, meaning all processing is done in background interrupts and fibers.

It also flashes the system LED.

To add DHT22 support to a project, the following must be done:

This demo can be used with any SBC66 Netcruzer board with an USB Port (not USB Host). It uses USB Commands and Debug Messages sent and received via the Netcruzer USB Terminal app to communicate with board.

Additionally this demo also implements debugging via the USB port and the Netcruzer USB Terminal App. During the program various debug messages are written, which will be displayed on the "Netcruzer USB Terminal" (running on a PC connected to the target board's USB port). This demo also monitors the USB port for Debug messages send to us.
- If "hi" is received, it replies with "Hello"
Use the "Netcruzer USB Terminal" App to send and receive USB Debug messages. It can be downloaded here: https://netcruzer.com/usbterminal/

===== Required Hardware =====

The project requires a SBC66 Netcruzer board with an USB Port (not USB Host), like the SBC66ZL for example. Additionally a DHT11 or DHT22 Sensor has to be connected to any of the SBC66 port pins.

===== Building Project =====

This project is located in the "src/demos/sensors/sensor_dht11_dht22" folder of the Netcruzer Download. To compile for Netcruzer Board, open this project in MPLAB X, and select the "Project Configuration" for desired board. For example "SBC66ZL_R1" for the SBC66ZL Revision 1 board. For details click here

===== Programming Board =====

After compiling (build), the board can be programmed via the USB Bootloader or a PIC Programmer. USB Programming is simplified when using the SBC board together with a Prototype Board.

===== File History =====

2014-02-10, David H. (DH):

#define THIS_IS_MAIN_FILE //Uniquely identifies this as the file with the main application entry function main()
// Includes /////////////////////////////////////
#include "HardwareProfile.h" //Required for all Netcruzer projects
#include "nz_helpersCx.h"
#include "nz_interrupt.h"
//Add debugging, and define debugging level. Is a DEBUG_LEVEL_xx define. Set to DEBUG_LEVEL_OFF to disable.
#define MY_DEBUG_LEVEL DEBUG_LEVEL_INFO
#include "nz_debug.h"
// Defines //////////////////////////////////////
// Variables ////////////////////////////////////
TICK16 tmrFlashLed; //Timer for flashing system LED
TICK16 tmrReadSensor; //Timer for reading DHT sensor
int main(void) {
tmrFlashLed = tick16Get(); //Timer for flashing system LED
tmrReadSensor = tick16Get();
//Default initialization. All ports inputs. All analog features disabled. Tick 1ms
debugPutString("\nThis is a test debug message from sensor_dht11_dht22_block");
//Set System LED port as outputs. Setting a DIR_xxx bit to 1 set's corresponding port to input, and 0 as output
DIR_SYSLED = OUTPUT_PIN;
//DHT22 Initialization.
//sensDht_init(IOPORT_ID_NA, 0); //No power switch
sensDht_init(3000, 1, 0); //Power switch = 1 (old port name X1), not inverted
sensDht_addSensor(7); //Initialize port 7 (old port name X7) as a DHT sensor input
sensDht_addSensor(8); //Initialize port 7 (old port name X7) as a DHT sensor input
delay_ms(120); //Wait for DHT22 to settle
//Main loop
while(1)
{
nzSysTaskDefault(); //Main netcruzer task, call in main loop.
sensDht_task(); //Main DHT Sensor task
//Read DHT22 sensors
if (tick16TestTmr(tmrReadSensor)) {
//Check if DHT sensor is ready
if (sensDht_isReady()) {
tick16SetTmrMS(tmrReadSensor, 2000); //Read again in 2 seconds
//Read sensor on port 7
if (sensDht_read(SENS_DHT_TYPE_DHT22, 7) == 0) {
sprintf(debugTempBuf, "\nTemperature(7)=%d.%d",
sensDht_getTemperatureInteger(),
sensDht_getTemperatureDecimal());
debugPutString(debugTempBuf);
sprintf(debugTempBuf, ", Humidity=%d.%d",
sensDht_getHumidityInteger(),
sensDht_getHumidityDecimal());
debugPutString(debugTempBuf);
}
//Error reading sensor!
else {
debugPutString("\nError reading DHT22 on port 7!");
}
//Read sensor on port 8
if (sensDht_read(SENS_DHT_TYPE_DHT22, 8) == 0) {
sprintf(debugTempBuf, "\nTemperature(8)=%d.%d",
sensDht_getTemperatureInteger(),
sensDht_getTemperatureDecimal());
debugPutString(debugTempBuf);
sprintf(debugTempBuf, ", Humidity=%d.%d",
sensDht_getHumidityInteger(),
sensDht_getHumidityDecimal());
debugPutString(debugTempBuf);
}
//Error reading sensor!
else {
debugPutString("\nError reading DHT22 on port 8!");
}
}
}
if (tick16TestTmr(tmrFlashLed)) {
tick16UpdateTmrMS(tmrFlashLed, 500); //Update timer to expire in 500ms again
LAT_SYSLED = !LAT_SYSLED; //Toggle System LED
//Enable this code if SENS_DHT_AUTO_READ_PERIOD is 0. No auto reading!
//if (sensDht_isIdle() == TRUE) {
// sensDht_startRead();
//}
}
}//end while
}
#if defined(HAS_NZ_DEBUGGING)
void debugService(void) {
//Check if a debug message was received
if (cbufHasWholePacket(CIRBUF_RX_DEBUG)) {
//'hi' - Reply with "Hello" on a new line
if (cbufPacketStrcmp(CIRBUF_RX_DEBUG, "hi") == 0) {
debugPutString("\nHello");
}
//Add custom debug message processing
else if (cbufPacketStrcmp(CIRBUF_RX_DEBUG, "...") == 0) {
debugPutString("\nReceived unknown debug message");
//..... Do something
}
//Remove received packet. If it was not processed above, it is lost!
cbufRemovePacket(CIRBUF_RX_DEBUG);
}
}
#endif