Pages: [1]   Go Down
Author Topic: Arduino + Ethernet + DHT22 temperature sensor controlled by Android app  (Read 3444 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
I develop android apps for fun, and recently I started playing also with Arduino. I have started with a basic webserver app for Arduino+Ethernet and the Android app to command/receive data to/from Arduino itself. In this project I plug also a DHT22 temperature and humidity sensor (thanks to Rob Tillaart for DHTlib) and show real time data in the android app (numbers and graph).

Actually the Arduino code is quite rough, so feel free to give me feedback and suggestions.

The android app is simple, but smart: there is no need to know the IP assigned by DHCP to the arduino, because the app scan IPs of the local subnet until an answer is received or you can directly set the IP address.

Source (both android app and arduino sketch) here : https://github.com/bradipao/Arduino-snippets/tree/master/WebserverAm2302



Code:
/*
   WebserverAm2302
   
   - connect to ethernet in DHCP mode
   - send IP on serial output
   - wait for incoming http request with format http://aaa.bbb.ccc.ddd/cantuina
   - read DHT22 am2302 sensor with DHTlib http://playground.arduino.cc/Main/DHTLib
   - answer in json format
   
 */

// include
#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>

// define
#define DHT22_PIN 6

// globals
char cmd;
char _buf[50];
int ethok=0;
byte mac[] = { 0x00,0xAA,0xBB,0xCC,0xDE,0x02 };
IPAddress ip;
dht DHT;

boolean isreqline = true;
String req = String();
String par = String();

// Ethernet server
EthernetServer server(80);

// the setup routine runs once when you press reset:
void setup() {
  // open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect
  }
  Serial.println("-- UART READY");
 
  // DHT22 test
  int chk = DHT.read22(DHT22_PIN);
  switch (chk) {
    case DHTLIB_OK: 
Serial.println("-- DHT22 READY");
break;
    case DHTLIB_ERROR_CHECKSUM:
Serial.println("-- DHT22 checksum error");
break;
    case DHTLIB_ERROR_TIMEOUT:
Serial.println("-- DHT22 time out error");
break;
    default:
Serial.println("-- DHT22 unknown error");
break;
  }
 
  // ethernet setup (using DHCP can be blocking for 60 seconds)
  ethok = Ethernet.begin(mac);
  if (ethok==0) Serial.println("ERROR : failed to configure Ethernet");
  else {
    ip = Ethernet.localIP();
    formatIP(ip);
    Serial.print("-- IP ADDRESS is ");
    Serial.println(_buf);
  }
}

// loop routine
void loop() {
 
  // web server
  if (ip[0]!=0) {
    EthernetClient client = server.available();   // listen to client connecting
    if (client) {
      //Serial.println("ETHERNET : new client http request");
      boolean currentLineIsBlank = true;   // an http request ends with a blank line
      while (client.connected()) {
        if (client.available()) {
          // read char from client
          char c = client.read();
          // append to request string
          if ((isreqline)&&(req.length()<127)) req += c;
          // stop parsing after first line
          if (c=='\n') isreqline = false;
         
          // 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) {
           
            // if request does not contain "cantuina" keyword send 404
            if (req.indexOf("cantuina")==-1) send_404(client);
            // else send JSON response
            else {
              // start sensor reading
              DHT.read22(DHT22_PIN);
              // response header
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: application/json");  // JSON response type
              client.println("Connection: close");               // close connection after response
              client.println();
              // open JSON
              client.print("{");
              // result header
              client.print("\"res\":\"OK\"");
              client.print(",\"hwid\":\"12345678\"");
              // temperature
              client.print(",\"temp\":\"");
              client.print(DHT.temperature,1);
              client.print("\"");
              // humidity
              client.print(",\"humi\":\"");
              client.print(DHT.humidity,1);
              client.print("\"");
              // pressure
              client.print(",\"pres\":\"");
              client.print(0);
              client.print("\"");
              // close json
              client.println("}");
              // serial logging
              Serial.print("ETHERNET req / temp: ");
              Serial.print(DHT.temperature,1);
              Serial.print(" C / humi: ");
              Serial.print(DHT.humidity,1);
              Serial.println(" %");
            }

            // prepare for next request
            req = "";
            isreqline = true;
            // exit
            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();
    }
  }
}

void formatIP(IPAddress ii) {
  sprintf(_buf,"%d.%d.%d.%d",ii[0],ii[1],ii[2],ii[3]);
}

static void send_404(EthernetClient client) {
  client.println("HTTP/1.1 404 Not Found");
  client.println("Content-Type: text/html");
  client.println();
  client.println("404 NOT FOUND");
}
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 583
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

looks great! how do I install the android app?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Excellent job!  I'm new to Arduino and all of this.  I found this easy to install and have it working on my Galaxy S3. 

What would cause the temp and humidity to periodically alternate between actual and -999.0?  I get a crash on Cantuina then restart and it works continuously just perfect again.

This is way fun!

Me being from the States, do you plan on coming out with a Fahrenheit temp?  I'll dig in and learn how to convert this too.

Thanks for all your time into this project  smiley  -James
Logged

Pages: [1]   Go Up
Jump to: