Files
M5Stack/examples/Advanced/WIFI/BasicHttpClient/BasicHttpClient.ino
T
2021-08-05 17:38:56 +08:00

53 lines
2.1 KiB
Arduino
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Core sample source code
* 配套 M5Core 示例源代码
* Visit the website for more informationhttps://docs.m5stack.com/en/core/gray
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray
*
* describeBasicHTTPClient.
* date2021/8/4
******************************************************************************
*/
#include <M5Stack.h>
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
WiFiMulti wifiMulti;
HTTPClient http;
void setup() {
M5.begin(); //Init M5Core. 初始化 M5Core
M5.Power.begin(); //Init power 初始化电源模块
wifiMulti.addAP("M5-", "Of"); //Storage wifi configuration information. 存储wifi配置信息
M5.Lcd.print("\nConnecting Wifi...\n"); //print format output string on lcd. 串口格式化输出字符串
}
void loop() {
M5.Lcd.setCursor(0,0); //Set the cursor at (0,0). 设置光标位于(0,0)处
if((wifiMulti.run() == WL_CONNECTED)) { // wait for WiFi connection. 等待连接至wifi
M5.Lcd.print("[HTTP] begin...\n");
http.begin("http://example.com/index.html"); // configure traged server and url. 配置被跟踪的服务器和URL
M5.Lcd.print("[HTTP] GET...\n");
int httpCode = http.GET(); // start connection and send HTTP header. 开始连接服务器并发送HTTP的标头
if(httpCode > 0) { // httpCode will be negative on error. 出错时httpCode将为负值
M5.Lcd.printf("[HTTP] GET... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) { // file found at server. 在服务器上找到文件
String payload = http.getString();
M5.Lcd.println(payload); //打印在服务器上读取的文件. Print files read on the server
}
}else {
M5.Lcd.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}else{
M5.Lcd.print("connect failed");
}
delay(5000);
M5.Lcd.createSprite(); //clear the screen. 清除屏幕
}