web server json

Dependencies:   MbedJSONValue WIZnetInterface httpServer_with_Ethernt mbed

Fork of Simple_HTTPClient_JSON by Soohwan Kim

Committer:
joon874
Date:
Thu Feb 25 07:59:46 2016 +0000
Revision:
1:97cf847e16cd
Parent:
0:e6cc33c4970b
web server json

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joon874 1:97cf847e16cd 1 //#include "mbed.h"
joon874 1:97cf847e16cd 2 //#include "EthernetInterface.h"
joon874 1:97cf847e16cd 3 //#include "MbedJSONValue.h"
embeddist 0:e6cc33c4970b 4
joon874 1:97cf847e16cd 5 /*
embeddist 0:e6cc33c4970b 6 MbedJSONValue parser;
embeddist 0:e6cc33c4970b 7 EthernetInterface eth;
embeddist 0:e6cc33c4970b 8 TCPSocketConnection sock;
embeddist 0:e6cc33c4970b 9
embeddist 0:e6cc33c4970b 10 int main()
embeddist 0:e6cc33c4970b 11 {
embeddist 0:e6cc33c4970b 12 int http_tx_msg_sz=800;
embeddist 0:e6cc33c4970b 13 char http_tx_msg[http_tx_msg_sz];
embeddist 0:e6cc33c4970b 14 int http_rx_msg_sz=500;
embeddist 0:e6cc33c4970b 15 char http_rx_msg[http_rx_msg_sz];
embeddist 0:e6cc33c4970b 16 int returnCode = 0;
embeddist 0:e6cc33c4970b 17
embeddist 0:e6cc33c4970b 18 // Enter a MAC address for your controller below.
embeddist 0:e6cc33c4970b 19 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
embeddist 0:e6cc33c4970b 20
embeddist 0:e6cc33c4970b 21 printf("initializing Ethernet\r\n");
embeddist 0:e6cc33c4970b 22 // initializing MAC address
embeddist 0:e6cc33c4970b 23 eth.init(mac_addr, "192.168.0.34", "255.255.255.0", "192.168.0.1");
embeddist 0:e6cc33c4970b 24
embeddist 0:e6cc33c4970b 25 // Check Ethenret Link
embeddist 0:e6cc33c4970b 26 if(eth.link() == true) printf("- Ethernet PHY Link-Done \r\n");
embeddist 0:e6cc33c4970b 27 else printf("- Ethernet PHY Link- Fail\r\n");
embeddist 0:e6cc33c4970b 28
embeddist 0:e6cc33c4970b 29 // Start Ethernet connecting: Trying to get an IP address using DHCP
embeddist 0:e6cc33c4970b 30 if (eth.connect()<0) printf("Fail - Ethernet Connecing");
embeddist 0:e6cc33c4970b 31
embeddist 0:e6cc33c4970b 32 // Print your local IP address:
embeddist 0:e6cc33c4970b 33 printf("IP=%s\n\r",eth.getIPAddress());
embeddist 0:e6cc33c4970b 34 printf("MASK=%s\n\r",eth.getNetworkMask());
embeddist 0:e6cc33c4970b 35 printf("GW=%s\n\r",eth.getGateway());
embeddist 0:e6cc33c4970b 36
embeddist 0:e6cc33c4970b 37 while(1) {
embeddist 0:e6cc33c4970b 38 sock.connect("192.168.0.223", 8000);
embeddist 0:e6cc33c4970b 39
embeddist 0:e6cc33c4970b 40 snprintf(http_tx_msg, http_tx_msg_sz, "GET / HTTP/1.1\r\nHost: 192.168.0.223:8000\r\nUser-Agent: WIZwiki-W7500ECO\r\nConection: close\r\n\r\n");
embeddist 0:e6cc33c4970b 41 sock.send_all(http_tx_msg, http_tx_msg_sz-1);
embeddist 0:e6cc33c4970b 42
embeddist 0:e6cc33c4970b 43 while ( (returnCode = sock.receive(http_rx_msg, http_rx_msg_sz-1)) > 0) {
embeddist 0:e6cc33c4970b 44 http_rx_msg[returnCode] = '\0';
embeddist 0:e6cc33c4970b 45 printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg);
embeddist 0:e6cc33c4970b 46 }
embeddist 0:e6cc33c4970b 47
embeddist 0:e6cc33c4970b 48 sock.close();
embeddist 0:e6cc33c4970b 49
embeddist 0:e6cc33c4970b 50 parse(parser, http_rx_msg);
embeddist 0:e6cc33c4970b 51 printf("name =%s\r\n" , parser["name"].get<string>().c_str());
embeddist 0:e6cc33c4970b 52 printf("age =%d\r\n" , parser["age"].get<int>());
embeddist 0:e6cc33c4970b 53 printf("gender =%s\r\n" , parser["gender"].get<string>().c_str());
embeddist 0:e6cc33c4970b 54
embeddist 0:e6cc33c4970b 55 wait(10);
embeddist 0:e6cc33c4970b 56 }
embeddist 0:e6cc33c4970b 57
embeddist 0:e6cc33c4970b 58 }
joon874 1:97cf847e16cd 59 */
joon874 1:97cf847e16cd 60
joon874 1:97cf847e16cd 61
joon874 1:97cf847e16cd 62 #include "mbed.h"
joon874 1:97cf847e16cd 63 #include "EthernetInterface.h"
joon874 1:97cf847e16cd 64 #include "MbedJSONValue.h"
joon874 1:97cf847e16cd 65
joon874 1:97cf847e16cd 66
joon874 1:97cf847e16cd 67 EthernetInterface eth;
joon874 1:97cf847e16cd 68 TCPSocketConnection sock;
joon874 1:97cf847e16cd 69
joon874 1:97cf847e16cd 70 MbedJSONValue WIZwikiREST;
joon874 1:97cf847e16cd 71 MbedJSONValue GPIOs;
joon874 1:97cf847e16cd 72
joon874 1:97cf847e16cd 73 char ip_addr[] = "192.168.0.100";
joon874 1:97cf847e16cd 74 char subnet_mask[] = "255.255.255.0";
joon874 1:97cf847e16cd 75 char gateway_addr[] = "192.168.0.1";
joon874 1:97cf847e16cd 76
joon874 1:97cf847e16cd 77 int main() {
joon874 1:97cf847e16cd 78
joon874 1:97cf847e16cd 79 std::string s;
joon874 1:97cf847e16cd 80
joon874 1:97cf847e16cd 81 // Fill the object
joon874 1:97cf847e16cd 82 WIZwikiREST["Name"] = "WIZwiki-REST-01";
joon874 1:97cf847e16cd 83 WIZwikiREST["Network"]["IP"] = ip_addr;
joon874 1:97cf847e16cd 84 WIZwikiREST["Network"]["SN"] = subnet_mask;
joon874 1:97cf847e16cd 85 WIZwikiREST["Network"]["GW"] = gateway_addr;
joon874 1:97cf847e16cd 86 WIZwikiREST["User"]["Name"] = "Lawrence";
joon874 1:97cf847e16cd 87 WIZwikiREST["User"]["ID"] = "law";
joon874 1:97cf847e16cd 88 WIZwikiREST["User"]["PSWD"] = "law1234";
joon874 1:97cf847e16cd 89 GPIOs["P5"]["Mode"] = "DIO";
joon874 1:97cf847e16cd 90 GPIOs["P6"]["Mode"] = "DIO";
joon874 1:97cf847e16cd 91 WIZwikiREST["GPIOs"] = GPIOs;
joon874 1:97cf847e16cd 92
joon874 1:97cf847e16cd 93
joon874 1:97cf847e16cd 94 // Serialize it into a JSON string
joon874 1:97cf847e16cd 95 s = WIZwikiREST.serialize();
joon874 1:97cf847e16cd 96 printf("json: %s\r\n", s.c_str());
joon874 1:97cf847e16cd 97 string item = "Name";
joon874 1:97cf847e16cd 98 printf("WIZwikiREST[item] =%s", WIZwikiREST[item].get<string>().c_str());
joon874 1:97cf847e16cd 99
joon874 1:97cf847e16cd 100
joon874 1:97cf847e16cd 101 int http_tx_msg_sz=800;
joon874 1:97cf847e16cd 102 char http_tx_msg[http_tx_msg_sz];
joon874 1:97cf847e16cd 103 int http_rx_msg_sz=500;
joon874 1:97cf847e16cd 104 char http_rx_msg[http_rx_msg_sz];
joon874 1:97cf847e16cd 105 int returnCode = 0;
joon874 1:97cf847e16cd 106
joon874 1:97cf847e16cd 107 // Enter a MAC address for your controller below.
joon874 1:97cf847e16cd 108 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE};
joon874 1:97cf847e16cd 109
joon874 1:97cf847e16cd 110 // initializing MAC address
joon874 1:97cf847e16cd 111 printf("initializing Ethernet\r\n");
joon874 1:97cf847e16cd 112 eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr);
joon874 1:97cf847e16cd 113
joon874 1:97cf847e16cd 114 // Check Ethernet Link
joon874 1:97cf847e16cd 115 printf("Check Ethernet Link\r\n");
joon874 1:97cf847e16cd 116 while(1) //Wait link up
joon874 1:97cf847e16cd 117 {
joon874 1:97cf847e16cd 118 if(eth.link() == true)
joon874 1:97cf847e16cd 119 break;
joon874 1:97cf847e16cd 120 }
joon874 1:97cf847e16cd 121 printf("Link up\r\n");
joon874 1:97cf847e16cd 122
joon874 1:97cf847e16cd 123 // Connect
joon874 1:97cf847e16cd 124 eth.connect();
joon874 1:97cf847e16cd 125 printf("Server IP Address is %s\r\n", eth.getIPAddress());
joon874 1:97cf847e16cd 126
joon874 1:97cf847e16cd 127 if (!svr.start(80, &eth)) {
joon874 1:97cf847e16cd 128
joon874 1:97cf847e16cd 129 error("Server not starting !");
joon874 1:97cf847e16cd 130 exit(0);
joon874 1:97cf847e16cd 131 }
joon874 1:97cf847e16cd 132
joon874 1:97cf847e16cd 133 while(1) {
joon874 1:97cf847e16cd 134 svr.poll();
joon874 1:97cf847e16cd 135 }
joon874 1:97cf847e16cd 136
joon874 1:97cf847e16cd 137 /*
joon874 1:97cf847e16cd 138 while(1) {
joon874 1:97cf847e16cd 139 sock.connect("192.168.0.223", 8000);
joon874 1:97cf847e16cd 140
joon874 1:97cf847e16cd 141 snprintf(http_tx_msg, http_tx_msg_sz, "GET / HTTP/1.1\r\nHost: 192.168.0.223:8000\r\nUser-Agent: WIZwiki-W7500ECO\r\nConection: close\r\n\r\n");
joon874 1:97cf847e16cd 142 sock.send_all(http_tx_msg, http_tx_msg_sz-1);
joon874 1:97cf847e16cd 143
joon874 1:97cf847e16cd 144 while ( (returnCode = sock.receive(http_rx_msg, http_rx_msg_sz-1)) > 0) {
joon874 1:97cf847e16cd 145 http_rx_msg[returnCode] = '\0';
joon874 1:97cf847e16cd 146 printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg);
joon874 1:97cf847e16cd 147 }
joon874 1:97cf847e16cd 148
joon874 1:97cf847e16cd 149 sock.close();
joon874 1:97cf847e16cd 150
joon874 1:97cf847e16cd 151 parse(parser, http_rx_msg);
joon874 1:97cf847e16cd 152 printf("name =%s\r\n" , parser["name"].get<string>().c_str());
joon874 1:97cf847e16cd 153 printf("age =%d\r\n" , parser["age"].get<int>());
joon874 1:97cf847e16cd 154 printf("gender =%s\r\n" , parser["gender"].get<string>().c_str());
joon874 1:97cf847e16cd 155
joon874 1:97cf847e16cd 156 wait(10);
joon874 1:97cf847e16cd 157 }
joon874 1:97cf847e16cd 158 */
joon874 1:97cf847e16cd 159
joon874 1:97cf847e16cd 160 }
joon874 1:97cf847e16cd 161
joon874 1:97cf847e16cd 162
joon874 1:97cf847e16cd 163
joon874 1:97cf847e16cd 164
joon874 1:97cf847e16cd 165 /** MbedJSONValue class
joon874 1:97cf847e16cd 166 *
joon874 1:97cf847e16cd 167 * Example:
joon874 1:97cf847e16cd 168 * - creation of an MbedJSONValue of type TypeObject containing two others MbedJSONValue:
joon874 1:97cf847e16cd 169 * -one array of one string and one integer identified by "my_array"
joon874 1:97cf847e16cd 170 * -a boolean identified by "my_boolean"
joon874 1:97cf847e16cd 171 * - serialization in JSON format of this object
joon874 1:97cf847e16cd 172 * @code
joon874 1:97cf847e16cd 173 * #include "mbed.h"
joon874 1:97cf847e16cd 174 * #include "MbedJSONValue.h"
joon874 1:97cf847e16cd 175 * #include <string>
joon874 1:97cf847e16cd 176 *
joon874 1:97cf847e16cd 177 * int main() {
joon874 1:97cf847e16cd 178 *
joon874 1:97cf847e16cd 179 * MbedJSONValue demo;
joon874 1:97cf847e16cd 180 * std::string s;
joon874 1:97cf847e16cd 181 *
joon874 1:97cf847e16cd 182 * //fill the object
joon874 1:97cf847e16cd 183 * demo["my_array"][0] = "demo_string";
joon874 1:97cf847e16cd 184 * demo["my_array"][1] = 10;
joon874 1:97cf847e16cd 185 * demo["my_boolean"] = false;
joon874 1:97cf847e16cd 186 *
joon874 1:97cf847e16cd 187 * //serialize it into a JSON string
joon874 1:97cf847e16cd 188 * s = demo.serialize();
joon874 1:97cf847e16cd 189 * printf("json: %s\r\n", s.c_str());
joon874 1:97cf847e16cd 190 * }
joon874 1:97cf847e16cd 191 *
joon874 1:97cf847e16cd 192 * @endcode
joon874 1:97cf847e16cd 193 *
joon874 1:97cf847e16cd 194 * Example:
joon874 1:97cf847e16cd 195 * - creation of an MbedJSONValue from a JSON string
joon874 1:97cf847e16cd 196 * - extraction of different values from this existing MbedJSONValue
joon874 1:97cf847e16cd 197 * @code
joon874 1:97cf847e16cd 198 * #include "mbed.h"
joon874 1:97cf847e16cd 199 * #include "MbedJSONValue.h"
joon874 1:97cf847e16cd 200 * #include <string>
joon874 1:97cf847e16cd 201 *
joon874 1:97cf847e16cd 202 * int main() {
joon874 1:97cf847e16cd 203 * MbedJSONValue demo;
joon874 1:97cf847e16cd 204 *
joon874 1:97cf847e16cd 205 * const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
joon874 1:97cf847e16cd 206 *
joon874 1:97cf847e16cd 207 * //parse the previous string and fill the object demo
joon874 1:97cf847e16cd 208 * parse(demo, json);
joon874 1:97cf847e16cd 209 *
joon874 1:97cf847e16cd 210 * std::string my_str;
joon874 1:97cf847e16cd 211 * int my_int;
joon874 1:97cf847e16cd 212 * bool my_bool;
joon874 1:97cf847e16cd 213 *
joon874 1:97cf847e16cd 214 * my_str = demo["my_array"][0].get<std::string>();
joon874 1:97cf847e16cd 215 * my_int = demo["my_array"][1].get<int>();
joon874 1:97cf847e16cd 216 * my_bool = demo["my_boolean"].get<bool>();
joon874 1:97cf847e16cd 217 *
joon874 1:97cf847e16cd 218 * printf("my_str: %s\r\n", my_str.c_str());
joon874 1:97cf847e16cd 219 * printf("my_int: %d\r\n", my_int);
joon874 1:97cf847e16cd 220 * printf("my_bool: %s\r\n", my_bool ? "true" : "false");
joon874 1:97cf847e16cd 221 * }
joon874 1:97cf847e16cd 222 * @endcode
joon874 1:97cf847e16cd 223 */