2012/11/16

Freeduino/Arduino Web Controlled (Configured) Automatic Switches

Inspired by Freeduino/Arduino Web Sensors And Switches on cheaphousetek blog on Oct. 20, 2012, go a step forward, automatically controll the switches by setting the sensors criteria. It comes out the sketch at the bottom of this post.
By setting the lower limits, upper limits and switch on/off using a browser, one can monitor the conditions of all switches  remotely.
To control the web server outside the local area network, the NAT function of IP router should be enabled. To secure your web server, the SSH or VNC connection should be established.

Pins Used

Sensors: A2, A3
Switches: D2, D3, D4, D5, D6, D7, D8, D9

Output


Sketch


// Freeduino/Arduino Web Controlled Automatic Switches version 1.0
// By Befun Hung on Nov. 16 2012
// Modified From Freeduino/Arduino Web Sensors And Switches At cheaphousetek Blog 
// The Sketch Works On Arduino IDE 1.02

#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module. 
#define sensorPinStart 2
#define noOfSensors 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
String inString = String(100);
String minValue, maxValue;
int defaultMinValue[] = {55, 56, 57, 58, 59, 60, 61, 62, 63};
int defaultMaxValue[] = {60, 61, 62, 63, 64, 65, 66, 67, 68};
String defaultOnValue[] = {"Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low"};
String Led;
int led[] = {00, 2, 3, 4 ,5 ,6 ,7 ,8 ,9  }; //pin num 0 in arry is not used
int numofleds = 8; //number of switches
String value[] = {"off","off","off","off","off","off","off","off","off"}; //startup all led are off
EthernetServer server(80);

void setup()
{
  delay(250); 
  Serial.begin(9600);
  Ethernet.begin(mac, ip,gateway,subnet); 
  server.begin();
  //set pin mode
  for (int j = 1; j < (numofleds + 1); j++)
  {
    pinMode(led[j], OUTPUT);
  }
  Serial.println("Serial READY");
  Serial.println("Ethernet READY");
  Serial.println("Server READY");
}

void loop()
{
  EthernetClient client = server.available();  
  if (client)
  {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) 
    {     
      if (client.available()) 
      {      
        char c = client.read();
        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (inString.length() < 35) 
        {
          inString.concat(c);
        } 
        if (c == '\n' && current_line_is_blank) 
        {                    
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html>");
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<body><form method=get>");
          client.println("<center><p><h1>Freeduino/Arduino Web Controlled Automatic Switches</h1></p><hr></center>"); 
          client.println("<h2>");
          // Sensor Reading and Setting -----------------------------------------------------------------------------
          client.println("Sensor Reading And Setting:<br>");
          for (int analogChannel = sensorPinStart ; analogChannel < sensorPinStart+noOfSensors ; analogChannel++)
          {
            minValue = String("Low") + analogChannel;
            // fixValue = String("Fix") + analogChannel;
            maxValue = String("High") + analogChannel;
            Led = String("RelayOn") + analogChannel;
            int sensorReading = analogRead(analogChannel);
            
            if (inString.indexOf(minValue+"=Dn") > 0) defaultMinValue[analogChannel] -= 1;
            if (inString.indexOf(minValue+"=Up") > 0) defaultMinValue[analogChannel] += 1;
            if (inString.indexOf(maxValue+"=Dn") > 0) defaultMaxValue[analogChannel] -= 1;
            if (inString.indexOf(maxValue+"=Up") > 0) defaultMaxValue[analogChannel] += 1; 
            if (inString.indexOf(Led+"=High") > 0)    defaultOnValue[analogChannel] = "Low";
            if (inString.indexOf(Led+"=Low") > 0)     defaultOnValue[analogChannel] = "High";
            client.print("Input A");
            client.print(analogChannel);
            client.print(":");
            client.print(sensorReading);
            client.print("  Low");
            // client.print(analogChannel);
            client.print(":");
            client.print(defaultMinValue[analogChannel]);
            client.print("<input type=submit name="+minValue+" value=Dn><input type=submit name="+minValue+" value=Fix><input type=submit name="+minValue+" value=Up>");
            client.print("  High");
            // client.print(analogChannel);
            client.print(":");
            client.print(defaultMaxValue[analogChannel]);
            client.print("<input type=submit name="+maxValue+" value=Dn><input type=submit name="+maxValue+" value=Fix><input type=submit name="+maxValue+" value=Up>");
            client.print("  On");
            // client.print(analogChannel);
            client.print(":");
            client.print("<input type=submit name="+Led+" value="+defaultOnValue[analogChannel]+">");
            client.println("<br>");
          }
          // Switch Monitoring -------------------------------------------------------------------------------------------
          client.println("<p></p>Switches Status:<br>"); 
          for (int i=1; i < (noOfSensors+1) ; i++)
          { 
            Led = String("Switch") + i;
            int sensorReading = analogRead(sensorPinStart+i-1);
            if ((sensorReading > defaultMaxValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "High")) {
              digitalWrite(led[i], HIGH);
              value[i] = "on";
            }
            if ((sensorReading < defaultMinValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "High")) {
              digitalWrite(led[i], LOW);
              value[i] = "off";
            }
            if ((sensorReading < defaultMinValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "Low")) {
              digitalWrite(led[i], HIGH);
              value[i] = "on";
            }
            if ((sensorReading > defaultMaxValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "Low")) {
              digitalWrite(led[i], LOW);
              value[i] = "off";
            }
            client.println(Led+"  <input type=submit name="+Led+" value="+value[i]+">"+"<br>");
          }
          client.println("</form><hr></h2>");
          client.println("<center>The web page will automatically refresh every 5 seconds.</center>");
          client.println("</html></body>");
          break;
        }
        if (c == '\n') 
        {
          // we're starting a new line
          current_line_is_blank = true;
        } 
        else if (c != '\r') 
        {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }  // End of if (client.available)
    }  // End of while (client.connected())
    // give the web browser time to receive the data
    delay(1);
    inString = "";
    client.stop();
  } //End of if (client)
}

2012/11/14

GlobalSat EM-411 GPS Module Test Sketch Using Arduino 0022

Once buying an GPS module, the first idea is to check whether the module works as expect. To check the module using Arduino, first connecting the module and Arduino Uno/Duemilanove, then using the following sketch running on Arduino IDE 0022 (should also run on 0023 but not tested). The sketch should work for other GPS module, because GPS module usually need only VCC, GND to run and additional TX and RX to transmit/recive data.
As soon as checked the connections and powering the Arduino Uno/Duemilanove , one can monitor the NMEA sentenc output using Arduino serial monitor or any terminal emulator such as Putty. For me, I use SSCOM to test GPS and bluetooth modules on Windows XP.

Pin Used And Connection

Arduino - GPS Module
5V - Pin2(VIN)
GND - Pin1(GND) and Pin5(GND)
D2(SoftwareSerial RX) - Pin3(TX)
D3(SoftwareSerial TX) - Pin4(RX)

Output


Sketch 

// Test Code for GlobalSat EM-411 GPS Modules 
// Arduino 0022 Has Built-in SoftwareSerial.h Library
// Download the Library is not Needed
// The sketch works on Arduino IDE 0022

#include <SoftwareSerial.h>
SoftwareSerial GPS = SoftwareSerial(2, 3);

void setup()  
{
  Serial.begin(9600);
  Serial.println("GlobatSat EM-411 GPS Module NMEA Sentence Test!");

  // EM-411 factory default baud rate is 4800, the module used has been changed the baud rate
  GPS.begin(9600);
}

void loop()  // whatever read from GPS, sent to Arduino serial monitor or terminal emulator
{
  Serial.print(GPS.read(), BYTE);
}