WizFi310 IoTMakers Example
Dependencies: NetworkSocketAPI WizFi310Interface mbed
main.cpp@0:325e3b1dbec8, 2016-10-10 (annotated)
- Committer:
- jehoon
- Date:
- Mon Oct 10 05:14:17 2016 +0000
- Revision:
- 0:325e3b1dbec8
IoTMakers Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jehoon | 0:325e3b1dbec8 | 1 | /* NetworkSocketAPI Example Program |
jehoon | 0:325e3b1dbec8 | 2 | * Copyright (c) 2015 ARM Limited |
jehoon | 0:325e3b1dbec8 | 3 | * |
jehoon | 0:325e3b1dbec8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jehoon | 0:325e3b1dbec8 | 5 | * you may not use this file except in compliance with the License. |
jehoon | 0:325e3b1dbec8 | 6 | * You may obtain a copy of the License at |
jehoon | 0:325e3b1dbec8 | 7 | * |
jehoon | 0:325e3b1dbec8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jehoon | 0:325e3b1dbec8 | 9 | * |
jehoon | 0:325e3b1dbec8 | 10 | * Unless required by applicable law or agreed to in writing, software |
jehoon | 0:325e3b1dbec8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jehoon | 0:325e3b1dbec8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jehoon | 0:325e3b1dbec8 | 13 | * See the License for the specific language governing permissions and |
jehoon | 0:325e3b1dbec8 | 14 | * limitations under the License. |
jehoon | 0:325e3b1dbec8 | 15 | */ |
jehoon | 0:325e3b1dbec8 | 16 | |
jehoon | 0:325e3b1dbec8 | 17 | #include "mbed.h" |
jehoon | 0:325e3b1dbec8 | 18 | #include "WizFi310Interface.h" |
jehoon | 0:325e3b1dbec8 | 19 | #include "TCPSocket.h" |
jehoon | 0:325e3b1dbec8 | 20 | |
jehoon | 0:325e3b1dbec8 | 21 | #if defined(TARGET_NUCLEO_F401RE) |
jehoon | 0:325e3b1dbec8 | 22 | Serial pc(USBTX,USBRX); |
jehoon | 0:325e3b1dbec8 | 23 | WizFi310Interface wifi(PA_11, PA_12, D6, D7, D3, NC, 115200); |
jehoon | 0:325e3b1dbec8 | 24 | #endif |
jehoon | 0:325e3b1dbec8 | 25 | |
jehoon | 0:325e3b1dbec8 | 26 | AnalogIn sensor_illu(PA_1); |
jehoon | 0:325e3b1dbec8 | 27 | DigitalOut led(PC_7); |
jehoon | 0:325e3b1dbec8 | 28 | InterruptIn button(PC_13); |
jehoon | 0:325e3b1dbec8 | 29 | |
jehoon | 0:325e3b1dbec8 | 30 | |
jehoon | 0:325e3b1dbec8 | 31 | #define AP_SSID "<AP SSID>" |
jehoon | 0:325e3b1dbec8 | 32 | #define AP_PASSWORD "<AP Password>" |
jehoon | 0:325e3b1dbec8 | 33 | #define AP_SECURITY NSAPI_SECURITY_WPA2 |
jehoon | 0:325e3b1dbec8 | 34 | |
jehoon | 0:325e3b1dbec8 | 35 | #define IoTMakers_GW_ID "<Gateway ID>" |
jehoon | 0:325e3b1dbec8 | 36 | #define IoTMakers_DEV_ID "<Device ID>" |
jehoon | 0:325e3b1dbec8 | 37 | #define IoTMakers_DEV_PWD "<Device Password>" |
jehoon | 0:325e3b1dbec8 | 38 | |
jehoon | 0:325e3b1dbec8 | 39 | #define IoTMakers_MQTT_IP "220.90.216.90" |
jehoon | 0:325e3b1dbec8 | 40 | #define IoTMakers_MQTT_PORT 10030 |
jehoon | 0:325e3b1dbec8 | 41 | |
jehoon | 0:325e3b1dbec8 | 42 | |
jehoon | 0:325e3b1dbec8 | 43 | #define DELAY_SEND_TIME_MS 5000 |
jehoon | 0:325e3b1dbec8 | 44 | |
jehoon | 0:325e3b1dbec8 | 45 | unsigned long g_time_ms = 0; |
jehoon | 0:325e3b1dbec8 | 46 | unsigned long g_prev_send_time = 0; |
jehoon | 0:325e3b1dbec8 | 47 | unsigned long g_prev_clicked_time = 0; |
jehoon | 0:325e3b1dbec8 | 48 | |
jehoon | 0:325e3b1dbec8 | 49 | //Button IRQ |
jehoon | 0:325e3b1dbec8 | 50 | int button_status = 0; |
jehoon | 0:325e3b1dbec8 | 51 | int button_clicked = 0; |
jehoon | 0:325e3b1dbec8 | 52 | |
jehoon | 0:325e3b1dbec8 | 53 | |
jehoon | 0:325e3b1dbec8 | 54 | enum Connection_Step { |
jehoon | 0:325e3b1dbec8 | 55 | csDISASSOCIATE = 0, |
jehoon | 0:325e3b1dbec8 | 56 | csASSOCIATE, |
jehoon | 0:325e3b1dbec8 | 57 | csKTCONNECTED, |
jehoon | 0:325e3b1dbec8 | 58 | }CON_STEP; |
jehoon | 0:325e3b1dbec8 | 59 | |
jehoon | 0:325e3b1dbec8 | 60 | TCPSocket socket(&wifi); |
jehoon | 0:325e3b1dbec8 | 61 | WizFi310* pwizfi310 = wifi.get_WizFi310_Pointer(); |
jehoon | 0:325e3b1dbec8 | 62 | |
jehoon | 0:325e3b1dbec8 | 63 | void Connect_to_AP(); |
jehoon | 0:325e3b1dbec8 | 64 | void Connect_to_IoTMakers(); |
jehoon | 0:325e3b1dbec8 | 65 | void Send_num_value_to_IoTMakers(const char* tag_stream, const float dvalue); |
jehoon | 0:325e3b1dbec8 | 66 | void Send_str_value_to_IoTMakers(const char* tag_stream, const char* svalue); |
jehoon | 0:325e3b1dbec8 | 67 | |
jehoon | 0:325e3b1dbec8 | 68 | void Button_IRQ(); |
jehoon | 0:325e3b1dbec8 | 69 | void Button_Push_Event(); |
jehoon | 0:325e3b1dbec8 | 70 | void Periodic_Event(); |
jehoon | 0:325e3b1dbec8 | 71 | |
jehoon | 0:325e3b1dbec8 | 72 | void time_ms(); |
jehoon | 0:325e3b1dbec8 | 73 | |
jehoon | 0:325e3b1dbec8 | 74 | void Button_IRQ() |
jehoon | 0:325e3b1dbec8 | 75 | { |
jehoon | 0:325e3b1dbec8 | 76 | if( button_clicked == 0 && (g_prev_clicked_time + 500) < g_time_ms) |
jehoon | 0:325e3b1dbec8 | 77 | { |
jehoon | 0:325e3b1dbec8 | 78 | button_clicked = 1; |
jehoon | 0:325e3b1dbec8 | 79 | g_prev_clicked_time = g_time_ms; |
jehoon | 0:325e3b1dbec8 | 80 | } |
jehoon | 0:325e3b1dbec8 | 81 | } |
jehoon | 0:325e3b1dbec8 | 82 | void Button_Push_Event() |
jehoon | 0:325e3b1dbec8 | 83 | { |
jehoon | 0:325e3b1dbec8 | 84 | |
jehoon | 0:325e3b1dbec8 | 85 | if( CON_STEP == csKTCONNECTED) |
jehoon | 0:325e3b1dbec8 | 86 | { |
jehoon | 0:325e3b1dbec8 | 87 | if( button_status == 0 ) |
jehoon | 0:325e3b1dbec8 | 88 | { |
jehoon | 0:325e3b1dbec8 | 89 | Send_str_value_to_IoTMakers("switch", "on"); |
jehoon | 0:325e3b1dbec8 | 90 | button_status = 1; |
jehoon | 0:325e3b1dbec8 | 91 | } |
jehoon | 0:325e3b1dbec8 | 92 | else if( button_status == 1 ) |
jehoon | 0:325e3b1dbec8 | 93 | { |
jehoon | 0:325e3b1dbec8 | 94 | Send_str_value_to_IoTMakers("switch", "off"); |
jehoon | 0:325e3b1dbec8 | 95 | button_status = 0; |
jehoon | 0:325e3b1dbec8 | 96 | } |
jehoon | 0:325e3b1dbec8 | 97 | } |
jehoon | 0:325e3b1dbec8 | 98 | } |
jehoon | 0:325e3b1dbec8 | 99 | |
jehoon | 0:325e3b1dbec8 | 100 | void Periodic_Event() |
jehoon | 0:325e3b1dbec8 | 101 | { |
jehoon | 0:325e3b1dbec8 | 102 | float illumination; |
jehoon | 0:325e3b1dbec8 | 103 | if( CON_STEP == csKTCONNECTED) |
jehoon | 0:325e3b1dbec8 | 104 | { |
jehoon | 0:325e3b1dbec8 | 105 | illumination = sensor_illu.read() * 100; |
jehoon | 0:325e3b1dbec8 | 106 | |
jehoon | 0:325e3b1dbec8 | 107 | Send_num_value_to_IoTMakers("illumination", (float)illumination); |
jehoon | 0:325e3b1dbec8 | 108 | } |
jehoon | 0:325e3b1dbec8 | 109 | } |
jehoon | 0:325e3b1dbec8 | 110 | |
jehoon | 0:325e3b1dbec8 | 111 | |
jehoon | 0:325e3b1dbec8 | 112 | int main() |
jehoon | 0:325e3b1dbec8 | 113 | { |
jehoon | 0:325e3b1dbec8 | 114 | pc.baud(115200); |
jehoon | 0:325e3b1dbec8 | 115 | |
jehoon | 0:325e3b1dbec8 | 116 | //Timer settings |
jehoon | 0:325e3b1dbec8 | 117 | Ticker timer_ms; |
jehoon | 0:325e3b1dbec8 | 118 | timer_ms.attach(time_ms, 0.001f); |
jehoon | 0:325e3b1dbec8 | 119 | g_prev_send_time = g_time_ms; |
jehoon | 0:325e3b1dbec8 | 120 | |
jehoon | 0:325e3b1dbec8 | 121 | //button Interrupt |
jehoon | 0:325e3b1dbec8 | 122 | button.rise(&Button_IRQ); |
jehoon | 0:325e3b1dbec8 | 123 | |
jehoon | 0:325e3b1dbec8 | 124 | |
jehoon | 0:325e3b1dbec8 | 125 | //normal settings |
jehoon | 0:325e3b1dbec8 | 126 | CON_STEP = csDISASSOCIATE; |
jehoon | 0:325e3b1dbec8 | 127 | |
jehoon | 0:325e3b1dbec8 | 128 | printf("KT IoTMakers Example\r\n"); |
jehoon | 0:325e3b1dbec8 | 129 | |
jehoon | 0:325e3b1dbec8 | 130 | char buffer[256]; |
jehoon | 0:325e3b1dbec8 | 131 | int count; |
jehoon | 0:325e3b1dbec8 | 132 | |
jehoon | 0:325e3b1dbec8 | 133 | while(1) |
jehoon | 0:325e3b1dbec8 | 134 | { |
jehoon | 0:325e3b1dbec8 | 135 | switch (CON_STEP) |
jehoon | 0:325e3b1dbec8 | 136 | { |
jehoon | 0:325e3b1dbec8 | 137 | case csDISASSOCIATE: |
jehoon | 0:325e3b1dbec8 | 138 | Connect_to_AP(); |
jehoon | 0:325e3b1dbec8 | 139 | break; |
jehoon | 0:325e3b1dbec8 | 140 | case csASSOCIATE: |
jehoon | 0:325e3b1dbec8 | 141 | Connect_to_IoTMakers(); |
jehoon | 0:325e3b1dbec8 | 142 | break; |
jehoon | 0:325e3b1dbec8 | 143 | |
jehoon | 0:325e3b1dbec8 | 144 | case csKTCONNECTED: |
jehoon | 0:325e3b1dbec8 | 145 | |
jehoon | 0:325e3b1dbec8 | 146 | if( ( g_prev_send_time + DELAY_SEND_TIME_MS ) < g_time_ms ) |
jehoon | 0:325e3b1dbec8 | 147 | { |
jehoon | 0:325e3b1dbec8 | 148 | Periodic_Event(); |
jehoon | 0:325e3b1dbec8 | 149 | g_prev_send_time = g_time_ms; |
jehoon | 0:325e3b1dbec8 | 150 | } |
jehoon | 0:325e3b1dbec8 | 151 | |
jehoon | 0:325e3b1dbec8 | 152 | if( button_clicked == 1 ) |
jehoon | 0:325e3b1dbec8 | 153 | { |
jehoon | 0:325e3b1dbec8 | 154 | Button_Push_Event(); |
jehoon | 0:325e3b1dbec8 | 155 | button_clicked = 0; |
jehoon | 0:325e3b1dbec8 | 156 | } |
jehoon | 0:325e3b1dbec8 | 157 | |
jehoon | 0:325e3b1dbec8 | 158 | if( pwizfi310->readable(0) > 0) |
jehoon | 0:325e3b1dbec8 | 159 | { |
jehoon | 0:325e3b1dbec8 | 160 | pwizfi310->recv(0, buffer, sizeof(buffer)); |
jehoon | 0:325e3b1dbec8 | 161 | printf("RECV : %s\r\n", buffer); |
jehoon | 0:325e3b1dbec8 | 162 | } |
jehoon | 0:325e3b1dbec8 | 163 | |
jehoon | 0:325e3b1dbec8 | 164 | |
jehoon | 0:325e3b1dbec8 | 165 | break; |
jehoon | 0:325e3b1dbec8 | 166 | default: |
jehoon | 0:325e3b1dbec8 | 167 | break; |
jehoon | 0:325e3b1dbec8 | 168 | }; |
jehoon | 0:325e3b1dbec8 | 169 | } |
jehoon | 0:325e3b1dbec8 | 170 | |
jehoon | 0:325e3b1dbec8 | 171 | |
jehoon | 0:325e3b1dbec8 | 172 | |
jehoon | 0:325e3b1dbec8 | 173 | |
jehoon | 0:325e3b1dbec8 | 174 | } |
jehoon | 0:325e3b1dbec8 | 175 | |
jehoon | 0:325e3b1dbec8 | 176 | |
jehoon | 0:325e3b1dbec8 | 177 | void Connect_to_AP() |
jehoon | 0:325e3b1dbec8 | 178 | { |
jehoon | 0:325e3b1dbec8 | 179 | int i; |
jehoon | 0:325e3b1dbec8 | 180 | int ret; |
jehoon | 0:325e3b1dbec8 | 181 | |
jehoon | 0:325e3b1dbec8 | 182 | for( i=0; i<5; i++ ) |
jehoon | 0:325e3b1dbec8 | 183 | { |
jehoon | 0:325e3b1dbec8 | 184 | |
jehoon | 0:325e3b1dbec8 | 185 | if( CON_STEP != csDISASSOCIATE ) |
jehoon | 0:325e3b1dbec8 | 186 | { |
jehoon | 0:325e3b1dbec8 | 187 | break; |
jehoon | 0:325e3b1dbec8 | 188 | } |
jehoon | 0:325e3b1dbec8 | 189 | |
jehoon | 0:325e3b1dbec8 | 190 | ret = wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); |
jehoon | 0:325e3b1dbec8 | 191 | |
jehoon | 0:325e3b1dbec8 | 192 | if( ret == 0 ) //connection success |
jehoon | 0:325e3b1dbec8 | 193 | { |
jehoon | 0:325e3b1dbec8 | 194 | //const char *ip = wifi.get_ip_address(); |
jehoon | 0:325e3b1dbec8 | 195 | //const char *mac = wifi.get_mac_address(); |
jehoon | 0:325e3b1dbec8 | 196 | //printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
jehoon | 0:325e3b1dbec8 | 197 | //printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
jehoon | 0:325e3b1dbec8 | 198 | |
jehoon | 0:325e3b1dbec8 | 199 | CON_STEP = csASSOCIATE; |
jehoon | 0:325e3b1dbec8 | 200 | wait(1); |
jehoon | 0:325e3b1dbec8 | 201 | break; |
jehoon | 0:325e3b1dbec8 | 202 | } |
jehoon | 0:325e3b1dbec8 | 203 | else |
jehoon | 0:325e3b1dbec8 | 204 | { |
jehoon | 0:325e3b1dbec8 | 205 | printf("CONNECTION FAIL\r\n", ret); |
jehoon | 0:325e3b1dbec8 | 206 | CON_STEP = csASSOCIATE; |
jehoon | 0:325e3b1dbec8 | 207 | } |
jehoon | 0:325e3b1dbec8 | 208 | } |
jehoon | 0:325e3b1dbec8 | 209 | } |
jehoon | 0:325e3b1dbec8 | 210 | |
jehoon | 0:325e3b1dbec8 | 211 | void Connect_to_IoTMakers() |
jehoon | 0:325e3b1dbec8 | 212 | { |
jehoon | 0:325e3b1dbec8 | 213 | char cmd[100]; |
jehoon | 0:325e3b1dbec8 | 214 | int ret = 0; |
jehoon | 0:325e3b1dbec8 | 215 | |
jehoon | 0:325e3b1dbec8 | 216 | //set_information |
jehoon | 0:325e3b1dbec8 | 217 | sprintf(cmd, "AT+TKTSET1=%s,%s,%s,0", IoTMakers_GW_ID, IoTMakers_DEV_ID, IoTMakers_DEV_PWD); |
jehoon | 0:325e3b1dbec8 | 218 | pwizfi310->sendCommand(cmd); |
jehoon | 0:325e3b1dbec8 | 219 | |
jehoon | 0:325e3b1dbec8 | 220 | //connect IoTMakers via MQTT |
jehoon | 0:325e3b1dbec8 | 221 | sprintf(cmd, "AT+TKTCON=2,%s,%d,0,1111", IoTMakers_MQTT_IP, IoTMakers_MQTT_PORT); |
jehoon | 0:325e3b1dbec8 | 222 | ret = pwizfi310->sendCommand(cmd,WizFi310::RES_NULL,5000); |
jehoon | 0:325e3b1dbec8 | 223 | |
jehoon | 0:325e3b1dbec8 | 224 | if(ret == 0) |
jehoon | 0:325e3b1dbec8 | 225 | { |
jehoon | 0:325e3b1dbec8 | 226 | CON_STEP = csKTCONNECTED; |
jehoon | 0:325e3b1dbec8 | 227 | } |
jehoon | 0:325e3b1dbec8 | 228 | } |
jehoon | 0:325e3b1dbec8 | 229 | |
jehoon | 0:325e3b1dbec8 | 230 | void Send_num_value_to_IoTMakers(const char* tag_stream, const float dvalue) |
jehoon | 0:325e3b1dbec8 | 231 | { |
jehoon | 0:325e3b1dbec8 | 232 | char cmd[100]; |
jehoon | 0:325e3b1dbec8 | 233 | //send numberic data |
jehoon | 0:325e3b1dbec8 | 234 | sprintf(cmd, "AT+TKTSEND=d,%s,%.1f", tag_stream, dvalue); |
jehoon | 0:325e3b1dbec8 | 235 | pwizfi310->sendCommand(cmd); |
jehoon | 0:325e3b1dbec8 | 236 | } |
jehoon | 0:325e3b1dbec8 | 237 | |
jehoon | 0:325e3b1dbec8 | 238 | void Send_str_value_to_IoTMakers(const char* tag_stream, const char* svalue) |
jehoon | 0:325e3b1dbec8 | 239 | { |
jehoon | 0:325e3b1dbec8 | 240 | char cmd[100]; |
jehoon | 0:325e3b1dbec8 | 241 | //send string data |
jehoon | 0:325e3b1dbec8 | 242 | sprintf(cmd, "AT+TKTSEND=s,%s,%s", tag_stream, svalue); |
jehoon | 0:325e3b1dbec8 | 243 | pwizfi310->sendCommand(cmd); |
jehoon | 0:325e3b1dbec8 | 244 | } |
jehoon | 0:325e3b1dbec8 | 245 | |
jehoon | 0:325e3b1dbec8 | 246 | |
jehoon | 0:325e3b1dbec8 | 247 | void time_ms() |
jehoon | 0:325e3b1dbec8 | 248 | { |
jehoon | 0:325e3b1dbec8 | 249 | g_time_ms++; |
jehoon | 0:325e3b1dbec8 | 250 | } |