WiFi - Embedded Remote Supervisory System ? ERSS

Source Code Part #1

/*************************************************************************
 
 Filename:    SSRE.pde
 
 Description: SSRE (Sistema Supervisorio Remoto Embarcado), Embedded Remote System
 
 ************************************************************************
  Demonstrates the use of Arduino and WiShield controlling temperature by PID 
 
 Contact Information:
  <edson@solsoft.com.br>  Edson Gaspar
  <gutosan@terra.com.br>  Jose Augusto Santaella
  
   Author               Date        Comment
  ---------------------------------------------------------------
   Gaspar & Guto            10/15/2010      Initial version
   
 **************************************************************************/
 
#include <WiServer.h>
#include <PID_Beta6.h>
#include <Servo.h>
#define WIRELESS_MODE_INFRA      1
#define WIRELESS_MODE_ADHOC      2

// Wireless configuration parameters
unsigned char local_ip[] = {192,168,0,111};      // IP address of WiShield
unsigned char gateway_ip[] = {192,168,0,1};      // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};      // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"SSRE"};            // max 32 bytes

unsigned char security_type = 0;      // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};      // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {      0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,      // Key 0
                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      0x00,      // Key 1
                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      0x00,      // Key 2
                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      0x00      // Key 3
                                                };

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;


//Pin configurations
#define PIN_INPUT_LM35 0
#define PIN_OUTPUT_PWM_LIGHT 3
#define PIN_OUTPUT_PWM_FAN 5

//global definitions
#define _USE_SERIAL 1
#define N_LOOP_FOR_SERIAL 200000

//Global Variables
  int nLoopCount = N_LOOP_FOR_SERIAL;
  int fan_speed = 0;

//PID variables
  double dSetPointTemperature, dInputTemperature, dOutputTemperature;
  double lightKc = 1.0;      
  double lightTauI = 2.0;      
  double lightTauD = 0.5;
  Servo temperatureServo;
  PID temperaturePID(&dInputTemperature, &dOutputTemperature, &dSetPointTemperature, lightKc, lightTauI, lightTauD);

//function to write input edit in html page
void writeInputHTM(char* title, char* type, char* name, const char* value, boolean bChecked = false)
{
  WiServer.print("<tr><td>");
  WiServer.print(title);
  WiServer.print("</td><td><input type=\"");
  WiServer.print(type);
  WiServer.print("\" name=\"");
  WiServer.print(name);
  WiServer.print("\" size=\"4\" value=\"");
  WiServer.print(value);
  WiServer.print("\"");
  if( bChecked ) WiServer.print("checked=\"checked\"");
  WiServer.println("></td></tr>"); 
}

//format value to string with 2 decimals
void sresprintf (char* ch, double valor)
{
  int valor1 = (valor - (int)valor)*100;
  sprintf ( ch, "%0d.%d", (int)valor, valor1);
}

//constants strings stored in flash memory to save RAM memory 
const prog_char htmlreader[]         PROGMEM = {"<!doctype html><html><head><title>Sistema SSRE v1.1a</title>" };
const prog_char ht_endhead[]         PROGMEM = {"</head>"};
const prog_char ht_body[]            PROGMEM = {"<body onload=\"timed();\"><b>Sistema SSRE v1.1a</b>
"};
const prog_char ht_credits[]         PROGMEM = {"Gaspar & Guto
edson@solsoft.com.br
gutosan@terra.com.br"};
const prog_char ht_script[]          PROGMEM = {"<script type=\"text/javascript\">"\
"var req;function timed(){"\
"if(window.XMLHttpRequest){req = new XMLHttpRequest();} else if (window.ActiveXObject) {req = new ActiveXObject('MSXML2.XMLHTTP.3.0'); }"\
"if(req != undefined){req.onreadystatechange=function(){statusDone();};"\
"req.open(\"GET\", \"/status.htm\", true);"\
"req.send(\"\");}"\
"}"\
"function statusDone(){"\
"if (req.readyState == 4) {if (req.status == 200|| req.status == 304) {document.getElementById('status').innerHTML = req.responseText;}t=setTimeout(\"timed()\",1500);}"\
"}"\
"</script>"};
const prog_char ht_endbody[]          PROGMEM = {"</body>"};
const prog_char ht_endhtml[]          PROGMEM = {"</html>"};
const prog_char ht_br[]               PROGMEM = {"
"};
const prog_char ht_form[]             PROGMEM = {"<form>"};
const prog_char ht_endform[]          PROGMEM = {"</form>"};
const prog_char ht_table_gray[]       PROGMEM = {"<table bgcolor=\"#cccccc\">"};
const prog_char ht_table_green[]      PROGMEM = {"<table border=\"1\" bgcolor=\"#ccffcc\">"};
const prog_char ht_endtable[]         PROGMEM = {"</table>"};
const prog_char ht_table_header[]     PROGMEM = {"<th>"};
const prog_char ht_table_endheader[]  PROGMEM = {"</th>"};
const prog_char ht_table_row[]        PROGMEM = {"<tr>"};
const prog_char ht_table_endrow[]     PROGMEM = {"</tr>"};
const prog_char ht_table_col[]        PROGMEM = {"<td>"};
const prog_char ht_table_endcol[]     PROGMEM = {"</td>"};
const prog_char ht_painel[]           PROGMEM = {"Painel de Controle"};
const prog_char ht_temp_var[]         PROGMEM = {"SetPointTemperature"};
const prog_char ht_pid_header[]       PROGMEM = {"Controlador PID"};
const prog_char ht_status_header[]    PROGMEM = {"Monitor"};
const prog_char ht_submit[]           PROGMEM = {"<tr><td colspan=\"2\"><input type=\"submit\" name=\"option\" value=\"Enviar\"/></td></tr>"};