![](/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@9:f7d5a168a693, 2016-10-06 (annotated)
- Committer:
- joon874
- Date:
- Thu Oct 06 04:13:57 2016 +0000
- Revision:
- 9:f7d5a168a693
- Parent:
- 8:60d99da6eeb2
- Child:
- 10:fe96beb315e3
WIZwikiREST-io code exercise for WIZnet Academy
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 | 9:f7d5a168a693 | 6 | #include "DHT.h" |
joon874 | 0:5886f525a4ad | 7 | |
joon874 | 0:5886f525a4ad | 8 | #define SERVER_PORT 80 |
joon874 | 8:60d99da6eeb2 | 9 | //#define DHCP |
joon874 | 4:0b5a199e91f3 | 10 | |
joon874 | 5:473a66ae9596 | 11 | |
joon874 | 6:2974bbc94e7a | 12 | //-- GPIO LED -- |
joon874 | 9:f7d5a168a693 | 13 | // GPIO Pin 선언 |
joon874 | 8:60d99da6eeb2 | 14 | |
joon874 | 6:2974bbc94e7a | 15 | //-- ADC -- |
joon874 | 9:f7d5a168a693 | 16 | // Anlog Pin 선언 |
joon874 | 9:f7d5a168a693 | 17 | |
joon874 | 9:f7d5a168a693 | 18 | //-- DHT -- |
joon874 | 9:f7d5a168a693 | 19 | // DHT Class 선언 |
joon874 | 9:f7d5a168a693 | 20 | |
joon874 | 6:2974bbc94e7a | 21 | |
joon874 | 0:5886f525a4ad | 22 | EthernetInterface eth; |
joon874 | 0:5886f525a4ad | 23 | HTTPServer WIZwikiWebSvr; |
joon874 | 0:5886f525a4ad | 24 | MbedJSONValue WIZwikiREST; |
joon874 | 0:5886f525a4ad | 25 | |
joon874 | 9:f7d5a168a693 | 26 | GetRequestHandler myGetReq; |
joon874 | 9:f7d5a168a693 | 27 | //PostRequestHandler myPostReq; |
joon874 | 9:f7d5a168a693 | 28 | PutRequestHandler myPutReq; |
joon874 | 9:f7d5a168a693 | 29 | |
joon874 | 0:5886f525a4ad | 30 | // Enter a MAC address for your controller below. |
joon874 | 0:5886f525a4ad | 31 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE}; |
joon874 | 0:5886f525a4ad | 32 | char mac_str[20]; |
joon874 | 4:0b5a199e91f3 | 33 | char ip_addr[] = "192.168.100.100"; |
joon874 | 4:0b5a199e91f3 | 34 | char subnet_mask[] = "255.255.255.0"; |
joon874 | 0:5886f525a4ad | 35 | char gateway_addr[] = "192.168.100.1"; |
joon874 | 0:5886f525a4ad | 36 | |
joon874 | 4:0b5a199e91f3 | 37 | |
joon874 | 9:f7d5a168a693 | 38 | //-- Callback 함수 구현 |
joon874 | 5:473a66ae9596 | 39 | //-- GPIO -- |
joon874 | 9:f7d5a168a693 | 40 | |
joon874 | 8:60d99da6eeb2 | 41 | |
joon874 | 6:2974bbc94e7a | 42 | //-- ADC -- |
joon874 | 9:f7d5a168a693 | 43 | |
MidnightCow | 1:728e5b5c8dae | 44 | |
joon874 | 9:f7d5a168a693 | 45 | //-- DHT -- |
joon874 | 9:f7d5a168a693 | 46 | |
joon874 | 9:f7d5a168a693 | 47 | |
joon874 | 4:0b5a199e91f3 | 48 | |
joon874 | 0:5886f525a4ad | 49 | void WIZwiki_REST_init(); |
joon874 | 0:5886f525a4ad | 50 | |
joon874 | 0:5886f525a4ad | 51 | int main(void) |
joon874 | 0:5886f525a4ad | 52 | { |
joon874 | 4:0b5a199e91f3 | 53 | 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 | 0:5886f525a4ad | 54 | |
joon874 | 0:5886f525a4ad | 55 | WIZwiki_REST_init(); |
MidnightCow | 1:728e5b5c8dae | 56 | |
joon874 | 0:5886f525a4ad | 57 | // Serialize it into a JSON string |
joon874 | 8:60d99da6eeb2 | 58 | printf("---------------------WIZwikiREST-------------------- \r\n"); |
joon874 | 4:0b5a199e91f3 | 59 | printf("\r\n%s\r\n", WIZwikiREST.serialize().c_str()); |
joon874 | 8:60d99da6eeb2 | 60 | printf("---------------------------------------------------- \r\n"); |
joon874 | 0:5886f525a4ad | 61 | |
joon874 | 4:0b5a199e91f3 | 62 | WIZwikiWebSvr.add_request_handler("GET", &myGetReq); |
joon874 | 8:60d99da6eeb2 | 63 | //WIZwikiWebSvr.add_request_handler("POST", &myPostReq); |
joon874 | 8:60d99da6eeb2 | 64 | WIZwikiWebSvr.add_request_handler("PUT", &myPutReq); |
joon874 | 4:0b5a199e91f3 | 65 | //WIZwikiWebSvr.add_request_handler("DELETE", new PostRequestHandler()); |
joon874 | 0:5886f525a4ad | 66 | |
joon874 | 0:5886f525a4ad | 67 | #ifdef DHCP |
joon874 | 0:5886f525a4ad | 68 | eth.init(mac_addr); //Use DHCP |
joon874 | 0:5886f525a4ad | 69 | #else |
joon874 | 0:5886f525a4ad | 70 | eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP |
joon874 | 0:5886f525a4ad | 71 | #endif |
joon874 | 0:5886f525a4ad | 72 | |
joon874 | 2:ca36ecca24c2 | 73 | |
joon874 | 0:5886f525a4ad | 74 | printf("Check Ethernet Link\r\n"); |
joon874 | 4:0b5a199e91f3 | 75 | |
joon874 | 2:ca36ecca24c2 | 76 | do{ |
joon874 | 2:ca36ecca24c2 | 77 | printf(" Link - Wait... \r\n"); |
joon874 | 2:ca36ecca24c2 | 78 | wait(1); |
joon874 | 2:ca36ecca24c2 | 79 | }while(!eth.ethernet_link()); |
joon874 | 2:ca36ecca24c2 | 80 | printf("-- Ethetnet PHY Link - Done -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 81 | |
joon874 | 2:ca36ecca24c2 | 82 | if (eth.connect() < 0 ) |
joon874 | 2:ca36ecca24c2 | 83 | printf("-- EThernet Connect - Fail -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 84 | else |
joon874 | 0:5886f525a4ad | 85 | { |
joon874 | 2:ca36ecca24c2 | 86 | printf("-- Assigned Network Information -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 87 | printf(" IP : %s\r\n\r\n", eth.getIPAddress()); |
joon874 | 2:ca36ecca24c2 | 88 | printf(" MASK : %s\r\n\r\n", eth.getNetworkMask()); |
joon874 | 2:ca36ecca24c2 | 89 | printf(" GW : %s\r\n\r\n", eth.getGateway()); |
joon874 | 0:5886f525a4ad | 90 | } |
joon874 | 0:5886f525a4ad | 91 | |
joon874 | 0:5886f525a4ad | 92 | printf("Link up\r\n"); |
joon874 | 0:5886f525a4ad | 93 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
joon874 | 0:5886f525a4ad | 94 | |
joon874 | 0:5886f525a4ad | 95 | if(!WIZwikiWebSvr.init(SERVER_PORT)) |
joon874 | 0:5886f525a4ad | 96 | { |
joon874 | 0:5886f525a4ad | 97 | eth.disconnect(); |
joon874 | 0:5886f525a4ad | 98 | return -1; |
joon874 | 0:5886f525a4ad | 99 | } |
joon874 | 0:5886f525a4ad | 100 | |
joon874 | 0:5886f525a4ad | 101 | while(1) |
joon874 | 0:5886f525a4ad | 102 | { |
joon874 | 0:5886f525a4ad | 103 | WIZwikiWebSvr.run(); |
joon874 | 0:5886f525a4ad | 104 | } |
joon874 | 0:5886f525a4ad | 105 | } |
joon874 | 0:5886f525a4ad | 106 | |
joon874 | 0:5886f525a4ad | 107 | void WIZwiki_REST_init(void) |
joon874 | 0:5886f525a4ad | 108 | { |
joon874 | 0:5886f525a4ad | 109 | //Fill the object |
joon874 | 9:f7d5a168a693 | 110 | WIZwikiREST["Name"] = "WIZwikiREST-io WIZnet Academy"; |
MidnightCow | 1:728e5b5c8dae | 111 | WIZwikiREST["Name"].accessible = false; |
MidnightCow | 1:728e5b5c8dae | 112 | |
joon874 | 0:5886f525a4ad | 113 | //Network |
joon874 | 0:5886f525a4ad | 114 | WIZwikiREST["Network"]["MAC"] = mac_str; |
joon874 | 0:5886f525a4ad | 115 | WIZwikiREST["Network"]["IP"] = ip_addr; |
MidnightCow | 1:728e5b5c8dae | 116 | WIZwikiREST["Network"]["IP"].accessible = true; |
joon874 | 0:5886f525a4ad | 117 | WIZwikiREST["Network"]["SN"] = subnet_mask; |
MidnightCow | 1:728e5b5c8dae | 118 | WIZwikiREST["Network"]["SN"].accessible = true; |
joon874 | 0:5886f525a4ad | 119 | WIZwikiREST["Network"]["GW"] = gateway_addr; |
MidnightCow | 1:728e5b5c8dae | 120 | WIZwikiREST["Network"]["GW"].accessible = true; |
joon874 | 9:f7d5a168a693 | 121 | |
joon874 | 9:f7d5a168a693 | 122 | //Object 생성 |
joon874 | 9:f7d5a168a693 | 123 | // GPIO |
joon874 | 5:473a66ae9596 | 124 | |
joon874 | 6:2974bbc94e7a | 125 | |
joon874 | 6:2974bbc94e7a | 126 | // ADC |
joon874 | 9:f7d5a168a693 | 127 | |
joon874 | 9:f7d5a168a693 | 128 | |
joon874 | 9:f7d5a168a693 | 129 | // DHT11 |
joon874 | 9:f7d5a168a693 | 130 | |
joon874 | 9:f7d5a168a693 | 131 | |
joon874 | 0:5886f525a4ad | 132 | } |