PHP POST 阿杜诺
PHP POST Arduino
谁能帮我解决这个问题。
这是我的arduino草图
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
// Default Pin GizDuino CC3000
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2);
//Router Specifics
#define WLAN_SSID "Cisco"
#define WLAN_PASS "nothingts"
//Router Security
#define WLAN_SECURITY WLAN_SEC_WPA2
//Web transactions on port 80
char server[] = "tmm.site50.net";
Adafruit_CC3000_Client client;
float tempPin = A1;
uint32_t ip;
void setup()
{
Serial.begin(115200);
Serial.println(F("Hello, Patient 1!\n"));
displayDriverMode();
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
/* Initialise CC3000 */
Serial.println(F("\nInitialising the CC3000 ..."));
if (!cc3000.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
/* Setting MAC Address Spoof */
uint8_t macAddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xB7 };
if (!cc3000.setMacAddress(macAddress))
{
Serial.println(F("Failed trying to update the MAC address"));
while(1);
}
/* End MAC Spoof */
uint16_t firmware = checkFirmwareVersion();
if ((firmware != 0x113) && (firmware != 0x118)) {
Serial.println(F("Wrong firmware version!"));
for(;;);
}
displayMACAddress();
/* Cleanup ng Past Connection */
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
/* Connecting to Wi-Fi Router */
char *ssid = WLAN_SSID;
Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode! */
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100);
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);
}
}
void loop()
{
uint32_t ip = 0;
Serial.print(F("tmm.site50.net -> "));
while (ip == 0)
{
if (! cc3000.getHostByName("tmm.site50.net", &ip))
{
Serial.println(F("Couldn't resolve!"));
while(1){}
}
}
cc3000.printIPdotsRev(ip);
Serial.println(F(""));
Serial.println(F("Posting!"));
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if (client.connected())
{
{
client.println("POST /sample.php HTTP/1.1");
Serial.println("POST /sample.php HTTP/1.1");
client.println("Host: tmm.site50.net");
Serial.println("Host: tmm.site50.net");
client.println("From: your@email.tld");
Serial.println("From: your@email.tld");
client.println("User-Agent: gizDuinodev1/1.0");
Serial.println("User-Agent: gizDuinodev1/1.0");
client.println("Content-Type: Application/x-www-form-urlencoded");
Serial.println("Content-Type: Application/x-www-form-urlencoded");
client.println("Content-Length: 7"); // notice not println to not get a linebreak
Serial.println("Content-Length: 7");
client.println(""); // to get the empty line
Serial.println("");
client.println("temp=50");
Serial.println("temp=50");
Serial.println("POSTED!");
delay (1000);
// While we're connected, print out anything the server sends:
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
}
Serial.println();
}
}
else // If the connection failed, print a message:
{
Serial.println(F("Connection failed"));
}
delay(5000);
}
/**************************************************************************/
/*!
@brief Displays the driver mode (tiny of normal), and the buffer
size if tiny mode is not being used
@note The buffer size and driver mode are defined in cc3000_common.h
*/
/**************************************************************************/
void displayDriverMode(void)
{
#ifdef CC3000_TINY_DRIVER
Serial.println(F("CC3000 is configure in 'Tiny' mode"));
#else
Serial.print(F("RX Buffer : "));
Serial.print(CC3000_RX_BUFFER_SIZE);
Serial.println(F(" bytes"));
Serial.print(F("TX Buffer : "));
Serial.print(CC3000_TX_BUFFER_SIZE);
Serial.println(F(" bytes"));
#endif
}
/**************************************************************************/
/*!
@brief Tries to read the CC3000's internal firmware patch ID
*/
/**************************************************************************/
uint16_t checkFirmwareVersion(void)
{
uint8_t major, minor;
uint16_t version;
#ifndef CC3000_TINY_DRIVER
if(!cc3000.getFirmwareVersion(&major, &minor))
{
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
version = 0;
}
else
{
Serial.print(F("Firmware V. : "));
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
version = major; version <<= 8; version |= minor;
}
#endif
return version;
}
/**************************************************************************/
/*!
@brief Tries to read the 6-byte MAC address of the CC3000 module
*/
/**************************************************************************/
void displayMACAddress(void)
{
uint8_t macAddress[6];
if(!cc3000.getMacAddress(macAddress))
{
Serial.println(F("Unable to retrieve MAC Address!\r\n"));
}
else
{
Serial.print(F("MAC Address : "));
cc3000.printHex((byte*)&macAddress, 6);
}
}
/**************************************************************************/
/*!
@brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
这里是 php
http://tmm.site50.net/sample.php
<html>
<body>
<label>Temperature:</label>
<input type="text" id="temp" name="temp" value="" />
</br>
<label>Device:</label>
<input type="text" id="module" name="module" value="" />
</body>
</html>
问题是,当设备发送..似乎有一个坏的header..
在 arduino 串行监视器中它给了我这个回复。
tmm.site50.net -> 31.170.162.243
Posting!
POST /sample.php HTTP/1.1
Host: 31.170.162.243
From: your@email.tld
User-Agent: gizDuinodev1/1.0
Content-Type: Application/x-www-form-urlencoded
Content-Length: 7
temp=50
POSTED!
HTTP/1.1 200 OK
Date: Fri, 30 Jan 2015 00:51:38 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 351
Connection: close
Content-Type: text/html
<html>
<body>
<label>Temperature:</label>
<input type="text" id="temp" name="temp" value="" />
</br>
<label>Device:</label>
<input type="text" id="module" name="module" value="" />
</body>
</html>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
这是服务器回复我的内容
HTTP 请求
格式正确
正确的 HTTP POST 请求看起来像这样:
POST /url HTTP/1.1\r\n
Host example.com\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 7\r\n
\r\n
foo=bar
这将发送 bar
作为 foo
的值。在 GET 请求中,您可以将其添加到 URL (/url/?foo=bar
),但 POST 请求不能像这样工作:您必须添加消息正文。
Content-Length 应该是正文中的字符数(这里:foo=bar
,所以 7 个字符)。 Content-Type 和 Content-Length 字段是执行您想要的操作所必需的。主机字段在任何 HTTP/1.1 请求中都是必需的。
请注意,换行符是 \r\n
。幸运的是,Arduino's println() 添加了回车符 return \r
和换行符 \n
。但是,您应该确保不要使用 print() ,除非您想在同一行上继续。所以这是一个工作示例:
Arduino 工作示例
client.println("POST /sample.php HTTP/1.1");
client.println("Host: tmm.site50.net");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: 7");
client.println("");
client.println("temp=50");
这将发送 50
作为温度。
PHP后端
将数据存储在文件中
那么您可能希望将发送的数据保存在服务器上。为此,您从 $_POST
超全局获取信息,并将其保存在文件中:
<?php
$handle = fopen('temp.log', 'w');
fwrite($handle, date('d/m/Y H:i:s') . " - " . $_POST['temp']) . "\n";
fclose($handle);
?>
这将以写入模式打开 temp.log(请参阅 fopen()), then writes a timestamp and the current value to that file (see fwrite()), and closes the handle (see fclose())。该文件看起来像这样:
30/01/2015 09:12:40 - 50
您只需转到 http://tmm.site50.net/temp.log 即可查看文件。
日志记录
每次Arduino发送一个请求,旧的结果都会被覆盖。或者,您可能想要 log 数据,并添加新行而不是覆盖旧数据。然后,在 fopen() 中使用 a
(附加)模式而不是 w
(写入):
$handle = fopen('temp.log', 'a');
这将自动在文件末尾追加新行。
权限
根据您服务器上的权限,您可能需要先创建 temp.log,然后 PHP 才能写入。
谁能帮我解决这个问题。
这是我的arduino草图
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
// Default Pin GizDuino CC3000
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2);
//Router Specifics
#define WLAN_SSID "Cisco"
#define WLAN_PASS "nothingts"
//Router Security
#define WLAN_SECURITY WLAN_SEC_WPA2
//Web transactions on port 80
char server[] = "tmm.site50.net";
Adafruit_CC3000_Client client;
float tempPin = A1;
uint32_t ip;
void setup()
{
Serial.begin(115200);
Serial.println(F("Hello, Patient 1!\n"));
displayDriverMode();
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
/* Initialise CC3000 */
Serial.println(F("\nInitialising the CC3000 ..."));
if (!cc3000.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
/* Setting MAC Address Spoof */
uint8_t macAddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xB7 };
if (!cc3000.setMacAddress(macAddress))
{
Serial.println(F("Failed trying to update the MAC address"));
while(1);
}
/* End MAC Spoof */
uint16_t firmware = checkFirmwareVersion();
if ((firmware != 0x113) && (firmware != 0x118)) {
Serial.println(F("Wrong firmware version!"));
for(;;);
}
displayMACAddress();
/* Cleanup ng Past Connection */
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
/* Connecting to Wi-Fi Router */
char *ssid = WLAN_SSID;
Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode! */
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100);
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);
}
}
void loop()
{
uint32_t ip = 0;
Serial.print(F("tmm.site50.net -> "));
while (ip == 0)
{
if (! cc3000.getHostByName("tmm.site50.net", &ip))
{
Serial.println(F("Couldn't resolve!"));
while(1){}
}
}
cc3000.printIPdotsRev(ip);
Serial.println(F(""));
Serial.println(F("Posting!"));
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if (client.connected())
{
{
client.println("POST /sample.php HTTP/1.1");
Serial.println("POST /sample.php HTTP/1.1");
client.println("Host: tmm.site50.net");
Serial.println("Host: tmm.site50.net");
client.println("From: your@email.tld");
Serial.println("From: your@email.tld");
client.println("User-Agent: gizDuinodev1/1.0");
Serial.println("User-Agent: gizDuinodev1/1.0");
client.println("Content-Type: Application/x-www-form-urlencoded");
Serial.println("Content-Type: Application/x-www-form-urlencoded");
client.println("Content-Length: 7"); // notice not println to not get a linebreak
Serial.println("Content-Length: 7");
client.println(""); // to get the empty line
Serial.println("");
client.println("temp=50");
Serial.println("temp=50");
Serial.println("POSTED!");
delay (1000);
// While we're connected, print out anything the server sends:
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
}
Serial.println();
}
}
else // If the connection failed, print a message:
{
Serial.println(F("Connection failed"));
}
delay(5000);
}
/**************************************************************************/
/*!
@brief Displays the driver mode (tiny of normal), and the buffer
size if tiny mode is not being used
@note The buffer size and driver mode are defined in cc3000_common.h
*/
/**************************************************************************/
void displayDriverMode(void)
{
#ifdef CC3000_TINY_DRIVER
Serial.println(F("CC3000 is configure in 'Tiny' mode"));
#else
Serial.print(F("RX Buffer : "));
Serial.print(CC3000_RX_BUFFER_SIZE);
Serial.println(F(" bytes"));
Serial.print(F("TX Buffer : "));
Serial.print(CC3000_TX_BUFFER_SIZE);
Serial.println(F(" bytes"));
#endif
}
/**************************************************************************/
/*!
@brief Tries to read the CC3000's internal firmware patch ID
*/
/**************************************************************************/
uint16_t checkFirmwareVersion(void)
{
uint8_t major, minor;
uint16_t version;
#ifndef CC3000_TINY_DRIVER
if(!cc3000.getFirmwareVersion(&major, &minor))
{
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
version = 0;
}
else
{
Serial.print(F("Firmware V. : "));
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
version = major; version <<= 8; version |= minor;
}
#endif
return version;
}
/**************************************************************************/
/*!
@brief Tries to read the 6-byte MAC address of the CC3000 module
*/
/**************************************************************************/
void displayMACAddress(void)
{
uint8_t macAddress[6];
if(!cc3000.getMacAddress(macAddress))
{
Serial.println(F("Unable to retrieve MAC Address!\r\n"));
}
else
{
Serial.print(F("MAC Address : "));
cc3000.printHex((byte*)&macAddress, 6);
}
}
/**************************************************************************/
/*!
@brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
这里是 php
http://tmm.site50.net/sample.php
<html>
<body>
<label>Temperature:</label>
<input type="text" id="temp" name="temp" value="" />
</br>
<label>Device:</label>
<input type="text" id="module" name="module" value="" />
</body>
</html>
问题是,当设备发送..似乎有一个坏的header..
在 arduino 串行监视器中它给了我这个回复。
tmm.site50.net -> 31.170.162.243
Posting!
POST /sample.php HTTP/1.1
Host: 31.170.162.243
From: your@email.tld
User-Agent: gizDuinodev1/1.0
Content-Type: Application/x-www-form-urlencoded
Content-Length: 7
temp=50
POSTED!
HTTP/1.1 200 OK
Date: Fri, 30 Jan 2015 00:51:38 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 351
Connection: close
Content-Type: text/html
<html>
<body>
<label>Temperature:</label>
<input type="text" id="temp" name="temp" value="" />
</br>
<label>Device:</label>
<input type="text" id="module" name="module" value="" />
</body>
</html>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
这是服务器回复我的内容
HTTP 请求
格式正确
正确的 HTTP POST 请求看起来像这样:
POST /url HTTP/1.1\r\n
Host example.com\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 7\r\n
\r\n
foo=bar
这将发送 bar
作为 foo
的值。在 GET 请求中,您可以将其添加到 URL (/url/?foo=bar
),但 POST 请求不能像这样工作:您必须添加消息正文。
Content-Length 应该是正文中的字符数(这里:foo=bar
,所以 7 个字符)。 Content-Type 和 Content-Length 字段是执行您想要的操作所必需的。主机字段在任何 HTTP/1.1 请求中都是必需的。
请注意,换行符是 \r\n
。幸运的是,Arduino's println() 添加了回车符 return \r
和换行符 \n
。但是,您应该确保不要使用 print() ,除非您想在同一行上继续。所以这是一个工作示例:
Arduino 工作示例
client.println("POST /sample.php HTTP/1.1");
client.println("Host: tmm.site50.net");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: 7");
client.println("");
client.println("temp=50");
这将发送 50
作为温度。
PHP后端
将数据存储在文件中
那么您可能希望将发送的数据保存在服务器上。为此,您从 $_POST
超全局获取信息,并将其保存在文件中:
<?php
$handle = fopen('temp.log', 'w');
fwrite($handle, date('d/m/Y H:i:s') . " - " . $_POST['temp']) . "\n";
fclose($handle);
?>
这将以写入模式打开 temp.log(请参阅 fopen()), then writes a timestamp and the current value to that file (see fwrite()), and closes the handle (see fclose())。该文件看起来像这样:
30/01/2015 09:12:40 - 50
您只需转到 http://tmm.site50.net/temp.log 即可查看文件。
日志记录
每次Arduino发送一个请求,旧的结果都会被覆盖。或者,您可能想要 log 数据,并添加新行而不是覆盖旧数据。然后,在 fopen() 中使用 a
(附加)模式而不是 w
(写入):
$handle = fopen('temp.log', 'a');
这将自动在文件末尾追加新行。
权限
根据您服务器上的权限,您可能需要先创建 temp.log,然后 PHP 才能写入。