TechSomething

PROJECT ARMADILLO NODE V0.0.1

First release of the code of the Armadillo-Node

2013/03/04 version 0.0.1

features

CODE:

/*
* WebServerParsing
* Respond to requests in the URL to change digital and analog output ports
* show the number of ports changed and the value of the analog input pins.
* for example:
* sending http://192.168.1.177/?pinD2=1 turns digital pin 2 on
* sending http://192.168.1.177/?pinD2=0 turns pin 2 off.
* This sketch demonstrates text parsing using the 1.0 Stream class.
*/

#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>

//*******************************************************************************************************
//DHT22
//*******************************************************************************************************
// Uncomment whatever type you’re using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTPIN 3 // what pin we’re connected to
DHT dht(DHTPIN, DHTTYPE);

//*******************************************************************************************************
//Ethernet Setup
//*******************************************************************************************************
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 172, 24, 33, 7 };
byte gateway[] = {172, 24, 33, 1 };
byte subnet[] = {255, 255, 255, 0 };

EthernetServer server(80);

int a = 0;
int b = 0;
int c = 0;
int d = 0;
long e = 0;
int f = 0;

int warn = 9; //pin for the warning led of no connection with the server
long timeout = 50000; //timeout in "program cycles", as of 2013/03/15 a cycle lasts about 0,006 secs, and every second about 166 cycles are executed

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.println("server ready");
dht.begin();
Serial.println("DHT ready");
pinMode(warn, OUTPUT);
}

void loop()
{
Serial.println(e);

if (e>0 & e<timeout & f!=1) {
digitalWrite(warn, HIGH);
}
if (e>0 & e<timeout & f==1) {
digitalWrite(warn, LOW);
}

if (e>timeout) {
f=99;
e=0;
}

e = e+1;

EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
// counters to show the number of pin change requests
int digitalRequests = 0;
int analogRequests = 0;
if( client.find("GET /") ) { // search for ‘GET’
// find tokens starting with "pin" and stop on the first blank line
// search to the end of line for ‘pin’
while(client.findUntil("pin", "nr")){
char type = client.read(); // D or A
// the next ascii integer value in the stream is the pin
int pin = client.parseInt();
int val = client.parseInt(); // the integer after that is the value
if( type == ‘D’ & pin > 6 & pin < 9) {
Serial.print("Digital pin ");
pinMode(pin, OUTPUT);
digitalWrite(pin, val);
digitalRequests++;
}
else if( type == ‘A’){
Serial.print("Analog pin ");
analogWrite(pin, val);
analogRequests++;
}
else if( type == ‘V’ & pin > 19 & pin < 26){
Serial.print("Variable ");
switch (pin) {
case 20 : a = val; break;
case 21 : b = val; break;
case 22 : c = val; break;
case 23 : d = val; break;
case 24 : e = val; break;
case 25 : f = val; break;
}
}
else {
Serial.print("Unexpected type ");
Serial.print(type);
}
Serial.print(pin);
Serial.print("=");
Serial.println(val);
}
}
Serial.println();

// the findUntil has detected the blank line (a lf followed by cr)
// so the http request has ended and we can send a reply
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

// output the number of pins handled by the request
// client.print(digitalRequests);
// client.print(" digital pin(s) written");
// client.println("<br />");
// client.print(analogRequests);
// client.print(" analog pin(s) written");
// client.println("<br />");
// client.println("<br />");

float t = dht.readTemperature();
if (isnan(t) && (t) == 0.00) {
client.println("ERROR reading the sensor ");
}
else {
client.print("T ");
client.print(" = ");
client.print(t);
client.print(" *C");
client.println(" "); }

client.println("<br />");

float h = dht.readHumidity();
if (isnan(h)) {
client.println("ERROR reading the sensor ");
// client.print("DEBUG content of h ");
// client.print(h);
// client.println(" ");
}
else {
client.print("H ");
client.print(" = ");
client.print(h);
client.print(" %t");
client.println(" "); }

client.println("<br />");
// client.println("<br />");

// output the value of each analog input pin
for (int i = 0; i < 6; i++) {
client.print("A");
client.print(i);
client.print(" = ");
client.print(analogRead(i));
client.println("<br />");
}

client.println("<br />");

// output the value of digital pin 4 to 9
for (int i = 4; i < 10; i++) {
client.print("D");
client.print(i);
client.print(" = ");
client.print(digitalRead(i));
client.println("<br />");
}

client.println("<br />");

client.print("varA ");
client.print(" = ");
client.print(a);
client.println("<br />");
client.print("varB ");
client.print(" = ");
client.print(b);
client.println("<br />");
client.print("varC ");
client.print(" = ");
client.print(c);
client.println("<br />");
client.print("varD ");
client.print(" = ");
client.print(d);
client.println("<br />");
client.print("varE ");
client.print(" = ");
client.print(e);
client.println("<br />");
client.print("varF ");
client.print(" = ");
client.print(f);
client.println("<br />");

break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}