我正在使用CC3000 WiFi盾牌开展Arduino UNO项目。
我已经到了这样的地步,我没有问题可以发布到本地托管的网络服务器(使用MAMP测试),但我似乎无法找到发布到远程服务器的正确方法 - 我尝试了两种不同的方法(请参阅下面的代码),但它们似乎都没有正确地发布到网络服务和数据库。
我想知道是否需要使用其他类型的连接,或者GET
请求是否有错误。
这是Arduino Sketch代码。然后PHP文件sensor.php
接收occupied=VALUE
,然后将其传递给连接的MySQL数据库。 PHP文件在直接加载路径时有效,所以我非常确定这是一个Arduino问题,而不是服务器端问题。
提前致谢!
// 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();
}
这是一个日志
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
答案 0 :(得分:0)
也许您的服务器不喜欢HTTP 1.0。尝试使用带有标题的HTTP / 1.1连接:已关闭,如果您只想发送请求而不关心服务器的响应。
GET /hello/sensor.php?occupied=Free HTTP/1.1
Host: www.webservice.com
Connection: closed
否则,您应该使用Connection: keep-alive
,因此请求实际上等待服务器响应,然后您就可以使用代码段的这一部分来阅读
// ...
while (client.connected()) {
while (client.available()) {
// Read answer
char c = client.read();
}
}
// ...
P.S。:考虑自己是否可以加入网址。只是为了确定这是一个Arduino问题。