4

I'm working on an Arduino UNO project with a CC3000 WiFi shield.

I've come to the point where I, without a problem can post to a locally hosted webserver (tested with MAMP), but I can't seem to find the right way to post to a remote server - I've tried two different approaches (see code below), but none of them seem to correctly post to the web service and database.

I'm wondering whether I need to use a different type of connection, or if there's an error with the GET request.

This is the Arduino Sketch code. The PHP file sensor.php then receives the occupied=VALUE and then passes it to the connected MySQL database. The PHP file works when loading the path directly, so I'm quite sure it is an Arduino problem and not a server-side problem.

Thanks in advance!

// Include required libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <elapsedMillis.h>

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

#define WLAN_SSID       "NAME_OF_WIFI"        // cannot be longer than 32 characters!
#define WLAN_PASS       "XXXXXXXXX"
#define WLAN_SECURITY   WLAN_SEC_WPA2

//#define WEBSITE "http://www.webservice.com"
//#define SENSORPATH "/hello/sensor.php?"

// Create CC3000 & DHT instances
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(XXX,XXX,X,XX);

int port = 8888;
String repository = "hello/";

int ledPin = 8;   // choose the pin for the LED
int ledPinSecond = 7;  
int inputPin = 2;              // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;   // variable for reading the pin status
String occupied;         

void setup(void)
{
  Serial.begin(115200);
  Serial.println("Wroom wroom start it up!");

  pinMode(ledPin, OUTPUT);    // declare LED as output
  pinMode(ledPinSecond, OUTPUT);    
  pinMode(inputPin, INPUT);     // declare sensor as input 

  // Initialise the CC3000 module
  if (!cc3000.begin())
  {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while(1);
  }

  // Connect to  WiFi network
  char *ssid = WLAN_SSID;             /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println("Connected to WiFi network!");

   if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
   Serial.println(F("Failed to connect to WiFi"));
   while(1);
  }  

  // Check DHCP
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
     Serial.println(F("DHCP requested - starting the void"));
    delay(3000);
  }  

}
void loop(void)
{
  //Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
 // if (www.connected()) {

  //  www.println(F("GET "));
  //  www.println(SENSORPATH);
   // www.println(F(" HTTP/1.0\r\n"));
   // www.println(F("Host: ")); 
   // www.println(WEBSITE); 
   // www.println(F("\n"));
   // www.println(F("Connection: close\n"));
   // www.println(F("\n"));
   // www.println();

   // Serial.println(F("Inserted data\n"));

  //} else {
  //  Serial.println(F("Testing: Connection failed"));    
  //  return;
  //}

    // Check connection, reset if connection is lost
   if(!cc3000.checkConnected()){while(1){}}

   val = digitalRead(inputPin);  // read input value
   Serial.print(F("Starting to detect motion"));
   //Detect motion 
    if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, LOW);  // turn LED ON
    digitalWrite(ledPinSecond, HIGH);
    occupied = "Occupied";
    delay(150);

    if (pirState == LOW) {
      // we have just turned on
      Serial.println(F("Motion detected!"));
      // We only want to print on the output change, not state
      pirState = HIGH;
       } 
     } else {
      digitalWrite(ledPin, HIGH); // turn LED OFF
      digitalWrite(ledPinSecond, LOW);
      occupied = "Free";
      delay(150);    

      if (pirState == HIGH){
      // we have just turned off
      Serial.println(F("Motion ended!"));
      // We only want to print on the output change, not state
      pirState = LOW;
      }  
    }

    Serial.print(F("Room state: "));
    Serial.println(occupied);
    // Send request
    String request = "GET www.webservice.com/"+ repository + "sensor.php?occupied=" + occupied + " HTTP/1.0\n";
    Serial.println(request);
    send_request(request);

    delay(1000);
    // Update every second

}

// 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();
}

And here's a log

Wroom wroom start it up!

Attempting to connect to WiFi
Connected to WiFi network!
Request DHCP
DHCP requested - starting the void
Starting to detect motionRoom state: Free
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0

Starting connection to server...
Connected & Data sent
Closing connection

Starting to detect motionRoom state: Free
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0

Starting connection to server...
Connected & Data sent
Closing connection

Starting to detect motionRoom state: Free
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0

Starting connection to server...
Connected & Data sent
Closing connection

Starting to detect motionRoom state: Free
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0

Starting connection to server...
Connected & Data sent
Closing connection

Starting to detect motion
Room state: Free
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0

Starting connection to server...
Connected & Data sent
Closing connection

Starting to detect motion
Motion detected!
Room state: Occupied
GET www.webservice.com/hello/sensor.php?occupied=Occupied HTTP/1.0

Starting connection to server...
Connected & Data sent
Closing connection

Starting to detect motion
Room state: Occupied
GET www.webservice.com/hello/sensor.php?occupied=Occupied HTTP/1.0

Starting connection to server...
Connected & Data sent
Closing connection
Thomas
  • 5,110
  • 1
  • 16
  • 17
  • Do you have any logs so we can see? – Namphibian Oct 07 '14 at 00:13
  • @Namphibian Sure. Sorry for not including in the first place. Here goes. `Attempting to connect to WiFi Connected to WiFi network! Request DHCP DHCP requested Starting to detect motion Room state: Free GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0 Starting connection to server... Connected & Data sent Closing connection` As you can see the Arduino loops the script just as it is supposed to, though the GET is never actually posted to the web service - meaning that it never reaches it. – Thomas Oct 07 '14 at 01:03
  • 1
    Check your access log on server – Styx Oct 07 '14 at 17:51
  • In your loop `while (client.connected()) { while (client.available()) { // Read answer char c = client.read(); } }` you are not logging anything suggesting it is definitely a connection problem with the arduino. – Namphibian Oct 07 '14 at 23:10
  • I dont know the library well but it seems that you are missing the `sendData(client,data,buffer_size);` command in your sketch. See this link: https://learn.adafruit.com/downloads/pdf/adafruit-cc3000-wifi-and-xively.pdf. Thus while you are connecting it does not appears that you are actually sending data. – Namphibian Oct 07 '14 at 23:14
  • @Namphibian that is unfortunately only related to the Xively library; you're correct re the client.read(); it does not output anything. I've now gotten the script to actually post to the web service, but now have problems keeping the script running. See http://stackoverflow.com/questions/26238307/arduino-uno-cc3000-whileclient-connected – Thomas Oct 07 '14 at 23:32
  • Take a look here: http://forums.adafruit.com/viewtopic.php?f=31&p=277649 – VeeeneX Feb 07 '15 at 11:44
  • Is the website you're trying to hit the default website? (i.e. if you access the server with just an IP, do you get the proper site displayed?) Perhaps you should add the HTTP Host header to the arduino request so your web server can correctly route the request to the proper app/php file. – chugadie Feb 07 '15 at 23:01

1 Answers1

0

Maybe your server doesn't like HTTP 1.0. Try using HTTP/1.1 with header Connection: Closed, if you just want to send the request and don't care about server's response.

GET /hello/sensor.php?occupied=Free HTTP/1.1
Host: www.webservice.com
Connection: closed

Otherwise, you should use Connection: keep-alive so request actually wait for server response and then you'd be able to read it using this part of your snippet

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

  // Read answer
  char c = client.read();
  }
}
// ...

P.S.: Consider checking yourself if the url can be accesed. Just to be sure that is an Arduino issue.

josegomezr
  • 888
  • 4
  • 15
  • Please also try explaining why this answer would work. I would hate to see a valid answer get flagged as low quality! – ficuscr Feb 13 '15 at 21:01