![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
WIZwiki-REST-io ver.2 Add POST Method
Dependencies: MbedJSONValue_copy WIZnetInterface mbed
Fork of WIZwiki-REST_mc by
main.cpp@0:5886f525a4ad, 2016-03-02 (annotated)
- Committer:
- joon874
- Date:
- Wed Mar 02 03:28:08 2016 +0000
- Revision:
- 0:5886f525a4ad
- Child:
- 1:728e5b5c8dae
WIZwikiREST ver1 (Memory problem)
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 | 0:5886f525a4ad | 8 | |
joon874 | 0:5886f525a4ad | 9 | EthernetInterface eth; |
joon874 | 0:5886f525a4ad | 10 | HTTPServer WIZwikiWebSvr; |
joon874 | 0:5886f525a4ad | 11 | MbedJSONValue WIZwikiREST; |
joon874 | 0:5886f525a4ad | 12 | |
joon874 | 0:5886f525a4ad | 13 | // Enter a MAC address for your controller below. |
joon874 | 0:5886f525a4ad | 14 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE}; |
joon874 | 0:5886f525a4ad | 15 | char mac_str[20]; |
joon874 | 0:5886f525a4ad | 16 | char ip_addr[] = "192.168.100.100"; |
joon874 | 0:5886f525a4ad | 17 | char subnet_mask[] = "255.255.255.0"; |
joon874 | 0:5886f525a4ad | 18 | char gateway_addr[] = "192.168.100.1"; |
joon874 | 0:5886f525a4ad | 19 | |
joon874 | 0:5886f525a4ad | 20 | DigitalOut LED_1(PA_1); |
joon874 | 0:5886f525a4ad | 21 | DigitalOut LED_2(PA_2); |
joon874 | 0:5886f525a4ad | 22 | |
joon874 | 0:5886f525a4ad | 23 | DigitalInOut P05(P5); |
joon874 | 0:5886f525a4ad | 24 | DigitalInOut P06(P6); |
joon874 | 0:5886f525a4ad | 25 | |
joon874 | 0:5886f525a4ad | 26 | void WIZwiki_REST_init(); |
joon874 | 0:5886f525a4ad | 27 | |
joon874 | 0:5886f525a4ad | 28 | int main(void) |
joon874 | 0:5886f525a4ad | 29 | { |
joon874 | 0:5886f525a4ad | 30 | sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X",mac_addr[0],mac_addr[1], |
joon874 | 0:5886f525a4ad | 31 | mac_addr[2],mac_addr[3], |
joon874 | 0:5886f525a4ad | 32 | mac_addr[4],mac_addr[5]); |
joon874 | 0:5886f525a4ad | 33 | //GPIO Set |
joon874 | 0:5886f525a4ad | 34 | P05.output(); |
joon874 | 0:5886f525a4ad | 35 | P05.write(1); |
joon874 | 0:5886f525a4ad | 36 | P06.input(); |
joon874 | 0:5886f525a4ad | 37 | |
joon874 | 0:5886f525a4ad | 38 | //LED Setbit |
joon874 | 0:5886f525a4ad | 39 | LED_1.write(0); // LED On |
joon874 | 0:5886f525a4ad | 40 | LED_2.write(1); // LED Off |
joon874 | 0:5886f525a4ad | 41 | |
joon874 | 0:5886f525a4ad | 42 | WIZwiki_REST_init(); |
joon874 | 0:5886f525a4ad | 43 | |
joon874 | 0:5886f525a4ad | 44 | // Serialize it into a JSON string |
joon874 | 0:5886f525a4ad | 45 | printf("\r\n"); |
joon874 | 0:5886f525a4ad | 46 | printf("-------------------------WIZwikiREST--------------------------- \r\n"); |
joon874 | 0:5886f525a4ad | 47 | printf("%s\r\n", WIZwikiREST.serialize().c_str()); |
joon874 | 0:5886f525a4ad | 48 | printf("--------------------------------------------------------------- \r\n"); |
joon874 | 0:5886f525a4ad | 49 | |
joon874 | 0:5886f525a4ad | 50 | WIZwikiWebSvr.add_request_handler("GET", new GetRequestHandler()); |
joon874 | 0:5886f525a4ad | 51 | //WIZwikiWebSvr.add_request_handler("DELETE", new DeleteRequestHandler()); |
joon874 | 0:5886f525a4ad | 52 | //WIZwikiWebSvr.add_request_handler("PUT", new PutRequestHandler()); |
joon874 | 0:5886f525a4ad | 53 | |
joon874 | 0:5886f525a4ad | 54 | #ifdef DHCP |
joon874 | 0:5886f525a4ad | 55 | eth.init(mac_addr); //Use DHCP |
joon874 | 0:5886f525a4ad | 56 | #else |
joon874 | 0:5886f525a4ad | 57 | eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP |
joon874 | 0:5886f525a4ad | 58 | #endif |
joon874 | 0:5886f525a4ad | 59 | |
joon874 | 0:5886f525a4ad | 60 | printf("Check Ethernet Link\r\n"); |
joon874 | 0:5886f525a4ad | 61 | |
joon874 | 0:5886f525a4ad | 62 | while(1) //Wait link up |
joon874 | 0:5886f525a4ad | 63 | { |
joon874 | 0:5886f525a4ad | 64 | if(eth.link() == true) |
joon874 | 0:5886f525a4ad | 65 | break; |
joon874 | 0:5886f525a4ad | 66 | } |
joon874 | 0:5886f525a4ad | 67 | |
joon874 | 0:5886f525a4ad | 68 | printf("Link up\r\n"); |
joon874 | 0:5886f525a4ad | 69 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
joon874 | 0:5886f525a4ad | 70 | |
joon874 | 0:5886f525a4ad | 71 | if(!WIZwikiWebSvr.init(SERVER_PORT)) |
joon874 | 0:5886f525a4ad | 72 | { |
joon874 | 0:5886f525a4ad | 73 | eth.disconnect(); |
joon874 | 0:5886f525a4ad | 74 | return -1; |
joon874 | 0:5886f525a4ad | 75 | } |
joon874 | 0:5886f525a4ad | 76 | |
joon874 | 0:5886f525a4ad | 77 | while(1) |
joon874 | 0:5886f525a4ad | 78 | { |
joon874 | 0:5886f525a4ad | 79 | WIZwikiWebSvr.run(); |
joon874 | 0:5886f525a4ad | 80 | } |
joon874 | 0:5886f525a4ad | 81 | } |
joon874 | 0:5886f525a4ad | 82 | |
joon874 | 0:5886f525a4ad | 83 | void WIZwiki_REST_init(void) |
joon874 | 0:5886f525a4ad | 84 | { |
joon874 | 0:5886f525a4ad | 85 | //Fill the object |
joon874 | 0:5886f525a4ad | 86 | WIZwikiREST["Name"] = "WIZwiki-REST-01"; |
joon874 | 0:5886f525a4ad | 87 | //Network |
joon874 | 0:5886f525a4ad | 88 | WIZwikiREST["Network"]["MAC"] = mac_str; |
joon874 | 0:5886f525a4ad | 89 | WIZwikiREST["Network"]["IP"] = ip_addr; |
joon874 | 0:5886f525a4ad | 90 | WIZwikiREST["Network"]["SN"] = subnet_mask; |
joon874 | 0:5886f525a4ad | 91 | WIZwikiREST["Network"]["GW"] = gateway_addr; |
joon874 | 0:5886f525a4ad | 92 | //LEDs |
joon874 | 0:5886f525a4ad | 93 | WIZwikiREST["LEDs"]["LED_1"]["Value"] = (LED_1.read() ? "Off" : "On"); |
joon874 | 0:5886f525a4ad | 94 | WIZwikiREST["LEDs"]["LED_2"]["Value"] = (LED_2.read() ? "Off" : "On"); |
joon874 | 0:5886f525a4ad | 95 | |
joon874 | 0:5886f525a4ad | 96 | // GPIOs |
joon874 | 0:5886f525a4ad | 97 | WIZwikiREST["GPIOs"]["P05"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_5) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 98 | WIZwikiREST["GPIOs"]["P05"]["Value"] = (P05.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 99 | WIZwikiREST["GPIOs"]["P06"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_6) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 100 | WIZwikiREST["GPIOs"]["P06"]["Value"] = (P06.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 101 | // WIZwikiREST["GPIOs"]["P07"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_7) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 102 | // WIZwikiREST["GPIOs"]["P07"]["Value"] = (P07.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 103 | // WIZwikiREST["GPIOs"]["P08"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_8) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 104 | // WIZwikiREST["GPIOs"]["P08"]["Value"] = (P08.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 105 | // WIZwikiREST["GPIOs"]["P09"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_9) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 106 | // WIZwikiREST["GPIOs"]["P09"]["Value"] = (P09.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 107 | // WIZwikiREST["GPIOs"]["P10"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_10) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 108 | // WIZwikiREST["GPIOs"]["P10"]["Value"] = (P10.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 109 | // WIZwikiREST["GPIOs"]["P11"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_11) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 110 | // WIZwikiREST["GPIOs"]["P11"]["Value"] = (P11.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 111 | // WIZwikiREST["GPIOs"]["P12"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_12) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 112 | // WIZwikiREST["GPIOs"]["P12"]["Value"] = (P12.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 113 | // WIZwikiREST["GPIOs"]["P13"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_13) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 114 | // WIZwikiREST["GPIOs"]["P13"]["Value"] = (P13.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 115 | // WIZwikiREST["GPIOs"]["P14"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_14) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 116 | // WIZwikiREST["GPIOs"]["P14"]["Value"] = (P14.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 117 | // WIZwikiREST["GPIOs"]["P15"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_0) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 118 | // WIZwikiREST["GPIOs"]["P15"]["Value"] = (P15.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 119 | // WIZwikiREST["GPIOs"]["P16"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_1) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 120 | // WIZwikiREST["GPIOs"]["P16"]["Value"] = (P16.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 121 | // WIZwikiREST["GPIOs"]["P17"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_2) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 122 | // WIZwikiREST["GPIOs"]["P17"]["Value"] = (P17.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 123 | // WIZwikiREST["GPIOs"]["P18"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_3) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 124 | // WIZwikiREST["GPIOs"]["P18"]["Value"] = (P18.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 125 | // WIZwikiREST["GPIOs"]["P19"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_8) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 126 | // WIZwikiREST["GPIOs"]["P19"]["Value"] = (P19.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 127 | // WIZwikiREST["GPIOs"]["P20"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_4) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 128 | // WIZwikiREST["GPIOs"]["P20"]["Value"] = (P20.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 129 | // WIZwikiREST["GPIOs"]["P21"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_0) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 130 | // WIZwikiREST["GPIOs"]["P21"]["Value"] = (P21.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 131 | // WIZwikiREST["GPIOs"]["P22"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_1) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 132 | // WIZwikiREST["GPIOs"]["P22"]["Value"] = (P22.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 133 | // WIZwikiREST["GPIOs"]["P23"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_2) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 134 | // WIZwikiREST["GPIOs"]["P23"]["Value"] = (P23.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 135 | // WIZwikiREST["GPIOs"]["P24"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_3) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 136 | // WIZwikiREST["GPIOs"]["P24"]["Value"] = (P24.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 137 | // WIZwikiREST["GPIOs"]["P25"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_9) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 138 | // WIZwikiREST["GPIOs"]["P25"]["Value"] = (P25.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 139 | // WIZwikiREST["GPIOs"]["P26"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_5) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 140 | // WIZwikiREST["GPIOs"]["P26"]["Value"] = (P26.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 141 | // WIZwikiREST["GPIOs"]["P27"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_12) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 142 | // WIZwikiREST["GPIOs"]["P27"]["Value"] = (P27.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 143 | // WIZwikiREST["GPIOs"]["P28"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_13) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 144 | // WIZwikiREST["GPIOs"]["P28"]["Value"] = (P28.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 145 | // WIZwikiREST["GPIOs"]["P29"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_14) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 146 | // WIZwikiREST["GPIOs"]["P29"]["Value"] = (P29.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 147 | // WIZwikiREST["GPIOs"]["P30"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_15) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 148 | // WIZwikiREST["GPIOs"]["P30"]["Value"] = (P30.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 149 | // WIZwikiREST["GPIOs"]["P31"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_6) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 150 | // WIZwikiREST["GPIOs"]["P31"]["Value"] = (P31.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 151 | // WIZwikiREST["GPIOs"]["P32"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_7) ? "Output" : "Input"); |
joon874 | 0:5886f525a4ad | 152 | // WIZwikiREST["GPIOs"]["P32"]["Value"] = (P32.read() ? "1" : "0"); |
joon874 | 0:5886f525a4ad | 153 | } |