The WeeFee
Dependencies: MbedJSONValue esp32-driver WebSocketClient JSON
Revision 4:1b8288ef7cbc, committed 2018-12-18
- Comitter:
- Tina Quach
- Date:
- Tue Dec 18 11:56:26 2018 -0500
- Parent:
- 3:0d7c5cabd93f
- Commit message:
- parse server messages
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed_app.json | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Dec 17 19:34:50 2018 -0500 +++ b/main.cpp Tue Dec 18 11:56:26 2018 -0500 @@ -13,6 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include <ios> +#include <stdio.h> +#include <string> // std::string +#include <iostream> // std::cout +#include <sstream> // std::stringstream, std::stringbuf +#include <vector> +#include <iomanip> + #include "mbed.h" #include "TCPSocket.h" @@ -25,6 +33,7 @@ #include "Json.h" #define WIFI_ESP32 3 +#define TOP D5 //#elif TARGET_GR_LYCHEE @@ -52,6 +61,7 @@ DigitalOut led3(LED3); DigitalOut led4(LED4); +PwmOut pin(D5); int main() { @@ -126,7 +136,7 @@ // ******* END CONNECTING *********/ - + // begin main loop while (true) { @@ -143,15 +153,15 @@ MbedJSONValue demo; MbedJSONValue payload; - + MbedJSONValue body; if (ws.read(recvbuffer)) { led = !led; led2 = !led2; printf("rcv: %s\r\n", recvbuffer); - + //parse(demo, recvbuffer); - + Json json (recvbuffer, strlen ( recvbuffer ) ); if ( !json.isValidJson () ) @@ -164,11 +174,11 @@ printf("Invalid JSON. ROOT element is not Object: %s", recvbuffer); } - // Let's get the value of key "city" in ROOT object, and copy into + // Let's get the value of key "city" in ROOT object, and copy into // payload char payloadValue [256]; int payloadKeyIndex = json.findKeyIndexIn ( "payload", 0 ); - + if ( payloadKeyIndex == -1 ) { @@ -185,7 +195,7 @@ int valueLength = json.tokenLength ( payloadValueIndex ); strncpy ( payloadValue, valueStart, valueLength ); payloadValue [ valueLength ] = 0; // NULL-terminate the string - + //let's print the value. It should be "San Jose" printf( "payload: %s\n", payloadValue ); } @@ -200,22 +210,22 @@ char bodyValue[32]; int eventKeyIndex = json2.findKeyIndexIn ( "event", 0 ); - int bodyKeyIndex = json2.findKeyIndexIn ( "body", 0 ); - + int bodyKeyIndex = json2.findKeyIndexIn ( "body", 0 ); + int eventValueIndex = json2.findChildIndexOf ( eventKeyIndex, -1 ); int bodyValueIndex = json2.findChildIndexOf ( bodyKeyIndex, -1 ); - + if ( eventValueIndex > 0 ) { const char * valueStart = json2.tokenAddress ( eventValueIndex ); int valueLength = json2.tokenLength ( eventValueIndex ); strncpy ( eventValue, valueStart, valueLength ); eventValue [ valueLength ] = 0; // NULL-terminate the string - + //let's print the value. It should be "San Jose" printf( "event: %s\n", eventValue ); } - + if ( bodyValueIndex > 0 ) { const char * valueStart = json2.tokenAddress ( bodyValueIndex ); @@ -224,47 +234,90 @@ /* printf("%i\n",valueStart); printf("%i\n",valueLength); printf("%s\n",strinly);*/ - + strncpy ( bodyValue, valueStart, valueLength ); bodyValue [ valueLength ] = 0; // NULL-terminate the string - + //let's print the value. It should be "San Jose" printf( "body: %s\n", bodyValue ); - } + + // OLD ATTEMPT THAT LEADS TO STACKOVERFLOW: + // using namespace std; + // string str(bodyValue); + // istringstream iss(bodyValue); + // vector<string> tokens; + // copy(istream_iterator<string>(iss), + // istream_iterator<string>(), + // back_inserter(tokens)); + + // std::cout << "myvector contains:"; + // for (unsigned i=0; i<tokens.size(); i++) + // std::cout << ' ' << tokens.at(i); + // std::cout << '\n'; + + // NEW ATTEMPT BASED ON QUORA ANSWER + // std::vector<std::string> result; + + // std::istringstream iss(bodyValue); + // for(std::string val; iss >> val; ) { + // result.push_back(val); + // } + - + // std::cout << "myvector contains:"; + // for (unsigned i=0; i<result.size(); i++) { + // std::cout << ' ' << result.at(i); + // std::cout << '\n'; + + // ATTEMPT 3 W/ strtok http://www.cplusplus.com/reference/cstring/strtok/ + // `${topspeed} ${topdirection} ${topstop} ${bottomspeed} ${bottomdirection} ${bottomstop}` + char* topspeed = strtok(bodyValue, " "); + char* topdirection = strtok(NULL, " "); + char* topstop = strtok(NULL, " "); + char* bottomspeed = strtok(NULL, " "); + char* bottomdirection = strtok(NULL, " "); + char* bottomstop = strtok(NULL, " "); + printf( "topspeed: %s\n", topspeed ); + printf( "topdirection: %s\n", topdirection ); + printf( "topstop: %s\n", topstop ); + printf( "bottomspeed: %s\n", bottomspeed ); + printf( "bottomdirection: %s\n", bottomdirection ); + printf( "bottomstop: %s\n", bottomstop ); + } + + //my_str = demo["payload"].get<std::string>(); - + //parse(payload,my_str); //my_stringo = payload(); /*std::vector<char> b(s.length() + 1); std::strcpy(&b[0], s.c_str()); - + error_c = ws.send(&b[0]); - + std::vector<char> zeroVec; b.swap(zeroVec);*/ - + //printf("%s",payload); - + memset(recvbuffer, 0, sizeof(recvbuffer)); //recvbuffer.clear(); - } - + } + //std::string my_str; // int my_int; // bool my_bool; - + // my_str = demo[""][0].get<std::string>(); //my_int = demo["my_array"][1].get<int>(); //my_bool = demo["my_boolean"].get<bool>(); - + - + // MbedJSONValue heartbeat; // std::string s; @@ -298,7 +351,7 @@ bodyM["body"] = "miracles happen sometimes"; messageM["payload"] = bodyM;//&m[0]; messageM["ref"] = "";*/ - + //std::vector<char> m(s.length() + 1); //std::strcpy(&m[0], s.c_str()); @@ -309,7 +362,7 @@ //std::vector<char> n(s.length() + 1); //std::strcpy(&n[0], s.c_str()); - + //error_c = ws.send(&n[0]); @@ -323,7 +376,7 @@ //delete [] &bodyM; /* char *recv;*/ - + /* if (ws.read(recvbuffer)) { led = !led; led2 = !led2; @@ -331,13 +384,11 @@ memset(recvbuffer, 0, sizeof(recvbuffer)); }*/ - + counter++; printf("count: %i\n",counter); } - - wifi.disconnect(); printf("\nDone\n");
--- a/mbed_app.json Mon Dec 17 19:34:50 2018 -0500 +++ b/mbed_app.json Tue Dec 18 11:56:26 2018 -0500 @@ -6,11 +6,11 @@ }, "wifi-ssid": { "help": "WiFi SSID", - "value": "\"Daniel's iPhone\"" + "value": "\"MIT\"" }, "wifi-password": { "help": "WiFi Password", - "value": "\"doobadooba4\"" + "value": "\"\"" }, "wifi-tx": { "help": "TX pin for serial connection to external device",