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