Another Arduino Sketch for the DHT-11 

If you would prefer a web-based sketch, I've also been playing around with this one. This may require a 2560. I haven't tried it on an Uno...

This one also has a telnet interface. There is no real need for it, I suppose. I just built it because it sounded interesting.

#include <Ethernet.h>
#include <SPI.h>
#include "DHTSensor.h"

// Network config
byte mac[] = { 0xAA, 0x00, 0x00, 0xE1, 0x00, 0x72};
byte ip[] = {192, 168, 100, 72};
byte gateway[] = {192, 168, 100, 254};
byte subnet[] = {255, 255, 255, 0};

// Device config
#define DEVID "F2"
#define SENSORNAME "Upstairs"

// Other globals
#define VERSION "1.3"
#define TELNETPORT 2323
#define WEBPORT 8080
#define DHTTYPE DHT11
#define DHTPIN 2
#define textBuffSize 20 //length of longest command string plus two spaces for CR + LF
String rcvdCmd = "";
int charsReceived = 0;
boolean connectFlag = 0;
unsigned long timeOfLastActivity; //time in milliseconds of last activity
unsigned long allowedConnectTime = 300000; //five minutes
EthernetServer telnetServer(TELNETPORT);
EthernetClient telnetClient = 0;
EthernetServer webServer(WEBPORT);
EthernetClient webClient = 0;
DHT dht(DHTPIN, DHTTYPE);
float humid;
float tempf;

// State Variables
char* commandText = "";
char* devId = DEVID;
char* sensorName = SENSORNAME;

void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
telnetServer.begin();
webServer.begin();
Serial.println("Remote Environment Monitor");
Serial.print("Version ");
Serial.println(VERSION);
Serial.println();
Serial.print("Telnet service listening on port ");
Serial.println(TELNETPORT);
Serial.print("Web service listening on port ");
Serial.println(WEBPORT);
Serial.println();
dht.begin();
}

void loop()
{
if (telnetServer.available() && !connectFlag) {
connectFlag = 1;
telnetClient = telnetServer.available();
telnetClient.println("\nRemote Environment Monitor");
telnetClient.print("Device: ");
telnetClient.print(devId);
telnetClient.print(" Sensor: ");
telnetClient.println(sensorName);
telnetClient.println();
showPrompt();
}

if (telnetClient.connected() && telnetClient.available()) getReceivedText();

if(connectFlag) checkConnectionTimeout();

webClient = webServer.available();

if (webClient) {
sendWebPage();
}
}

void showPrompt()
{
timeOfLastActivity = millis();
telnetClient.flush();
charsReceived = 0;
telnetClient.println();
telnetClient.print(devId);
telnetClient.print("> ");
}

void checkConnectionTimeout()
{
if(millis() - timeOfLastActivity > allowedConnectTime) {
telnetClient.println();
telnetClient.println("\n*** SESSION TIMED OUT ***\n");
telnetClient.stop();
connectFlag = 0;
}
}

void getReceivedText()
{
char c;
int charsWaiting;

charsWaiting = telnetClient.available();
do {
c = telnetClient.read();
if(c != 0x0d) {
rcvdCmd += c;
charsReceived++;
charsWaiting--;
}
}
while(charsReceived <= textBuffSize && c != 0x0d && charsWaiting > 0);

if(c == 0x0d) {
parseReceivedText();
showPrompt();
}

if(charsReceived >= textBuffSize) {
telnetClient.println();
showErrorMessage();
showPrompt();
}
}

void parseReceivedText()
{
if (rcvdCmd == "help") {
showHelpMessage();
} else if (rcvdCmd == "show version") {
telnetClient.print("\nVersion ");
telnetClient.println(VERSION);
telnetClient.println();
} else if (rcvdCmd == "quit") {
closeConnection();
} else if (rcvdCmd == "show reading") {
showReading();
} else if (rcvdCmd == "reset") {
telnetClient.println("\n--- Reset in 5 Seconds");
closeConnection();
delay(5000);
softwareReset();
} else {
showErrorMessage();
}
rcvdCmd = "";
}

void showErrorMessage()
{
telnetClient.println(">>> Unrecognized Command");
}

void closeConnection()
{
telnetClient.println("\n*** DISCONNECT ***\n");
telnetClient.stop();
connectFlag = 0;
}

void showHelpMessage()
{
telnetClient.println("\nSupported commands:\n");
telnetClient.println(" reset Restart the firmware");
telnetClient.println(" show reading Show current temperature/humidity");
telnetClient.println(" show version Show version number");
telnetClient.println(" help Print this help message");
telnetClient.println(" quit Exit telnet session");
telnetClient.println();
}

void showReading() {
humid = dht.readHumidity();
tempf = dht.readTemperature(1);
telnetClient.println("\nCurrent Reading");
telnetClient.println("================================");
telnetClient.print("Device ID : ");
telnetClient.println(devId);
telnetClient.print("Sensor Name : ");
telnetClient.println(sensorName);
telnetClient.print("Temperature : ");
telnetClient.println(tempf);
telnetClient.print("Humidity : ");
telnetClient.println(humid);
telnetClient.println("================================\n");
}

void sendWebPage() {
humid = dht.readHumidity();
tempf = dht.readTemperature(1);
boolean currentLineIsBlank = true;
while (webClient.connected()) {
if (webClient.available()) {
char c = webClient.read();
if (c == '\n' && currentLineIsBlank) {
webClient.println("HTTP/1.1 200 OK");
webClient.println("Content-Type: text/html");
webClient.println();
webClient.print("Device:");
webClient.print(devId);
webClient.print(",SensorName:");
webClient.print(sensorName);
webClient.print(",Temperature:");
webClient.print(tempf);
webClient.print(",Humidity:");
webClient.println(humid);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
webClient.stop();
}

void softwareReset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile (" jmp 0");
}


[ add comment ] ( 2830 views ) permalink

<Back | 1 | 2 | 3 | Next> Last>>