完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
大家好,我正在使用 ESP8266(NodeMCU),在 Arduino IDE 中,我正在检测不同设备的功耗并将该数据作为 Json 文件发送到 Web 服务器,为了不向服务器发送垃圾邮件,我' m 创建一个包含 60 个 Json 对象的数组,其中保存数据。我尝试使用 For 循环来创建 Jsons 对象,但它在串行监视器上显示了一个名为“异常(2)”的问题,该问题重置了 ESP8266,我进行了研究,发现它发生在闪存被覆盖时,通常由 Wifi 函数,如 Wifi.begin() 和解决方案实施 Wifi.Persistent(false)。它对我没有用,所以我实施了其他策略,比如逐个创建 Json 对象,尽管它在我创建第十五个对象时似乎一开始工作,但 Exception(2) 再次出现并且消息“Connection Refused”。有些人通过用直接 IP 地址替换“localhost”来解决这个问题,但是对于我正在使用的服务器 Herokuapp,它不允许你直接使用它。
这是我的代码: 代码:全选#include #include #include #include String bearer = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3N1ZWRBdCI6MTU2MDQ4NjQ5NS45OTQ5MzUsImlzc3VlciI6I"; String Host = "http://efihome.herokuapp.com/api/logs"; int httpCode; const char* ID = "abcdefg"; const char* PASS = "12345678"; HTTPClient http; //Objeto de clase HTTPClient const int capacity = JSON_ARRAY_SIZE(30) + 30 * JSON_OBJECT_SIZE(10); StaticJsonDocument JsonObject obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12, obj13, obj14, obj15, obj16, obj17, obj18, obj19, obj20, obj21, obj22, obj23, obj24, obj25, obj26, obj27, obj28, obj29, obj30, obj31, obj32, obj33, obj34, obj35, obj36, obj37, obj38, obj39, obj40, obj41, obj42, obj43, obj44, obj45, obj46, obj47, obj48, obj49, obj50; float current[60] = {1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 6.6}; double date = 1560456080; String deviceID = "97367434-CA04-4FC7-A8BA-703DCFCBA081"; float power[60] = {20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 15.2, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 15.2}; float FactorPower[60] = {0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8}; float voltage[60] = {117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 116.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 116.8}; void setup() { Serial.begin(9600); // WiFi.persistent(false); WiFi.begin(ID, PASS); //Inicia la conexión con la red Wi-Fi. while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting..."); } Serial.println("WiFi Conectado"); GETServidor(); } void loop() { // put your main code here, to run repeatedly: } void GETServidor() { CreateObj(obj1, 0); CreateObj(obj2, 1); CreateObj(obj3, 2); CreateObj(obj4, 3); CreateObj(obj5, 4); CreateObj(obj6, 5); CreateObj(obj7, 6); CreateObj(obj8, 7); CreateObj(obj9, 8); CreateObj(obj10, 9); CreateObj(obj11, 10); CreateObj(obj12, 11); CreateObj(obj13, 12); CreateObj(obj14, 13); CreateObj(obj15, 14); CreateObj(obj16, 15); CreateObj(obj17, 16); CreateObj(obj18, 17); CreateObj(obj19, 18); CreateObj(obj20, 19); CreateObj(obj21, 20); CreateObj(obj22, 21); CreateObj(obj23, 22); CreateObj(obj24, 23); CreateObj(obj25, 24); CreateObj(obj26, 25); CreateObj(obj27, 26); CreateObj(obj28, 27); CreateObj(obj29, 28); CreateObj(obj30, 29); CreateObj(obj31, 30); CreateObj(obj32, 31); CreateObj(obj33, 32); CreateObj(obj34, 33); CreateObj(obj35, 34); CreateObj(obj36, 35); CreateObj(obj37, 36); CreateObj(obj38, 37); CreateObj(obj39, 38); CreateObj(obj40, 39); CreateObj(obj41, 40); CreateObj(obj42, 41); CreateObj(obj43, 42); CreateObj(obj44, 43); CreateObj(obj45, 44); CreateObj(obj46, 45); CreateObj(obj47, 46); CreateObj(obj48, 47); CreateObj(obj49, 48); CreateObj(obj50, 49); // Each array until , = 185 char rootBuffer[185*50]; serializeJsonPretty(doc, rootBuffer); //Serial.println(rootBuffer); if (http.begin(Host)) { http.addHeader("Accept", "application/json"); http.addHeader("Authorization", "Bearer " + bearer); http.addHeader("Content-Type", "application/json"); httpCode = http.POST(rootBuffer); if (httpCode > 0) { Serial.printf("[HTTP] POST... code: %dn", httpCode); if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] POST... failed, error: %sn", http.errorToString(httpCode).c_str()); // String payload = http.getString(); // Serial.println(payload); } http.end(); } else { Serial.println("Fallo en conexión con servidor"); } //=================================================================================== //=================================================================================== //=================================================================================== } void CreateObj(JsonObject obj, int i) { obj = doc.createNestedObject(); obj["current"] = current; obj["date"] = date; obj["device_id"] = deviceID; obj["power"] = power; obj["factor_power"] = FactorPower; obj["voltage"] = voltage; } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
1153 浏览 1 评论
583浏览 6评论
480浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
465浏览 5评论
466浏览 4评论
441浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 11:03 , Processed in 0.963126 second(s), Total 74, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号