/*---------------------------------------------------------------------------* * Routine: App_StartWPS *---------------------------------------------------------------------------* * Description: * Put the unit into WPS pushbutton mode. After pushing the button on the * AP the unit will retrieve its SSID and pass phrase, then connect to it * Inputs: * void * Outputs: * void *---------------------------------------------------------------------------*/ void App_StartWPS(void) { ATLIBGS_MSG_ID_E rxMsgId; ATLIBGS_NetworkStatus network; AtLibGs_WPSResult result; char text[20]; while (1) { // Ensure we are not connected to any network (from previous runs) AtLibGs_DisAssoc(); /* Pushbutton WPS demo */ /* Use Wi-Fi Protected Setup (WPS) FW */ /* turn on DHCP client */ AtLibGs_DHCPSet(1); /* set to connect to AP mode */ AtLibGs_Mode(ATLIBGS_STATIONMODE_INFRASTRUCTURE); DisplayLCD(LCD_LINE5, " Push the "); DisplayLCD(LCD_LINE6, "button on AP"); /* push the button on the AP so the GS module can connect */ while (AtLibGs_StartWPSPUSH(&result) != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE8, " Not found!"); MSTimerDelay(1000); DisplayLCD(LCD_LINE8, " Retrying..."); MSTimerDelay(1000); DisplayLCD(LCD_LINE8, ""); } /* Connect to AP (found from pushbutton) after setting pass phrase */ AtLibGs_SetPassPhrase(result.password); AtLibGs_Assoc(result.ssid, "", result.channel); rxMsgId = AtLibGs_GetNetworkStatus(&network); if (rxMsgId != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE8, "Bad Network!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE8, ""); continue; } else { strncpy(text, network.ssid, 12); DisplayLCD(LCD_LINE4, (const uint8_t *)text); } break; } DisplayLCD(LCD_LINE5, ""); DisplayLCD(LCD_LINE6, ""); }
/*---------------------------------------------------------------------------*/ void App_TCPClientDemo(void) { ATLIBGS_MSG_ID_E rxMsgId = ATLIBGS_MSG_ID_NONE; static char content[512]; uint8_t cid = 0; static int16_t G_adc_int[2] = { 0, 0 }; static char G_temp_int[2] = { 0, 0 }; uint8_t remoteTcpSrvIp[20]; bool connected = false; // when connected to TCP server this is true uint32_t time = MSTimerGet(); ATLIBGS_TCPMessage msg; ATLIBGS_NetworkStatus networkStatus; AtLibGs_GetNetworkStatus(&networkStatus); AppTCPSetIPMenu(&networkStatus); sprintf((char*)remoteTcpSrvIp, "%d.%d.%d.%d", networkStatus.addr.ipv4[0], networkStatus.addr.ipv4[1], networkStatus.addr.ipv4[2], G_nvsettings.webprov.tcpIPClientHostIP); App_PrepareIncomingData(); while (1) { if (!AtLibGs_IsNodeAssociated()) { App_Connect(&G_nvsettings.webprov); connected = false; } else if (!connected) { DisplayLCD(LCD_LINE7, "Connecting"); // Start a TCP client rxMsgId = AtLibGs_TCPClientStart((char *)remoteTcpSrvIp, TCP_DEMO_REMOTE_TCP_SRVR_PORT, &cid); if (rxMsgId != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE7, "No Connect!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); continue; } if (cid == ATLIBGS_INVALID_CID) { DisplayLCD(LCD_LINE7, "No CID!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); continue; } DisplayLCD(LCD_LINE7, ""); App_PrepareIncomingData(); connected = true; } else { App_TemperatureReadingUpdate(G_temp_int, true); App_PotentiometerUpdate(G_adc_int, true); // Look to see if there is a message if ((G_receivedCount) || (AtLibGs_WaitForTCPMessage(250) == ATLIBGS_MSG_ID_DATA_RX)) { // Got data! Its sitting in G_received, but in a <CID> <data> format // We need to send it back AtLibGs_ParseTCPData(G_received, G_receivedCount, &msg); // Prepare for the next batch of incoming data App_PrepareIncomingData(); // Copy the data out of the receive message (its sitting in G_recieved) memcpy(content, msg.message, msg.numBytes); // Now send this back over the TCP/IP connection rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content, msg.numBytes); if (rxMsgId != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE7, " Send Fail!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); continue; } } else if (MSTimerDelta(time) >= TCP_DEMO_UPDATE_INTERVAL) { time = MSTimerGet(); // send temp and ADC to last received TCP client every X seconds DisplayLCD(LCD_LINE7, " Sending"); sprintf(content, "Temp: %d.%d, Pot: %d.%d%%\r\n", G_temp_int[0], G_temp_int[1], G_adc_int[0], G_adc_int[1]); // send data to server rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content, strlen( content)); if (rxMsgId != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE7, " Send Fail!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); connected = false; continue; } // end if DisplayLCD(LCD_LINE7, ""); } // end else if } // end else } // end while }
/*---------------------------------------------------------------------------* * Routine: App_WebProvisioning *---------------------------------------------------------------------------* * Description: * Put the unit into web provisioning mode and wait for the user to * connect with a web browser, change the settings, and click Save. * The settings will then be parsed by the AtLibGs library and * get saved into the nv settings. * Inputs: * void * Outputs: * void *---------------------------------------------------------------------------*/ void App_WebProvisioning(void) { ATLIBGS_MSG_ID_E r; /* At power up, load up the default settings */ if(NVSettingsLoad(&G_nvsettings)) NVSettingsSave(&G_nvsettings); App_InitModule(); while(1) { r = AtLibGs_GetMAC(WiFiMAC); if(r != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE6, "Get MAC Failed!"); MSTimerDelay(2000); continue; } break; }; if(r == ATLIBGS_MSG_ID_OK) AtLibGs_ParseGetMacResponse(WiFiMACStr); strcpy(str_config_ssid, (char const*)ATLIBGS_ADK_SSID); strcat(str_config_ssid, &WiFiMACStr[6]); // concatenate last 6 digis of MAC as SSID App_StartupLimitedAP(str_config_ssid); /* Before going into web provisioning, provide DNS to give a link. */ /* The user can then go to http://webprov.gainspan.com/gsclient.html to get */ /* access to the web provisioning screen. */ while (1) { #if 0 AtLibGs_DisableDNSServer(); r = AtLibGs_EnableDNSServer("webprov.gainspan.com"); if (r != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE6, "Bad DNS!"); MSTimerDelay(2000); continue; } #endif r = AtLibGs_WebProv(",", ","); if (r != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE6, "Bad WebProv!"); MSTimerDelay(2000); continue; } break; } DisplayLCD(LCD_LINE6, "WebProv ON"); DisplayLCD(LCD_LINE7, (const uint8_t *) "192.168.240."); DisplayLCD(LCD_LINE8, (const uint8_t *) "1/prov.html"); #if 0 do { DisplayLCD(LCD_LINE7, "IP: ???.???."); DisplayLCD(LCD_LINE8, " ???.???"); r = AtLibGs_GetNetworkStatus(&network_status); } while (ATLIBGS_MSG_ID_OK != r); sprintf(text, "IP: " _F8_ "." _F8_ ".", network_status.addr.ipv4[0], network_status.addr.ipv4[1]); DisplayLCD(LCD_LINE7, (uint8_t *)text); sprintf(text, " " _F8_ "." _F8_, network_status.addr.ipv4[2], network_status.addr.ipv4[3]); DisplayLCD(LCD_LINE8, (uint8_t *)text); #endif #ifdef ATLIBGS_DEBUG_ENABLE ConsolePrintf("Web Provisioning ON\n"); #endif /* Now wait for a list of responses until we get a blank line */ #ifdef ATLIBGS_DEBUG_ENABLE ConsolePrintf("Waiting for web provisioning response...\n"); #endif AtLibGs_GetWebProvSettings(&G_nvsettings.webprov, 0); /* Save the above settings */ NVSettingsSave(&G_nvsettings); #ifdef ATLIBGS_DEBUG_ENABLE ConsolePrintf("Web provisioning complete.\n"); #endif DisplayLCD(LCD_LINE6, "WebProv Done"); DisplayLCD(LCD_LINE7, ""); DisplayLCD(LCD_LINE8, "Press RESET"); while (1) {} }