Skip to content

Instantly share code, notes, and snippets.

@jcantara
Created November 14, 2012 15:31
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 jcantara/4072780 to your computer and use it in GitHub Desktop.
Save jcantara/4072780 to your computer and use it in GitHub Desktop.
thermnet
#include "DHT.h"
#include "mux.h"
#include <SPI.h>
#include <Ethernet.h>
#define DHTTYPE DHT11
#define NUM_NODES 48
struct Node {
bool active;
float temp;
float hum;
};
Node nodes[NUM_NODES];
DHT *dhts[NUM_NODES];
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0x73, 0x2E };
IPAddress ip(192,168,1, 170);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80);
void setup() {
// disable microSD
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.begin(9600);
Serial.println("Thermnet");
for(int i=0; i<NUM_NODES; i++) {
nodes[i].active = false;
nodes[i].temp = 0.0;
nodes[i].hum = 0.0;
uint8_t pin = MUX::pin_for(i);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" for ");
Serial.println(i);
dhts[i] = new DHT(pin, DHTTYPE);
dhts[i]->begin();
}
// start the Ethernet connection:
Serial.println(F("Trying to get an IP address using DHCP"));
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Failed to configure Ethernet using DHCP"));
// initialize the ethernet device not using DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
// print your local IP address:
Serial.print(F("My IP address: "));
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(F("."));
}
Serial.println();
server.begin();
}
void loop() {
for(int i=0; i<NUM_NODES; i++) {
Serial.print(F("Reading MUX pin "));
Serial.println(i);
MUX::activate_mux_pin(i);
nodes[i].temp = dhts[i]->readTemperature(true);
nodes[i].hum = dhts[i]->readHumidity();
nodes[i].active = !isnan(nodes[i].temp) && !isnan(nodes[i].hum);
if (!nodes[i].active) {
Serial.println(F("Failed to read pin"));
} else {
Serial.print(F("Humidity: "));
Serial.print(nodes[i].hum);
Serial.print(F(" %\t"));
Serial.print(F("Temperature: "));
Serial.print(nodes[i].temp);
Serial.println(F(" *F"));
}
// because sensors are so slow, check for a pending ethernet connection after every sensor read
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: application/json; charset=UTF-8"));
client.println();
//output
client.print(F("{\"nodes\":{"));
for(int i=0; i<NUM_NODES; i++) {
if(i!=0) { // prepend a comma if not on the first one
client.print(F(", "));
}
client.print(F("\""));
client.print(i);
client.print(F("\" : {"));
if(nodes[i].active) {
client.print(F("\"t\""));
client.print(F(" : "));
client.print(nodes[i].temp);
client.print(F(", \"h\" : "));
client.print(nodes[i].hum);
} else {
client.print(F("null"));
}
client.print(F("}"));
}
client.println(F("}}"));
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment