/* * A sketch to send data to Pachube when requested (Automatic mode). It also * sends a web page with the latest data in HTML format upon a general * request with the addition of a "/data" code. * * Author: Caleb Engineering, LLC 4-17-10 */ #include #include #include //----- Custom definitions #define TURBINE_CURRENT_PIN 0 #define SOLAR_CURRENT_PIN 1 #define ANEMOMETER_PIN_OUT 4 #define ANEMOMETER_PIN_IN 5 #define RPM_PIN_OUT 6 #define RPM_PIN_IN 7 #define PERIOD_1 10 //----- end Custom definitions // Wireless configuration parameters ---------------------------------------- #define WIRELESS_MODE_INFRA 1 #define WIRELESS_MODE_ADHOC 2 unsigned char local_ip[] = {192,168,1,YOUR_IP}; // IP address of WiShield unsigned char gateway_ip[] = {192,168,1,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 = {"YOUR_SSID"}; // max 32 bytes unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 // WPA/WPA2 passphrase const prog_char security_passphrase[] PROGMEM = {"YOUR_PASS_HERE"}; // 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; // End of wireless configuration parameters ---------------------------------------- //----- Custom global veriables boolean intTimer; int turbine_current; int turbine_count; int solar_current; int solar_count; int anemometer_read; int anemometer_count; int turbine_speed; int RPM_read; int RPM_count; int period_counter_1; float turbine_sum; float solar_sum; float wind_speed; //----- end Custom global variables //***** MsTimer2 one minute timer ISR void timerISR() { intTimer = true; } //---------------- Custom functions ----------------- //***** page_Serve construct and serve web page upon request boolean sendMyPage(char* URL) { if (strcmp(URL, "/") == 0) { // If URL is "/" then eeml data WiServer.println(""); WiServer.println(""); WiServer.println(""); WiServer.println(""); WiServer.println(""); WiServer.print(""); WiServer.print(""); WiServer.print(""); WiServer.print(""); WiServer.println("
"); WiServer.print(turbine_current); WiServer.println(""); WiServer.print(solar_current); WiServer.println(""); WiServer.print(wind_speed); WiServer.println(""); WiServer.print(turbine_speed); WiServer.println("
"); WiServer.println(""); WiServer.println(""); return true; } else { // If URL is not recognized return false; } } //***** read_Turbine measure turbine current and add for averaging later void read_Turbine() { int turbine_data; turbine_data = analogRead(TURBINE_CURRENT_PIN); turbine_sum = turbine_sum + (float)turbine_data * 19.63; turbine_count++; } //***** read_Solar measure solar current and add for averaging later void read_Solar() { int solar_data; solar_data = analogRead(SOLAR_CURRENT_PIN); solar_sum = solar_sum + (float)solar_data * 19.63; solar_count++; } //***** poll_Anemometer check for anemometer input void poll_Anemometer() { digitalWrite(ANEMOMETER_PIN_OUT, HIGH); //-- re-enable the flip-flop anemometer_read = digitalRead(ANEMOMETER_PIN_IN); //-- check flip-flop output if(anemometer_read==HIGH){ anemometer_count++; //-- increment pulse count digitalWrite(ANEMOMETER_PIN_OUT, LOW); //-- clear the flip-flop } } //***** poll_RPM check for turbine RPM sensor input void poll_RPM() { digitalWrite(RPM_PIN_OUT, HIGH); //-- re-enable the flip-flop RPM_read = digitalRead(RPM_PIN_IN); //-- check flip-flop output if(RPM_read==HIGH){ RPM_count++; //-- increment pulse count digitalWrite(RPM_PIN_OUT, LOW); //-- clear the flip-flop } } //---------------- end Custom functions ----------------- //***** set up initialization void setup() { turbine_current = 0; turbine_sum = 0.0; turbine_count = 0; solar_current = 0; solar_sum = 0.0; solar_count = 0; wind_speed = 0.0; anemometer_count = 0; turbine_speed = 0; RPM_count = 0; period_counter_1 = 0; // set up pins as inputs and outputs pinMode(ANEMOMETER_PIN_IN, INPUT); pinMode(ANEMOMETER_PIN_OUT, OUTPUT); pinMode(RPM_PIN_IN, INPUT); pinMode(RPM_PIN_OUT, OUTPUT); // initialize WiServer WiServer.init(sendMyPage); WiServer.enableVerboseMode(true); // set up global state data intTimer = false; //set up the timerISR to be called every minute MsTimer2::set(2000, timerISR); // 2000ms is a 2 second period MsTimer2::start(); } //***** main loop void loop(){ //----- Routine to be executed when the timer times out, once every 2 sec if(true == intTimer && ~WiServer.sendInProgress()) { intTimer = false; // reset flag to wait for next time out read_Turbine(); read_Solar(); period_counter_1++; } //----- Routine to be executed when the timer times out, once per 20 sec if(period_counter_1 >= PERIOD_1 && ~WiServer.sendInProgress()) { period_counter_1 = 0; if(turbine_count != 0) { turbine_current = (int)(turbine_sum / (float)turbine_count); // average turbine current readings turbine_sum = 0.0; turbine_count = 0; } if(solar_count != 0) { solar_current = (int)(solar_sum / (float)solar_count); // average solar current readings solar_sum = 0.0; solar_count = 0; } wind_speed = (float)anemometer_count * .05588001; anemometer_count = 0; turbine_speed = (int)((float)RPM_count * .75); RPM_count = 0; } poll_Anemometer(); poll_RPM(); WiServer.server_task(); delay(10); }