I am working on an Arduino UNO project with a CC3000 WiFi screen.
I have come to the conclusion that, without any problems, I can send to a locally hosted web server (verified using MAMP), but I cannot find the correct path to send to a remote server. tried two different approaches (see code below), but none of them seem to publish correctly in the web service and database.
I am wondering if I need to use a different type of connection or if there is an error with the request GET.
This is the Arduino sketch code. Then the PHP file sensor.phpreceives occupied=VALUEand then transfers it to the connected MySQL database. The PHP file works when loading the path directly, so I'm sure this is an Arduino problem, not a server side problem.
Thanks in advance!
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <elapsedMillis.h>
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
#define WLAN_SSID "NAME_OF_WIFI"
#define WLAN_PASS "XXXXXXXXX"
#define WLAN_SECURITY WLAN_SEC_WPA2
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2);
uint32_t ip = cc3000.IP2U32(XXX,XXX,X,XX);
int port = 8888;
String repository = "hello/";
int ledPin = 8;
int ledPinSecond = 7;
int inputPin = 2;
int pirState = LOW;
int val = 0;
String occupied;
void setup(void)
{
Serial.begin(115200);
Serial.println("Wroom wroom start it up!");
pinMode(ledPin, OUTPUT);
pinMode(ledPinSecond, OUTPUT);
pinMode(inputPin, INPUT);
if (!cc3000.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
char *ssid = WLAN_SSID;
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);
}
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
Serial.println(F("DHCP requested - starting the void"));
delay(3000);
}
}
void loop(void)
{
if(!cc3000.checkConnected()){while(1){}}
val = digitalRead(inputPin);
Serial.print(F("Starting to detect motion"));
if (val == HIGH) {
digitalWrite(ledPin, LOW);
digitalWrite(ledPinSecond, HIGH);
occupied = "Occupied";
delay(150);
if (pirState == LOW) {
Serial.println(F("Motion detected!"));
pirState = HIGH;
}
} else {
digitalWrite(ledPin, HIGH);
digitalWrite(ledPinSecond, LOW);
occupied = "Free";
delay(150);
if (pirState == HIGH){
Serial.println(F("Motion ended!"));
pirState = LOW;
}
}
Serial.print(F("Room state: "));
Serial.println(occupied);
String request = "GET www.webservice.com/"+ repository + "sensor.php?occupied=" + occupied + " HTTP/1.0\n";
Serial.println(request);
send_request(request);
delay(1000);
}
void send_request (String request) {
Serial.println("Starting connection to server...");
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
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()) {
char c = client.read();
}
}
Serial.println("Closing connection");
Serial.println("");
client.close();
}
And here is the magazine
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