ssc-wifi application

Dependencies:   DHT esp8266-driver

Committer:
drrrnino7
Date:
Mon Nov 06 06:47:07 2017 +0000
Revision:
3:97041d02648e
Parent:
1:be7ba474fb5f
Child:
4:7161fd64b2ef
Add WiFi Logic

Who changed what in which revision?

UserRevisionLine numberNew contents of line
drrrnino7 0:6f274a82671c 1 #include "mbed.h"
drrrnino7 0:6f274a82671c 2
drrrnino7 1:be7ba474fb5f 3 #if defined(FEATURE_COMMON_PAL)
drrrnino7 1:be7ba474fb5f 4 #include "mbed_trace.h"
drrrnino7 1:be7ba474fb5f 5 #define TRACE_GROUP "APPL"
drrrnino7 1:be7ba474fb5f 6 #else
drrrnino7 1:be7ba474fb5f 7 #define tr_debug(...) (void(0)) //dummies if feature common pal is not added
drrrnino7 1:be7ba474fb5f 8 #define tr_info(...) (void(0)) //dummies if feature common pal is not added
drrrnino7 1:be7ba474fb5f 9 #define tr_error(...) (void(0)) //dummies if feature common pal is not added
drrrnino7 1:be7ba474fb5f 10 #endif //defined(FEATURE_COMMON_PAL)
drrrnino7 1:be7ba474fb5f 11
drrrnino7 3:97041d02648e 12 #define TW_TCP_TEST_DEVICE_NO 1
drrrnino7 1:be7ba474fb5f 13 #define TW_TCP_TEST_SERVER_IP "1.221.6.243"
drrrnino7 1:be7ba474fb5f 14 #define TW_TCP_TEST_SERVER_PORT 41112
drrrnino7 1:be7ba474fb5f 15
drrrnino7 1:be7ba474fb5f 16 #include "ESP8266Interface.h"
drrrnino7 1:be7ba474fb5f 17 ESP8266Interface wifi(PC_4, PC_5);
drrrnino7 1:be7ba474fb5f 18
drrrnino7 3:97041d02648e 19 const char *sec2str(nsapi_security_t sec)
drrrnino7 3:97041d02648e 20 {
drrrnino7 3:97041d02648e 21 switch (sec) {
drrrnino7 3:97041d02648e 22 case NSAPI_SECURITY_NONE:
drrrnino7 3:97041d02648e 23 return "None";
drrrnino7 3:97041d02648e 24 case NSAPI_SECURITY_WEP:
drrrnino7 3:97041d02648e 25 return "WEP";
drrrnino7 3:97041d02648e 26 case NSAPI_SECURITY_WPA:
drrrnino7 3:97041d02648e 27 return "WPA";
drrrnino7 3:97041d02648e 28 case NSAPI_SECURITY_WPA2:
drrrnino7 3:97041d02648e 29 return "WPA2";
drrrnino7 3:97041d02648e 30 case NSAPI_SECURITY_WPA_WPA2:
drrrnino7 3:97041d02648e 31 return "WPA/WPA2";
drrrnino7 3:97041d02648e 32 case NSAPI_SECURITY_UNKNOWN:
drrrnino7 3:97041d02648e 33 default:
drrrnino7 3:97041d02648e 34 return "Unknown";
drrrnino7 3:97041d02648e 35 }
drrrnino7 3:97041d02648e 36 }
drrrnino7 3:97041d02648e 37
drrrnino7 3:97041d02648e 38 void scan_demo(WiFiInterface *wifi)
drrrnino7 3:97041d02648e 39 {
drrrnino7 3:97041d02648e 40 WiFiAccessPoint *ap;
drrrnino7 3:97041d02648e 41
drrrnino7 3:97041d02648e 42 tr_info("Scan:\r\n");
drrrnino7 3:97041d02648e 43
drrrnino7 3:97041d02648e 44 int count = wifi->scan(NULL,0);
drrrnino7 3:97041d02648e 45
drrrnino7 3:97041d02648e 46 /* Limit number of network arbitrary to 15 */
drrrnino7 3:97041d02648e 47 count = count < 15 ? count : 15;
drrrnino7 3:97041d02648e 48
drrrnino7 3:97041d02648e 49 ap = new WiFiAccessPoint[count];
drrrnino7 3:97041d02648e 50 count = wifi->scan(ap, count);
drrrnino7 3:97041d02648e 51 for (int i = 0; i < count; i++)
drrrnino7 3:97041d02648e 52 {
drrrnino7 3:97041d02648e 53 tr_info("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
drrrnino7 3:97041d02648e 54 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
drrrnino7 3:97041d02648e 55 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
drrrnino7 3:97041d02648e 56 }
drrrnino7 3:97041d02648e 57 tr_info("%d networks available.\r\n", count);
drrrnino7 3:97041d02648e 58
drrrnino7 3:97041d02648e 59 delete[] ap;
drrrnino7 3:97041d02648e 60 }
drrrnino7 1:be7ba474fb5f 61
drrrnino7 0:6f274a82671c 62 int main()
drrrnino7 0:6f274a82671c 63 {
drrrnino7 3:97041d02648e 64 TCPSocket tcp_socket;
drrrnino7 3:97041d02648e 65
drrrnino7 3:97041d02648e 66 printf("\r\nSSU Temperature/Humidity WiFi Comm Board\r\n");
drrrnino7 3:97041d02648e 67 mbed_trace_init();
drrrnino7 3:97041d02648e 68
drrrnino7 3:97041d02648e 69 int ret;
drrrnino7 3:97041d02648e 70 tr_info("WiFi Connecting...");
drrrnino7 3:97041d02648e 71 while(wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2) != 0) {
drrrnino7 3:97041d02648e 72 tr_err("WiFi Connection error, Retry");
drrrnino7 3:97041d02648e 73 }
drrrnino7 3:97041d02648e 74
drrrnino7 3:97041d02648e 75 tr_info("Successfully connected to WiFi AP");
drrrnino7 3:97041d02648e 76 tr_debug("MAC: %s", wifi.get_mac_address());
drrrnino7 3:97041d02648e 77 tr_debug("IP: %s", wifi.get_ip_address());
drrrnino7 3:97041d02648e 78 tr_debug("Netmask: %s", wifi.get_netmask());
drrrnino7 3:97041d02648e 79 tr_debug("Gateway: %s", wifi.get_gateway());
drrrnino7 3:97041d02648e 80 tr_debug("RSSI: %d", wifi.get_rssi());
drrrnino7 3:97041d02648e 81
drrrnino7 3:97041d02648e 82 tr_info("Create WiFi TCP Socket");
drrrnino7 3:97041d02648e 83 if((ret = tcp_socket.open(&wifi)) < 0) {
drrrnino7 3:97041d02648e 84 tr_err("Error: Cannot Open UDP Socket (err code:%d)", ret);
drrrnino7 3:97041d02648e 85 NVIC_SystemReset();
drrrnino7 3:97041d02648e 86 }
drrrnino7 3:97041d02648e 87 tr_info("Successfully created WiFi TCP Socket");
drrrnino7 3:97041d02648e 88
drrrnino7 0:6f274a82671c 89 while(1) {
drrrnino7 3:97041d02648e 90 float humid = 0.0f;
drrrnino7 3:97041d02648e 91 float temp = 0.0f;
drrrnino7 0:6f274a82671c 92
drrrnino7 3:97041d02648e 93 char json_buf[100];
drrrnino7 3:97041d02648e 94
drrrnino7 3:97041d02648e 95 //TODO: DHT humid & temp read
drrrnino7 3:97041d02648e 96
drrrnino7 3:97041d02648e 97 tr_debug("Humidity = %f%% Temperature = %fC", humid, temp);
drrrnino7 3:97041d02648e 98 sprintf(json_buf, "{\r\n\t\"temp\":%f,\r\n\t\"humid\":%f\r\n}\r\n", temp, humid);
drrrnino7 3:97041d02648e 99
drrrnino7 3:97041d02648e 100 if((ret = tcp_socket.connect(TW_TCP_TEST_SERVER_IP, TW_TCP_TEST_SERVER_PORT)) < 0) {
drrrnino7 3:97041d02648e 101 tr_err("Cannot connect to TCP Server");
drrrnino7 3:97041d02648e 102 } else {
drrrnino7 3:97041d02648e 103 if((ret = tcp_socket.send(json_buf, strlen(json_buf))) < 0) {
drrrnino7 3:97041d02648e 104 tr_err("Cannot send to TCP Server");
drrrnino7 3:97041d02648e 105 } else {
drrrnino7 3:97041d02648e 106 tr_info("Sucessfully send TCP packet via WiFi");
drrrnino7 3:97041d02648e 107 }
drrrnino7 3:97041d02648e 108 }
drrrnino7 3:97041d02648e 109
drrrnino7 3:97041d02648e 110 wait(1);
drrrnino7 0:6f274a82671c 111 }
drrrnino7 0:6f274a82671c 112 }