arduino在网页中查找文本
我正试着发出arduino天气警报,从 我需要在“main”之后的“weather”中找到引号的内容。我已经找了一个文本查找器,但是上面的那个,但是它太不规范了,可能根本不存在 当前我的代码如下所示:arduino在网页中查找文本,arduino,Arduino,我正试着发出arduino天气警报,从 我需要在“main”之后的“weather”中找到引号的内容。我已经找了一个文本查找器,但是上面的那个,但是它太不规范了,可能根本不存在 当前我的代码如下所示: #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; char server[] = "api.openweathermap.org";
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "api.openweathermap.org";
IPAddress ip(192,168,1,77);
EthernetClient client;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(1000);
if (client.connect(server, 80)) {
client.println("GET /data/2.5/weather?q=Melbourne,au HTTP/1.1");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
}
else {
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
client.stop();
while(true);
}
}
对于这个输出,我希望它说:
Clear
从Arduino游乐场获取所需数据是一种非常简单的方法。我相信这应该行得通。请注意,我还没有测试它,所以祈祷好运
在开头加上这个
#include <TextFinder.h>
#include <String.h>
String result = "";
TextFinder finder( client );
如果您感兴趣,请在下面提供更多信息。
您得到的响应是json,要正确解释它,您需要一个json解析器。我在github上找到了一个库:它在arduino上解析json。它似乎也很容易使用。如果你需要帮助,你可以浏览它的
在启动json之前,您需要首先从响应中删除头。为此,您可以一直读取字符,直到获得双CRLF(\r\n\r\n)我不确定是否已经有文本查找器。但是你可以自己做一个。您所需要做的就是逐个解析缓冲区并逐个字符进行检查。应该不是很难。例如,如果(Buff[i++]=''')'{…(buff [i++]=''')] {…}警告:我不是很先进!我是在测试它,但是它是Saye:天气:Io:7:20:错误:“客户端”在这个作用域中没有声明:“函数‘无效环()”:天气。I:44:17:错误:ISO C++禁止指针和整数的比较。[f-容错]编译。现在,它只是说:WeiTr.In:在函数“无效环()):Web.II:44:17:错误:禁止指针和整数之间的比较[fPrimix]错误编译。我移动:String结果=“”;TextFinder finder(客户端);@ HaZodood尝试替换(C,=“\”){{带(C++)=“\”){
#include <TextFinder.h>
#include <String.h>
String result = "";
TextFinder finder( client );
if (client.connected()){
finder.find("\"weather\"");
finder.find("\"main\":\"");
char c = client.read();
while(c != '\"'){
result += c;
c = client.read();
}
Serial.print(result);
}
if (!client.connected()) {
Serial.println();
client.stop();
while(true);
}