Premise: I'm an improvised coder.
I have added two parts to the server, one for the temperature readings (but I hadn't time to test it) and one for the reading/writing of 6 variables:
Temperature:
library and settings:
#include <DHT.h>
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTPIN 2 // what pin we're connected to
DHT dht(DHTPIN, DHTTYPE);
in the void setup:
dht.begin();
in the void parseReceivedText added:
case 'e' : envCommand(); break;
and the rest of the code:
void envCommand()
// if we got here, textBuff[0] = 'e'
{
switch (textBuff[1]) {
case 't' : envTempCommand(); break;
case 'h' : envHumCommand(); break;
default: printErrorMessage(); break;
}
}
void envTempCommand()
// if we got here, textBuff[0] = 'e' and textBuff[1] = 't'
{
//float h = dht.readHumidity();
float t = dht.readTemperature();
// if (isnan(t)) {
if (isnan(t) && (t) == 0.00) {
client.println("ERROR reading the sensor ");
client.print("DEBUG content of t ");
client.print(t);
client.println(" "); }
else {
client.print("Temp: ");
client.print(t);
client.print(" *C");
client.println(" "); }
}
void envHumCommand()
// if we got here, textBuff[0] = 'e' and textBuff[1] = 'h'
{
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("Hum: ");
client.print(h);
client.print(" %\t");
client.println(" "); }
}
the variables part:
define the variables:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
in the void parseReceivedText added:
case 'v' : varCommand(); break;
and the variables code:
void varCommand()
// if we got here, textBuff[0] = 'v'
{
switch (textBuff[1]) {
case 's' : varSetCommand(); break;
case 'r' : varReadCommand(); break;
default: printErrorMessage(); break;
}
}
void varSetCommand()
// if we got here, textBuff[0] = 'v' and textBuff[1] = 's'
{
if (textBuff[3] == '=') {
int variable = 0; // varible to store my command variable
variable = parseDigit(textBuff[4]); // reads the fourth char of my command
if (variable >= 0 && variable <= 9) { // check if a number have been entered, isnan didn't quite work
switch (textBuff[2]) {
case 'a':
a = variable;
client.print("'a' is: ");
client.println(a);
client.println("OK! ");
break;
case 'b':
b = variable;
client.print("'b' is: ");
client.println(b);
client.println("OK! ");
break;
case 'c':
c = variable;
client.print("'c' is: ");
client.println(c);
client.println("OK! ");
break;
case 'd':
d = variable;
client.print("'d' is: ");
client.println(d);
client.println("OK! ");
break;
case 'e':
e = variable;
client.print("'e' is: ");
client.println(e);
client.println("OK! ");
break;
case 'f':
f = variable;
client.print("'f' is: ");
client.println(f);
client.println("OK! ");
break;
default:
client.println("ERROR: wrong variable ");
}
}
else client.println("ERROR: unfortunately we can only process one digit numbers"); }
else client.println("ERROR: wrong command "); }
void varReadCommand()
// if we got here, textBuff[0] = 'v' and textBuff[1] = 'r'
{
switch (textBuff[2]) {
case 'a':
client.println(a);
break;
case 'b':
client.println(b);
break;
case 'c':
client.println(c);
break;
case 'd':
client.println(d);
break;
case 'e':
client.println(e);
break;
case 'f':
client.println(f);
break;
default:
client.println("ERROR: wrong variable ");
}
}
obviously I added the needed text to the help ( void printHelpMessage ):
client.println(" et -environmental TEMP read: returns the state of the temp sensor");
client.println(" eh -environmental HUMidity read: returns the state of the humidity sensor");
client.println(" vr -variable read: reads a variable");
client.println(" vs -variable set: sets a variable");
client.println(" allowed variables are: a b c d e f ");
client.println(" allowed variables values are numbers between 0-9 ");