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