ESP8266,客户端通过股票代码

时间:2019-03-10 11:27:14

标签: callback arduino client esp8266 ticker

嘿堆栈溢出社区,

非常感谢这个社区,对我有很大帮助。但是,从现在开始,我一直陷于困境,无法在网络上找到任何解决方案。

使用ESP8266芯片非常有趣。那里有许多简单的示例,说明如何使用ESP连接到服务器。目前,我正在编写嵌套类以创建不同的客户端连接。

我喜欢使用Ticker / Timer对象(定期或一次回调)创建“子任务”以释放CPU。但是,一旦我创建了一个新的WiFiClient对象并调用了Ticker对象的回调函数,当客户端尝试连接到主机时,我将收到运行时错误。

包含我的整个项目和类会很混乱,所以我写了这个小代码,但是它将说明我的问题。 两个功能void post(int var) and void postPtr(int var)可以单独调用,例如在loop()中成功。但是,一旦我将其中一个功能附加到股票报价回调中,我就会收到运行时错误。


#ifdef DEBUG_ESP_PORT
#define DEBUG_MSG(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
#define debugmode 1
#else
#define debugmode 0
#define DEBUG_MSG(...)
#endif

#include <ESP8266WiFi.h>
#include <Ticker.h>

#ifndef STASSID
#define STASSID "###SSID###"
#define STAPSK  "###PWK###"
#endif

Ticker myTicker;
Ticker* myTickerPtr;
WiFiClient* myClientPtr;

const char* ssid     = STASSID;
const char* password = STAPSK;

const char* host = "192.168.10.40";
const uint16_t port = 8080;

const char* postMessage = "POST /plug HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded; charset=UTF-8\r\nContent-Length: 14\r\nConnection: close\r\n\r\nname=2&value=1";

static int prevTime=millis();
int cnt=0;
int packages=0;
int speed=0;

//#########################################################################
// checking time duration for code execution
int time() {
    int tmp = millis();
    int value = tmp - prevTime;
    prevTime = tmp;

    return value;
}


//#########################################################################
// simple connection / package speed test
void speedtest() {
    speed = (cnt - packages)*5;
    packages = cnt;
}

//#########################################################################
// connect to host, send a post message, stop connection
void post(int var) {
    WiFiClient client;

    DEBUG_MSG("[DONE %dms] Client[%d] waiting for connection... \n", time(), var);
    while (!client.connect(host, port)) {delay(10);}
    DEBUG_MSG("[DONE %dms] Client[%d] connected \n", time(), var);

    client.println(postMessage);
    client.printf("[DONE %dms] Client[%d] Data Package[%d] Sent. Avg speed: %d\n\n", time(), var, cnt, speed);
    DEBUG_MSG("[DONE %dms] Client[%d] Data Package[%d] Sent. Avg speed: %d\n\n", time(), var, cnt, speed);

    client.stop();
    cnt++;
}

//#########################################################################
// connect to host, send a post message, stop connection (using a WifiClient Pointer)
void postPtr(int var) {
    myClientPtr = new WiFiClient;

    DEBUG_MSG("[DONE %dms] ClientPtr[%d] waiting for connection... \n", time(), var);
    while (!myClientPtr->connect(host, port)) {delay(10);}
    DEBUG_MSG("[DONE %dms] ClientPtr[%d] connected \n", time(), var);

    myClientPtr->println(postMessage);
    myClientPtr->printf("[DONE %dms] ClientPtr[%d] Data Package[%d] Sent. Avg speed: %d\n\n", time(), var, cnt, speed);
    DEBUG_MSG("[DONE %dms] ClientPtr[%d] Data Package[%d] Sent. Avg speed: %d\n\n", time(), var, cnt, speed);

    myClientPtr->stop();
    delete myClientPtr;
    cnt++;
}

//#########################################################################
void setup() {
    if (debugmode) {
    Serial.begin(115200);
    while (!Serial) {delay(10);}
    }
    DEBUG_MSG("\n\n");
    DEBUG_MSG("[DONE %dms] Serial done \n", time());


    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    DEBUG_MSG("[DONE %dms] Waiting for WiFi connection... \n", time());
    while (!WiFi.isConnected()) { delay(10);}
    DEBUG_MSG("[DONE %dms] WiFi connected. IP %s\n", time(), WiFi.localIP().toString().c_str());

    myTicker.attach_ms(200, speedtest);


    //R U N  T I M E  E R R O R - while in callback function - STACK OVERFLOW !!!
    myTickerPtr = new Ticker;
    myTickerPtr->once_ms(100, post, 0x02);
    //or
    //myTickerPtr->attach_ms(100, post, 0x02);
}

//#########################################################################
void loop() {
    // calling functions, working fine...
    post(0x01);
    postPtr(0x01);
    //ESP.deepSleep(0);
}

调试消息:

[DONE 0ms] Serial done 
[DONE 5ms] Waiting for WiFi connection... 
[DONE 185ms] WiFi connected. IP 192.168.10.88
[DONE 101ms] Client[2] waiting for connection... 

Soft WDT reset

>>>stack>>>

ctx: sys
sp: 3fffecb0 end: 3fffffb0 offset: 01b0
3fffee60:  00000000 4020e220 00000030 4010097c  
3fffee70:  3ffee63c 3fffeef0 00000030 4020e220  
3fffee80:  00000000 3fffeef0 3fffeef0 402034c1  
3fffee90:  00001f90 3fffeea0 00000008 60000600  
3fffeea0:  401052ec 001fbdcb 3ffee700 00001f90  
3fffeeb0:  3ffee63c 00001f90 3fffeef0 40202c54  
3fffeec0:  40204810 280aa8c0 402013f0 0000000a  
3fffeed0:  3ffe87a4 0000000a 0000000a 402013fb  
3fffeee0:  3ffee63c 00000002 3ffe84e0 4020266e  
3fffeef0:  40204788 00000000 00001388 00000001  
3fffef00:  00000000 00000000 3ffedfc0 00d99aa9  
3fffef10:  4010505e 0014fa1c 3ffedfc0 60000600  
3fffef20:  00d9d2f2 3ffedfc0 3ffef1a4 40230809  
3fffef30:  4023084e 3fffdab0 00000000 3fffdcb0  
3fffef40:  3ffedfd0 3fffdad0 3ffee6a8 402040c7  
3fffef50:  40000f49 40000f49 3fffdab0 40000f49  
3fffef60:  40000e19 40001878 00000002 00000000  
3fffef70:  3fffff10 ffffffff ffffffff 401049d0  
3fffef80:  401049d6 00000002 00000000 54e55bf8  
3fffef90:  4010000d 6f3d99f3 6dad4d79 c13de55d  
3fffefa0:  00000000 3fffef3c 00000000 3fffff38  
3fffefb0:  3fffffc0 00000000 00000000 feefeffe  
3fffefc0:  feefeffe feefeffe feefeffe feefeffe  
3fffefd0:  feefeffe feefeffe feefeffe feefeffe  
3fffefe0:  feefeffe feefeffe feefeffe feefeffe 
...
...
3ffffc20:  feefeffe feefeffe feefeffe feefeffe  
3ffffc30:  feefeffe feefeffe feefeffe feefeffe  
3ffffc40:  feefeffe feefeffe feefeffe feefeffe  
3ffffc50:  feefeffe feefeffe feefeffe feefeffe  
3ffffc60:  feefeffe feefeffe feefeffe feefeffe  
3ffffc70:  00000003 00000000 0000000a 00000000  
3ffffc80:  00000003 00000000 0000000a 00000000  
3ffffc90:  feefeffe feefeffe feefeffe feefeffe  
3ffffca0:  00000000 a0000000 00000000 0000001c  
3ffffcb0:  00003000 feefeffe 00003000 00000000  
3ffffcc0:  3ffffe20 00000000 3ffffe20 4020bf66  
3ffffcd0:  0000a000 3ffffdc3 3ffe8e01 00000008  
3ffffce0:  4022a65a 3ffeedec 40207ce0 0000000d  
3ffffcf0:  3ffffe20 0000000d 3ffffe20 4020bf66  
3ffffd00:  3ffffd80 3ffef434 3ffffd30 00000000  
3ffffd10:  3ffe8cb0 40229717 3ffeedec 4020bea0  
3ffffd20:  3ffffe20 3ffef434 3ffffd80 40207de0  
3ffffd30:  0000000d 0000002c 0000002c 00000001  
3ffffd40:  00000000 00000001 3ffffe20 4020bf66  
3ffffd50:  3ffffdc3 3ffe8681 00000000 4021f23f  
3ffffd60:  3ffed780 4021ee10 40221480 3ffe8681  
3ffffd70:  00000000 3ffe8682 3ffffe20 4020c150  
3ffffd80:  00000000 0000000d 00000000 00000000  
3ffffd90:  0000000d 0000002f 40302073 3ffed878  
3ffffda0:  3ffed8b0 40221480 3ffed780 aa55aa55  
3ffffdb0:  3ffed110 40220d8c 3ffed08c 33000012  
3ffffdc0:  00353239 4021edd4 3ffed110 00000012  
3ffffdd0:  4021ed9f 3ffed08c 00000000 40232c0d  
3ffffde0:  3fffff50 3fffff20 00000010 3ffe8304  
3ffffdf0:  00000002 00000000 00000020 401014c2  
3ffffe00:  3ffe8cb2 401048ac 3ffec420 3ffee6a8  
3ffffe10:  00000001 40103acf 3ffece78 402096c9  
3ffffe20:  40103f32 3ffe84ec 00000010 ffff0208  
3ffffe30:  3ffffed0 0000003f 3fffc200 00000022  
3ffffe40:  40103a16 0000002c 7fffffff 00000002  
3ffffe50:  0000012c 00000030 0000001e ffffffff  
3ffffe60:  4010288b 00080000 3fffff40 40203d5c  
3ffffe70:  00000000 0000000a 00002200 4000050c  
3ffffe80:  00000000 00000000 0000001f 401051a9  
3ffffe90:  00000000 00000000 0000001f 401051a9  
3ffffea0:  4000050c 00000030 00000010 ffffffff  
3ffffeb0:  402041d4 00000030 00000011 ffffffff  
3ffffec0:  402041d4 000000a0 3fffc704 00000001  
3ffffed0:  00000000 00000000 00000000 fffffffe  
3ffffee0:  ffffffff 3fffc6fc 00000001 3ffe851c  
3ffffef0:  00000000 3fffdad0 3ffee6a8 00000030  
3fffff00:  401052ec 0014fa1c 3ffef1a4 00000000  
3fffff10:  3ffedfc0 3ffef1a4 00000020 401009a2  
3fffff20:  00000000 0014800a 402036bc 3ffef42c  
3fffff30:  00000000 00000064 00000002 4020369e  
3fffff40:  4020262c 4020e220 00000020 401009a2  
3fffff50:  00000000 3ffee608 00000014 3ffee6a8  
3fffff60:  00000f55 3ffee608 3ffef42c 4020259c  
3fffff70:  00000000 00000000 00000000 00000000  
3fffff80:  402023ac 580aa8c0 402043e0 402043cc  
3fffff90:  00000000 00000000 00000001 4020415d  
3fffffa0:  3fffdad0 00000000 3ffee674 402041ee  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,6)


 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset

如果有任何帮助,建议或建议,我将不胜感激。感谢您阅读:D

0 个答案:

没有答案