gpt4 book ai didi

c++ - GET HTTP 请求 (Arduino)

转载 作者:行者123 更新时间:2023-11-28 06:37:56 25 4
gpt4 key购买 nike

我正在尝试从 Arduino 向我的服务器发送一些 http 请求。

我的代码可以上传一次,但是第一次上传后就断开了。

知道如何修改我的代码以便 Arduino 每 2-3 秒发送一次 http 请求吗?

这是我的代码:

#include <SPI.h>
#include <Ethernet.h>

int counter = 0;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(xxx, xx, xx, xx);
IPAddress ip(192, 168, 1, 1);
EthernetClient client;

void setup() { //connect to server
Serial.begin(9600);

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}

// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
connect();
}

void loop()
{

send(counter);
delay(1000);
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while(true);
}
counter++;
}

void connect(){
// if you get a connection, report back via serial:
client.connect(server, 80);
Serial.println("CONNECTED");
delay(1000);
}

void send(int value){
// Make a HTTP request:
client.print("GET /arduino.php?rom=D201");
client.print("&count=");
client.print(value);
client.println(" HTTP/1.0");
client.println("Host: 158.36.70.36");
client.println("Connection: close");
client.println();
Serial.print("Sending value");
Serial.println(value);
}

最佳答案

此 Web 客户端草图已经过测试并且可以完美运行。

#include <SPI.h>
#include <Ethernet.h>

// this must be unique
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip(192,168,2,2);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(74,125,227,16); // Google

//Change to your domain name for virtual servers
char serverName[] = "www.google.com";
// If no domain name, use the ip address above
// char serverName[] = "74.125.227.16";

// change to your server's port
int serverPort = 80;

EthernetClient client;
int totalCount = 0;
char pageAdd[64];

// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 30000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);

// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);

// Start ethernet
Serial.println(F("Starting ethernet..."));
Ethernet.begin(mac, ip, gateway, gateway, subnet);

// If using dhcp, comment out the line above
// and uncomment the next 2 lines

// if(!Ethernet.begin(mac)) Serial.println(F("failed"));
// else Serial.println(F("ok"));

Serial.println(Ethernet.localIP());

delay(2000);
Serial.println(F("Ready"));
}

void loop()
{
thisMillis = millis();

if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;

// Modify next line to load different page
// or pass values to server
sprintf(pageAdd,"/",totalCount);

// sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
else Serial.print(F("Pass "));
totalCount++;
Serial.println(totalCount,DEC);
}
}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
int inChar;
char outBuf[128];

Serial.print(F("connecting..."));

if(client.connect(ipBuf,thisPort) == 1)
{
Serial.println(F("connected"));

sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
client.println(F("Connection: close\r\n"));
}
else
{
Serial.println(F("failed"));
return 0;
}

// connectLoop controls the hardware fail timeout
int connectLoop = 0;

while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}

connectLoop++;

// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
// this is a delay for the connectLoop timing
delay(1);
}

Serial.println();

Serial.println(F("disconnecting."));
// close client end
client.stop();

return 1;
}

如果由于某种原因它一直挂起,您可以实现看门狗定时器机制。有关 WDT 的更多信息,请访问 ATmega328P Datasheet.我有一个来 self 的项目的例子:

void watchdogSetup(void) {
cli(); // disable all interrupts
wdt_reset(); // reset the WDT timer
/*
WDTCSR configuration:
WDIE = 1: Interrupt Enable
WDE = 1 :Reset Enable
WDP3 = 0 :For 2000ms Time-out
WDP2 = 1 :For 2000ms Time-out
WDP1 = 1 :For 2000ms Time-out
WDP0 = 1 :For 2000ms Time-out
*/
// Enter Watchdog Configuration mode:
WDTCSR |= (1<<WDCE) | (1<<WDE); //x |= y is the same as x = x | y
// Set Watchdog settings:
WDTCSR = (1<<WDIE) | (1<<WDE) | (0<<WDP3) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
sei();
}

ISR(WDT_vect){// Watchdog timer interrupt.
digitalWrite(ETHERNET_SHIELD_RESET_PIN, LOW);
}

关于c++ - GET HTTP 请求 (Arduino),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com