本程序适用于局域网,采用最简单的HTTP协议。
每个地址都不一样,使用前用netstat -an看本地地址,填入host中。
ssid WIFI 密码
password wifi密码
http地址:
String postRequest = (String)(“GET “)+“userupdate?device_id=3&wendu=35&shidu=50”+” HTTP/1.1\r\n”+
“Content-Type: text/html;charset=utf-8\r\n”+
"Host: "+host + “\r\n”+
“User-Agent: BuildFailureDetectorESP8266\r\n”+
“Connection: Keep-Alive\r\n\r\n”;
//https://www.arduino.cn/thread-92176-1-1.html
程序开始:
//https://www.arduino.cn/thread-92176-1-1.html
#include <ESP8266WiFi.h>
const char* ssid="…";
const char* password = “…”;
const char* host=“192.168.2.218”;
const int port=5000;
const char* url=“http://se.360.cn/cc/fwl_11.dat”;
char buffer[1024];
#define BASE_URL “GET %s HTTP/1.1\r\nHost:%s\r\nAccept-Language:zh-cn\r\nConnection:keep-alive\r\n\r\n”
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println();
Serial.print(“connecting to “);
Serial.println(ssid);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(”.”);
}
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
sprintf(buffer,BASE_URL,”/v3/weather/now.json?key=smtq3n0ixdggurox&location=beijing&language=zh-Hans&unit=c”,“api.seniverse.com”);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
if(!client.connect(host,5000)){
Serial.println(“connection failed”);
return;
}
delay(10);
String postRequest = (String)("GET ")+"userupdate?device_id=3&wendu=35&shidu=50"+" HTTP/1.1\r\n"+
//"Content-Type: text/html;charset=utf-8\r\n"+
"Host: "+host + "\r\n"+
//"User-Agent: BuildFailureDetectorESP8266\r\n"+
//"Connection: Keep-Alive\r\n\r\n";
Serial.println(buffer);
//client.print(newurl);
//读取天气
client.print(postRequest);
String line = client.readStringUntil('\n');
while(line.length()!=0){
Serial.println(line);
line=client.readStringUntil('\n');
}
Serial.println(line);
client.stop();
delay(3000);
}