|
入手一个 ESP8266 12F 模块,正在学习当中,官网的资料太散乱了,入门难;
发现用 Arduoni 烧录挺简单的,
现在想用定时器定时调用 httpClient函数 连接云端 api 获取数据,这一部分一直连不上:
但是我把 httpClient 函数放到 loop 主循环里面就连接正常,
因为loop 主循环函数里面我还想放webServer来监听局域网的连接, 而httpClient 里面延时的时候就监听不到了,各位大神指点一下,谢谢,代码如下:
#include "userConfig.h" //其它头文件都在 userConfig.h里面
const char* ssid = "******"; //用星头代替
const char* password = "*****"; //用星头代替
const char* host = "esp8266fs";
const int led = 13;
const char* hostName = "iot.espressif.cn";
HTTPClient http;
ESP8266WiFiMulti WiFiMulti;
Ticker flipper; //定时器
void httpClientGet()
{
Serial.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("http://iot.espressif.cn/v1/ping/"); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(10000);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 1);
Serial.begin(74880);
Serial.print("\n");
//WIFI INIT
Serial.printf("Connecting to %s\n", ssid);
if (String(WiFi.SSID()) != String(ssid)) {
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(ssid, password);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
//flipper.attach_ms(8000,httpClientGet);
//我是想用定时器每8秒连接一次,每次都连不上,如下错误反馈
/*[HTTP] begin...
[HTTP] GET...
[HTTP] GET... failed, error: connection refused
*/
}
void loop(void) {
httpClientGet(); //如果到这里调用函数就连接正常,如下反馈
/*
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200
{"datetime": "2018-02-28 20:28:32", "epoch": 1519820912, "message": "ping success", "status": 200}
*/
}
|
|