Phonegap fan control via moisture sensor

3rd February 2019
Arduino
0

Seems like phonegap is really going to make my life so much easier when it comes to building custom mobile apps. This demonstration does not rely on any additional server(s) (I have used a web server + php in my previous blogs) that receives commands from the smartphone which in turn talks to the microcontroller. All the "brains" is on the nano itself. So in theory I could build a framework that you could just burn onto the arduino microcontroller(s) and expand my current app to allow you to add each unit to the list. Make a nice layout to display the outputs, write a list of supported sensors... you get the idea. So far I will provide you with the source code I have used in this example. For the very tech savvy people - yes, I could make the code monitor the moisture without the need of the mobile app (it can be done within 10-15 minutes - nothing spectacular). But hey, where would the fun be in that? 🙂 The arduino responds to GET commands. You give it a right "string" and it does something for you (if it has been hardcoded obviously). To switch the relay off you would just need to provide the IP address (assuming you are listening on port 80) and the associated command (relay1off does the job). Your url would look like this: http://192.168.7.177/?relay1off
I am also super aware that this kind of setup would not be ideal for deployment. If two or more users would have the app open the DHT11 sensor would struggle to provide readings (you can only read from it every 2 seconds or so). As a result I would need to create some sort of "proxy" code on the arduino itself that would feed all of the clients the most recent values from the sensor(s) while in the background updating them figures lets say every 15 seconds. Plus, the watchdog would need to be implemented... list goes on. You get the point 😀

// This works for the nano only !!!!
#include <SPI.h>
#include <UIPEthernet.h>

#include <dht.h>
#define aPin A5 // Defines pin number to which the sensor is connected
dht DHT; // Creats a DHT object

#define CH3 3   // relay control !

boolean reading = false; 
// Setup the static network info for you arduino
byte ip[] = { 192, 168, 7, 177 };   // IP Address
byte subnet[] = { 255, 255, 255, 0 }; // Subnet Mask
byte gateway[] = { 192, 168, 7, 1 }; // Gateway
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
 
EthernetServer server = EthernetServer(80); // Port 80
String HTTPget = "";
 
void setup()
{
  pinMode(CH3, OUTPUT);
  //Turn OFF any power to the Relay channels
  digitalWrite(CH3, LOW);  
  // pin 10, 11, 12 and 13 are used by the ethernet shield
  Ethernet.begin(mac, ip, gateway, subnet); // setup ethernet with params from above
  server.begin();
}
 
void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
  {
    // send http reponse header
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("Access-Control-Allow-Origin: *"); // guess why this is here....
    client.println();
    // process request.
    processClient(client);
  }
}
 
void processClient(EthernetClient client)
{
  // http request will end with a blank line
  boolean lineIsBlank = true;
 
  while (client.connected())
  {
    if (client.available())
    {
      char c = client.read();
 
      if(reading && c == ' ') reading = false;
      if(c == '?') reading = true; // ? in GET request was found, start reading the info
      //check that we are reading, and ignore the '?' in the URL, then append the get parameter into a single string
      if(reading && c != '?') HTTPget += c;
      if (c == '\n' && lineIsBlank)  break;
      if (c == '\n')
      {
        lineIsBlank = true;
      }
      else if (c != '\r')
      {
        lineIsBlank = false;
      }
    }
  }
 
  if (HTTPget == "sensor1")
  {
      int readData = DHT.read11(aPin); // Reads the data from the sensor
      float t = DHT.temperature; // Gets the values of the temperature
      //float h = DHT.humidity; // Gets the values of the humidity
      HTTPget = String(t);
  }

  if (HTTPget == "sensor2")
  {
      // Read the moist Data from the sensor !!
      int sensorValue = analogRead(A2);
      // The calibration part of your moisture sensor... pain in the butt really !
      // I am sure there are other better solutions but ohh well - you don't know what you don't know
      sensorValue = map(sensorValue, 140, 725, 0, 100);
      sensorValue = (sensorValue - 102) * (-1);
      // if moisture > 20% => turn the relay on (fan will start)
      if (sensorValue > 20)
      {
        digitalWrite(CH3, HIGH);  
      }

      // if moisture < 20% => turn the relay off (fan will stop)
      if (sensorValue < 20)
      {
        digitalWrite(CH3, LOW);  
      }
      
      HTTPget = String(sensorValue);
  }

  if (HTTPget == "relay1on")
  {
      // switch relay1 on !
      digitalWrite(CH3, HIGH);  
      HTTPget = "1";
  }

  if (HTTPget == "relay1off")
  {
      // switch 1 relay off !
      digitalWrite(CH3, LOW);  
      HTTPget = "0";
  }

  client.print(HTTPget);
  delay(20); // give the client a moment to receive
  client.stop(); // close connection
  HTTPget = ""; // clear out the get output we saved for client (app)
}

Hey, like this? Why not share it with a buddy?