DHT22 Sketch

This sketch has a DHT22 temperature/humidity sensor attached.  It queries the DHT22 whenever HCA tells it to, and reports the results to HCA.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <stdlib.h>
#include <DHT22.h>
// Only used for sprintf
#include <stdio.h>

// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22a_PIN 6
#define DHT22b_PIN 7

// Setup a DHT22 instance
DHT22 myDHT22a(DHT22a_PIN);
DHT22 myDHT22b(DHT22b_PIN);

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x71 };
IPAddress ip(192,168,0,71);
unsigned int Port_Remote = 5000;      // remote port
IPAddress IP_Remote(192, 168, 0, 10);           // IP address for smarthome
unsigned int localPort = 5001;      // local port to listen on
char cmdGetData = ‘1’;     // a “1” means read all the data
char cmdIn = ‘0’;        // used to obtain and compare buffer character
char cmdStatusCheck = ‘9’;   // a “9” means smarthome is asking for status
// can not use dig pins 2, 4, 10, 11, 12, 13
// An EthernetUDP instance to let us receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
char fahr[10];
char RH[10];
char cent[10];
String dht22TaName = “ard71atemp=”;
String dht22HaName = “ard71ahum=”;
String dht22TbName = “ard71btemp=”;
String dht22HbName = “ard71bhum=”;
String tempPacket = “”;

void setup()
{
Serial.begin(9600);
delay(5000);
Serial.println(“Starting 71-v1”);
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);           // no need for gateway or mask
Udp.begin(localPort);
}

void loop() {
DHT22_ERROR_t errorCode;
// initialize variables
delay(10000);
float tCent;
float tFahr;
float tRH;

// listen for incoming packets
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print(“Received packet of size “);
Serial.println(packetSize);
Serial.print(“From “);
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(“.”);
}
}
//
Serial.print(“, port “);
Serial.println(Udp.remotePort());

// read the packet into packetBufffer
Udp.read(packetBuffer,1);
Serial.print(“Contents:”);
Serial.println(packetBuffer);
cmdIn = (packetBuffer[0]);
if (cmdIn == cmdGetData)
{

// gather data for a
Serial.print(“Requesting data…”);
errorCode = myDHT22a.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
Serial.print(“Got Data “);
tCent = (myDHT22a.getTemperatureC());
tFahr = (tCent * 9 / 5) + 32;
dtostrf(tFahr,4,1,fahr);
tempPacket = dht22TaName + fahr;
//      Serial.println(tempPacket);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(tempPacket);
Udp.endPacket();
tRH = (myDHT22a.getHumidity());
dtostrf(tRH,4,1,RH);
tempPacket = dht22HaName + RH;
//      Serial.println(tempPacket);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(tempPacket);
Udp.endPacket();
break;
case DHT_ERROR_CHECKSUM:
Serial.print(“check sum error “);
Serial.print(myDHT22a.getTemperatureC());
Serial.print(“C “);
Serial.print(myDHT22a.getHumidity());
Serial.println(“%”);
break;
case DHT_BUS_HUNG:
Serial.println(“BUS Hung “);
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println(“Not Present “);
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println(“ACK time out “);
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println(“Sync Timeout “);
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println(“Data Timeout “);
break;
case DHT_ERROR_TOOQUICK:
Serial.println(“Polled too quick “);
break;
}
// end of switch section for a

delay(1000);

/* //gather data for b
//    Serial.print(“Requesting data…”);
errorCode = myDHT22b.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
//      Serial.print(“Got Data “);
tCent = (myDHT22b.getTemperatureC());
tFahr = (tCent * 9 / 5) + 32;
dtostrf(tFahr,4,1,fahr);
tempPacket = dht22TbName + fahr;
//      Serial.println(tempPacket);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(tempPacket);
Udp.endPacket();
tRH = (myDHT22b.getHumidity());
dtostrf(tRH,4,1,RH);
tempPacket = dht22HbName + RH;
//      Serial.println(tempPacket);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(tempPacket);
Udp.endPacket();
break;
case DHT_ERROR_CHECKSUM:
Serial.print(“check sum error “);
Serial.print(myDHT22b.getTemperatureC());
Serial.print(“C “);
Serial.print(myDHT22b.getHumidity());
Serial.println(“%”);
break;
case DHT_BUS_HUNG:
Serial.println(“BUS Hung “);
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println(“Not Present “);
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println(“ACK time out “);
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println(“Sync Timeout “);
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println(“Data Timeout “);
break;
case DHT_ERROR_TOOQUICK:
Serial.println(“Polled too quick “);
break;
}
*/ //end of switch section for b
}
// end of cmdGetData = 1 section

// respond to status check
if (cmdIn == cmdStatusCheck)
{
Serial.println(“matched9!”);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ard71ok=yes”);        // send packet good status
Udp.endPacket();
}  // end of status check
}  // end of packet size section
}

As In Various and Sundry