Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MbedJSONValue esp32-driver WebSocketClient JSON
main.cpp@4:1b8288ef7cbc, 2018-12-18 (annotated)
- Committer:
- Tina Quach
- Date:
- Tue Dec 18 11:56:26 2018 -0500
- Revision:
- 4:1b8288ef7cbc
- Parent:
- 1:b78ddc854739
parse server messages
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DVLevine | 0:f6b0c22b2a78 | 1 | /* WiFi Example |
DVLevine | 0:f6b0c22b2a78 | 2 | * Copyright (c) 2016 ARM Limited |
DVLevine | 0:f6b0c22b2a78 | 3 | * |
DVLevine | 0:f6b0c22b2a78 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
DVLevine | 0:f6b0c22b2a78 | 5 | * you may not use this file except in compliance with the License. |
DVLevine | 0:f6b0c22b2a78 | 6 | * You may obtain a copy of the License at |
DVLevine | 0:f6b0c22b2a78 | 7 | * |
DVLevine | 0:f6b0c22b2a78 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
DVLevine | 0:f6b0c22b2a78 | 9 | * |
DVLevine | 0:f6b0c22b2a78 | 10 | * Unless required by applicable law or agreed to in writing, software |
DVLevine | 0:f6b0c22b2a78 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
DVLevine | 0:f6b0c22b2a78 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DVLevine | 0:f6b0c22b2a78 | 13 | * See the License for the specific language governing permissions and |
DVLevine | 0:f6b0c22b2a78 | 14 | * limitations under the License. |
DVLevine | 0:f6b0c22b2a78 | 15 | */ |
Tina Quach |
4:1b8288ef7cbc | 16 | #include <ios> |
Tina Quach |
4:1b8288ef7cbc | 17 | #include <stdio.h> |
Tina Quach |
4:1b8288ef7cbc | 18 | #include <string> // std::string |
Tina Quach |
4:1b8288ef7cbc | 19 | #include <iostream> // std::cout |
Tina Quach |
4:1b8288ef7cbc | 20 | #include <sstream> // std::stringstream, std::stringbuf |
Tina Quach |
4:1b8288ef7cbc | 21 | #include <vector> |
Tina Quach |
4:1b8288ef7cbc | 22 | #include <iomanip> |
Tina Quach |
4:1b8288ef7cbc | 23 | |
DVLevine | 0:f6b0c22b2a78 | 24 | |
DVLevine | 0:f6b0c22b2a78 | 25 | #include "mbed.h" |
DVLevine | 0:f6b0c22b2a78 | 26 | #include "TCPSocket.h" |
DVLevine | 0:f6b0c22b2a78 | 27 | |
DVLevine | 1:b78ddc854739 | 28 | //#include "PHX/phxSocket.h" |
DVLevine | 1:b78ddc854739 | 29 | //#include "PHX/phxChannel.h" |
DVLevine | 1:b78ddc854739 | 30 | //#include "PHX/phxMessage.h" |
DVLevine | 1:b78ddc854739 | 31 | #include "Websocket.h" |
DVLevine | 1:b78ddc854739 | 32 | #include "MbedJSONValue.h" |
DVLevine | 1:b78ddc854739 | 33 | #include "Json.h" |
DVLevine | 0:f6b0c22b2a78 | 34 | |
DVLevine | 1:b78ddc854739 | 35 | #define WIFI_ESP32 3 |
Tina Quach |
4:1b8288ef7cbc | 36 | #define TOP D5 |
DVLevine | 0:f6b0c22b2a78 | 37 | |
DVLevine | 0:f6b0c22b2a78 | 38 | |
DVLevine | 1:b78ddc854739 | 39 | //#elif TARGET_GR_LYCHEE |
DVLevine | 1:b78ddc854739 | 40 | #include "ESP32Interface.h" |
DVLevine | 1:b78ddc854739 | 41 | ESP32Interface wifi(P5_3, P3_14, P7_1, P0_1); |
DVLevine | 1:b78ddc854739 | 42 | /* |
DVLevine | 1:b78ddc854739 | 43 | #else // External WiFi modules |
DVLevine | 0:f6b0c22b2a78 | 44 | |
DVLevine | 1:b78ddc854739 | 45 | #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266 |
DVLevine | 1:b78ddc854739 | 46 | #include "ESP8266Interface.h" |
DVLevine | 1:b78ddc854739 | 47 | ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
DVLevine | 1:b78ddc854739 | 48 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
DVLevine | 1:b78ddc854739 | 49 | #include "SpwfSAInterface.h" |
DVLevine | 1:b78ddc854739 | 50 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
DVLevine | 1:b78ddc854739 | 51 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP32 |
DVLevine | 1:b78ddc854739 | 52 | #include "ESP32Interface.h" |
DVLevine | 1:b78ddc854739 | 53 | ESP32Interface wifi(MBED_CONF_APP_WIFI_EN, MBED_CONF_APP_WIFI_IO0, MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
DVLevine | 1:b78ddc854739 | 54 | #endif |
DVLevine | 0:f6b0c22b2a78 | 55 | |
DVLevine | 1:b78ddc854739 | 56 | #endif*/ |
DVLevine | 0:f6b0c22b2a78 | 57 | |
DVLevine | 1:b78ddc854739 | 58 | // Blinky |
DVLevine | 1:b78ddc854739 | 59 | DigitalOut led(LED1); |
DVLevine | 1:b78ddc854739 | 60 | DigitalOut led2(LED2); |
DVLevine | 1:b78ddc854739 | 61 | DigitalOut led3(LED3); |
DVLevine | 1:b78ddc854739 | 62 | DigitalOut led4(LED4); |
DVLevine | 0:f6b0c22b2a78 | 63 | |
Tina Quach |
4:1b8288ef7cbc | 64 | PwmOut pin(D5); |
DVLevine | 0:f6b0c22b2a78 | 65 | |
DVLevine | 0:f6b0c22b2a78 | 66 | int main() |
DVLevine | 0:f6b0c22b2a78 | 67 | { |
DVLevine | 0:f6b0c22b2a78 | 68 | int count = 0; |
DVLevine | 0:f6b0c22b2a78 | 69 | |
DVLevine | 1:b78ddc854739 | 70 | printf("WiFi example\n\n"); |
DVLevine | 0:f6b0c22b2a78 | 71 | |
DVLevine | 0:f6b0c22b2a78 | 72 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
DVLevine | 1:b78ddc854739 | 73 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
DVLevine | 0:f6b0c22b2a78 | 74 | if (ret != 0) { |
DVLevine | 1:b78ddc854739 | 75 | printf("\nConnection error\n"); |
DVLevine | 0:f6b0c22b2a78 | 76 | return -1; |
DVLevine | 0:f6b0c22b2a78 | 77 | } |
DVLevine | 0:f6b0c22b2a78 | 78 | |
DVLevine | 0:f6b0c22b2a78 | 79 | printf("Success\n\n"); |
DVLevine | 1:b78ddc854739 | 80 | printf("MAC: %s\n", wifi.get_mac_address()); |
DVLevine | 1:b78ddc854739 | 81 | printf("IP: %s\n", wifi.get_ip_address()); |
DVLevine | 1:b78ddc854739 | 82 | printf("Netmask: %s\n", wifi.get_netmask()); |
DVLevine | 1:b78ddc854739 | 83 | printf("Gateway: %s\n", wifi.get_gateway()); |
DVLevine | 1:b78ddc854739 | 84 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
DVLevine | 1:b78ddc854739 | 85 | |
DVLevine | 1:b78ddc854739 | 86 | printf("Daredevil Websocket 0.1\r\n"); |
DVLevine | 1:b78ddc854739 | 87 | printf("Opening Websocket\n"); |
DVLevine | 1:b78ddc854739 | 88 | |
DVLevine | 1:b78ddc854739 | 89 | Websocket ws("ws://dlevs.me:4000/socket/websocket", &wifi); |
DVLevine | 1:b78ddc854739 | 90 | int connect_error = ws.connect(); |
DVLevine | 1:b78ddc854739 | 91 | |
DVLevine | 1:b78ddc854739 | 92 | |
DVLevine | 1:b78ddc854739 | 93 | // ******* ITEMS TO BE REUSED A BUNCH *********/ |
DVLevine | 1:b78ddc854739 | 94 | MbedJSONValue message; |
DVLevine | 1:b78ddc854739 | 95 | std::string s; |
DVLevine | 1:b78ddc854739 | 96 | |
DVLevine | 1:b78ddc854739 | 97 | MbedJSONValue heartbeat; |
DVLevine | 1:b78ddc854739 | 98 | MbedJSONValue messageM; |
DVLevine | 1:b78ddc854739 | 99 | MbedJSONValue bodyM; |
DVLevine | 1:b78ddc854739 | 100 | int counter = 0; |
DVLevine | 1:b78ddc854739 | 101 | //char buf[50]; |
DVLevine | 1:b78ddc854739 | 102 | // *************** END **********************/ |
DVLevine | 1:b78ddc854739 | 103 | |
DVLevine | 1:b78ddc854739 | 104 | |
DVLevine | 1:b78ddc854739 | 105 | |
DVLevine | 1:b78ddc854739 | 106 | // ******* CONNECTING *********/ |
DVLevine | 1:b78ddc854739 | 107 | //fill the object |
DVLevine | 1:b78ddc854739 | 108 | message["topic"] = "room:lobby"; |
DVLevine | 1:b78ddc854739 | 109 | message["event"] = "phx_join"; |
DVLevine | 1:b78ddc854739 | 110 | message["payload"] = ""; |
DVLevine | 1:b78ddc854739 | 111 | message["ref"] = ""; |
DVLevine | 1:b78ddc854739 | 112 | |
DVLevine | 1:b78ddc854739 | 113 | //serialize it into a JSON string |
DVLevine | 1:b78ddc854739 | 114 | s = message.serialize(); |
DVLevine | 1:b78ddc854739 | 115 | //printf("json: %s\r\n", s.c_str()); |
DVLevine | 1:b78ddc854739 | 116 | |
DVLevine | 1:b78ddc854739 | 117 | //make non constant string to send |
DVLevine | 1:b78ddc854739 | 118 | |
DVLevine | 1:b78ddc854739 | 119 | //char* pc; |
DVLevine | 1:b78ddc854739 | 120 | std::vector<char> v(s.length() + 1); |
DVLevine | 1:b78ddc854739 | 121 | std::strcpy(&v[0], s.c_str()); |
DVLevine | 1:b78ddc854739 | 122 | //pc = &v[0]; |
DVLevine | 1:b78ddc854739 | 123 | |
DVLevine | 1:b78ddc854739 | 124 | |
DVLevine | 1:b78ddc854739 | 125 | char recvbuffer[32]; |
DVLevine | 1:b78ddc854739 | 126 | int error_c = ws.send(&v[0]); |
DVLevine | 1:b78ddc854739 | 127 | |
DVLevine | 1:b78ddc854739 | 128 | |
DVLevine | 1:b78ddc854739 | 129 | wait(1.0); |
DVLevine | 1:b78ddc854739 | 130 | |
DVLevine | 1:b78ddc854739 | 131 | if (ws.read(recvbuffer)) { |
DVLevine | 1:b78ddc854739 | 132 | printf("rcv: %s\r\n", recvbuffer); |
DVLevine | 1:b78ddc854739 | 133 | memset(recvbuffer, 0, sizeof(recvbuffer)); |
DVLevine | 1:b78ddc854739 | 134 | //recvbuffer.clear(); |
DVLevine | 1:b78ddc854739 | 135 | } |
DVLevine | 1:b78ddc854739 | 136 | |
DVLevine | 1:b78ddc854739 | 137 | |
DVLevine | 1:b78ddc854739 | 138 | // ******* END CONNECTING *********/ |
Tina Quach |
4:1b8288ef7cbc | 139 | |
DVLevine | 1:b78ddc854739 | 140 | |
DVLevine | 1:b78ddc854739 | 141 | // begin main loop |
DVLevine | 1:b78ddc854739 | 142 | while (true) { |
DVLevine | 1:b78ddc854739 | 143 | |
DVLevine | 1:b78ddc854739 | 144 | // blink... |
DVLevine | 1:b78ddc854739 | 145 | led3 = !led3; |
DVLevine | 1:b78ddc854739 | 146 | led4 = !led4; |
DVLevine | 1:b78ddc854739 | 147 | wait(0.2); |
DVLevine | 1:b78ddc854739 | 148 | |
DVLevine | 1:b78ddc854739 | 149 | std::string my_str; |
DVLevine | 1:b78ddc854739 | 150 | std::string my_stringo; |
DVLevine | 1:b78ddc854739 | 151 | int my_int; |
DVLevine | 1:b78ddc854739 | 152 | bool my_bool; |
DVLevine | 1:b78ddc854739 | 153 | |
DVLevine | 1:b78ddc854739 | 154 | MbedJSONValue demo; |
DVLevine | 1:b78ddc854739 | 155 | MbedJSONValue payload; |
Tina Quach |
4:1b8288ef7cbc | 156 | MbedJSONValue body; |
DVLevine | 1:b78ddc854739 | 157 | |
DVLevine | 1:b78ddc854739 | 158 | if (ws.read(recvbuffer)) { |
DVLevine | 1:b78ddc854739 | 159 | led = !led; |
DVLevine | 1:b78ddc854739 | 160 | led2 = !led2; |
DVLevine | 1:b78ddc854739 | 161 | printf("rcv: %s\r\n", recvbuffer); |
Tina Quach |
4:1b8288ef7cbc | 162 | |
DVLevine | 1:b78ddc854739 | 163 | //parse(demo, recvbuffer); |
Tina Quach |
4:1b8288ef7cbc | 164 | |
DVLevine | 1:b78ddc854739 | 165 | Json json (recvbuffer, strlen ( recvbuffer ) ); |
DVLevine | 1:b78ddc854739 | 166 | |
DVLevine | 1:b78ddc854739 | 167 | if ( !json.isValidJson () ) |
DVLevine | 1:b78ddc854739 | 168 | { |
DVLevine | 1:b78ddc854739 | 169 | printf( "Invalid JSON: %s", recvbuffer); |
DVLevine | 1:b78ddc854739 | 170 | } |
DVLevine | 1:b78ddc854739 | 171 | |
DVLevine | 1:b78ddc854739 | 172 | if ( json.type (0) != JSMN_OBJECT ) |
DVLevine | 1:b78ddc854739 | 173 | { |
DVLevine | 1:b78ddc854739 | 174 | printf("Invalid JSON. ROOT element is not Object: %s", recvbuffer); |
DVLevine | 1:b78ddc854739 | 175 | } |
DVLevine | 1:b78ddc854739 | 176 | |
Tina Quach |
4:1b8288ef7cbc | 177 | // Let's get the value of key "city" in ROOT object, and copy into |
DVLevine | 1:b78ddc854739 | 178 | // payload |
DVLevine | 1:b78ddc854739 | 179 | char payloadValue [256]; |
DVLevine | 1:b78ddc854739 | 180 | int payloadKeyIndex = json.findKeyIndexIn ( "payload", 0 ); |
Tina Quach |
4:1b8288ef7cbc | 181 | |
DVLevine | 1:b78ddc854739 | 182 | |
DVLevine | 1:b78ddc854739 | 183 | if ( payloadKeyIndex == -1 ) |
DVLevine | 1:b78ddc854739 | 184 | { |
DVLevine | 1:b78ddc854739 | 185 | // Error handling part ... |
DVLevine | 1:b78ddc854739 | 186 | printf( "\"payload\" does not exist ... do something!!" ); |
DVLevine | 1:b78ddc854739 | 187 | } |
DVLevine | 1:b78ddc854739 | 188 | else |
DVLevine | 1:b78ddc854739 | 189 | { |
DVLevine | 1:b78ddc854739 | 190 | // Find the first child index of key-node "city" |
DVLevine | 1:b78ddc854739 | 191 | int payloadValueIndex = json.findChildIndexOf ( payloadKeyIndex, -1 ); |
DVLevine | 1:b78ddc854739 | 192 | if ( payloadValueIndex > 0 ) |
DVLevine | 1:b78ddc854739 | 193 | { |
DVLevine | 1:b78ddc854739 | 194 | const char * valueStart = json.tokenAddress ( payloadValueIndex ); |
DVLevine | 1:b78ddc854739 | 195 | int valueLength = json.tokenLength ( payloadValueIndex ); |
DVLevine | 1:b78ddc854739 | 196 | strncpy ( payloadValue, valueStart, valueLength ); |
DVLevine | 1:b78ddc854739 | 197 | payloadValue [ valueLength ] = 0; // NULL-terminate the string |
Tina Quach |
4:1b8288ef7cbc | 198 | |
DVLevine | 1:b78ddc854739 | 199 | //let's print the value. It should be "San Jose" |
DVLevine | 1:b78ddc854739 | 200 | printf( "payload: %s\n", payloadValue ); |
DVLevine | 1:b78ddc854739 | 201 | } |
DVLevine | 1:b78ddc854739 | 202 | } |
DVLevine | 1:b78ddc854739 | 203 | |
DVLevine | 1:b78ddc854739 | 204 | |
DVLevine | 1:b78ddc854739 | 205 | |
DVLevine | 1:b78ddc854739 | 206 | //now look for event top or bottom |
DVLevine | 1:b78ddc854739 | 207 | // and then pull out the body |
DVLevine | 1:b78ddc854739 | 208 | Json json2 ( payloadValue, strlen (payloadValue) ); |
DVLevine | 1:b78ddc854739 | 209 | char eventValue[32]; |
DVLevine | 1:b78ddc854739 | 210 | char bodyValue[32]; |
DVLevine | 0:f6b0c22b2a78 | 211 | |
DVLevine | 1:b78ddc854739 | 212 | int eventKeyIndex = json2.findKeyIndexIn ( "event", 0 ); |
Tina Quach |
4:1b8288ef7cbc | 213 | int bodyKeyIndex = json2.findKeyIndexIn ( "body", 0 ); |
Tina Quach |
4:1b8288ef7cbc | 214 | |
DVLevine | 1:b78ddc854739 | 215 | int eventValueIndex = json2.findChildIndexOf ( eventKeyIndex, -1 ); |
DVLevine | 1:b78ddc854739 | 216 | int bodyValueIndex = json2.findChildIndexOf ( bodyKeyIndex, -1 ); |
Tina Quach |
4:1b8288ef7cbc | 217 | |
DVLevine | 1:b78ddc854739 | 218 | if ( eventValueIndex > 0 ) |
DVLevine | 1:b78ddc854739 | 219 | { |
DVLevine | 1:b78ddc854739 | 220 | const char * valueStart = json2.tokenAddress ( eventValueIndex ); |
DVLevine | 1:b78ddc854739 | 221 | int valueLength = json2.tokenLength ( eventValueIndex ); |
DVLevine | 1:b78ddc854739 | 222 | strncpy ( eventValue, valueStart, valueLength ); |
DVLevine | 1:b78ddc854739 | 223 | eventValue [ valueLength ] = 0; // NULL-terminate the string |
Tina Quach |
4:1b8288ef7cbc | 224 | |
DVLevine | 1:b78ddc854739 | 225 | //let's print the value. It should be "San Jose" |
DVLevine | 1:b78ddc854739 | 226 | printf( "event: %s\n", eventValue ); |
DVLevine | 1:b78ddc854739 | 227 | } |
Tina Quach |
4:1b8288ef7cbc | 228 | |
DVLevine | 1:b78ddc854739 | 229 | if ( bodyValueIndex > 0 ) |
DVLevine | 1:b78ddc854739 | 230 | { |
DVLevine | 1:b78ddc854739 | 231 | const char * valueStart = json2.tokenAddress ( bodyValueIndex ); |
DVLevine | 1:b78ddc854739 | 232 | int valueLength = json2.tokenLength ( bodyValueIndex ); |
DVLevine | 1:b78ddc854739 | 233 | //std::string strinly(bodyValue); |
DVLevine | 1:b78ddc854739 | 234 | /* printf("%i\n",valueStart); |
DVLevine | 1:b78ddc854739 | 235 | printf("%i\n",valueLength); |
DVLevine | 1:b78ddc854739 | 236 | printf("%s\n",strinly);*/ |
Tina Quach |
4:1b8288ef7cbc | 237 | |
DVLevine | 1:b78ddc854739 | 238 | strncpy ( bodyValue, valueStart, valueLength ); |
DVLevine | 1:b78ddc854739 | 239 | bodyValue [ valueLength ] = 0; // NULL-terminate the string |
Tina Quach |
4:1b8288ef7cbc | 240 | |
DVLevine | 1:b78ddc854739 | 241 | //let's print the value. It should be "San Jose" |
DVLevine | 1:b78ddc854739 | 242 | printf( "body: %s\n", bodyValue ); |
Tina Quach |
4:1b8288ef7cbc | 243 | |
Tina Quach |
4:1b8288ef7cbc | 244 | // OLD ATTEMPT THAT LEADS TO STACKOVERFLOW: |
Tina Quach |
4:1b8288ef7cbc | 245 | // using namespace std; |
Tina Quach |
4:1b8288ef7cbc | 246 | // string str(bodyValue); |
Tina Quach |
4:1b8288ef7cbc | 247 | // istringstream iss(bodyValue); |
Tina Quach |
4:1b8288ef7cbc | 248 | // vector<string> tokens; |
Tina Quach |
4:1b8288ef7cbc | 249 | // copy(istream_iterator<string>(iss), |
Tina Quach |
4:1b8288ef7cbc | 250 | // istream_iterator<string>(), |
Tina Quach |
4:1b8288ef7cbc | 251 | // back_inserter(tokens)); |
Tina Quach |
4:1b8288ef7cbc | 252 | |
Tina Quach |
4:1b8288ef7cbc | 253 | // std::cout << "myvector contains:"; |
Tina Quach |
4:1b8288ef7cbc | 254 | // for (unsigned i=0; i<tokens.size(); i++) |
Tina Quach |
4:1b8288ef7cbc | 255 | // std::cout << ' ' << tokens.at(i); |
Tina Quach |
4:1b8288ef7cbc | 256 | // std::cout << '\n'; |
Tina Quach |
4:1b8288ef7cbc | 257 | |
Tina Quach |
4:1b8288ef7cbc | 258 | // NEW ATTEMPT BASED ON QUORA ANSWER |
Tina Quach |
4:1b8288ef7cbc | 259 | // std::vector<std::string> result; |
Tina Quach |
4:1b8288ef7cbc | 260 | |
Tina Quach |
4:1b8288ef7cbc | 261 | // std::istringstream iss(bodyValue); |
Tina Quach |
4:1b8288ef7cbc | 262 | // for(std::string val; iss >> val; ) { |
Tina Quach |
4:1b8288ef7cbc | 263 | // result.push_back(val); |
Tina Quach |
4:1b8288ef7cbc | 264 | // } |
Tina Quach |
4:1b8288ef7cbc | 265 | |
DVLevine | 1:b78ddc854739 | 266 | |
Tina Quach |
4:1b8288ef7cbc | 267 | // std::cout << "myvector contains:"; |
Tina Quach |
4:1b8288ef7cbc | 268 | // for (unsigned i=0; i<result.size(); i++) { |
Tina Quach |
4:1b8288ef7cbc | 269 | // std::cout << ' ' << result.at(i); |
Tina Quach |
4:1b8288ef7cbc | 270 | // std::cout << '\n'; |
Tina Quach |
4:1b8288ef7cbc | 271 | |
Tina Quach |
4:1b8288ef7cbc | 272 | // ATTEMPT 3 W/ strtok http://www.cplusplus.com/reference/cstring/strtok/ |
Tina Quach |
4:1b8288ef7cbc | 273 | // `${topspeed} ${topdirection} ${topstop} ${bottomspeed} ${bottomdirection} ${bottomstop}` |
Tina Quach |
4:1b8288ef7cbc | 274 | char* topspeed = strtok(bodyValue, " "); |
Tina Quach |
4:1b8288ef7cbc | 275 | char* topdirection = strtok(NULL, " "); |
Tina Quach |
4:1b8288ef7cbc | 276 | char* topstop = strtok(NULL, " "); |
Tina Quach |
4:1b8288ef7cbc | 277 | char* bottomspeed = strtok(NULL, " "); |
Tina Quach |
4:1b8288ef7cbc | 278 | char* bottomdirection = strtok(NULL, " "); |
Tina Quach |
4:1b8288ef7cbc | 279 | char* bottomstop = strtok(NULL, " "); |
Tina Quach |
4:1b8288ef7cbc | 280 | printf( "topspeed: %s\n", topspeed ); |
Tina Quach |
4:1b8288ef7cbc | 281 | printf( "topdirection: %s\n", topdirection ); |
Tina Quach |
4:1b8288ef7cbc | 282 | printf( "topstop: %s\n", topstop ); |
Tina Quach |
4:1b8288ef7cbc | 283 | printf( "bottomspeed: %s\n", bottomspeed ); |
Tina Quach |
4:1b8288ef7cbc | 284 | printf( "bottomdirection: %s\n", bottomdirection ); |
Tina Quach |
4:1b8288ef7cbc | 285 | printf( "bottomstop: %s\n", bottomstop ); |
Tina Quach |
4:1b8288ef7cbc | 286 | } |
Tina Quach |
4:1b8288ef7cbc | 287 | |
Tina Quach |
4:1b8288ef7cbc | 288 | |
DVLevine | 1:b78ddc854739 | 289 | //my_str = demo["payload"].get<std::string>(); |
Tina Quach |
4:1b8288ef7cbc | 290 | |
DVLevine | 1:b78ddc854739 | 291 | //parse(payload,my_str); |
DVLevine | 1:b78ddc854739 | 292 | //my_stringo = payload(); |
DVLevine | 1:b78ddc854739 | 293 | |
DVLevine | 1:b78ddc854739 | 294 | /*std::vector<char> b(s.length() + 1); |
DVLevine | 1:b78ddc854739 | 295 | std::strcpy(&b[0], s.c_str()); |
Tina Quach |
4:1b8288ef7cbc | 296 | |
DVLevine | 1:b78ddc854739 | 297 | error_c = ws.send(&b[0]); |
Tina Quach |
4:1b8288ef7cbc | 298 | |
DVLevine | 1:b78ddc854739 | 299 | std::vector<char> zeroVec; |
DVLevine | 1:b78ddc854739 | 300 | b.swap(zeroVec);*/ |
DVLevine | 1:b78ddc854739 | 301 | |
DVLevine | 1:b78ddc854739 | 302 | |
Tina Quach |
4:1b8288ef7cbc | 303 | |
DVLevine | 1:b78ddc854739 | 304 | //printf("%s",payload); |
Tina Quach |
4:1b8288ef7cbc | 305 | |
DVLevine | 1:b78ddc854739 | 306 | |
DVLevine | 1:b78ddc854739 | 307 | memset(recvbuffer, 0, sizeof(recvbuffer)); |
DVLevine | 1:b78ddc854739 | 308 | //recvbuffer.clear(); |
Tina Quach |
4:1b8288ef7cbc | 309 | } |
Tina Quach |
4:1b8288ef7cbc | 310 | |
DVLevine | 1:b78ddc854739 | 311 | //std::string my_str; |
DVLevine | 1:b78ddc854739 | 312 | // int my_int; |
DVLevine | 1:b78ddc854739 | 313 | // bool my_bool; |
Tina Quach |
4:1b8288ef7cbc | 314 | |
DVLevine | 1:b78ddc854739 | 315 | // my_str = demo[""][0].get<std::string>(); |
DVLevine | 1:b78ddc854739 | 316 | //my_int = demo["my_array"][1].get<int>(); |
DVLevine | 1:b78ddc854739 | 317 | //my_bool = demo["my_boolean"].get<bool>(); |
Tina Quach |
4:1b8288ef7cbc | 318 | |
DVLevine | 1:b78ddc854739 | 319 | |
Tina Quach |
4:1b8288ef7cbc | 320 | |
DVLevine | 1:b78ddc854739 | 321 | |
DVLevine | 1:b78ddc854739 | 322 | // MbedJSONValue heartbeat; |
DVLevine | 1:b78ddc854739 | 323 | // std::string s; |
DVLevine | 0:f6b0c22b2a78 | 324 | |
DVLevine | 1:b78ddc854739 | 325 | //fill the object |
DVLevine | 1:b78ddc854739 | 326 | /* heartbeat["topic"] = "phoenix"; |
DVLevine | 1:b78ddc854739 | 327 | heartbeat["event"] = "heartbeat"; |
DVLevine | 1:b78ddc854739 | 328 | heartbeat["payload"] = ""; |
DVLevine | 1:b78ddc854739 | 329 | heartbeat["ref"] = "0"; |
DVLevine | 1:b78ddc854739 | 330 | |
DVLevine | 1:b78ddc854739 | 331 | //message["my_boolean"] = false; |
DVLevine | 1:b78ddc854739 | 332 | |
DVLevine | 1:b78ddc854739 | 333 | //serialize it into a JSON string |
DVLevine | 1:b78ddc854739 | 334 | s = heartbeat.serialize(); |
DVLevine | 1:b78ddc854739 | 335 | //printf("json: %s\r\n", s.c_str()); |
DVLevine | 1:b78ddc854739 | 336 | |
DVLevine | 1:b78ddc854739 | 337 | std::vector<char> b(s.length() + 1); |
DVLevine | 1:b78ddc854739 | 338 | std::strcpy(&b[0], s.c_str()); |
DVLevine | 1:b78ddc854739 | 339 | |
DVLevine | 1:b78ddc854739 | 340 | error_c = ws.send(&b[0]); |
DVLevine | 1:b78ddc854739 | 341 | |
DVLevine | 1:b78ddc854739 | 342 | std::vector<char> zeroVec; |
DVLevine | 1:b78ddc854739 | 343 | b.swap(zeroVec);*/ |
DVLevine | 1:b78ddc854739 | 344 | |
DVLevine | 1:b78ddc854739 | 345 | |
DVLevine | 1:b78ddc854739 | 346 | |
DVLevine | 1:b78ddc854739 | 347 | //Sample Object to fill |
DVLevine | 1:b78ddc854739 | 348 | /*messageM["topic"] = "room:lobby"; |
DVLevine | 1:b78ddc854739 | 349 | messageM["event"] = "touch"; |
DVLevine | 1:b78ddc854739 | 350 | itoa (rand(),buf,10); |
DVLevine | 1:b78ddc854739 | 351 | bodyM["body"] = "miracles happen sometimes"; |
DVLevine | 1:b78ddc854739 | 352 | messageM["payload"] = bodyM;//&m[0]; |
DVLevine | 1:b78ddc854739 | 353 | messageM["ref"] = "";*/ |
Tina Quach |
4:1b8288ef7cbc | 354 | |
DVLevine | 1:b78ddc854739 | 355 | //std::vector<char> m(s.length() + 1); |
DVLevine | 1:b78ddc854739 | 356 | //std::strcpy(&m[0], s.c_str()); |
DVLevine | 1:b78ddc854739 | 357 | |
DVLevine | 1:b78ddc854739 | 358 | //message["my_boolean"] = false; |
DVLevine | 1:b78ddc854739 | 359 | |
DVLevine | 1:b78ddc854739 | 360 | //serialize it into a JSON string |
DVLevine | 1:b78ddc854739 | 361 | //s = messageM.serialize(); |
DVLevine | 1:b78ddc854739 | 362 | |
DVLevine | 1:b78ddc854739 | 363 | //std::vector<char> n(s.length() + 1); |
DVLevine | 1:b78ddc854739 | 364 | //std::strcpy(&n[0], s.c_str()); |
Tina Quach |
4:1b8288ef7cbc | 365 | |
DVLevine | 1:b78ddc854739 | 366 | |
DVLevine | 1:b78ddc854739 | 367 | //error_c = ws.send(&n[0]); |
DVLevine | 1:b78ddc854739 | 368 | |
DVLevine | 1:b78ddc854739 | 369 | //freeing memory |
DVLevine | 1:b78ddc854739 | 370 | //std::vector<char> zeroVec; |
DVLevine | 1:b78ddc854739 | 371 | //n.swap(zeroVec); |
DVLevine | 1:b78ddc854739 | 372 | |
DVLevine | 1:b78ddc854739 | 373 | |
DVLevine | 1:b78ddc854739 | 374 | //delete [] &n; |
DVLevine | 1:b78ddc854739 | 375 | //delete [] &messageM; |
DVLevine | 1:b78ddc854739 | 376 | //delete [] &bodyM; |
DVLevine | 1:b78ddc854739 | 377 | |
DVLevine | 1:b78ddc854739 | 378 | /* char *recv;*/ |
Tina Quach |
4:1b8288ef7cbc | 379 | |
DVLevine | 1:b78ddc854739 | 380 | /* if (ws.read(recvbuffer)) { |
DVLevine | 1:b78ddc854739 | 381 | led = !led; |
DVLevine | 1:b78ddc854739 | 382 | led2 = !led2; |
DVLevine | 1:b78ddc854739 | 383 | printf("rcv: %s\r\n", recvbuffer); |
DVLevine | 1:b78ddc854739 | 384 | memset(recvbuffer, 0, sizeof(recvbuffer)); |
DVLevine | 1:b78ddc854739 | 385 | }*/ |
DVLevine | 1:b78ddc854739 | 386 | |
Tina Quach |
4:1b8288ef7cbc | 387 | |
DVLevine | 1:b78ddc854739 | 388 | counter++; |
DVLevine | 1:b78ddc854739 | 389 | printf("count: %i\n",counter); |
DVLevine | 1:b78ddc854739 | 390 | } |
DVLevine | 1:b78ddc854739 | 391 | |
DVLevine | 1:b78ddc854739 | 392 | wifi.disconnect(); |
DVLevine | 0:f6b0c22b2a78 | 393 | |
DVLevine | 0:f6b0c22b2a78 | 394 | printf("\nDone\n"); |
DVLevine | 0:f6b0c22b2a78 | 395 | } |