![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
WIZwikiREST-io exercise code for WIZnet Academy
Dependencies: MbedJSONValue_v102 WIZnetInterface mbed
Fork of WIZwiki-REST-io_v103 by
main.cpp@8:60d99da6eeb2, 2016-09-05 (annotated)
- Committer:
- joon874
- Date:
- Mon Sep 05 06:29:55 2016 +0000
- Revision:
- 8:60d99da6eeb2
- Parent:
- 6:2974bbc94e7a
- Child:
- 9:f7d5a168a693
Add PUT method
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joon874 | 0:5886f525a4ad | 1 | #include "mbed.h" |
joon874 | 0:5886f525a4ad | 2 | #include "HTTPServer.h" |
joon874 | 0:5886f525a4ad | 3 | #include "RequestHandler.h" |
joon874 | 0:5886f525a4ad | 4 | #include "EthernetInterface.h" |
joon874 | 0:5886f525a4ad | 5 | #include "MbedJSONValue.h" |
joon874 | 0:5886f525a4ad | 6 | |
joon874 | 0:5886f525a4ad | 7 | #define SERVER_PORT 80 |
joon874 | 8:60d99da6eeb2 | 8 | //#define DHCP |
joon874 | 4:0b5a199e91f3 | 9 | //#define DEBUG |
joon874 | 4:0b5a199e91f3 | 10 | |
joon874 | 5:473a66ae9596 | 11 | //-- I2C OLED -- |
joon874 | 8:60d99da6eeb2 | 12 | #include "Adafruit_SSD1306.h" |
joon874 | 8:60d99da6eeb2 | 13 | |
joon874 | 5:473a66ae9596 | 14 | class I2CPreInit : public I2C |
joon874 | 5:473a66ae9596 | 15 | { |
joon874 | 5:473a66ae9596 | 16 | public: |
joon874 | 5:473a66ae9596 | 17 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
joon874 | 5:473a66ae9596 | 18 | { |
joon874 | 5:473a66ae9596 | 19 | frequency(100000); |
joon874 | 5:473a66ae9596 | 20 | start(); |
joon874 | 5:473a66ae9596 | 21 | }; |
joon874 | 5:473a66ae9596 | 22 | }; |
joon874 | 6:2974bbc94e7a | 23 | |
joon874 | 6:2974bbc94e7a | 24 | //-- I2C OLED -- |
joon874 | 5:473a66ae9596 | 25 | I2CPreInit gI2C(PA_10,PA_9); |
joon874 | 5:473a66ae9596 | 26 | Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
joon874 | 5:473a66ae9596 | 27 | |
joon874 | 5:473a66ae9596 | 28 | //-- PWM DC -- |
joon874 | 5:473a66ae9596 | 29 | PwmOut DC(D6); |
joon874 | 5:473a66ae9596 | 30 | |
joon874 | 6:2974bbc94e7a | 31 | //-- GPIO LED -- |
joon874 | 5:473a66ae9596 | 32 | DigitalInOut GP05(D5); |
joon874 | 5:473a66ae9596 | 33 | |
joon874 | 8:60d99da6eeb2 | 34 | DigitalInOut GP04(D4); |
joon874 | 8:60d99da6eeb2 | 35 | DigitalInOut GP03(D3); |
joon874 | 8:60d99da6eeb2 | 36 | |
joon874 | 8:60d99da6eeb2 | 37 | |
joon874 | 6:2974bbc94e7a | 38 | //-- ADC -- |
joon874 | 6:2974bbc94e7a | 39 | AnalogIn ain(A0); |
joon874 | 6:2974bbc94e7a | 40 | |
joon874 | 0:5886f525a4ad | 41 | EthernetInterface eth; |
joon874 | 0:5886f525a4ad | 42 | HTTPServer WIZwikiWebSvr; |
joon874 | 0:5886f525a4ad | 43 | MbedJSONValue WIZwikiREST; |
joon874 | 0:5886f525a4ad | 44 | |
joon874 | 0:5886f525a4ad | 45 | // Enter a MAC address for your controller below. |
joon874 | 0:5886f525a4ad | 46 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE}; |
joon874 | 0:5886f525a4ad | 47 | char mac_str[20]; |
joon874 | 4:0b5a199e91f3 | 48 | char ip_addr[] = "192.168.100.100"; |
joon874 | 4:0b5a199e91f3 | 49 | char subnet_mask[] = "255.255.255.0"; |
joon874 | 0:5886f525a4ad | 50 | char gateway_addr[] = "192.168.100.1"; |
joon874 | 6:2974bbc94e7a | 51 | float ain_value; |
joon874 | 0:5886f525a4ad | 52 | |
joon874 | 8:60d99da6eeb2 | 53 | GetRequestHandler myGetReq; |
joon874 | 8:60d99da6eeb2 | 54 | //PostRequestHandler myPostReq; |
joon874 | 8:60d99da6eeb2 | 55 | PutRequestHandler myPutReq; |
joon874 | 4:0b5a199e91f3 | 56 | |
joon874 | 5:473a66ae9596 | 57 | //-- I2C OLED -- |
joon874 | 5:473a66ae9596 | 58 | bool oled_set(void* param) |
joon874 | 4:0b5a199e91f3 | 59 | { |
joon874 | 5:473a66ae9596 | 60 | char * oled; |
joon874 | 4:0b5a199e91f3 | 61 | if(!param) return false; |
joon874 | 5:473a66ae9596 | 62 | oled = (char*) param; |
joon874 | 5:473a66ae9596 | 63 | gOled.clearDisplay(); |
joon874 | 5:473a66ae9596 | 64 | gOled.setTextCursor(0,0); |
joon874 | 5:473a66ae9596 | 65 | gOled.printf("%s",oled); |
joon874 | 5:473a66ae9596 | 66 | gOled.display(); |
joon874 | 4:0b5a199e91f3 | 67 | return true; |
joon874 | 4:0b5a199e91f3 | 68 | } |
joon874 | 5:473a66ae9596 | 69 | //-- PWM DC -- |
joon874 | 5:473a66ae9596 | 70 | bool pwm_set(void* param) |
joon874 | 5:473a66ae9596 | 71 | { |
joon874 | 5:473a66ae9596 | 72 | if(!param) return false; |
joon874 | 5:473a66ae9596 | 73 | DC.write((float)(*(int*)param)/100.0); |
joon874 | 5:473a66ae9596 | 74 | return true; |
joon874 | 5:473a66ae9596 | 75 | } |
joon874 | 5:473a66ae9596 | 76 | //-- GPIO -- |
MidnightCow | 1:728e5b5c8dae | 77 | bool p5_set(void* param) |
MidnightCow | 1:728e5b5c8dae | 78 | { |
joon874 | 6:2974bbc94e7a | 79 | if(!param) return false; |
MidnightCow | 1:728e5b5c8dae | 80 | GP05.write(*(int*)param); |
MidnightCow | 1:728e5b5c8dae | 81 | return true; |
MidnightCow | 1:728e5b5c8dae | 82 | } |
joon874 | 8:60d99da6eeb2 | 83 | bool p4_set(void* param) |
joon874 | 8:60d99da6eeb2 | 84 | { |
joon874 | 8:60d99da6eeb2 | 85 | if(!param) return false; |
joon874 | 8:60d99da6eeb2 | 86 | GP04.write(*(int*)param); |
joon874 | 8:60d99da6eeb2 | 87 | return true; |
joon874 | 8:60d99da6eeb2 | 88 | } |
joon874 | 8:60d99da6eeb2 | 89 | bool p3_set(void* param) |
joon874 | 8:60d99da6eeb2 | 90 | { |
joon874 | 8:60d99da6eeb2 | 91 | if(!param) return false; |
joon874 | 8:60d99da6eeb2 | 92 | GP03.write(*(int*)param); |
joon874 | 8:60d99da6eeb2 | 93 | return true; |
joon874 | 8:60d99da6eeb2 | 94 | } |
joon874 | 8:60d99da6eeb2 | 95 | |
joon874 | 6:2974bbc94e7a | 96 | //-- ADC -- |
joon874 | 6:2974bbc94e7a | 97 | bool ain_read(void* param) |
joon874 | 6:2974bbc94e7a | 98 | { |
joon874 | 6:2974bbc94e7a | 99 | ((MbedJSONValue*)param)->_value.asDouble = ain.read(); |
joon874 | 6:2974bbc94e7a | 100 | return true; |
joon874 | 6:2974bbc94e7a | 101 | } |
MidnightCow | 1:728e5b5c8dae | 102 | |
MidnightCow | 1:728e5b5c8dae | 103 | void debug_info() |
MidnightCow | 1:728e5b5c8dae | 104 | { |
MidnightCow | 1:728e5b5c8dae | 105 | printf("SP:0x%X\r\n",__current_sp()); |
MidnightCow | 1:728e5b5c8dae | 106 | __heapstats((__heapprt)fprintf,stderr); |
joon874 | 4:0b5a199e91f3 | 107 | #ifdef DEBUG |
joon874 | 4:0b5a199e91f3 | 108 | __heapvalid((__heapprt)fprintf,stderr, 1); |
joon874 | 4:0b5a199e91f3 | 109 | #endif |
MidnightCow | 1:728e5b5c8dae | 110 | printf("\r\n"); |
MidnightCow | 1:728e5b5c8dae | 111 | } |
joon874 | 4:0b5a199e91f3 | 112 | |
joon874 | 0:5886f525a4ad | 113 | void WIZwiki_REST_init(); |
joon874 | 0:5886f525a4ad | 114 | |
joon874 | 0:5886f525a4ad | 115 | int main(void) |
joon874 | 0:5886f525a4ad | 116 | { |
joon874 | 4:0b5a199e91f3 | 117 | #ifdef DEBUG |
joon874 | 4:0b5a199e91f3 | 118 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 119 | #endif |
joon874 | 4:0b5a199e91f3 | 120 | sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X",mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]); |
joon874 | 4:0b5a199e91f3 | 121 | |
joon874 | 5:473a66ae9596 | 122 | // OLED init |
joon874 | 5:473a66ae9596 | 123 | gOled.begin(); |
joon874 | 5:473a66ae9596 | 124 | gOled.clearDisplay(); |
joon874 | 5:473a66ae9596 | 125 | |
joon874 | 5:473a66ae9596 | 126 | // PWM init |
joon874 | 5:473a66ae9596 | 127 | DC.period_ms(1); |
joon874 | 5:473a66ae9596 | 128 | DC.write(0); |
joon874 | 5:473a66ae9596 | 129 | |
joon874 | 5:473a66ae9596 | 130 | //GPIO set & init |
MidnightCow | 1:728e5b5c8dae | 131 | GP05.output(); |
joon874 | 2:ca36ecca24c2 | 132 | GP05.write(1); |
joon874 | 6:2974bbc94e7a | 133 | |
joon874 | 6:2974bbc94e7a | 134 | //ADC init |
joon874 | 8:60d99da6eeb2 | 135 | |
MidnightCow | 1:728e5b5c8dae | 136 | printf("START \r\n"); |
MidnightCow | 1:728e5b5c8dae | 137 | printf("sizeof(MbedJSONValue)=%d\r\n",sizeof(MbedJSONValue)); |
MidnightCow | 1:728e5b5c8dae | 138 | printf("sizeof(vector)=%d\r\n",sizeof(std::vector<string*>)); |
MidnightCow | 1:728e5b5c8dae | 139 | printf("sizeof(string)=%d\r\n",sizeof(std::string)); |
joon874 | 4:0b5a199e91f3 | 140 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 141 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 142 | #endif |
joon874 | 0:5886f525a4ad | 143 | |
joon874 | 0:5886f525a4ad | 144 | WIZwiki_REST_init(); |
MidnightCow | 1:728e5b5c8dae | 145 | |
joon874 | 4:0b5a199e91f3 | 146 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 147 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 148 | #endif |
MidnightCow | 1:728e5b5c8dae | 149 | |
joon874 | 0:5886f525a4ad | 150 | // Serialize it into a JSON string |
joon874 | 8:60d99da6eeb2 | 151 | printf("---------------------WIZwikiREST-------------------- \r\n"); |
joon874 | 4:0b5a199e91f3 | 152 | printf("\r\n%s\r\n", WIZwikiREST.serialize().c_str()); |
joon874 | 8:60d99da6eeb2 | 153 | printf("---------------------------------------------------- \r\n"); |
joon874 | 0:5886f525a4ad | 154 | |
joon874 | 4:0b5a199e91f3 | 155 | WIZwikiWebSvr.add_request_handler("GET", &myGetReq); |
joon874 | 8:60d99da6eeb2 | 156 | //WIZwikiWebSvr.add_request_handler("POST", &myPostReq); |
joon874 | 8:60d99da6eeb2 | 157 | WIZwikiWebSvr.add_request_handler("PUT", &myPutReq); |
joon874 | 4:0b5a199e91f3 | 158 | //WIZwikiWebSvr.add_request_handler("DELETE", new PostRequestHandler()); |
joon874 | 0:5886f525a4ad | 159 | |
joon874 | 0:5886f525a4ad | 160 | #ifdef DHCP |
joon874 | 0:5886f525a4ad | 161 | eth.init(mac_addr); //Use DHCP |
joon874 | 0:5886f525a4ad | 162 | #else |
joon874 | 0:5886f525a4ad | 163 | eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP |
joon874 | 0:5886f525a4ad | 164 | #endif |
joon874 | 0:5886f525a4ad | 165 | |
joon874 | 2:ca36ecca24c2 | 166 | |
joon874 | 0:5886f525a4ad | 167 | printf("Check Ethernet Link\r\n"); |
joon874 | 4:0b5a199e91f3 | 168 | |
joon874 | 2:ca36ecca24c2 | 169 | do{ |
joon874 | 2:ca36ecca24c2 | 170 | printf(" Link - Wait... \r\n"); |
joon874 | 2:ca36ecca24c2 | 171 | wait(1); |
joon874 | 2:ca36ecca24c2 | 172 | }while(!eth.ethernet_link()); |
joon874 | 2:ca36ecca24c2 | 173 | printf("-- Ethetnet PHY Link - Done -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 174 | |
joon874 | 2:ca36ecca24c2 | 175 | if (eth.connect() < 0 ) |
joon874 | 2:ca36ecca24c2 | 176 | printf("-- EThernet Connect - Fail -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 177 | else |
joon874 | 0:5886f525a4ad | 178 | { |
joon874 | 2:ca36ecca24c2 | 179 | printf("-- Assigned Network Information -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 180 | printf(" IP : %s\r\n\r\n", eth.getIPAddress()); |
joon874 | 2:ca36ecca24c2 | 181 | printf(" MASK : %s\r\n\r\n", eth.getNetworkMask()); |
joon874 | 2:ca36ecca24c2 | 182 | printf(" GW : %s\r\n\r\n", eth.getGateway()); |
joon874 | 0:5886f525a4ad | 183 | } |
joon874 | 0:5886f525a4ad | 184 | |
joon874 | 0:5886f525a4ad | 185 | printf("Link up\r\n"); |
joon874 | 0:5886f525a4ad | 186 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
joon874 | 4:0b5a199e91f3 | 187 | #ifdef DEBUG |
joon874 | 4:0b5a199e91f3 | 188 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 189 | #endif |
joon874 | 0:5886f525a4ad | 190 | |
joon874 | 0:5886f525a4ad | 191 | if(!WIZwikiWebSvr.init(SERVER_PORT)) |
joon874 | 0:5886f525a4ad | 192 | { |
joon874 | 0:5886f525a4ad | 193 | eth.disconnect(); |
joon874 | 0:5886f525a4ad | 194 | return -1; |
joon874 | 0:5886f525a4ad | 195 | } |
joon874 | 0:5886f525a4ad | 196 | |
joon874 | 0:5886f525a4ad | 197 | while(1) |
joon874 | 0:5886f525a4ad | 198 | { |
joon874 | 0:5886f525a4ad | 199 | WIZwikiWebSvr.run(); |
joon874 | 0:5886f525a4ad | 200 | } |
joon874 | 0:5886f525a4ad | 201 | } |
joon874 | 0:5886f525a4ad | 202 | |
joon874 | 0:5886f525a4ad | 203 | void WIZwiki_REST_init(void) |
joon874 | 0:5886f525a4ad | 204 | { |
joon874 | 0:5886f525a4ad | 205 | //Fill the object |
joon874 | 6:2974bbc94e7a | 206 | WIZwikiREST["Name"] = "WIZwikiREST-io ver1.01"; |
MidnightCow | 1:728e5b5c8dae | 207 | WIZwikiREST["Name"].accessible = false; |
joon874 | 4:0b5a199e91f3 | 208 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 209 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 210 | #endif |
MidnightCow | 1:728e5b5c8dae | 211 | |
joon874 | 0:5886f525a4ad | 212 | //Network |
joon874 | 0:5886f525a4ad | 213 | WIZwikiREST["Network"]["MAC"] = mac_str; |
joon874 | 0:5886f525a4ad | 214 | WIZwikiREST["Network"]["IP"] = ip_addr; |
MidnightCow | 1:728e5b5c8dae | 215 | WIZwikiREST["Network"]["IP"].accessible = true; |
joon874 | 0:5886f525a4ad | 216 | WIZwikiREST["Network"]["SN"] = subnet_mask; |
MidnightCow | 1:728e5b5c8dae | 217 | WIZwikiREST["Network"]["SN"].accessible = true; |
joon874 | 0:5886f525a4ad | 218 | WIZwikiREST["Network"]["GW"] = gateway_addr; |
MidnightCow | 1:728e5b5c8dae | 219 | WIZwikiREST["Network"]["GW"].accessible = true; |
joon874 | 5:473a66ae9596 | 220 | |
joon874 | 5:473a66ae9596 | 221 | // I2C OLED |
joon874 | 5:473a66ae9596 | 222 | WIZwikiREST["I2C"]["OLED"] = "none"; |
joon874 | 5:473a66ae9596 | 223 | WIZwikiREST["I2C"]["OLED"].accessible = true; |
joon874 | 5:473a66ae9596 | 224 | WIZwikiREST["I2C"]["OLED"].cb_action = oled_set; |
joon874 | 4:0b5a199e91f3 | 225 | |
joon874 | 5:473a66ae9596 | 226 | // PWM DC |
joon874 | 5:473a66ae9596 | 227 | WIZwikiREST["PWM"]["DC"] = DC.read(); |
joon874 | 5:473a66ae9596 | 228 | WIZwikiREST["PWM"]["DC"].accessible = true; |
joon874 | 5:473a66ae9596 | 229 | WIZwikiREST["PWM"]["DC"].cb_action = pwm_set; |
joon874 | 5:473a66ae9596 | 230 | |
joon874 | 5:473a66ae9596 | 231 | // GPIO |
joon874 | 8:60d99da6eeb2 | 232 | WIZwikiREST["GPIOs"]["P03"] = GP03.read(); |
joon874 | 8:60d99da6eeb2 | 233 | WIZwikiREST["GPIOs"]["P03"].accessible = true; |
joon874 | 8:60d99da6eeb2 | 234 | WIZwikiREST["GPIOs"]["P03"].cb_action = p3_set; |
joon874 | 8:60d99da6eeb2 | 235 | WIZwikiREST["GPIOs"]["P04"] = GP04.read(); |
joon874 | 8:60d99da6eeb2 | 236 | WIZwikiREST["GPIOs"]["P04"].accessible = true; |
joon874 | 8:60d99da6eeb2 | 237 | WIZwikiREST["GPIOs"]["P04"].cb_action = p4_set;\ |
MidnightCow | 1:728e5b5c8dae | 238 | WIZwikiREST["GPIOs"]["P05"] = GP05.read(); |
MidnightCow | 1:728e5b5c8dae | 239 | WIZwikiREST["GPIOs"]["P05"].accessible = true; |
MidnightCow | 1:728e5b5c8dae | 240 | WIZwikiREST["GPIOs"]["P05"].cb_action = p5_set; |
joon874 | 6:2974bbc94e7a | 241 | |
joon874 | 6:2974bbc94e7a | 242 | // ADC |
joon874 | 6:2974bbc94e7a | 243 | WIZwikiREST["ADC"]["A0"] = ain.read(); |
joon874 | 6:2974bbc94e7a | 244 | WIZwikiREST["ADC"]["A0"].accessible = false; |
joon874 | 6:2974bbc94e7a | 245 | WIZwikiREST["ADC"]["A0"].cb_action = ain_read; |
joon874 | 4:0b5a199e91f3 | 246 | |
joon874 | 4:0b5a199e91f3 | 247 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 248 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 249 | #endif |
joon874 | 0:5886f525a4ad | 250 | } |