diff --git a/examples/arduino/EspLinkSample/EspLinkSample.ino b/examples/arduino/EspLinkSample/EspLinkSample.ino index 937cc07..ab672b0 100644 --- a/examples/arduino/EspLinkSample/EspLinkSample.ino +++ b/examples/arduino/EspLinkSample/EspLinkSample.ino @@ -62,8 +62,16 @@ void ledHtmlCallback(WebServerCommand command, char * data, int dataLen) next_ts = millis() + elapse; } break; + case SET_FIELD: + if( strcmp_P(data, PSTR("frequency") ) == 0 ) + { + frequency = webServer.getArgInt(); + elapse = 1000 / frequency; + } + break; case LOAD: - webServer.setArgNum(1); + webServer.setArgNum(2); + webServer.setArgInt("frequency", frequency); case REFRESH: if( command == REFRESH ) webServer.setArgNum(1); @@ -79,103 +87,3 @@ void ledHtmlCallback(WebServerCommand command, char * data, int dataLen) } -/* -#include "EspLink.h" - - -void packetReceived(CmdRequest *req); - -EspLink espLink(Serial, packetReceived); - -void packetReceived(CmdRequest *req) -{ - Serial.println("\nReceived\n"); - uint16_t shrt, port; - espLink.cmdPopArg(req, &shrt, 2); - RequestReason reason = (RequestReason)shrt; - Serial.print("Reason: "); - Serial.println(reason); - - uint8_t ip[4]; - espLink.cmdPopArg(req, &ip, 4); - Serial.print("IP: "); - for(int i=0; i < 4; i++) - { - Serial.print(ip[i], DEC); - if( i != 3 ) - Serial.print("."); - } - Serial.println(); - - espLink.cmdPopArg(req, &port, 2); - Serial.print("Port: "); - Serial.println(port); - - { - uint16_t len = espLink.cmdArgLen(req); - char bf[len+1]; - bf[len] = 0; - espLink.cmdPopArg(req, bf, len); - Serial.print("Url: "); - Serial.println(bf); - } - - switch( reason ) - { - case BUTTON: - { - uint16_t len = espLink.cmdArgLen(req); - char bf[len+1]; - bf[len] = 0; - espLink.cmdPopArg(req, bf, len); - Serial.print("Arg: "); - Serial.println(bf); - } - break; - case SUBMIT: - { - int arg = 4; - while( espLink.cmdGetArgc(req) > arg ) - { - arg++; - - uint16_t len = espLink.cmdArgLen(req); - char bf[len+1]; - bf[len] = 0; - espLink.cmdPopArg(req, bf, len); - - Serial.print(bf + 1); - Serial.print(" -> "); - Serial.println(bf + strlen(bf+1) + 2); - } - return; - } - } - - espLink.sendPacketStart(CMD_WEB_JSON_DATA, 100, 3); - espLink.sendPacketArg(4, ip); - espLink.sendPacketArg(2, (uint8_t *)&port); - - char outBuf[30]; - outBuf[0] = 0; - strcpy(outBuf+1, "last_name"); - strcpy(outBuf+11,"helloka"); - espLink.sendPacketArg(19, (uint8_t *)outBuf); - - espLink.sendPacketEnd(); - -} - -void setup() { - Serial.begin(57600); - - delay(10); - espLink.sendPacketStart(CMD_CB_ADD, 100, 1); - espLink.sendPacketArg(5, (uint8_t *)"webCb"); - espLink.sendPacketEnd(); -} - -void loop() { - espLink.readLoop(); -} -*/ diff --git a/examples/arduino/EspLinkSample/WebServer.cpp b/examples/arduino/EspLinkSample/WebServer.cpp index df7e50f..5e100d5 100644 --- a/examples/arduino/EspLinkSample/WebServer.cpp +++ b/examples/arduino/EspLinkSample/WebServer.cpp @@ -46,7 +46,21 @@ void WebServer::invokeMethod(RequestReason reason, WebMethod * method, CmdReques break; case WS_SUBMIT: { - /* TODO: iterate through fields */ + int arg_len = espLink.cmdGetArgc( req ); + int cnt = 4; + + while( cnt < arg_len ) + { + uint16_t len = espLink.cmdArgLen(req); + char bf[len+1]; + bf[len] = 0; + espLink.cmdPopArg(req, bf, len); + + value_ptr = bf + 2 + strlen(bf+1); + method->callback(SET_FIELD, bf+1, strlen(bf+1)); + + cnt++; + } } return; default: @@ -148,3 +162,23 @@ void WebServer::setArgStringP(const char * name, const char * value) args_to_send--; } +void WebServer::setArgInt(const char * name, int32_t value) +{ + if( args_to_send <= 0 ) + return; + + uint8_t nlen = strlen(name); + char buf[nlen + 7]; + buf[0] = WEB_INTEGER; + strcpy(buf+1, name); + memcpy(buf+2+nlen, &value, 4); + espLink.sendPacketArg(nlen+6, (uint8_t *)buf); + + args_to_send--; +} + +int32_t WebServer::getArgInt() +{ + return (int32_t)atol(value_ptr); +} + diff --git a/examples/arduino/EspLinkSample/WebServer.h b/examples/arduino/EspLinkSample/WebServer.h index 0914b50..6d70817 100644 --- a/examples/arduino/EspLinkSample/WebServer.h +++ b/examples/arduino/EspLinkSample/WebServer.h @@ -53,6 +53,8 @@ class WebServer uint16_t remote_port; int16_t args_to_send; + + char * value_ptr; protected: EspLink espLink; @@ -70,8 +72,11 @@ class WebServer uint16_t getRemotePort() { return remote_port; } void setArgNum(uint8_t num); + void setArgInt(const char * name, int32_t value); void setArgString(const char * name, const char * value); void setArgStringP(const char * name, const char * value); + + int32_t getArgInt(); }; #endif /* WEB_SERVER_H */ diff --git a/web-server/web-server.c b/web-server/web-server.c index f6a8861..330c436 100644 --- a/web-server/web-server.c +++ b/web-server/web-server.c @@ -263,7 +263,7 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData) int c = 2; while( c++ < cmdGetArgc(req) ) { - if( c < 3 ) // skip the first argument + if( c > 3 ) // skip the first argument jsonBuf[jsonPtr++] = ','; int len = cmdArgLen(req);