Go Down

Topic: Arduino Telnet Server (Read 3 times) previous topic - next topic

Frisky


gandalfk7

#6
Jan 18, 2013, 12:14 pm Last Edit: Jan 18, 2013, 12:17 pm by gandalfk7 Reason: 1
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:
Code: [Select]

#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:
Code: [Select]
dht.begin();

in the void parseReceivedText added:
Code: [Select]
case 'e' : envCommand(); break;

and the rest of the code:
Code: [Select]
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:
Code: [Select]
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;


in the void parseReceivedText added:
Code: [Select]
case 'v' : varCommand(); break;


and the variables code:
Code: [Select]
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 ):
Code: [Select]
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 ");

gandalfk7

Tested with a DHT22, the temp/hum part seems to work without problems

Elijahg


Go Up