![](/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@5:473a66ae9596, 2016-07-04 (annotated)
- Committer:
- joon874
- Date:
- Mon Jul 04 00:14:31 2016 +0000
- Revision:
- 5:473a66ae9596
- Parent:
- 4:0b5a199e91f3
- Child:
- 6:2974bbc94e7a
This is WIZwiki-REST-io upper version. This platform possible to handle PWM, I2C, GPIO.
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 | 5:473a66ae9596 | 6 | // 20160630 |
joon874 | 5:473a66ae9596 | 7 | #include "Adafruit_SSD1306.h" |
joon874 | 0:5886f525a4ad | 8 | |
joon874 | 0:5886f525a4ad | 9 | #define SERVER_PORT 80 |
joon874 | 0:5886f525a4ad | 10 | |
joon874 | 5:473a66ae9596 | 11 | //#define DHCP |
joon874 | 2:ca36ecca24c2 | 12 | |
joon874 | 4:0b5a199e91f3 | 13 | //#define DEBUG |
joon874 | 4:0b5a199e91f3 | 14 | |
joon874 | 5:473a66ae9596 | 15 | //-- I2C OLED -- |
joon874 | 5:473a66ae9596 | 16 | class I2CPreInit : public I2C |
joon874 | 5:473a66ae9596 | 17 | { |
joon874 | 5:473a66ae9596 | 18 | public: |
joon874 | 5:473a66ae9596 | 19 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
joon874 | 5:473a66ae9596 | 20 | { |
joon874 | 5:473a66ae9596 | 21 | frequency(100000); |
joon874 | 5:473a66ae9596 | 22 | start(); |
joon874 | 5:473a66ae9596 | 23 | }; |
joon874 | 5:473a66ae9596 | 24 | }; |
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 | 5:473a66ae9596 | 31 | //-- GPIO -- |
joon874 | 5:473a66ae9596 | 32 | DigitalInOut GP05(D5); |
joon874 | 5:473a66ae9596 | 33 | |
joon874 | 0:5886f525a4ad | 34 | EthernetInterface eth; |
joon874 | 0:5886f525a4ad | 35 | HTTPServer WIZwikiWebSvr; |
joon874 | 0:5886f525a4ad | 36 | MbedJSONValue WIZwikiREST; |
joon874 | 0:5886f525a4ad | 37 | |
joon874 | 0:5886f525a4ad | 38 | // Enter a MAC address for your controller below. |
joon874 | 0:5886f525a4ad | 39 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE}; |
joon874 | 0:5886f525a4ad | 40 | char mac_str[20]; |
joon874 | 4:0b5a199e91f3 | 41 | char ip_addr[] = "192.168.100.100"; |
joon874 | 4:0b5a199e91f3 | 42 | char subnet_mask[] = "255.255.255.0"; |
joon874 | 0:5886f525a4ad | 43 | char gateway_addr[] = "192.168.100.1"; |
joon874 | 0:5886f525a4ad | 44 | |
joon874 | 4:0b5a199e91f3 | 45 | GetRequestHandler myGetReq; |
joon874 | 4:0b5a199e91f3 | 46 | PostRequestHandler myPostReq; |
joon874 | 4:0b5a199e91f3 | 47 | |
joon874 | 5:473a66ae9596 | 48 | //-- I2C OLED -- |
joon874 | 5:473a66ae9596 | 49 | bool oled_set(void* param) |
joon874 | 4:0b5a199e91f3 | 50 | { |
joon874 | 5:473a66ae9596 | 51 | printf("param : %c \r\n", param); |
joon874 | 5:473a66ae9596 | 52 | char * oled; |
joon874 | 4:0b5a199e91f3 | 53 | if(!param) return false; |
joon874 | 5:473a66ae9596 | 54 | oled = (char*) param; |
joon874 | 5:473a66ae9596 | 55 | gOled.clearDisplay(); |
joon874 | 5:473a66ae9596 | 56 | gOled.setTextCursor(0,0); |
joon874 | 5:473a66ae9596 | 57 | gOled.printf("%s",oled); |
joon874 | 5:473a66ae9596 | 58 | gOled.display(); |
joon874 | 4:0b5a199e91f3 | 59 | return true; |
joon874 | 4:0b5a199e91f3 | 60 | } |
joon874 | 5:473a66ae9596 | 61 | //-- PWM DC -- |
joon874 | 5:473a66ae9596 | 62 | bool pwm_set(void* param) |
joon874 | 5:473a66ae9596 | 63 | { |
joon874 | 5:473a66ae9596 | 64 | printf("%d\r\n",(*(int*)param)); |
joon874 | 5:473a66ae9596 | 65 | if(!param) return false; |
joon874 | 5:473a66ae9596 | 66 | DC.write((float)(*(int*)param)/100.0); |
joon874 | 5:473a66ae9596 | 67 | return true; |
joon874 | 5:473a66ae9596 | 68 | } |
joon874 | 5:473a66ae9596 | 69 | //-- GPIO -- |
MidnightCow | 1:728e5b5c8dae | 70 | bool p5_set(void* param) |
MidnightCow | 1:728e5b5c8dae | 71 | { |
joon874 | 4:0b5a199e91f3 | 72 | if(!param) return false; |
MidnightCow | 1:728e5b5c8dae | 73 | GP05.write(*(int*)param); |
MidnightCow | 1:728e5b5c8dae | 74 | return true; |
MidnightCow | 1:728e5b5c8dae | 75 | } |
MidnightCow | 1:728e5b5c8dae | 76 | |
MidnightCow | 1:728e5b5c8dae | 77 | void debug_info() |
MidnightCow | 1:728e5b5c8dae | 78 | { |
MidnightCow | 1:728e5b5c8dae | 79 | printf("SP:0x%X\r\n",__current_sp()); |
MidnightCow | 1:728e5b5c8dae | 80 | __heapstats((__heapprt)fprintf,stderr); |
joon874 | 4:0b5a199e91f3 | 81 | #ifdef DEBUG |
joon874 | 4:0b5a199e91f3 | 82 | __heapvalid((__heapprt)fprintf,stderr, 1); |
joon874 | 4:0b5a199e91f3 | 83 | #endif |
MidnightCow | 1:728e5b5c8dae | 84 | printf("\r\n"); |
MidnightCow | 1:728e5b5c8dae | 85 | } |
joon874 | 4:0b5a199e91f3 | 86 | |
joon874 | 0:5886f525a4ad | 87 | void WIZwiki_REST_init(); |
joon874 | 0:5886f525a4ad | 88 | |
joon874 | 0:5886f525a4ad | 89 | int main(void) |
joon874 | 0:5886f525a4ad | 90 | { |
joon874 | 4:0b5a199e91f3 | 91 | #ifdef DEBUG |
joon874 | 4:0b5a199e91f3 | 92 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 93 | #endif |
joon874 | 4:0b5a199e91f3 | 94 | 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 | 95 | |
joon874 | 5:473a66ae9596 | 96 | // OLED init |
joon874 | 5:473a66ae9596 | 97 | gOled.begin(); |
joon874 | 5:473a66ae9596 | 98 | gOled.clearDisplay(); |
joon874 | 5:473a66ae9596 | 99 | |
joon874 | 5:473a66ae9596 | 100 | // PWM init |
joon874 | 5:473a66ae9596 | 101 | DC.period_ms(1); |
joon874 | 5:473a66ae9596 | 102 | DC.write(0); |
joon874 | 5:473a66ae9596 | 103 | |
joon874 | 5:473a66ae9596 | 104 | //GPIO set & init |
MidnightCow | 1:728e5b5c8dae | 105 | GP05.output(); |
joon874 | 2:ca36ecca24c2 | 106 | GP05.write(1); |
joon874 | 2:ca36ecca24c2 | 107 | |
MidnightCow | 1:728e5b5c8dae | 108 | printf("START \r\n"); |
MidnightCow | 1:728e5b5c8dae | 109 | printf("sizeof(MbedJSONValue)=%d\r\n",sizeof(MbedJSONValue)); |
MidnightCow | 1:728e5b5c8dae | 110 | printf("sizeof(vector)=%d\r\n",sizeof(std::vector<string*>)); |
MidnightCow | 1:728e5b5c8dae | 111 | printf("sizeof(string)=%d\r\n",sizeof(std::string)); |
joon874 | 4:0b5a199e91f3 | 112 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 113 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 114 | #endif |
joon874 | 0:5886f525a4ad | 115 | |
joon874 | 0:5886f525a4ad | 116 | WIZwiki_REST_init(); |
MidnightCow | 1:728e5b5c8dae | 117 | |
joon874 | 4:0b5a199e91f3 | 118 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 119 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 120 | #endif |
MidnightCow | 1:728e5b5c8dae | 121 | |
joon874 | 0:5886f525a4ad | 122 | // Serialize it into a JSON string |
joon874 | 0:5886f525a4ad | 123 | printf("-------------------------WIZwikiREST--------------------------- \r\n"); |
joon874 | 4:0b5a199e91f3 | 124 | printf("\r\n%s\r\n", WIZwikiREST.serialize().c_str()); |
joon874 | 0:5886f525a4ad | 125 | printf("--------------------------------------------------------------- \r\n"); |
joon874 | 0:5886f525a4ad | 126 | |
joon874 | 4:0b5a199e91f3 | 127 | WIZwikiWebSvr.add_request_handler("GET", &myGetReq); |
joon874 | 4:0b5a199e91f3 | 128 | WIZwikiWebSvr.add_request_handler("POST", &myPostReq); |
joon874 | 4:0b5a199e91f3 | 129 | //WIZwikiWebSvr.add_request_handler("DELETE", new PostRequestHandler()); |
joon874 | 0:5886f525a4ad | 130 | //WIZwikiWebSvr.add_request_handler("PUT", new PutRequestHandler()); |
joon874 | 0:5886f525a4ad | 131 | |
joon874 | 0:5886f525a4ad | 132 | #ifdef DHCP |
joon874 | 0:5886f525a4ad | 133 | eth.init(mac_addr); //Use DHCP |
joon874 | 0:5886f525a4ad | 134 | #else |
joon874 | 0:5886f525a4ad | 135 | eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP |
joon874 | 0:5886f525a4ad | 136 | #endif |
joon874 | 0:5886f525a4ad | 137 | |
joon874 | 2:ca36ecca24c2 | 138 | |
joon874 | 0:5886f525a4ad | 139 | printf("Check Ethernet Link\r\n"); |
joon874 | 4:0b5a199e91f3 | 140 | |
joon874 | 2:ca36ecca24c2 | 141 | do{ |
joon874 | 2:ca36ecca24c2 | 142 | printf(" Link - Wait... \r\n"); |
joon874 | 2:ca36ecca24c2 | 143 | wait(1); |
joon874 | 2:ca36ecca24c2 | 144 | }while(!eth.ethernet_link()); |
joon874 | 2:ca36ecca24c2 | 145 | printf("-- Ethetnet PHY Link - Done -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 146 | |
joon874 | 2:ca36ecca24c2 | 147 | if (eth.connect() < 0 ) |
joon874 | 2:ca36ecca24c2 | 148 | printf("-- EThernet Connect - Fail -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 149 | else |
joon874 | 0:5886f525a4ad | 150 | { |
joon874 | 2:ca36ecca24c2 | 151 | printf("-- Assigned Network Information -- \r\n"); |
joon874 | 2:ca36ecca24c2 | 152 | printf(" IP : %s\r\n\r\n", eth.getIPAddress()); |
joon874 | 2:ca36ecca24c2 | 153 | printf(" MASK : %s\r\n\r\n", eth.getNetworkMask()); |
joon874 | 2:ca36ecca24c2 | 154 | printf(" GW : %s\r\n\r\n", eth.getGateway()); |
joon874 | 0:5886f525a4ad | 155 | } |
joon874 | 0:5886f525a4ad | 156 | |
joon874 | 0:5886f525a4ad | 157 | printf("Link up\r\n"); |
joon874 | 0:5886f525a4ad | 158 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
joon874 | 4:0b5a199e91f3 | 159 | #ifdef DEBUG |
joon874 | 4:0b5a199e91f3 | 160 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 161 | #endif |
joon874 | 0:5886f525a4ad | 162 | |
joon874 | 0:5886f525a4ad | 163 | if(!WIZwikiWebSvr.init(SERVER_PORT)) |
joon874 | 0:5886f525a4ad | 164 | { |
joon874 | 0:5886f525a4ad | 165 | eth.disconnect(); |
joon874 | 0:5886f525a4ad | 166 | return -1; |
joon874 | 0:5886f525a4ad | 167 | } |
joon874 | 0:5886f525a4ad | 168 | |
joon874 | 0:5886f525a4ad | 169 | while(1) |
joon874 | 0:5886f525a4ad | 170 | { |
joon874 | 0:5886f525a4ad | 171 | WIZwikiWebSvr.run(); |
joon874 | 0:5886f525a4ad | 172 | } |
joon874 | 0:5886f525a4ad | 173 | } |
joon874 | 0:5886f525a4ad | 174 | |
joon874 | 0:5886f525a4ad | 175 | void WIZwiki_REST_init(void) |
joon874 | 0:5886f525a4ad | 176 | { |
joon874 | 0:5886f525a4ad | 177 | //Fill the object |
MidnightCow | 1:728e5b5c8dae | 178 | WIZwikiREST["Name"] = "WIZwiki-RESTful-01"; |
MidnightCow | 1:728e5b5c8dae | 179 | WIZwikiREST["Name"].accessible = false; |
joon874 | 4:0b5a199e91f3 | 180 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 181 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 182 | #endif |
MidnightCow | 1:728e5b5c8dae | 183 | |
joon874 | 0:5886f525a4ad | 184 | //Network |
joon874 | 0:5886f525a4ad | 185 | WIZwikiREST["Network"]["MAC"] = mac_str; |
joon874 | 0:5886f525a4ad | 186 | WIZwikiREST["Network"]["IP"] = ip_addr; |
MidnightCow | 1:728e5b5c8dae | 187 | WIZwikiREST["Network"]["IP"].accessible = true; |
joon874 | 0:5886f525a4ad | 188 | WIZwikiREST["Network"]["SN"] = subnet_mask; |
MidnightCow | 1:728e5b5c8dae | 189 | WIZwikiREST["Network"]["SN"].accessible = true; |
joon874 | 0:5886f525a4ad | 190 | WIZwikiREST["Network"]["GW"] = gateway_addr; |
MidnightCow | 1:728e5b5c8dae | 191 | WIZwikiREST["Network"]["GW"].accessible = true; |
joon874 | 5:473a66ae9596 | 192 | |
joon874 | 5:473a66ae9596 | 193 | // I2C OLED |
joon874 | 5:473a66ae9596 | 194 | WIZwikiREST["I2C"]["OLED"] = "none"; |
joon874 | 5:473a66ae9596 | 195 | WIZwikiREST["I2C"]["OLED"].accessible = true; |
joon874 | 5:473a66ae9596 | 196 | WIZwikiREST["I2C"]["OLED"].cb_action = oled_set; |
joon874 | 4:0b5a199e91f3 | 197 | |
joon874 | 5:473a66ae9596 | 198 | // PWM DC |
joon874 | 5:473a66ae9596 | 199 | WIZwikiREST["PWM"]["DC"] = DC.read(); |
joon874 | 5:473a66ae9596 | 200 | WIZwikiREST["PWM"]["DC"].accessible = true; |
joon874 | 5:473a66ae9596 | 201 | WIZwikiREST["PWM"]["DC"].cb_action = pwm_set; |
joon874 | 5:473a66ae9596 | 202 | |
joon874 | 5:473a66ae9596 | 203 | // GPIO |
MidnightCow | 1:728e5b5c8dae | 204 | WIZwikiREST["GPIOs"]["P05"] = GP05.read(); |
MidnightCow | 1:728e5b5c8dae | 205 | WIZwikiREST["GPIOs"]["P05"].accessible = true; |
MidnightCow | 1:728e5b5c8dae | 206 | WIZwikiREST["GPIOs"]["P05"].cb_action = p5_set; |
joon874 | 4:0b5a199e91f3 | 207 | |
joon874 | 4:0b5a199e91f3 | 208 | #ifdef DEBUG |
MidnightCow | 1:728e5b5c8dae | 209 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 210 | #endif |
joon874 | 0:5886f525a4ad | 211 | } |