cc3000 stops writing to database

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
alharlow
 
Posts: 14
Joined: Thu Jan 30, 2014 11:30 pm

cc3000 stops writing to database

Post by alharlow »

I have an Uno R3 with an Adafruit cc3000 wifi shield. My initial goal is to record temperatures in a mySQL database. It's taken me weeks to get it to find my network, get an IP address, and start talking to my database. Most of that time was trying to make the MySQL Arduino/connector work. Apparently that only works with the Arduino Ethernet shield. So I now have the Uno calling a web page and passing it one parameter, a temperature. The Uno reads a temp sensor every 5 seconds and sends the temp to the database through the php web page.

My problem is after about 10 or 15 minutes it just stops. Initially, it would loop 8 times and just freeze. At that time in the sketch I had the cc3000 connect to the database outside the loop and leaving the thing connected while looping. I then moved the db connect inside the loop, with it connecting, calling the page, then closing the connection then loop to start it all over again. Once I moved the connect statement inside the loop it would, like I said, run for 10 or 15 minutes then freeze. I did notice in the database table that sometimes during that 10 to 15 minutes it would go 10 or 15 seconds between inserts into the db - meaning some of the loops did not result in an insert into the db. The code is below. Any suggestions would be greatly appreciated.

Code: Select all

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include <stdlib.h>
#include "utility/debug.h"

// 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       "Cepheus-S"           // cannot be longer than 32 characters!
#define WLAN_PASS       "BANNED"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define IDLE_TIMEOUT_MS  4000      // Amount of time to wait (in milliseconds) with no data 
                                   // received before closing the connection.  If you know the server
                                   // you're accessing is quick to respond, you can reduce this value.

/**************************************************************************/
/*!
    @brief  Sets up the HW and the CC3000 module (called automatically
            on startup)
*/
/**************************************************************************/

uint32_t ip;
Adafruit_CC3000_Client www;

/**************************************************************************/
void setup(void)
{
  Serial.begin(9600);

  /* Initialise the module */
  Serial.println(F("\nInitializing Wifi..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't init Wifi!"));
    while(1);
  }
  
  //  Connect ot he LAN
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  } else {
  Serial.println(F("\nConnected!"));
  }
  
  //  Lets get an IP address
  Serial.println(F("Requesting IP address..."));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  
  /* Display the IP address DNS, Gateway, etc. */  
  while (! displayConnectionDetails()) {
    delay(1000);
  }

  ip = 0;
  ip = cc3000.IP2U32(192, 168, 1, 9);
  
  // Do a ping test on the website
  Serial.print(F("\n\rPinging server ")); cc3000.printIPdotsRev(ip); Serial.print(" 5 times... ");  
  int replies = cc3000.ping(ip, 5);
  Serial.print(replies); Serial.println(F(" replies"));

}
/**************************************************************************/
/**************************************************************************/
const int temperaturePin = 0;
float voltage, degreesC, degreesF; 
int degF = 0;
char str[3];

void loop()
{
  voltage = analogRead(temperaturePin) * 0.004882814;
  degreesC = (voltage - 0.5) * 100.0;
  degreesF = degreesC * (9.0/5.0) + 32.0;
  degF = int(degreesF);
  itoa(degF,str,10);

  www = cc3000.connectTCP(ip, 80);
  if (www.connected()) {
    www.fastrprint(F("GET /Arduino/save_temps.php?"));
    www.fastrprint(F("temp1="));
    www.fastrprint(( str ));
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); 
    www.fastrprint(F("\r\n"));
    www.println();
  }  
  
  www.close();
  delay(5000);
}

/**************************************************************************/
/**************************************************************************/
bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
  
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nObtained Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.println();
    return true;
  }
}

User avatar
ibucky
 
Posts: 112
Joined: Wed Jan 11, 2012 10:40 pm

Re: cc3000 stops writing to database

Post by ibucky »

I'm experiencing a similar issue with the CC3000 just stopping while printing back to the server. See post: http://forums.adafruit.com/viewtopic.php?f=25&t=49868 In my case, it is freezing when it goes to do the client.println("HTTP/1.1 200 OK");

I wonder if you are stopping at the
www.fastrprint(F("GET /Arduino/save_temps.php?"));
Or
www.println();

Not sure if it will be of help but a couple things I put in to see what was going 1: a bunch of tracers (Serial.print) 2: checkConnected () to see if I still had a valid connection. I know you have the if (www.connected()) but when it quits doesn't hurt to check to perhaps gain some insight.

I'll be watching your post to see any resolutions. Good luck.

alharlow
 
Posts: 14
Joined: Thu Jan 30, 2014 11:30 pm

Re: cc3000 stops writing to database

Post by alharlow »

I added a serial.println after each line in the fastrprint area of the code. It ran for a few minutes then stop at the www.println line at the bottom of that section of code. I then comment out that line of code. It ran fine for a while except that it was not writing to the database. After a while the most unusual thing happened. All the sudden the serial monitor screen started scrolling rapidly to the right without scrolling down any longer. It went a very long ways to the right. I couldn't do anything with the scroll bars while this was happening so I enter 'stop' in the input box (not knowing if this was even a valid command) and clicked send and everything stop. It was repeatedly adding either a space or some character that can't be seen all on the same line.

As far as the checkConnected suggestion, the compiler keeps telling me error: 'class Adafruit_CC3000_Client' has no member named 'checkConnected'. I can see it in the .h and the .cpp file. Go figure. I think I'll reboot...

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: cc3000 stops writing to database

Post by adafruit_support_mike »

The name of the function is 'connected()':

Code: Select all

if (client.connected()) {
  [. . .]
}

User avatar
ibucky
 
Posts: 112
Joined: Wed Jan 11, 2012 10:40 pm

Re: cc3000 stops writing to database

Post by ibucky »

I saw the checkConnected() in library file Adafruit_CC3000.h and gave it a shot to see if I was still connected. I didn't see a connected() function.

Did you check your www.connected() right before the www.println to see if you are still connected right before it stops?

I'm seeing the same client.println line work and then fail for me as well. Can that be turned to a fastprintln and/or will it make any difference?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: cc3000 stops writing to database

Post by adafruit_support_mike »

'checkConnected()' is a method that belongs to the Adafruit_CC3000 class. 'connected()' is a method that belongs to the Adafruit_CC3000_Client class.

alharlow
 
Posts: 14
Joined: Thu Jan 30, 2014 11:30 pm

Re: cc3000 stops writing to database

Post by alharlow »

I added serial.println statements around every fastrprint line. This time it ran for about 40 minutes. The code I added should have printed "Not Connected". Below is the tail end of the print and I'm adding code that section of the code.

Connect to 192.168.1.9:80
Before GET
After GET
After temp=
After str
After HTTP
After Host
After cr

Connect to 192.168.1.9:80
Before GET
After GET
After temp=
After str
After HTTP
After Host
After cr

Connect to 192.168.1.9:80
Before GET
After GET
After temp=

This is the end of the output. It did not print the "Not Connected".

Code: Select all

void loop()
{
  voltage = analogRead(temperaturePin) * 0.004882814;
  degreesC = (voltage - 0.5) * 100.0;
  degreesF = degreesC * (9.0/5.0) + 32.0;
  degF = int(degreesF);
  itoa(degF,str,10);

  www = cc3000.connectTCP(ip, 80);
  if (www.connected()) {
    Serial.println(F("Before GET"));
    if (!www.connected()) {
      Serial.println(F("Not Connected"));
      while (1);
    } else {
      www.fastrprint(F("GET /Arduino/save_temps.php?"));
    }
    Serial.println(F("After GET"));
    if (!www.connected()) {
      Serial.println(F("Not Connected"));
      while (1);
    } else {
      www.fastrprint(F("temp1="));
    }
    Serial.println(F("After temp="));                           <---------- This was the last line printed
    if (!www.connected()) {
      Serial.println(F("Not Connected"));
      while (1);
    } else {
      www.fastrprint(( str ));
    }
    Serial.println(F("After str"));
    if (!www.connected()) {
      Serial.println(F("Not Connected"));
      while (1);
    } else {
      www.fastrprint(F(" HTTP/1.1\r\n"));
    }
    Serial.println(F("After HTTP"));
    if (!www.connected()) {
      Serial.println(F("Not Connected"));
      while (1);
    } else {
      www.fastrprint(F("Host: ")); 
    }
    Serial.println(F("After Host"));
    if (!www.connected()) {
      Serial.println(F("Not Connected"));
      while (1);
    } else {
      www.fastrprint(F("\r\n"));
    }
    Serial.println(F("After cr"));
    if (!www.connected()) {
      Serial.println(F("Not Connected"));
      while (1);
    } else {
      www.println();
    }
  }

User avatar
ibucky
 
Posts: 112
Joined: Wed Jan 11, 2012 10:40 pm

Re: cc3000 stops writing to database

Post by ibucky »

This time it ran for about 40 minutes.
Wild guess here - do you need a delay? To give it time. If you only could trap the freeze to break;

alharlow
 
Posts: 14
Joined: Thu Jan 30, 2014 11:30 pm

Re: cc3000 stops writing to database

Post by alharlow »

Do you mean delay between each fastrprint?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: cc3000 stops writing to database

Post by adafruit_support_mike »

Try adding a Serial.flush() before each while(1) loop. It's possible that the loop is freezing execution while the output string is stuck in the chip's output buffer.

User avatar
ibucky
 
Posts: 112
Joined: Wed Jan 11, 2012 10:40 pm

Re: cc3000 stops writing to database

Post by ibucky »

<<Do you mean delay between each fastrprint?>>
Yes, because the duration time (15 to 40 mins) increased when you put the Serial.print tracers. So I thought maybe it needs sometime. Just a thought

Are you recording Free memory? If not, I'd put free memory in the mix to see if your having a leak? Or just getting low.

alharlow
 
Posts: 14
Joined: Thu Jan 30, 2014 11:30 pm

Re: cc3000 stops writing to database

Post by alharlow »

Well, weird stuff. I did the memory free space measurement. It showed 1120 constantly. Not sure what that number represents. Anyway, a couple of other things. I realized I have an air filter with an electric motor running constantly, within 3 feet of the wireless router. I turned it off. Didn't make any difference in the looping process freezing up. Another thing I did, trying to uncomplicate things, was to disable the wifi on my computer and direct wired it to the router. Oh, another thing I noticed was that sometime my wireless mouse becomes unusable in that the pointer on the screen jerks from one place to another on the screen and eventually freezes up. Has to be a frequency interference thing with the cc3000.

User avatar
s1sm1x
 
Posts: 4
Joined: Sun Jul 24, 2016 11:05 pm

Re: cc3000 stops writing to database

Post by s1sm1x »

Hi,

I'm also having this problem of writing to the database because at the fourth iteration it just freezes on connectTCP function, but it is able to send POSTs and writing to the database on the 3 first iterations. It has been a rough time trying to figure what was the problem.
The whole explanation is here: viewtopic.php?f=31&t=100442


Thanks for the atention,

Pedro

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”