0
Offline
Newbie
Karma: 0
Posts: 9
Arduino rocks
|
 |
« Reply #3 on: November 01, 2009, 07:55:21 am » |
#include "WiServer.h" #include <EEPROM.h> #include <Wire.h>
#define enableWifi 0
#define TRUE 1 #define FALSE 0 #define on 1 #define off 0
#define I2C_Write(a) (a&0x0xFEU) #define I2C_Read(a) (a|0x01U)
#define MCP9802 72U #define MCP9802_R 0b10010001 #define MCP9802_W 0b10010000
#define M41T81_R 0b11010001 #define M41T81_W 0b11010000
#define ledPin 9
boolean bLed = on;
#if enableWifi == TRUE // Wireless configuration parameters ----------------------------------------
#define WIRELESS_MODE_INFRA 1 #define WIRELESS_MODE_ADHOC 2
unsigned char local_ip[] = {192,168,0,10}; // 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 = {"thegrannybashers"}; // max 32 bytes
unsigned char serialBufIdx = 0U; #define serialBufMax sizeof(serialBuf)
unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase const prog_char security_passphrase[] PROGMEM = {"QSPELQJP"}; // 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; #endif
/* Meter declarations */ unsigned int impCount = 0U;
unsigned char kwhTotalDay = 0U;
/* Cost of KW-Hr */ #define penceKWH 12U
/* How many pulses to one KWH */ #define impPerKWH 5U
/* How many days to cache in eeprom */ #define numDaysCache 30U
unsigned char eepromIdx = 0U;
#if enableWifi == TRUE boolean processHTMLReq ( char* URL ); #endif
void processBlip ( void );
void resetDevice( void );
boolean getTemperature ( void );
boolean getTime ( void );
#if enableWifi == TRUE void serialPrintEnv ( void ) { Serial.print("\nWifi-Status:"); Serial.print("\nLocal Ip: "); Serial.print(local_ip[0], DEC); Serial.print("."); Serial.print(local_ip[1], DEC); Serial.print("."); Serial.print(local_ip[2], DEC); Serial.print("."); Serial.print(local_ip[3], DEC);
Serial.print("\nGateway Ip: "); Serial.print(gateway_ip[0], DEC); Serial.print("."); Serial.print(gateway_ip[1], DEC); Serial.print("."); Serial.print(gateway_ip[2], DEC); Serial.print("."); Serial.print(gateway_ip[3], DEC); Serial.print("\nSubnet Ip: "); Serial.print(subnet_mask[0], DEC); Serial.print("."); Serial.print(subnet_mask[1], DEC); Serial.print("."); Serial.print(subnet_mask[2], DEC); Serial.print("."); Serial.print(subnet_mask[3], DEC); Serial.print("\nSSID: "); Serial.print(&ssid[0]); Serial.print(", Passphrase: "); Serial.print(&security_passphrase[0]);
Serial.print(", SECURITY-"); switch (security_type) { case 0: Serial.print("OPEN"); break; case 1: Serial.print("WEP"); break; case 2: Serial.print("WPA"); break; case 3: Serial.print("WPA2"); break; defualt: Serial.print("UNKOWN"); break; } Serial.print("\n\n"); } #endif
void resetDevice ( void ) { unsigned char i = 0U; /* Reset eeprom values */ for(i=0U;i<=numDaysCache;i++) { EEPROM.write(i,0U); } EEPROM.write(255U,0); impCount = 0U; kwhTotalDay = 0U; }
void processBlip ( void ) { impCount = impCount + 1U;
if ( impCount >= impPerKWH ) { impCount = 0U; /* Increment totalKWH */ kwhTotalDay = kwhTotalDay + 1U; EEPROM.write(eepromIdx,kwhTotalDay); }
}
boolean getTime ( void ) { Wire.beginTransmission(M41T81_R); Wire.send(0x02); Wire.endTransmission(); Wire.requestFrom(M41T81_R,1); while(Wire.available()) { Serial.println(Wire.receive(),DEC); } return false; }
boolean getTemperature ( void ) { unsigned char tempVal = 0; unsigned char tempRead1 = 0U; unsigned char tempRead2 = 0U; boolean tempNegative = false; bReturnVal=false; /* Begin transmission to MCP9800X */ Wire.beginTransmission((int)MCP9802_R); /* Request temperature register */ Wire.send(0x00); Wire.endTransmission();
Wire.requestFrom((int)MCP9802_R,2); if (Wire.available() > 0 ) { Serial.println("Data available"); } else { Serial.println("NO data available"); } if(Wire.available()) { tempRead1 = (unsigned char)Wire.receive();
if(Wire.available()) { tempRead2 = Wire.receive(); bReturnVal=true; }
}
if (tempRead2 & 0b10000000) { tempNegative = true; } tempVal = (tempRead2 & 0b10000000) >> 7; tempVal = (tempRead1 << 1) | tempVal; Serial.print("Temperature: "); if (tempNegative) { Serial.print("-"); } Serial.print(tempVal,DEC); Serial.println(""); return bReturnVal; } void setup() { bInitStatus=true; Serial.begin(9600); #if enableWifi == TRUE Serial.println("Initializing Wifi"); WiServer.init(processHTMLReq); #else Serial.println("NO Wifi enabled"); #endif
Serial.println("Retrieving values from eeprom"); eepromIdx = EEPROM.read(255); kwhTotalDay = EEPROM.read(eepromIdx); Serial.println("Setting up Phototransistor"); #if 0 pinMode(3, INPUT); digitalWrite(3, HIGH);
pinMode(4, OUTPUT); digitalWrite(4, HIGH); pinMode(5, INPUT); digitalWrite(5, LOW); #endif
Serial.println("connecting to I2C bus"); Serial.println("Retrieving temperature"); Wire.begin(); if (getTemperature()) { bInitStatus=false; Serial.println("Failed to get the temperature, I2C connected?"); }
Serial.println("Retrieving time"); if (getTime()) { bInitStatus=false; Serial.println("Failed to get the time, I2C connected?); }
Serial.println("Initializing complete\n"); #if enableWifi == TRUE Serial.println("Wifi enabled to:"); serialPrintEnv(); #endif
if (!bInitStatus) { while(1) { Serial.println("\n\nERROR: Failed to initiliaze some components!\nHALTING !!!\n"); delay(1000); } } }
void loop() {
#if enableWifi == TRUE /* Keep the webpage alive */ WiServer.server_task(); #endif
if (digitalRead(3) == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }
if ( Serial.available() > 0U ) { char serialChar = Serial.read(); switch (serialChar) { case '1': //fake electric meter blip processBlip(); break; case '9': //increment the day eepromIdx += 1U; if (eepromIdx > numDaysCache) { eepromIdx = 0U; } /* Reset the KWH count */ EEPROM.write(eepromIdx,0U); kwhTotalDay = 0U;
break; case 'R': /* Reset device */ Serial.println("Resetting device"); resetDevice(); Serial.println("done!"); } } }
#if enableWifi == TRUE boolean processHTMLReq ( char* URL ) { unsigned char i = 0U; unsigned char day = 0U; unsigned char cacheMinusIdx = numDaysCache - eepromIdx; unsigned char kwhRead = 0U; unsigned int kwhTotalMonth = 0U; WiServer.print("<html>"); /* Auto-refresh page every 60 seconds */ WiServer.print("<head><meta http-equiv=\"refresh\" content=\"60\"></head>"); for(i=eepromIdx;i>0;i--) { WiServer.print("Days-ago: "); WiServer.print(day,DEC); WiServer.print(", usage: <b>"); kwhRead = EEPROM.read(i); WiServer.print(kwhRead,DEC); WiServer.print(" KW-Hours</b><br>"); kwhTotalMonth += kwhRead; day += 1; }
for(i=numDaysCache;i>eepromIdx;i--) { WiServer.print("Days-ago: "); WiServer.print(day,DEC); WiServer.print(", usage: <b>"); kwhRead = EEPROM.read(i); WiServer.print(kwhRead,DEC); WiServer.print(" KW-Hours</b><br>"); kwhTotalMonth += kwhRead; day += 1; }
WiServer.print("<hr>KWH cost today: <b>"); WiServer.print((penceKWH*kwhTotalDay)/100, DEC); WiServer.print("."); WiServer.print(((penceKWH*kwhTotalDay)%100), DEC); WiServer.print("p</b>, Past month: <b>"); WiServer.print(((penceKWH*kwhTotalMonth)/100), DEC); WiServer.print("."); WiServer.print(((penceKWH*kwhTotalMonth)%100), DEC); WiServer.print("p</b>, Cost per KWH: <b>0."); WiServer.print(penceKWH, DEC); WiServer.print("p</b>, KWH used past hour: <b>"); WiServer.print("NOT IMPLEMENTED"); WiServer.print("</b>, Blips Counted: "); WiServer.print(impCount,DEC); WiServer.print("</html>");
return true; } #endif
|