【发布时间】:2016-02-03 19:11:32
【问题描述】:
我创建了一个 PHP 脚本,因此我可以从我的在线数据库 (MySQL) 中检索数据,但我无法将该信息传递给我的 arduino+Ethernet Shield(使用 Ethercard.h)。
我想要的是将我的 PHP 脚本检索到的所有内容作为字符串向量。
我的 PHP 脚本返回的示例:
ABCDF
GHYEJ
JDYDI
HSTSU
PIFYF
我希望它在我的 Arduino 中变成这样:
char* test = {"ABCDF", "GHYEJ". "JDYDI", "HSTSU", "PIFYF" };
有人可以帮我吗?
编辑: 所以,我正在尝试@frarugi87 解决方案,但我的 DNS 失败。这是我正在使用的代码:
#include <enc28j60.h>
#include <EtherCard.h>
#include <net.h>
#define HTTP_HEADER_OFFSET 0
/* Setup for Ethernet Library */
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = {192,168,1,58};
static byte gatewayip[] = {192,168,1,1};
static byte dnsip[] = {8,8,8,8}; //Google DNS
const char website[] PROGMEM = "http://arksecurity.net16.net";
byte Ethernet::buffer[700];
static uint32_t timer = 0;
static void response_callback (byte status, word off, word len){
Serial.print((const char*) Ethernet::buffer + off + HTTP_HEADER_OFFSET);
}
void setup() {
Serial.begin(57600);
if (ether.begin(sizeof Ethernet::buffer, mymac, 8) == 0)
Serial.println("Failed to access Ethernet controller");
else
Serial.println("Ethernet controller initialized");
ether.staticSetup(myip, gatewayip, dnsip);
if(!ether.dnsLookup(website)){
Serial.println("DNS failed");
while(1);
}
else
Serial.println("DNS resolution done");
ether.printIp("SRV IP:\t", ether.hisip);
Serial.println();
}
void loop() {
word pos = ether.packetLoop(ether.packetReceive());
if (millis() > timer){
timer = millis() + 5000;
ether.browseUrl("/", "test.php", website, response_callback);
}
}
我在这里做错了吗?
Edit2:好的,我让它运行起来了。 DNS 问题是由我的路由器引起的,所以我必须将其设置为 192.168.1.1,并且我还必须对 ether.browseUrl 进行一些调整,因为它也返回错误。 这就是我的代码现在的样子:
#include <enc28j60.h>
#include <EtherCard.h>
#include <net.h>
#define HTTP_HEADER_OFFSET 50
/* Setup for Ethernet Library */
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = {192,168,1,58};
static byte gatewayip[] = {192,168,1,1};
static byte dnsip[] = {192,168,1,1}; //Google DNS
const char website[] PROGMEM = "www.arksecurity.net16.net";
byte Ethernet::buffer[700];
static uint32_t timer = 0;
static void response_callback (byte status, word off, word len){
Serial.print((const char*) Ethernet::buffer + off + HTTP_HEADER_OFFSET);
}
void setup() {
Serial.begin(57600);
if (ether.begin(sizeof Ethernet::buffer, mymac, 8) == 0)
Serial.println("Failed to access Ethernet controller");
else
Serial.println("Ethernet controller initialized");
ether.staticSetup(myip, gatewayip, dnsip);
if(!ether.dnsLookup(website)){
Serial.println("DNS failed");
while(1);
}
else
Serial.println("DNS resolution done");
ether.printIp("SRV IP:\t", ether.hisip);
Serial.println();
}
void loop() {
word pos = ether.packetLoop(ether.packetReceive());
if (millis() > timer){
timer = millis() + 5000;
ether.browseUrl(PSTR("/test."),"php", website, response_callback);
}
}
现在我必须将获得的数据保存到 char* [] 中。有什么想法吗?
编辑3:
这就是我的代码到目前为止的样子。问题是,在最后一次比较中,我做了“for(int i=0; i
之后,我想让它每天只调用一次 response_callback,比如在午夜。有可能吗?
#define HTTP_HEADER_OFFSET 163
#define MAX_STRINGS 20
#define MAX_STRING_LENGTH 8
#define REQUEST_EVERY_X_MS 5000
/* Setup for Ethernet Library */
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
const char website[] PROGMEM = "www.arksecurity.net16.net";
const char device[] = "0001";
char test[MAX_STRINGS][MAX_STRING_LENGTH+1];
String test2 = "1234";
char * comp[20];
uint8_t receivedResponse;
unsigned long timer;
byte Ethernet::buffer[700];
static void response_callback (byte status, word off, word len) {
int i_string = 0;
int i_char = 0;
int i_ethBuff = off + HTTP_HEADER_OFFSET;
char carat;
for (i_ethBuff = off + HTTP_HEADER_OFFSET; (carat =(char)Ethernet::buffer[i_ethBuff]) != 0; i_ethBuff++)
{
if (carat == '\n')
{ // New line char = new string
if (i_string < MAX_STRINGS - 1){
i_string++;
i_char = 0;
}
else
break;
}
else
{
if (i_char < MAX_STRING_LENGTH)
{
test[i_string][i_char] = carat;
i_char++;
} // otherwise discard the char (max length of string reached)
}
}
receivedResponse = 1;
}
void setup() {
if (ether.begin(sizeof Ethernet::buffer, mymac, 8) == 0)
{
Serial.println("Failed to access Ethernet controller");
while(1);
}
else
Serial.println("Ethernet controller initialized");
Serial.println();
if (!ether.dhcpSetup())
{
Serial.println("Failed to get configuration from DHCP");
while(1);
}
else
Serial.println("DHCP configuration done");
if (!ether.dnsLookup(website))
{
Serial.println("DNS failed");
while(1);
}
else
Serial.println("DNS resolution done");
ether.printIp("SRV IP:\t", ether.hisip);
Serial.println();
timer = millis() - REQUEST_EVERY_X_MS;
}
void loop() {
word pos = ether.packetLoop(ether.packetReceive());
if (millis() - timer > REQUEST_EVERY_X_MS){
timer += REQUEST_EVERY_X_MS;
receivedResponse = 0;
ether.browseUrl(PSTR("/DevicesQuery.php?device="),device , website, response_callback);
while (!receivedResponse);
for(int i=0; i < 20 - 1; i++){
comp[i] = test[i];
if(test2.equals(comp[i])){
Serial.println("OK");
}
else
Serial.println("Wrong");
}
}
}
Edit4:使它与另一个比较一起工作。我希望它每 5 秒捕获一次数据,直到他收到任何东西并且他们让它等待很长时间。问题是当我让 timer2 = 500000;它实际上使它更快地捕获数据。这是为什么呢?
#include <enc28j60.h>
#include <EtherCard.h>
#include <net.h>
#define HTTP_HEADER_OFFSET 163
#define MAX_STRINGS 10
#define MAX_STRING_LENGTH 20
#define REQUEST_EVERY_X_MS 5000
/* Setup for Ethernet Library */
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
const char website[] PROGMEM = "www.arksecurity.net16.net"; //link para o banco de dados do usuário em questão
const char device[] = "0001"; // adicionar aqui o número do dispositivo equivalente ao que aparece no Bando de Dados
char test[MAX_STRINGS][MAX_STRING_LENGTH+1];
String test2 = "9999";
//uint8_t receivedResponse;
unsigned long timer;
unsigned long timer2 = 5000;
byte Ethernet::buffer[700];
static void response_callback (byte status, word off, word len) {
for(int i=0;i<MAX_STRINGS;i++)
for(int j=0;j<=MAX_STRING_LENGTH;j++)
test[i][j] = 0;
int i_string = 0;
int i_char = 0;
int i_ethBuff = off + HTTP_HEADER_OFFSET;
char carat;
for (i_ethBuff = off + HTTP_HEADER_OFFSET; (carat = (char)Ethernet::buffer[i_ethBuff]) != 0; i_ethBuff++)
{
if (carat == '\n')
{ // New line char = new string
if (i_string < MAX_STRINGS - 1){
i_string++;
i_char = 0;
}
else
break; // Limite de memória do Arduino
}
else
{
if (i_char < MAX_STRING_LENGTH)
{
test[i_string][i_char] = carat;
i_char++;
} // otherwise discard the char (max length of string reached)
}
}
//receivedResponse[0] = 1;
}
void setup() {
Serial.begin(57600);
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
{
Serial.println("Failed to access Ethernet controller");
while(1);
}
else
Serial.println("Ethernet controller initialized");
Serial.println();
if (!ether.dhcpSetup())
{
Serial.println("Failed to get configuration from DHCP");
while(1);
}
else
Serial.println("DHCP configuration done");
if (!ether.dnsLookup(website))
{
Serial.println("DNS failed");
while(1);
}
else
Serial.println("DNS resolution done");
ether.printIp("SRV IP:\t", ether.hisip);
Serial.println();
timer = millis() - timer2;
}
void loop() {
word pos = ether.packetLoop(ether.packetReceive());
if (millis() - timer > timer2)
{
ether.browseUrl(PSTR("/DevicesQuery.php?device="),device , website, response_callback);
if(test[0][0] != 0){
Serial.println("Data Received");
Serial.println(test[0]);
Serial.println(test[1]);
Serial.println(test[2]);
Serial.println(test[3]);
Serial.println(test[4]);
Serial.println(test[5]);
timer2 = 500000;
}
else{
Serial.println("Nothing");
timer2 = 5000;
}
timer += timer2;
}
Edit5:我将这部分添加到我的代码中,因此我也可以将数据发送到我的服务器,但由于某种原因,我只发送了一个数据,即使我正在使用循环也很难。为什么会这样?
代码:
for(int i = 0; i < countRegister ; i++)
{
register[i].toCharArray(tempRegister, 80);
ether.browserUrl(PTSR("/log.php"), tempRegister , browser_callback);
}
static void browser_callback (byte status, word off, word len)
{
Serial.println("Data sent");
}
【问题讨论】:
-
所以您的 arduino 正在向您的 php 脚本所在的任何位置发出 http 请求?
-
我什至不知道如何用这个 EtherCard.h 请求 http 我是 Arduino 新手,我找到的所有示例都是基于 Ethernet.h 我已经将它连接到互联网,我ping 了,我可以将它用作 WebServer。
标签: php arduino arduino-uno