API地址
如何使用esp8266获取这个地址中的天气,时间,和城市信息
首先我们先将arduino和esp8266接线完成。如下图
基本上是按照上图所连接,但是有一些小地方不一样,我们选择将CH_PD和VCC都连接到3.3V(可以通过面包板来将两根线连接到一起),则与上图 3.3V和5V的接线口不一样。实际接线图如下
然后我们就将arduino与电脑相连接。然后打开我们的arduino的ide(即编译器),开始编写我们要烧录的程序,烧录也就是把我们所需要的功能通过代码烧录到arduino板子上,这样他就可以运行程序,实现我们需要的功能。
我们烧录的代码如下(可直接复制粘贴)
#include<SoftwareSerial.h>
SoftwareSerial mySerial(3, 2); // RX, TX
void setup() {
Serial.begin(9600);
while (!Serial) {;}
Serial.println("hardware serial!");
mySerial.begin(115200);
mySerial.println("software seria");
}
void loop() {
if(mySerial.available())
Serial.write(mySerial.read());
if(Serial.available())
mySerial.write(Serial.read());
}
要注意
SoftwareSerial mySerial(3, 2); // RX, TX
这行代码的意思是我们的arduino和esp所连接的引脚口,你若是直接复制此代码,则需要把RX和TX连接到代码中的引脚口,若是自己连接好了,那你需要修改代码的引脚口。如果你还是上传不成功,可以试试将RX和TX的接线换一下。
上传成功图示。(上传为左上角对号旁边的右箭头)
对了,很重要的一点,当我们烧录的时候,要选对开发板和端口,我之前一直上传不成功,发现原来是选错了,我们要选arduino,而不是esp8266,端口每个电脑是不一样的,要看自己的。如图
我可以利用Arduino和ESP8266模块来实现在指定API地址中获取某个城市的天气、时间等信息,具体步骤如下:
1.首先,需要确认要使用的API地址,并将其添加到Arduino程序中。这可以通过使用ESP8266 WiFi库的GET请求来完成。通过这种方式,我们可以将API响应作为字符串获取到Arduino中。
#include <ESP8266WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
WiFiClient client;
const int httpPort = 80;
if (!client.connect("api.openweathermap.org", httpPort)) {
Serial.println("Connection failed");
return;
}
client.print("GET /data/2.5/weather?q=London&APPID={API Key}\r\n");
client.print("Host: api.openweathermap.org\r\n");
client.print("Connection: close\r\n\r\n");
while (!client.available()) {
delay(1000);
Serial.println("Waiting for response...");
}
String response;
while (client.available()) {
char c = client.read();
response += c;
}
Serial.println(response);
delay(60000);
}
2.接下来,我们需要从API响应中提取我们需要的信息。通常,我们可以使用json解析库,例如ArduinoJSON,来帮助我们解析响应。
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
WiFiClient client;
const int httpPort = 80;
if (!client.connect("api.openweathermap.org", httpPort)) {
Serial.println("Connection failed");
return;
}
client.print("GET /data/2.5/weather?q=London&APPID={API Key}\r\n");
client.print("Host: api.openweathermap.org\r\n");
client.print("Connection: close\r\n\r\n");
while (!client.available()) {
delay(1000);
Serial.println("Waiting for response...");
}
String response;
while (client.available()) {
char c = client.read();
response += c;
}
const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(8) + JSON_OBJECT_SIZE(9) + JSON_OBJECT_SIZE(32) + 500;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(response);
if (!root.success()) {
Serial.println("Parsing failed");
return;
}
const char* city = root["name"];
float temp = root["main"]["temp"];
int humidity = root["main"]["humidity"];
const char* description = root["weather"][0]["description"];
Serial.print("City: ");
Serial.println(city);
Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Description: ");
Serial.println(description);
delay(60000);
}
在这个示例中,我们从OpenWeatherMap API响应中提取了城市名称,温度,湿度和天气描述,并将它们打印到串口监视器中。对于更复杂的API响应,您可能需要使用更大的缓冲区,并且需要更长的时间来找到您需要的信息的位置。
3.最后,您可能需要连接其他传感器和设备来根据天气和时间做出决策。根据特定的用例,这可能需要编写额外的代码和/或硬件。