#include #include #include "utility/debug.h" #include #include #include // These are the interrupt and control pins #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! // These can be any two pins #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 // Use hardware SPI for the remaining pins // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER); // you can change this clock speed #define WLAN_SSID "SSID-name" #define WLAN_PASS "password" // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA // These #defines make it easy to set the backlight color #define RED 0x1 #define YELLOW 0x3 #define GREEN 0x2 #define TEAL 0x6 #define BLUE 0x4 #define VIOLET 0x5 #define WHITE 0x7 #define HIGH_MARK 210 #define LOW_MARK 190 #define HTTP_SERVER "server.domain.blah" #define URI_CGI "GET /recordWeight.php?weight=%ld&ram=%d&reading=%ld HTTP/1.1\r\n" MCP3424 MCP(0x68); Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield(); uint32_t serverIP = 0; long Voltage = 0; long reading = 0; char buffer[256]; void IPdotsRev(uint32_t ip, char* retString); void printMessage( const char* msg, boolean showOnLcd=true ); void printMessage( const __FlashStringHelper* msg, boolean showOnLcd=true ); void printLcdMessage( const char* msg ); void printLcdMessage( const __FlashStringHelper* msg ); void triggerWatchdog(); void setup(void) { Serial.begin(115200); Serial.println(F("MCP configured with 18 bits...")); MCP.Configuration(2,18,0,1); // Channel 1, 18 bits resolution, one-shot mode, amplifier gain = 1 lcd.begin(16, 2); lcd.clear(); lcd.setBacklight(WHITE); printMessage( F("Initializing...")); if (!cc3000.begin()) { printMessage( F("Couldn't begin()! Check your wiring?") ); while(1); } Serial.print( F("\nAttempting to connect to ") ); Serial.println(WLAN_SSID); if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { printMessage( F("Failed!")); while(1); } printMessage( F("Connected! Request DHCP") ); while (!cc3000.checkDHCP()) { delay(100); // ToDo: Insert a DHCP timeout! } uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; char ipString[20]; if( cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv) ) { IPdotsRev( ipAddress, ipString ); } else { strcpy( ipString, "FAILED." ); } printMessage(ipString); wdt_enable(WDTO_8S); // Try looking up the website's IP address if ( !cc3000.getHostByName(HTTP_SERVER, &serverIP) ) { sprintf( buffer, "Couldn't resolve: %s", HTTP_SERVER ); printMessage( buffer ); triggerWatchdog(); } wdt_reset(); } void loop(void) { if( serverIP == 0 ) { // should never be zero at this point triggerWatchdog(); } MCP.NewConversion(); // New conversion is initiated Voltage = MCP.Measure(); // Measure, note that the library waits for a complete conversion if( Voltage < LOW_MARK ) { lcd.setBacklight(RED); // need a refill soon.. } else if ( Voltage > HIGH_MARK ) { lcd.setBacklight(GREEN); // lots! } else { lcd.setBacklight(WHITE); } sprintf( buffer, "%ld uV : %ld ", Voltage, ++reading ); lcd.setCursor(0, 1); lcd.print( buffer ); Serial.println(buffer); wdt_reset(); Adafruit_CC3000_Client www = cc3000.connectTCP(serverIP, 80); if( www.connected() ) { sprintf( buffer, URI_CGI, Voltage, getFreeRam(), reading ); www.fastrprint(buffer); Serial.println(buffer); www.fastrprint(F("Host: ")); www.fastrprint( HTTP_SERVER ); www.fastrprint(F("\r\n")); www.fastrprint(F("\r\n")); www.println(); unsigned long startTime = millis(); while( www.connected() && ((millis() - startTime) < (5L * 1000L)) ) { // read data while( www.available() ) { char c = www.read(); Serial.print(c); startTime = millis(); } } } else { printMessage( F("Connection failed")); triggerWatchdog(); } www.close(); wdt_reset(); delay(5000); } void printLcdMessage( const __FlashStringHelper* msg ) { lcd.home(); lcd.clear(); lcd.print(msg); } void printLcdMessage( const char* msg ) { lcd.home(); lcd.clear(); lcd.print(msg); } void printMessage( const __FlashStringHelper* msg, boolean showOnLcd ) { Serial.print(msg); if (showOnLcd) { printLcdMessage( msg ); } } void printMessage( const char* msg, boolean showOnLcd ) { Serial.print(msg); if (showOnLcd) { printLcdMessage( msg ); } } void triggerWatchdog() { while(1) delay(10000); } void IPdotsRev(uint32_t ip, char* retString) { sprintf( retString, "%d.%d.%d.%d", (uint8_t)(ip >> 24), (uint8_t)(ip >> 16), (uint8_t)(ip >> 8), (uint8_t)(ip) ); }