CC300 weather station

by mh512 on Tue Dec 03, 2013 12:39 pm

Hi

I'm sending data to a server running on a PC on the same network (static IP of 192.168.0.100). This is a WAMP server running on port 880.

I can successful send two random numbers between 1 and 50 for both temperature and humidity using the following code.

My question, however, is why it stops working after about 4 successful requests?

I have restarted Apache running on wamp with no change.

Last line of serial monitor is "Connected & Data Sent". The connection is not closed and a new pair of numbers aren't sent.

Any ideas?

I don't think it's server related as I can close and re-open the serial monitor and it works fine again for another set of requests.

Code: Select all | TOGGLE FULL SIZE
// Include required libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "DHT.h"
#include<stdlib.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

#define WLAN_SSID "TP-LINK AP"
#define WLAN_PASS "password"
#define WLAN_SECURITY WLAN_SEC_WPA2

// DHT11 sensor pins
#define DHTPIN 7
#define DHTTYPE DHT11

// Create CC3000 & DHT instances
DHT dht(DHTPIN, DHTTYPE);
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);
                                         
// Local server IP, port, and repository (change with your settings !)
uint32_t ip = cc3000.IP2U32(192,168,0,100);
int port = 880;
String repository = "/";
                                         
void setup(void)
{
 
  // Initialize DHT sensor
  dht.begin();
 
  Serial.begin(115200);
   
  // Initialise the CC3000 module
  if (!cc3000.begin())
  {
    while(1);
  }

  // Connect to  WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println("Successfully connected to network!");
   
  // Check DHCP
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  } 
 
}

void loop(void)
{
 
    // Measure the humidity & temperature
    float h = random(50);
    float t = random(50);
     
    // Transform to String
    String temperature = String((int) t);
    String humidity = String((int) h);
   
    // Print data
    Serial.print("Temperature: ");
    Serial.println(temperature);
    Serial.print("Humidity: ");
    Serial.println(humidity);
    Serial.println("");
   
    // Send request
    String request = "GET "+ repository + "sensor.php?temp=" + temperature + "&hum=" + humidity + " HTTP/1.0";
    send_request(request);
   
    // Update every second
    delay(1000);
}

// Function to send a TCP request and get the result as a string
void send_request (String request) {
     
    // Connect   
    Serial.println("Starting connection to server...");
    Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
   
    // Send request
    if (client.connected()) {
      client.println(request);     
      client.println(F(""));
      Serial.println("Connected & Data sent");
    }
    else {
      Serial.println(F("Connection failed"));   
    }

    while (client.connected()) {
      while (client.available()) {

      // Read answer
      char c = client.read();
      }
    }
    Serial.println("Closing connection");
    Serial.println("");
    client.close();
   
}
Last edited by mh512 on Tue Dec 03, 2013 1:06 pm, edited 1 time in total.
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by mh512 on Tue Dec 03, 2013 12:54 pm

I cleared the Apache error log and it's done twice as many but that may be coincidence.

Last attempt the serial monitor displayed the following before it stopped.

"Temperature: 29
Humidity: 27

Starting connection to server...


Connect to 192.168.0.100:880
Connected & Data sent
à"

The numbers 29 and 27 got to the PHP script and then it gave up.
Again, restarting the arduino (hence reconnecting it to the AP) let it successfully do another 7 or so requests.

Problem must be on the arduino's side I think.

Strange!
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by mh512 on Tue Dec 03, 2013 1:44 pm

Ok it's working perfectly now.

Haven't changed anything.
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by mh512 on Wed Dec 04, 2013 1:07 pm

Back to only sending a couple of requests before it stops doing anything now

Serial monitor shows this:

Code: Select all | TOGGLE FULL SIZE
Started AP/SSID scan




Connecting to TP-LINK AP...Waiting to connect...Successfully connected to network!
Request DHCP
Temperature: 49
Humidity: 7

Starting connection to server...


Connect to 192.168.0.100:880
Connected & Data sent
ÿ


Definitely something dodgy going on with the CC3000 board.

Look what happens when I try to ping it from a PC on the same network as it. The CC300's IP is 192.168.0.101

When the ping goes through it's when the get request works, when it doesn't its when it stops working.

Is the board faulty? I've tried two arduinos with the same problem.

I don't understand why it would suddenly start doing this.

ss.png
ss.png (23.14 KiB) Viewed 859 times
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by mh512 on Wed Dec 04, 2013 3:04 pm

Could it be because I was using port 880?
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by adafruit_support_mike on Wed Dec 04, 2013 4:39 pm

Using port 880 won't make any difference. If you can connect even once, the port is fine.

The "destination host unreachable" error indicates a failed ARP (Address Routing Protocol) request. Your computer can't map the IP address 192.168.0.101 to a MAC (Media Access Control) address.

In human words, your CC3000 has lost its connection to your network.

The on-again/off-again nature of the problem makes me think it's due to a noisy signal environment. Try setting your wireless router to different channels and see if any of them work better.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8675
Joined: Thu Feb 11, 2010 1:51 pm

Re: CC300 weather station

by mh512 on Tue Dec 10, 2013 1:45 pm

Changing router channel hasn't made a difference. The board will still send a random number of requests before it just sits there.
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by adafruit_support_mike on Tue Dec 10, 2013 4:18 pm

For whatever reason, it sounds like you're losing the WiFi connection. That can happen regardless of what hardware you use.

Your best bet is to check the connection just before using it, and to re-establish a connection if the old one has been dropped. The CC3000 class has a method named `checkConnected()` which you can use for the test. If it returns FALSE, discard that CC3000 object and create a new one.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8675
Joined: Thu Feb 11, 2010 1:51 pm

Re: CC300 weather station

by mh512 on Tue Jan 28, 2014 5:03 pm

Still having major issues with this.

The arduino sends 2/3 requests and then just sits there. When it stops, the last line in the serial monitor o/p is "Connected & Data sent"

// Include required libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "DHT.h"
#include<stdlib.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10

// WiFi network (change with your settings !)
#define WLAN_SSID "TP-LINK AP" // cannot be longer than 32 characters!
#define WLAN_PASS "password"
#define WLAN_SECURITY WLAN_SEC_WPA2 // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

// DHT11 sensor pins
#define DHTPIN 7
#define DHTTYPE DHT11

// Create CC3000 & DHT instances
DHT dht(DHTPIN, DHTTYPE);
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2);

// Local server IP, port, and repository (change with your settings !)
uint32_t ip = cc3000.IP2U32(192,168,0,100);
int port = 80;
String repository = "/";

void setup(void)
{

// Initialize DHT sensor
dht.begin();

Serial.begin(115200);

// Initialise the CC3000 module
if (!cc3000.begin())
{
while(1);
}

// Connect to WiFi network
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Serial.println("Connected to WiFi network!");

// Check DHCP
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100);
}

}

void loop(void)
{

// Measure the humidity & temperature
float h = random(77);
float t = random(77);

// Transform to String
String temperature = String((int) t);
String humidity = String((int) h);

// Print data
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.println("");

// Send request
String request = "GET "+ repository + "submit.php?temp=" + temperature + "&hum=" + humidity + " HTTP/1.0";
send_request(request);

// Update every second
delay(1000);
}

// Function to send a TCP request and get the result as a string
void send_request (String request) {

// Connect
Serial.println("Starting connection to server...");
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);

// Send request
if (client.connected()) {
client.println(request);
client.println(F(""));
Serial.println("Connected & Data sent");
}
else {
Serial.println(F("Connection failed"));
}

while (client.connected()) {
while (client.available()) {

// Read answer
char c = client.read();
}
}
Serial.println("Closing connection");
Serial.println("");
client.close();

}
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by adafruit_support_mike on Tue Jan 28, 2014 6:32 pm

Hmm.. you may be getting caught by a server that defaults to keep-alive connections.

Try printing the response data and see if that gives you anything:

Code: Select all | TOGGLE FULL SIZE
while (client.connected()) {
    while (client.available()) {

    // Read answer
    // char c = client.read();
    Serial.print( client.read() );
    }
}
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8675
Joined: Thu Feb 11, 2010 1:51 pm

Re: CC300 weather station

by mh512 on Wed Jan 29, 2014 1:11 pm

Thanks Mike but surely it would always do that?

Sometimes it does 1, sometimes it does 4 and if i'm really lucky it will go forever.

I've just added the code you suggested.

Last line is Connected & Data sent then it hangs so it never gets to print reply.

I have control over the server - what should I adjust?

Really frustrating as I know the arduino can do what it's supposed to do but it seems very hit and miss as to getting it to do its job properly.
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by adafruit_support_mike on Wed Jan 29, 2014 7:07 pm

mh512 wrote:Thanks Mike but surely it would always do that?

If only networks were so predictable.. ;-) There are lots of things going on behind the scenes, and things that aren't visible at the application level have an effect on the connection.

In this case, I was mostly looking for information about where the code was hanging. It sounds like something is happening before the CC3000 returns the first character.

Let's see if we can isolate the problem a little more tightly:

Code: Select all | TOGGLE FULL SIZE
Serial.println("Just aout the enter the read loop.");
while (client.connected()) {
Serial.println("  Outer loop - just executed client.connected().");
    while (client.available()) {
Serial.println("    Inner loop - just executed client.available().");
    // Read answer
    // char c = client.read();
    Serial.println( client.read() );
    }
}
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8675
Joined: Thu Feb 11, 2010 1:51 pm

Re: CC300 weather station

by mh512 on Thu Jan 30, 2014 6:40 am

Gets stuck in and endless loop at Serial.println(" Outer loop - just executed client.connected().");
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm

Re: CC300 weather station

by adafruit_support_mike on Thu Jan 30, 2014 7:33 pm

Okay, that tells us 'client.available()' is returning '0', meaning there's nothing in the input buffer to read.

Let's check the webserver's logfiles to see what's happening at that end of the connection. Those live any number of places in various distros, but start looking in /var/log for a directory named 'httpd' or some variant of 'apache'. What you want are 'access_log' and 'error_log', which keep track of all the requests the server has handled.

Specifically, we want to look at the status code and 'bytes sent' fields of the line for a request that hangs.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8675
Joined: Thu Feb 11, 2010 1:51 pm

Re: CC300 weather station

by mh512 on Sat Feb 08, 2014 12:51 pm

Hi Mike

It got stuck today at 17:09:45. This was the last request time phpmyadmin recorded.

The last bits of the access log:
Code: Select all | TOGGLE FULL SIZE
192.168.0.101 - - [08/Feb/2014:17:07:36 +0000] "GET /submit.php?temp=19&hum=48 HTTP/1.0" 200 1862
laptop1 - - [08/Feb/2014:17:07:53 +0000] "GET /phpmyadmin/index.php?ajax_request=1&recent_table=1&token=d12e7bdbf94a622036d43d7e942019b4 HTTP/1.1" 200 236
192.168.0.101 - - [08/Feb/2014:17:07:55 +0000] "GET /submit.php?temp=19&hum=48 HTTP/1.0" 200 1862
192.168.0.101 - - [08/Feb/2014:17:08:14 +0000] "GET /submit.php?temp=19&hum=48 HTTP/1.0" 200 1862
192.168.0.101 - - [08/Feb/2014:17:08:32 +0000] "GET /submit.php?temp=19&hum=48 HTTP/1.0" 200 1862
192.168.0.101 - - [08/Feb/2014:17:08:51 +0000] "GET /submit.php?temp=19&hum=48 HTTP/1.0" 200 1862
192.168.0.101 - - [08/Feb/2014:17:09:10 +0000] "GET /submit.php?temp=19&hum=48 HTTP/1.0" 200 1862
192.168.0.101 - - [08/Feb/2014:17:09:28 +0000] "GET /submit.php?temp=19&hum=48 HTTP/1.0" 200 1862
laptop1 - - [08/Feb/2014:17:28:14 +0000] "GET /phpmyadmin/index.php?token=d12e7bdbf94a622036d43d7e942019b4 HTTP/1.1" 200 45428
laptop1 - - [08/Feb/2014:17:28:14 +0000] "GET /phpmyadmin/themes/pmahomme/jquery/jquery-ui-1.9.2.custom.css HTTP/1.1" 304 -
laptop1 - - [08/Feb/2014:17:28:14 +0000] "GET /phpmyadmin/themes/pmahomme/img/logo_left.png HTTP/1.1" 304 -
laptop1 - - [08/Feb/2014:17:28:14 +0000] "GET


Last bits of the error log:
Code: Select all | TOGGLE FULL SIZE
[Sat Feb 08 17:08:49.552243 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2360] PHP   1. {main}() C:\\wamp\\www\\submit.php:0
[Sat Feb 08 17:09:08.205310 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2361] PHP Notice:  Undefined index: light in C:\\wamp\\www\\submit.php on line 19
[Sat Feb 08 17:09:08.205310 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2361] PHP Stack trace:
[Sat Feb 08 17:09:08.205310 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2361] PHP   1. {main}() C:\\wamp\\www\\submit.php:0
[Sat Feb 08 17:09:08.205310 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2361] PHP Notice:  Undefined index: smc in C:\\wamp\\www\\submit.php on line 20
[Sat Feb 08 17:09:08.205310 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2361] PHP Stack trace:
[Sat Feb 08 17:09:08.205310 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2361] PHP   1. {main}() C:\\wamp\\www\\submit.php:0
[Sat Feb 08 17:09:26.842376 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2362] PHP Notice:  Undefined index: light in C:\\wamp\\www\\submit.php on line 19
[Sat Feb 08 17:09:26.842376 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2362] PHP Stack trace:
[Sat Feb 08 17:09:26.842376 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2362] PHP   1. {main}() C:\\wamp\\www\\submit.php:0
[Sat Feb 08 17:09:26.842376 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2362] PHP Notice:  Undefined index: smc in C:\\wamp\\www\\submit.php on line 20
[Sat Feb 08 17:09:26.842376 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2362] PHP Stack trace:
[Sat Feb 08 17:09:26.842376 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2362] PHP   1. {main}() C:\\wamp\\www\\submit.php:0
[Sat Feb 08 17:09:45.465441 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2363] PHP Notice:  Undefined index: light in C:\\wamp\\www\\submit.php on line 19
[Sat Feb 08 17:09:45.465441 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2363] PHP Stack trace:
[Sat Feb 08 17:09:45.465441 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2363] PHP   1. {main}() C:\\wamp\\www\\submit.php:0
[Sat Feb 08 17:09:45.465441 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2363] PHP Notice:  Undefined index: smc in C:\\wamp\\www\\submit.php on line 20
[Sat Feb 08 17:09:45.465441 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2363] PHP Stack trace:
[Sat Feb 08 17:09:45.465441 2014] [:error] [pid 2940:tid 1520] [client 192.168.0.101:2363] PHP   1. {main}() C:\\wamp\\www\\submit.php:0
[Sat Feb 08 17:29:05.760806 2014] [:error] [pid 2940:tid 1532] [client 192.168.0.101:2366] PHP Notice:  Undefined index: light in C:\\wamp\\www\\submit.php on line 19
[Sat Feb 08 17:29:05.760806 2014] [:error] [pid 2940:tid 1532] [client 192.168.0.101:2366] PHP Stack trace:


I'm using this code now:
/* 
*  DigiPlant arduino sketch
*  Rev 1.0  06/02/2014
*/

// Include required libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "DHT.h"
#include<stdlib.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// WiFi network settings
#define WLAN_SSID       "TP-LINK AP"
#define WLAN_PASS       "password"
#define WLAN_SECURITY   WLAN_SEC_WPA2

// DHT11 & I2C LCD configuration
#define DHTPIN 2 
#define DHTTYPE DHT11
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// Create CC3000 & DHT instances
DHT dht(DHTPIN, DHTTYPE);
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);
                                         
// Local server IP, port, and repository
uint32_t ip = cc3000.IP2U32(192,168,0,100);
int port = 80;
String repository = "/";

// Setup routine                                         
void setup(void)
{
 lcd.begin(16,2);
  // Initialize DHT sensor
  dht.begin();
  
  Serial.begin(115200);
  
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(F("Initialising..."));
       
  // Initialise the CC3000 module
  if (!cc3000.begin())
  {
    while(1);
  }

    lcd.setCursor(0,1);
    lcd.clear();
    lcd.print(F("Connecting..."));

  // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println("Connected to AP!");
    
  // Check DHCP
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  
  
  lcd.setCursor(0,1);
  lcd.clear();
    lcd.print(F("Connected!"));
  
}



void loop(void)
{
   
    // Measure the humidity & temperature
    float h = dht.readHumidity();
    float t = dht.readTemperature();
   
    // Print humidity & temperature on LCD display
    if (isnan(t) || isnan(h)) {
      lcd.setCursor(0, 0);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(F("No data!"));
    }
    else {
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(F("Current values:"));
    lcd.setCursor(0,1);
    lcd.print(t, 1);
    lcd.print(F("C"));
    lcd.print(F(" "));
    lcd.print(h, 1);
    lcd.print(F("%"));
     }
     
    // Transform to String
    String temperature = String((int) t);
    String humidity = String((int) h);
    
    // Print data
    Serial.print("Temperature: ");
    Serial.println(temperature);
    Serial.print("Humidity: ");
    Serial.println(humidity);
    Serial.println("");
    
    // Send request
    String request = "GET "+ repository + "submit.php?temp=" + temperature + "&hum=" + humidity + " HTTP/1.0";
    send_request(request);
    
    // Update every second
    delay(1000);
}

// Function to send a TCP request and get the result as a string
void send_request (String request) {
     
    // Connect
    Serial.println("Starting connection to server...");
    Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
    
    // Send request
    if (client.connected()) {
      client.println(request);
      client.println(F(""));
      Serial.println("Connected & Data sent");
    } 
    else {
      Serial.println(F("Connection failed"));
    }

    while (client.connected()) {
      while (client.available()) {

      // Read answer
      char c = client.read();
      }
    }
    Serial.println("Closing connection");
    Serial.println("");
    client.close();
    
}



What's interesting is that when it gets stuck the LCD display shows "Initialising..." which would suggest that the arduino has gone to the beginning of the setup fuction?

Hope this helps shed some light on the issue. It seems totally random when it cuts out. Some days it can go for ours, others it can do just 4 requests.

Thanks for your help
mh512
 
Posts: 38
Joined: Wed Nov 13, 2013 3:06 pm