1

I have a SparkFun Red board (Arduino Uno R3) with a SparkFun CC3000 shield attached. I am trying to post data to the "data.sparkfun.com" service in order to test a basic Wifi data logger. I am using the AdaFruit CC3000 library for the sketch.

The problem is am having is that immediately after a power off and reset, the HTTP Get is successful. However, when the loop runs again, the post fails. Usually on the 2nd attempt. I have had a few times where it has looped 2-3 time before failing. But it always fails. If I reset the board with the push button or by disconnecting the serial monitor and reconnecting, it still fails to send the HTTP request.

I have checked my ram usage, and it doesn't appear to have any sort of leak in that regards. I have spent days looking online for similar issues, and can't seem to find anything related.

To make this all the more confusing, the board just seems to freeze, it doesn't give any sort of error on anything. And the freeze seems to happen at different places, nothing seems consistent.

Would someone mind checking my sketch code, seeing if there is anything glaring; maybe I have a bad board?

    #include <Adafruit_CC3000.h>
    #include <SPI.h>
    #include <avr/wdt.h>

    ////////////////////////////////////
    // CC3000 Shield Pins & Variables //
    ////////////////////////////////////
    // Don't change these unless you're using a breakout board.
    #define ADAFRUIT_CC3000_IRQ   2
    #define ADAFRUIT_CC3000_VBAT  7
    #define ADAFRUIT_CC3000_CS    10
    #define IP_ADDR_LEN     4   // Length of IP address in bytes

    #define SensorPin A0

    ////////////////////
    // WiFi Constants //
    ////////////////////
    #define WLAN_SSID "Hayes"
    #define WLAN_PASS "123456"
    #define WLAN_SECURITY WLAN_SEC_WPA2
    #define timeout 30000
    #define website "data.sparkfun.com"
    #define publicKey "MGg93JbM8LFJAQxG3jK0"
    #define privateKey "nz9M8WZVaXU7WXz4VnlX"
    #define NUM_FIELDS 1

    // Initialize the CC3000 objects (shield and client):
    Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS,
                             ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);

    /////////////////
    // Phant Stuff //
    /////////////////
    const String fieldNames[1] = { "value" };
    String fieldData[1];
    uint32_t ip = 0;

    //////////////////////
    // Input Pins, Misc //
    //////////////////////

    void setup()
    {
      wdt_disable();
      Serial.flush();
      Serial.begin(115200);
      Serial.println(F("\nInitializing..."));
      if (!cc3000.begin())
      {
        Serial.println(F("Couldn't being()! Check your wiring?"));
        while (1);
      }

      //Connect to WiFi network
      Serial.print(F("Connecting to WiFi network... "));
      Serial.println(String(WLAN_SSID));
      cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY, 3);
      Serial.println(F("done!"));
      while (!cc3000.checkDHCP())
      {
        delay(1000);
      }
      Serial.print(F("data.sparkfun.com -> "));
      while (ip == 0)
      {
        if (!cc3000.getHostByName("data.sparkfun.com", &ip)) {
          Serial.println(F("Couldn't Resolve!"));
          while (1) {}
        }
        delay(1000);
      }
    }

    void loop()
    {
      cc3000.printIPdotsRev(ip);
      Serial.println(F(""));
      float moisture = analogRead(SensorPin);
      fieldData[0] = String(moisture);
      Serial.println("Sensor Reading: " + String(moisture));
      int t = millis();
      //wdt_enable(WDTO_8S);
      Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
      if (client.connected()&&
        ((millis() - t) < timeout))
      {
        Serial.println(F("Connection to Sparkfun data server."));
        Serial.print(F("Sending Request..."));
        client.print("GET /input/");
        client.print(publicKey);
        client.print("?private_key=");
        client.print(privateKey);
          client.print("&");
          client.print(fieldNames[0]);
          client.print("=");
          client.print(fieldData[0]);    
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println(website);
        client.println("Connection: close");
        client.println();
        Serial.println(F("HTTP command posted, getting response."));
      }
      else {
          Serial.println(F("Connection to website failed."));    
        }

    //  while (client.connected())
    //  {
    //    while (client.available())
    //    {
    //      char c = client.read();
    //      Serial.print(c);
    //    }
    //  }
      client.close();
      Serial.println(F("Posting Data Complete"));
      //wdt_reset();
      //cc3000.disconnect();

      //reset watchdog and disable
      //wdt_reset();
      wdt_disable();

      delay(15000);
    }
  • Why do you have //cc3000.disconnect(); disabled? Without experience with your wifi board, it seems like this line could be important causing it to not reset, thus causing it to not totally disconnect from the client. – Anonymous Penguin Jan 28 '15 at 2:55
  • the cc3000.disconnect(); is used to disconnect from the AP. At this point, since it is plugged into constant power source, I assumed that I wouldn't want to disconnect from the AP, thus allowing my subsequent posts to the site to occur faster... by not having to reconnect to the Wifi AP again and again. – Cloudedturtle Feb 24 '15 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.