1.注册thingspeak
新建2.填写添加信息
https://www.worldometers.info/coronavirus/
3.解析网页
USA数据右击复制Xpath
xpath
保存 生成:
GET https://api.thingspeak.com/apps/thinghttp/send_request?api_key=UT0T1VC08JWKL43N
get
total death
GET https://api.thingspeak.com/apps/thinghttp/send_request?api_key=D4HW8EAQ3IIL6XRJ
recovery:
GET https://api.thingspeak.com/apps/thinghttp/send_request?api_key=K5IVK4JAGBW24A8P
change
上传后 出现下面
错误
4.安装环境
添加开发板
首选项-开发板
开发板
http://arduino.esp8266.com/stable/package_esp8266com_index.json
-
开发板管理器
开发板 -
安装esp8266
安装
下载完成后:
开发板
这里上传后
对应数据参考:
这里如果解析的xpath不对的话需要自己分析一下重新填写数据
/text()
下面复杂程序:
对应的程序:
#include <ESP8266WiFi.h> //Use ESP8266 functions
#include <ESP8266HTTPClient.h>
const char* ssid = "chenjichick"; //Your router SSID and password
const char* password = "66669999";
const char* host = "api.thingspeak.com"; //We read the data from this host
const int httpPortRead = 80;
const char* url1 = "/apps/thinghttp/send_request?api_key=UT0T1VC08JWKL43N"; //Those URLs are mine but I kept them so you can replace the key only
const char* url2 = "/apps/thinghttp/send_request?api_key=D4HW8EAQ3IIL6XRJ";
const char* url3 = "/apps/thinghttp/send_request?api_key=K5IVK4JAGBW24A8P";
int To_remove; //There are some irrelevant data on the string and here's how I keep the index
//of those characters
String Cases,Death,Recover,Data_Raw; //Here I keep the numbers that I got
WiFiClient client; //Create a WiFi client and http client
HTTPClient http;
void setup() {
Serial.begin(9600); //Start the serial communication
WiFi.disconnect(); //Disconnect and reconnect to the Wifi you set
delay(1000);
WiFi.begin(ssid, password);
Serial.println("Connected to the WiFi network"); //Display feedback on the serial monitor
Serial.println(WiFi.localIP());
}
//In the loop I read every URL separately and I get the data string then I remove the unecessary characters
//I keep only the values that I can display either on the serial monitor or the display
//I believe that this part is repeated 3 times and can be set on a single function that can be called
//Keep updated on www.SurtrTech.com for an optimized code
void loop()
{
//Reading 1: Reading of cases
if( http.begin(host,httpPortRead,url1)) //Connect to the host and the url
{
int httpCode = http.GET(); //Check feedback if there's a response
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString(); //Here we store the raw data string
Serial.print("Cases (RAW value): "); //I choosed to display it on the serial monitor to help you debug
Serial.println(Data_Raw);
To_remove = Data_Raw.indexOf(">"); //I look for the position of this symbol ">"
Data_Raw.remove(0,To_remove+1); //I remove it and everything that's before
To_remove = Data_Raw.indexOf("<"); //I look for the position of this symbol ">"
Data_Raw.remove(To_remove,Data_Raw.length()); //I remove it and everything that's after
//Example: This is the raw data received <td style="font-weight: bold; text-align:right">63,927</td>
//First we look for ">" and we remove everything before it including it
//We stay with this 63,927</td>
//We look for "<" symbol and we remove it + everything after
//We keep only this 63,927 as string
Cases=Data_Raw;
Serial.print("Cases: "); //I choosed to display it on the serial monitor to help you debug
Serial.println(Cases);
}
}
else //If we can't get data
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else //If we can't connect to the HTTP
{
Serial.printf("[HTTP} Unable to connect\n");
}
//Reading 2 is the same thing as reading 1
if( http.begin(host,httpPortRead,url2))
{
int httpCode = http.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString();
Serial.print("Deaths (RAW value): ");
Serial.println(Data_Raw);
To_remove = Data_Raw.indexOf(">");
Data_Raw.remove(0,To_remove+1);
To_remove = Data_Raw.indexOf("<");
Data_Raw.remove(To_remove,Data_Raw.length());
Death=Data_Raw;
Serial.print("Deaths: ");
Serial.println(Death);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
}
//Reading 3 is the same thing as reading 1
if( http.begin(host,httpPortRead,url3))
{
int httpCode = http.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString();
Serial.print("Recoveries (RAW value): ");
Serial.println(Data_Raw);
To_remove = Data_Raw.indexOf(">");
Data_Raw.remove(0,To_remove+1);
To_remove = Data_Raw.indexOf("<");
Data_Raw.remove(To_remove,Data_Raw.length());
Recover=Data_Raw;
Serial.print("Recoveries: ");
Serial.println(Recover);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
}
delay(5000);
while (WiFi.status() != WL_CONNECTED) //In case the Wifi connexion is lost
{
WiFi.disconnect();
delay(1000);
WiFi.begin(ssid, password);
Serial.println("Reconnecting to WiFi..");
delay(10000);
}
}
改进后程序:
/* This code works with ESP8266 12E (NodeMcu v1.0) its main function is to read the data from https://www.worldometers.info/
* Through ThingSpeak app ThingHTTP and displays on the Serial monitor
* Refer to www.SurtrTech.com for more details and video about the project
* This project was created during the COVID-19 Pandemic and the example here used to track the numbers in Italy
*/
#include <ESP8266WiFi.h> //Use ESP8266 functions
#include <ESP8266HTTPClient.h>
const char* ssid = "cjjdmx"; //Your router SSID and password
const char* password = "66669999";
const char* host = "api.thingspeak.com"; //We read the data from this host
const int httpPortRead = 80;
const char* url1 = "/apps/thinghttp/send_request?api_key=UT0T1VC08JWKL43N"; //Those URLs are mine but I kept them so you can replace the key only
const char* url2 = "/apps/thinghttp/send_request?api_key=D4HW8EAQ3IIL6XRJ";
const char* url3 = "/apps/thinghttp/send_request?api_key=K5IVK4JAGBW24A8P";
int To_remove; //There are some irrelevant data on the string and here's how I keep the index
//of those characters
String Cases,Death,Recover,Data_Raw; //Here I keep the numbers that I got
WiFiClient client; //Create a WiFi client and http client
HTTPClient http;
void setup() {
Serial.begin(9600); //Start the serial communication
WiFi.disconnect(); //Disconnect and reconnect to the Wifi you set
delay(1000);
WiFi.begin(ssid, password);
Serial.println("Connected to the WiFi network"); //Display feedback on the serial monitor
Serial.println(WiFi.localIP());
}
//In the loop I read every URL separately and I get the data string then I remove the unecessary characters
//I keep only the values that I can display either on the serial monitor or the display
//I believe that this part is repeated 3 times and can be set on a single function that can be called
//Keep updated on www.SurtrTech.com for an optimized code
void loop()
{
//Reading 1: Reading of cases
if( http.begin(host,httpPortRead,url1)) //Connect to the host and the url
{
int httpCode = http.GET(); //Check feedback if there's a response
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString(); //Here we store the raw data string
Serial.print("Cases "); //I choosed to display it on the serial monitor to help you debug
Serial.println(Data_Raw);
}
}
else //If we can't get data
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else //If we can't connect to the HTTP
{
Serial.printf("[HTTP} Unable to connect\n");
}
//Reading 2 is the same thing as reading 1
if( http.begin(host,httpPortRead,url2))
{
int httpCode = http.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString();
Serial.print("Deaths: ");
Serial.println(Data_Raw);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
}
//Reading 3 is the same thing as reading 1
if( http.begin(host,httpPortRead,url3))
{
int httpCode = http.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString();
Serial.print("Recoveries: ");
Serial.println(Data_Raw);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
}
delay(5000);
while (WiFi.status() != WL_CONNECTED) //In case the Wifi connexion is lost
{
WiFi.disconnect();
delay(1000);
WiFi.begin(ssid, password);
Serial.println("Reconnecting to WiFi..");
delay(10000);
}
}
网友评论