完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
您好,
我正在尝试使用 twitter api 库或任何其他社交媒体 api 来接收统计信息。 一开始一切似乎都很好,但在某些时候它停止接收 json 消息。它发生在不同的请求中,而不仅仅是推特(Facebook 等)。 我已经检查过是否存在 RAM 问题,似乎每次有请求时,堆 RAM 都会变得越来越小,以至于它不再解析。我还检查了 ESP 是否已断开连接但它保持连接。 我也尝试过使用 StaticjsonBuffer 并清除 jsonbuffer 但它没有改变。 这是我的代码: 代码:全选#include /******************************************************************* * An Example of getting data back from the TwitterAPI * * https://github.com/witnessmenow/arduino-twitter-api * * * * Written by Brian Lough * *******************************************************************/ #include #include //------- Replace the following! ------ char ssid[] = "ssss"; // your network SSID (name) char password[] = "kkkkk"; // your network key // Normally we would use these to generate the bearer token but its not working yet :/ // Use steps on the readme to generate the Bearer Token #define BEARER_TOKEN "zzzzz" //Using curl to get bearer token // curl -u "$CONSUMER_KEY:$CONSUMER_SECRET" // --data 'grant_type=client_credentials' // 'https://api.twitter.com/oauth2/token' WiFiClientSecure client; TwitterApi api(client); unsigned long api_mtbs = 5000; //mean time between api requests unsigned long api_lasttime = 0; //last time api request has been done bool firstTime = true; //Inputs String screenName = "xxx"; bool haveBearerToken = false; void setup() { Serial.begin(115200); // Set WiFi to station mode and disconnect from an AP if it was Previously // connected WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); // Attempt to connect to Wifi network: Serial.print("Connecting Wifi: "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); IPAddress ip = WiFi.localIP(); Serial.println(ip); Serial.printf("Internal Total heap %d, internal Free Heap %dn",ESP.getHeapSize(),ESP.getFreeHeap()); Serial.printf("SPIRam Total heap %d, SPIRam Free Heap %dn",ESP.getPsramSize(),ESP.getFreePsram()); api.setBearerToken(BEARER_TOKEN); haveBearerToken = true; getTwitterStats(screenName); // uint8_t* ps=(byte *)ps_malloc(900000); } void getTwitterStats(String name) { Serial.println("Getting twitter stats for " + name); String responseString = api.getUserStatistics(name); Serial.println(responseString); DynamicJsonBuffer jsonBuffer; JsonObject& response = jsonBuffer.parseObject(responseString); if (response.success()) { Serial.println("parsed Json"); // Use Arduino Json to parse the data int followerCount = response["followers_count"]; Serial.println(followerCount); } else { Serial.println("Failed to parse Json"); } Serial.printf("Internal Total heap %d, internal Free Heap %dn",ESP.getHeapSize(),ESP.getFreeHeap()); Serial.printf("SPIRam Total heap %d, SPIRam Free Heap %dn",ESP.getPsramSize(),ESP.getFreePsram()); } void loop() { if(haveBearerToken){ if ((millis() > api_lasttime + api_mtbs)) { getTwitterStats(screenName); api_lasttime = millis(); if (WiFi.status() == WL_CONNECTED) { Serial.println("Still connected"); } } } } 这是我在串行监视器中得到的: 代码:全选Connecting Wifi: sssss ... WiFi connected IP address: 192.168.1.xxx Internal Total heap 337244, internal Free Heap 282376 SPIRam Total heap 4194116, SPIRam Free Heap 4190924 Getting twitter stats for xxxx {"......very long Json......."} parsed Json 219 Internal Total heap 336328, internal Free Heap 234060 SPIRam Total heap 4194100, SPIRam Free Heap 4190788 ps=0x3f801288 Getting twitter stats for xxxx Failed to parse Json Internal Total heap 336900, internal Free Heap 247180 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx {"......very long Json......."} parsed Json 219 Internal Total heap 336148, internal Free Heap 199752 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx Failed to parse Json Internal Total heap 336764, internal Free Heap 213076 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx {"......very long Json......."} parsed Json 219 Internal Total heap 336040, internal Free Heap 165672 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx .......... Internal Total heap 335664, internal Free Heap 63468 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx Failed to parse Json Internal Total heap 336192, internal Free Heap 76104 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx Failed to parse Json Internal Total heap 336196, internal Free Heap 76096 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx Failed to parse Json Internal Total heap 336156, internal Free Heap 75676 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxxx Failed to parse Json Internal Total heap 336160, internal Free Heap 75668 SPIRam Total heap 4194084, SPIRam Free Heap 3290772 Still connected Getting twitter stats for xxxx 按照Arduino JSON小助手的说法,JSON报文的大小是: ESP32/ESP8266 1464+1994 = 3458 Bytes 真不知道还能做什么。Arduino 的 instagram 库工作正常,但它使用 JsonStreamingParser.h。是否可以改用该库?它会改变结果吗? 看起来一旦内部空闲堆 RAM 达到 7000 左右,它就不再工作了。 我也每 5 秒调用一次该函数。我试图增加时间,但似乎在某些时候发生了同样的事情。Free Heap RAM 似乎也增加了一点。 谢谢 |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
350 浏览 0 评论
1196 浏览 1 评论
588浏览 6评论
484浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
468浏览 5评论
467浏览 4评论
444浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 13:51 , Processed in 0.697223 second(s), Total 45, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号