int main0(void){ // "Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers", // ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2014, Volume 2, Program 11.2 UINT8 IsDHCP = 0; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr; UINT16 AddrSize = 0; INT16 SockID = 0; UINT32 data; unsigned char len = sizeof(_NetCfgIpV4Args_t); initClk(); // PLL 50 MHz, ADC needs PPL active 15 ADC0_InitSWTriggerSeq3(7); // Ain7 is on PD0 16 sl_Start(0, 0, 0); // Initializing the CC3100 device 17 WlanConnect(); // connect to AP 18 sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len, // 19 (unsigned char *)&ipV4); // 20 Addr.sin_family = SL_AF_INET; // 21 Addr.sin_port = sl_Htons((UINT16)PORT_NUM); // 22 Addr.sin_addr.s_addr = sl_Htonl((UINT32)IP_ADDR); // 23 AddrSize = sizeof(SlSockAddrIn_t); // 24 SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); // 25 while(1){ uBuf[0] = ATYPE; // analog data type 26 uBuf[1] = '='; // 27 data = ADC0_InSeq3(); // 0 to 4095, Ain7 is on PD0 28 Int2Str(data,(char*)&uBuf[2]); // 6 digit number 29 sl_SendTo(SockID, uBuf, BUF_SIZE, 0, // 30 (SlSockAddr_t *)&Addr, AddrSize); // 31 ROM_SysCtlDelay(ROM_SysCtlClockGet() / 25); // 40ms 32 } }
static int RegisterSocketServices(void) { int retval; char servicename[MDNS_SERVICE_NAME_MAXLEN]; LOG(LOG_IMPORTANT, "%sRegistering services on mDNS...",wifi_log_prefix); //retreive the device mac address char macstring[20]; unsigned char mac[SL_MAC_ADDR_LEN]; unsigned char maclen = SL_MAC_ADDR_LEN; retval = sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &maclen, mac); if(retval < 0) { RETURN_ERROR(retval, "Sock: Unable to get MAC address."); } sprintf(macstring, "[%02X:%02X:%02X:%02X:%02X:%02X]", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); for(int i=0; i<NUM_SOCKETS; i++){ sprintf(servicename, "%s - %s", macstring, socket_mdns_names_fixedpart[i]); retval = sl_NetAppMDNSRegisterService((signed char*)servicename, (unsigned char)strlen(servicename), (signed char*)socket_mdns_descriptions[i], (unsigned char)strlen(socket_mdns_descriptions[i]), socket_ports[i],MDNS_SERVICE_TTL,1); if(retval < 0) { RETURN_ERROR(retval, "Sock: Unable to register mDNS."); } } LOG(LOG_IMPORTANT, "%sServices registered.",wifi_log_prefix); return RET_SUCCESS; }
//**************************************************************************** // //! \brief Display the IP Adderess of device //! //! \param[in] none //! //! \return none //! // //**************************************************************************** static void DisplayIP() { unsigned long mask, dns, gateway; unsigned char len, dhcpIsOn; if(g_ulDeviceIp == 0) { /* Device is in GO mode, Get the IP of Device */ len = sizeof(_NetCfgIpV4Args_t); dhcpIsOn = 0; _NetCfgIpV4Args_t ipV4 = {0}; sl_NetCfgGet(SL_IPV4_AP_P2P_GO_GET_INFO,&dhcpIsOn,&len,(unsigned char *)&ipV4); g_ulDeviceIp=ipV4.ipV4; mask=ipV4.ipV4Mask; gateway=ipV4.ipV4Gateway; dns=ipV4.ipV4DnsServer; } UNUSED(mask); UNUSED(gateway); UNUSED(dns); UART_PRINT("CC3200 Device IP : IP=%d.%d.%d.%d \n\r", SL_IPV4_BYTE(g_ulDeviceIp,3), SL_IPV4_BYTE(g_ulDeviceIp,2), SL_IPV4_BYTE(g_ulDeviceIp,1), SL_IPV4_BYTE(g_ulDeviceIp,0)); }
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) { if (!_initialized) { init(); } // // Set the local_IP indicating that the network // is configured for static IP. // local_IP = local_ip; // //get current configuration // SlNetCfgIpV4Args_t config = {0}; unsigned char len = sizeof(SlNetCfgIpV4Args_t); sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, NULL, &len, (unsigned char*)&config); // //Assign new ip address and new dns server to current config //and use netcfgset to set the new configuration in memory // config.ipV4 = sl_Ntohl((uint32_t)local_ip); config.ipV4DnsServer = sl_Ntohl((uint32_t)dns_server); config.ipV4Gateway = sl_Ntohl((uint32_t)gateway); config.ipV4Mask = sl_Ntohl((uint32_t)subnet); sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, 1, sizeof(SlNetCfgIpV4Args_t), (unsigned char*)&config); }
void WiFiClass::config(IPAddress local_ip) { if (!_initialized) { init(); } // // Set the local_IP indicating that the network // is configured for static IP. // local_IP = local_ip; // //get current configuration // _NetCfgIpV4Args_t config = {0}; unsigned char len = sizeof(_NetCfgIpV4Args_t); sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, NULL, &len, (unsigned char*)&config); // //Assign new ip address to current config //and use netcfgset to set the new configuration in memory // config.ipV4 = sl_Ntohl((uint32_t)local_ip); sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, 1, sizeof(_NetCfgIpV4Args_t), (unsigned char*)&config); }
//**************************************************************************** // //! Get the device mac address //! //! \brief This function gets the MAC address of the device //! //! \param pMACAddress is pointer to variable where MAC address will be stored //! //! \return None. // //**************************************************************************** void NetMACAddressGet(unsigned char *pMACAddress) { unsigned char macAddressLen = SL_MAC_ADDR_LEN; // // Get the MAC address // sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &macAddressLen, pMACAddress); }
//**************************************************************************** // //! \brief Display the IP Adderess of device //! //! \param[in] none //! //! \return none //! // //**************************************************************************** static signed long DisplayIP() { unsigned long mask, dns, gateway; long lRetVal = -1; unsigned char len, dhcpIsOn; if(g_ulDeviceIp == 0) { /* Device is in GO mode, Get the IP of Device */ len = sizeof(SlNetCfgIpV4Args_t); dhcpIsOn = 0; SlNetCfgIpV4Args_t ipV4 = {0}; // Get the IP address of device #ifdef P2P_ROLE_TYPE_NEGOTIATE lRetVal = sl_NetCfgGet(SL_IPV4_AP_P2P_GO_GET_INFO,&dhcpIsOn, &len,(unsigned char *)&ipV4); #else lRetVal = sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&dhcpIsOn, &len,(unsigned char *)&ipV4) #endif ASSERT_ON_ERROR(lRetVal); g_ulDeviceIp=ipV4.ipV4; mask=ipV4.ipV4Mask; gateway=ipV4.ipV4Gateway; dns=ipV4.ipV4DnsServer; } UNUSED(mask); UNUSED(gateway); UNUSED(dns); UART_PRINT("CC3200 Device IP : IP=%d.%d.%d.%d \n\r", SL_IPV4_BYTE(g_ulDeviceIp,3), SL_IPV4_BYTE(g_ulDeviceIp,2), SL_IPV4_BYTE(g_ulDeviceIp,1), SL_IPV4_BYTE(g_ulDeviceIp,0)); return 0; }
// "Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers", // ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2014, Volume 2, Program 11.3 int main3(void){ UINT8 IsDHCP = 0; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr, LocalAddr; UINT16 AddrSize = 0; INT16 SockID = 0; INT16 Status = 1; // ok UINT32 data; unsigned char len = sizeof(_NetCfgIpV4Args_t); initClk(); // PLL 50 MHz, ADC needs PPL active 16 ST7735_InitR(INITR_REDTAB); // Initialize 17 ST7735_OutString("Internet of Things\n"); // 18 ST7735_OutString("Embedded Systems\n"); // 19 ST7735_OutString("Vol. 2, Valvano"); // 20 ST7735_PlotClear(0,4095); // range from 0 to 4095 21 sl_Start(0, 0, 0); // Initializing the CC3100 device 22 WlanConnect(); // connect to AP 23 sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len, // 24 (unsigned char *)&ipV4); // 25 LocalAddr.sin_family = SL_AF_INET; // 26 LocalAddr.sin_port = sl_Htons((UINT16)PORT_NUM); // 27 LocalAddr.sin_addr.s_addr = 0; // 28 AddrSize = sizeof(SlSockAddrIn_t); // 29 while(1){ SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); // 31 Status = sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, // 32 AddrSize); // 33 Status = sl_RecvFrom(SockID, uBuf, BUF_SIZE, 0, // 34 (SlSockAddr_t *)&Addr, (SlSocklen_t*)&AddrSize );// 35 if((uBuf[0]==ATYPE)&&(uBuf[1]== '=')){ // 36 int i,bOk; uint32_t place; // 37 data = 0; bOk = 1; // 38 i=4; // ignore possible negative sign 39 for(place = 1000; place; place = place/10){ // 40 if((uBuf[i]&0xF0)==0x30){ // ignore spaces 41 data += place*(uBuf[i]-0x30); // 42 }else{ // 43 if((uBuf[i]&0xF0)!= ' '){ // 44 bOk = 0; // 45 } // 46 } // 47 i++; // 48 } // 49 if(bOk){ // 50 ST7735_PlotLine(data); // 51 ST7735_PlotNextErase(); // 51 } } } }
//***************************************************************************** // //! Network_IF_IpConfigGet Get the IP Address of the device. //! //! \param pulIP IP Address of Device //! \param pulSubnetMask Subnetmask of Device //! \param pulDefaultGateway Default Gateway value //! \param pulDNSServer DNS Server //! //! \return none // //***************************************************************************** int Network_IF_IpConfigGet(unsigned long *pulIP, unsigned long *pulSubnetMask, unsigned long *pulDefaultGateway, unsigned long *pulDNSServer) { unsigned char isDhcp; unsigned char len = sizeof(_NetCfgIpV4Args_t); _NetCfgIpV4Args_t ipV4 = {0}; sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&isDhcp,&len,(unsigned char *)&ipV4); *pulIP=ipV4.ipV4; *pulSubnetMask=ipV4.ipV4Mask; *pulDefaultGateway=ipV4.ipV4Gateway; *pulDefaultGateway=ipV4.ipV4DnsServer; return 1; }
int CMD_who(int argc, char **argv) { UINT8 IsDHCP; _NetCfgIpV4Args_t ipV4; unsigned char len = sizeof(_NetCfgIpV4Args_t); sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP, &len,(unsigned char *)&ipV4); UARTprintf(" Server IP: %d.%d.%d.%d", SL_IPV4_BYTE(IP_ADDR,3), SL_IPV4_BYTE(IP_ADDR,2), SL_IPV4_BYTE(IP_ADDR,1), SL_IPV4_BYTE(IP_ADDR,0)); UARTprintf("\n"); UARTprintf("This node is at IP: %d.%d.%d.%d\n", SL_IPV4_BYTE(ipV4.ipV4,3), SL_IPV4_BYTE(ipV4.ipV4,2), SL_IPV4_BYTE(ipV4.ipV4,1), SL_IPV4_BYTE(ipV4.ipV4,0)); return 0; }
int CMD_ask(int argc, char **argv) { UINT8 IsDHCP; _NetCfgIpV4Args_t ipV4; unsigned char len = sizeof(_NetCfgIpV4Args_t); char packet[256]; char last3[10]; int temp; sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP, &len,(unsigned char *)&ipV4); temp = SL_IPV4_BYTE(ipV4.ipV4,0); sprintf(last3, "%d", temp); UARTprintf("%d", temp); UARTprintf("%s", last3); return 0; }
//--tested, working--// IPAddress WiFiClass::localIP() { // //the local IP is maintained with callbacks, so _SlNonOsMainLoopTask() //is critical. The IP is "written" into the buffer to avoid memory errors // _SlNonOsMainLoopTask(); SlNetCfgIpV4Args_t config = {0}; unsigned char len = sizeof(SlNetCfgIpV4Args_t); sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, NULL, &len, (unsigned char*)&config); // //change the uint32_t IP to the IPAddress class and return // IPAddress retIP(0,0,0,0); retIP = sl_Htonl(config.ipV4); return retIP; }
//--tested, working--// IPAddress WiFiClass::gatewayIP() { if (!_initialized) { init(); } // //get current configuration // _NetCfgIpV4Args_t config = {0}; unsigned char len = sizeof(_NetCfgIpV4Args_t); sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, NULL, &len, (unsigned char*)&config); // //change the uint32_t IP to the IPAddress class and return // IPAddress retIP(0,0,0,0); retIP = sl_Htonl(config.ipV4Gateway); return retIP; }
void WiFiClass::setDNS(IPAddress dns_server1) { if (!_initialized) { init(); } // //get current configuration // _NetCfgIpV4Args_t config = {0}; unsigned char len = sizeof(_NetCfgIpV4Args_t); sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, NULL, &len, (unsigned char*)&config); // //Assign new ip address and new dns server to current config //and use netcfgset to set the new configuration in memory // config.ipV4DnsServer = (uint32_t)SL_IPV4_VAL(dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]); sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, 1, sizeof(_NetCfgIpV4Args_t), (unsigned char*)&config); }
uint8_t* WiFiClass::macAddress(uint8_t* mac) { if (!_initialized) { init(); } // //Get the mac address and return the pointer to the array // uint8_t macTemp[6]; uint8_t macLength = 6; sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &macLength, (unsigned char *)macTemp); // //All the arduino examples return the mac address reverse from simplelink // memcpy(mac, macTemp, 6); return mac; }
//***************************************************************************** // // Ping an IP Address // Arguments: // [1] IP address to ping // [2] (optional) max number of tries // [3] (optional) timeout in milliseconds // //***************************************************************************** int CMD_ping(int argc, char **argv) { SlPingStartCommand_t PingParams; SlPingReport_t Report; _NetCfgIpV4Args_t ipV4; unsigned char len = sizeof(_NetCfgIpV4Args_t);; unsigned char IsDHCP = 0; /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len, (unsigned char *)&ipV4); /* Set the ping parameters */ PingParams.PingIntervalTime = PING_INTERVAL; PingParams.PingSize = PING_SIZE; PingParams.PingRequestTimeout = PING_TIMEOUT; PingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; PingParams.Flags = 0; PingParams.Ip = ipV4.ipV4Gateway; /* Fill the GW IP address, which is our AP address */ /* Check for LAN connection */ UARTprintf("Pinging the Gateway...\n"); sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, SimpleLinkPingReport); while(0 == (g_Status & PING_DONE)) { _SlNonOsMainLoopTask(); } g_Status &= ~(1 << PING_DONE_STATUS_BIT); UARTprintf("Ping Done\n"); if (!g_PingPacketsRecv) { /* Problem with LAN connection */ UARTprintf("Problem with the LAN connection\n"); return -1; } return 0; }
//***************************************************************************** // //! Network_IF_IpConfigGet Get the IP Address of the device. //! //! \param pulIP IP Address of Device //! \param pulSubnetMask Subnetmask of Device //! \param pulDefaultGateway Default Gateway value //! \param pulDNSServer DNS Server //! //! \return On success, zero is returned. On error, -1 is returned // //***************************************************************************** long Network_IF_IpConfigGet(unsigned long *pulIP, unsigned long *pulSubnetMask, unsigned long *pulDefaultGateway, unsigned long *pulDNSServer) { unsigned char isDhcp; unsigned char len = sizeof(SlNetCfgIpV4Args_t); long lRetVal = -1; SlNetCfgIpV4Args_t ipV4 = {0}; lRetVal = sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&isDhcp,&len, (unsigned char *)&ipV4); ASSERT_ON_ERROR(lRetVal); *pulIP=ipV4.ipV4; *pulSubnetMask=ipV4.ipV4Mask; *pulDefaultGateway=ipV4.ipV4Gateway; *pulDefaultGateway=ipV4.ipV4DnsServer; return lRetVal; }
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) { if (!_initialized) { init(); } // //get current configuration // _NetCfgIpV4Args_t config = {0}; unsigned char len = sizeof(_NetCfgIpV4Args_t); sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, NULL, &len, (unsigned char*)&config); // //Assign new ip address and new dns server to current config //and use netcfgset to set the new configuration in memory // config.ipV4 = (uint32_t)local_ip; config.ipV4DnsServer = (uint32_t)dns_server; config.ipV4Gateway = (uint32_t)gateway; sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, 1, sizeof(_NetCfgIpV4Args_t), (unsigned char*)&config); }
//***************************************************************************** //! getMacAddress //! //! Returns the MAC Address as string //! //**************************************************************************** char * getMacAddress() { int i; unsigned char macAddressVal[SL_MAC_ADDR_LEN]; unsigned char macAddressLen = SL_MAC_ADDR_LEN; sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(unsigned char *)macAddressVal); char macAddressPart[2]; static char macAddressFull[18]; //18 for (i = 0 ; i < 6 ; i++) { sprintf(macAddressPart, "%02X", macAddressVal[i]); strcat(macAddressFull, (char *)macAddressPart); strcat(macAddressFull, ":"); } macAddressFull[17] = '\0'; // Replace the the last : with a zero termination return macAddressFull; }
//**************************************************************************** // //! \brief start simplelink, wait for the sta to connect to the device and //! run the ping test for that sta //! //! \param pvparameters is the pointer to the list of parameters that can be //! passed to the task while creating it //! //! \return None // //**************************************************************************** void WlanAPMode( void *pvParameters ) { int iTestResult = 0; unsigned char ucDHCP; long retVal = -1; InitializeAppVariables(); // // Following function configure the device to default state by cleaning // the persistent settings stored in NVMEM (viz. connection profiles & // policies, power policy etc) // // Applications may choose to skip this step if the developer is sure // that the device is in its default state at start of applicaton // // Note that all profiles and persistent settings that were done on the // device will be lost // retVal = ConfigureSimpleLinkToDefaultState(); if(retVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == retVal) UART_PRINT("Failed to configure the device in its default state \n\r"); LOOP_FOREVER(__LINE__); } UART_PRINT("Device is configured in default state \n\r"); // // Asumption is that the device is configured in station mode already // and it is in its default state // retVal = sl_Start(NULL,NULL,NULL); if (retVal < 0) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(__LINE__); } UART_PRINT("Device started as STATION \n\r"); // // Configure the networking mode and ssid name(for AP mode) // if(retVal != ROLE_AP) { if(ConfigureMode(retVal) !=ROLE_AP) { UART_PRINT("Unable to set AP mode, exiting Application...\n\r"); sl_Stop(SL_STOP_TIMEOUT); return; } } while(!IS_IP_ACQUIRED(g_ulStatus)) { //looping till ip is acquired } unsigned char len = sizeof(_NetCfgIpV4Args_t); _NetCfgIpV4Args_t ipV4 = {0}; retVal = sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&ucDHCP,&len, (unsigned char *)&ipV4); if (retVal < 0) { UART_PRINT("Failed to get network configuration \n\r"); LOOP_FOREVER(__LINE__); } UART_PRINT("Connect a client to Device\n\r"); while(!IS_IP_LEASED(g_ulStatus)) { //wating for the client to connect } UART_PRINT("Client is connected to Device\n\r"); iTestResult = PingTest(g_ulStaIp); UNUSED(ucDHCP); UNUSED(iTestResult); sl_WlanSetMode(ROLE_STA); /* Switch off Network processor */ sl_Stop(SL_STOP_TIMEOUT); UART_PRINT("Application exits\n\r"); while(1); }
int main(void) { UINT8 IsDHCP = 0; int32_t i32CommandStatus; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr; UINT16 AddrSize = 0; INT16 SockID = 0; UINT32 data; long x = 0; //counter unsigned char len = sizeof(_NetCfgIpV4Args_t); int Status = 0; /* Stop WDT */ stopWDT(); /* Initialize the system clock of MCU */ initClk(); Board_Init(); // initialize LaunchPad I/O and PD1 LED ConfigureUART(); // Initialize the UART. UARTprintf("Section 11.4 IoT example, Volume 2 Real-time interfacing\n"); UARTprintf("This application is configured to generate text\n"); UARTprintf(" and send UDP packets to IP: %d.%d.%d.%d Port: %d\n\n", SL_IPV4_BYTE(IP_ADDR,3), SL_IPV4_BYTE(IP_ADDR,2), SL_IPV4_BYTE(IP_ADDR,1), SL_IPV4_BYTE(IP_ADDR,0),PORT_NUM); //added code from the powerpoint slide /* Initializing the CC3100 device */ sl_Start(0, 0, 0); /* Connecting to WLAN AP - Set with static parameters defined at the top After this call we will be connected and have IP address */ WlanConnect(); /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len,(unsigned char *)&ipV4); UARTprintf("This node is at IP: %d.%d.%d.%d\n", SL_IPV4_BYTE(ipV4.ipV4,3), SL_IPV4_BYTE(ipV4.ipV4,2), SL_IPV4_BYTE(ipV4.ipV4,1), SL_IPV4_BYTE(ipV4.ipV4,0)); Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons((UINT16)PORT_NUM); Addr.sin_addr.s_addr = sl_Htonl((UINT32)IP_ADDR); AddrSize = sizeof(SlSockAddrIn_t); SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); // Loop forever waiting for commands from PC... // while(1) { // Print prompt for user. UARTprintf("\n>"); // Peek to see if a full command is ready for processing. while(UARTPeek('\r') == -1) LED_On(); // Approximately 1 millisecond delay. ROM_SysCtlDelay(ROM_SysCtlClockGet() / 3000); } // A '\r' was detected so get the line of text from the receive buffer. while(Status >= 0){ UARTprintf("\nSending a UDP packet ...\n"); UARTgets(g_cInput,sizeof(g_cInput)); //this function receives the input from the Putty and places it in a string //DO NOT CHANGE ANYTHING ABOVE THIS COMMENT //WHAT WE NEED TO DO: //work with the g_cInput to get the array of letters typed into the Putty //then send that Array using UARTprintf uBuf[0] = ATYPE; // defines this as an analog data type uBuf[1] = '='; data = 1000; Int2Str(data,(char*)&uBuf[2]); // [2] to [7] is 6 digit number UARTprintf(" %s ",uBuf); //this line sends a string to the receiver //the above 5 lines print out a = 1000; //everything below this is just error cases if( SockID < 0 ){ UARTprintf("SockIDerror "); Status = -1; // error }else{ LED_Toggle(); Status = sl_SendTo(SockID, uBuf, BUF_SIZE, 0, (SlSockAddr_t *)&Addr, AddrSize); if( Status <= 0 ){ sl_Close(SockID); UARTprintf("SockIDerror %d ",Status); }else{ UARTprintf("ok"); } } ROM_SysCtlDelay(ROM_SysCtlClockGet() / 100); // 10ms LED_Off(); } }
void device_get_mac_address(uint8_t mac[6]) { uint8_t mac_len = 6; sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &mac_len, mac); }
int main2(void) { UINT8 IsDHCP = 0; int32_t i32CommandStatus; _NetCfgIpV4Args_t ipV4; unsigned char len = sizeof(_NetCfgIpV4Args_t); int Status = 0; /* Stop WDT */ stopWDT(); /* Initialize the system clock of MCU */ initClk(); Board_Init(); // initialize LaunchPad I/O and PD1 LED ConfigureUART(); // Initialize the UART. UARTprintf("Section 11.4 IoT example, Volume 2 Real-time interfacing\n"); UARTprintf("This application is configured to measure analog signals from Ain7=PD0\n"); UARTprintf(" and send UDP packets to IP: %d.%d.%d.%d Port: %d\n\n", SL_IPV4_BYTE(IP_ADDR,3), SL_IPV4_BYTE(IP_ADDR,2), SL_IPV4_BYTE(IP_ADDR,1), SL_IPV4_BYTE(IP_ADDR,0),PORT_NUM); /* Initializing the CC3100 device */ sl_Start(0, 0, 0); /* Connecting to WLAN AP - Set with static parameters defined at the top After this call we will be connected and have IP address */ WlanConnect(); /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len, (unsigned char *)&ipV4); //Print the IP UARTprintf("This node is at IP: %d.%d.%d.%d\n", SL_IPV4_BYTE(ipV4.ipV4,3), SL_IPV4_BYTE(ipV4.ipV4,2), SL_IPV4_BYTE(ipV4.ipV4,1), SL_IPV4_BYTE(ipV4.ipV4,0)); // // Loop forever waiting for commands from PC... // while(1) { // // Print prompt for user. // UARTprintf("\n>"); // // Peek to see if a full command is ready for processing. // while(UARTPeek('\r') == -1) { // // Approximately 1 millisecond delay. // ROM_SysCtlDelay(ROM_SysCtlClockGet() / 3000); } // // A '\r' was detected so get the line of text from the receive buffer. // UARTgets(g_cInput,sizeof(g_cInput)); // // Pass the line from the user to the command processor. // It will be parsed and valid commands executed. // i32CommandStatus = CmdLineProcess(g_cInput); // // Handle the case of bad command. // if(i32CommandStatus == CMDLINE_BAD_CMD) { UARTprintf(" Bad command. Try again.\n"); } // // Handle the case of too many arguments. // else if(i32CommandStatus == CMDLINE_TOO_MANY_ARGS) { UARTprintf(" Too many arguments for command. Try again.\n"); } // // Handle the case of too few arguments. // else if(i32CommandStatus == CMDLINE_TOO_FEW_ARGS) { UARTprintf(" Too few arguments for command. Try again.\n"); } // // Handle the case of too few arguments. // else if(i32CommandStatus == CMDLINE_INVALID_ARG) { UARTprintf(" Invalid command argument(s). Try again.\n"); } } }
int main(void){ UINT8 IsDHCP = 0; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr; SlSockAddrIn_t LocalAddr; UINT16 AddrSize = 0; INT16 SockID = 0; INT16 Status = 1; // ok UINT32 data; unsigned char len = sizeof(_NetCfgIpV4Args_t); stopWDT(); // Stop WDT initClk(); // PLL 50 MHz, ADC needs PPL active Board_Init(); // initialize LaunchPad I/O ConfigureUART(); // Initialize the UART. UARTprintf("Section 11.4 IoT example, Volume 2 Real-time interfacing\n"); UARTprintf("This node is configured to receive UDP packets\n"); UARTprintf("This node should be at IP: %d.%d.%d.%d Port: %d\n\n", SL_IPV4_BYTE(IP_ADDR,3), SL_IPV4_BYTE(IP_ADDR,2), SL_IPV4_BYTE(IP_ADDR,1), SL_IPV4_BYTE(IP_ADDR,0),PORT_NUM); ST7735_InitR(INITR_REDTAB); ST7735_OutString("Internet of Things\n"); ST7735_OutString("Embedded Systems\n"); ST7735_OutString("Vol. 2, Valvano"); ST7735_PlotClear(0,4095); // range from 0 to 4095 while(1){ sl_Start(0, 0, 0); /* Initializing the CC3100 device */ /* Connecting to WLAN AP - Set with static parameters defined at the top After this call we will be connected and have IP address */ WlanConnect(); // connect to AP /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len,(unsigned char *)&ipV4); UARTprintf("This node is at IP: %d.%d.%d.%d\n", SL_IPV4_BYTE(ipV4.ipV4,3), SL_IPV4_BYTE(ipV4.ipV4,2), SL_IPV4_BYTE(ipV4.ipV4,1), SL_IPV4_BYTE(ipV4.ipV4,0)); while(Status > 0){ UARTprintf("\nReceiving a UDP packet ..."); LocalAddr.sin_family = SL_AF_INET; LocalAddr.sin_port = sl_Htons((UINT16)PORT_NUM); LocalAddr.sin_addr.s_addr = 0; AddrSize = sizeof(SlSockAddrIn_t); SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); if( SockID < 0 ){ UARTprintf("SockIDerror\n"); Status = -1; // error }else{ Status = sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize); if( Status < 0 ){ sl_Close(SockID); UARTprintf("Sock Bind error\n"); }else{ Status = sl_RecvFrom(SockID, uBuf, BUF_SIZE, 0, (SlSockAddr_t *)&Addr, (SlSocklen_t*)&AddrSize ); if( Status <= 0 ){ sl_Close(SockID); UARTprintf("Receive error %d ",Status); }else{ LED_Toggle(); sl_Close(SockID); UARTprintf("ok %s ",uBuf); if((uBuf[0]==ATYPE)&&(uBuf[1]== '=')){ int i,bOk; uint32_t place; data = 0; bOk = 1; i=4; // ignore possible negative sign for(place = 1000; place; place = place/10){ if((uBuf[i]&0xF0)==0x30){ // ignore spaces data += place*(uBuf[i]-0x30); }else{ if((uBuf[i]&0xF0)!= ' '){ bOk = 0; } } i++; } if(bOk){ ST7735_PlotLine(data); ST7735_PlotNextErase(); } } } } } ROM_SysCtlDelay(ROM_SysCtlClockGet() / 25); // 120ms } } }
STATIC void wlan_get_sl_mac (void) { // Get the MAC address uint8_t macAddrLen = SL_MAC_ADDR_LEN; sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &macAddrLen, wlan_obj.mac); }
int main1(void){ UINT8 IsDHCP = 0; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr; UINT16 AddrSize = 0; INT16 SockID = 0; INT16 Status = 1; // ok UINT32 data; unsigned char len = sizeof(_NetCfgIpV4Args_t); stopWDT(); // Stop WDT initClk(); // PLL 50 MHz, ADC needs PPL active Board_Init(); // initialize LaunchPad I/O ConfigureUART(); // Initialize the UART. UARTprintf("Section 11.4 IoT example, Volume 2 Real-time interfacing\n"); #if ADC ADC0_InitSWTriggerSeq3(7); // Ain7 is on PD0 UARTprintf("This node is configured to measure signals from Ain7=PD0\n"); #endif #if EKG UARTprintf("This node is configured to generate simulated EKG data\n"); #endif UARTprintf(" and send UDP packets to IP: %d.%d.%d.%d Port: %d\n\n", SL_IPV4_BYTE(IP_ADDR,3), SL_IPV4_BYTE(IP_ADDR,2), SL_IPV4_BYTE(IP_ADDR,1), SL_IPV4_BYTE(IP_ADDR,0),PORT_NUM); while(1){ sl_Start(0, 0, 0);/* Initializing the CC3100 device */ /* Connecting to WLAN AP - Set with static parameters defined at the top After this call we will be connected and have IP address */ WlanConnect(); // connect to AP /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len,(unsigned char *)&ipV4); UARTprintf("This node is at IP: %d.%d.%d.%d\n", SL_IPV4_BYTE(ipV4.ipV4,3), SL_IPV4_BYTE(ipV4.ipV4,2), SL_IPV4_BYTE(ipV4.ipV4,1), SL_IPV4_BYTE(ipV4.ipV4,0)); while(Status > 0){ Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons((UINT16)PORT_NUM); Addr.sin_addr.s_addr = sl_Htonl((UINT32)IP_ADDR); AddrSize = sizeof(SlSockAddrIn_t); SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); if( SockID < 0 ){ UARTprintf("SockIDerror "); Status = -1; // error }else{ while(Status>0){ UARTprintf("\nSending a UDP packet ..."); uBuf[0] = ATYPE; // defines this as an analog data type uBuf[1] = '='; #if ADC data = ADC0_InSeq3(); // 0 to 4095, Ain7 is on PD0 #endif #if EKG data = EKGbuf[EKGindex]; EKGindex = (EKGindex+1)%EKGSIZE; // 100 Hz #endif Int2Str(data,(char*)&uBuf[2]); // [2] to [7] is 6 digit number UARTprintf(" %s ",uBuf); LED_Toggle(); Status = sl_SendTo(SockID, uBuf, BUF_SIZE, 0, (SlSockAddr_t *)&Addr, AddrSize); ROM_SysCtlDelay(ROM_SysCtlClockGet() / 25); // 80ms if( Status <= 0 ){ UARTprintf("SockIDerror %d ",Status); }else{ UARTprintf("ok"); } } sl_Close(SockID); } } } }
static int WifiDefaultSettings(void) { long retval = -1; unsigned char val = 1; unsigned char config, config_len; //filters _WlanRxFilterOperationCommandBuff_t filter_mask = { .Padding = {0} }; //initialize the SimpleLink API retval = sl_Start(0,0,0); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL start fail"); } //set device in station mode if(retval != ROLE_STA) { if(retval == ROLE_AP) { //we need to wait for an event before doing anything while(!IS_IP_ACQUIRED(wifi_state.status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //change mode to Station retval = sl_WlanSetMode(ROLE_STA); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "WLAN mode fail"); } //restart retval = sl_Stop(0xFF); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL stop fail"); } retval = sl_Start(0,0,0); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL start fail"); } if(retval != ROLE_STA) { RETURN_ERROR(ERROR_UNKNOWN, "WLAN mode fail"); } } //get SimpleLink version config = SL_DEVICE_GENERAL_VERSION; config_len = sizeof(SlVersionFull); retval = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &config, &config_len, (unsigned char*)&wifi_state.version); if(retval<0) { RETURN_ERROR(retval, "WIFI conf fail"); } //default connection policy retval = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 0), NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } //disconnect retval = sl_WlanDisconnect(); if(retval == 0) { //not yet disconnected while(IS_CONNECTED(wifi_state.status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //Enable DHCP client retval = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&val); if(retval<0) { RETURN_ERROR(retval, "WIFI conf fail"); } //Disable scan policy config = SL_SCAN_POLICY(0); retval = sl_WlanPolicySet(SL_POLICY_SCAN, config, NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } //Set Tx power level for station mode //Number between 0-15, as dB offset from max power - 0 will set max power val = 0; retval = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&val); if(retval<0) { RETURN_ERROR(retval, "WIFI set fail"); } // Set PM policy to normal retval = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } // Unregister mDNS services retval = sl_NetAppMDNSUnRegisterService(0, 0); if(retval<0) { RETURN_ERROR(retval, "mDNS fail"); } //Set mDNS device hostname char hostname[64]; char macstring[20]; unsigned char maclen = SL_MAC_ADDR_LEN; retval = sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &maclen, wifi_state.mac); if(retval < 0) { RETURN_ERROR(retval, "WIFI conf fail"); } snprintf(macstring, 20, "%02X%02X%02X%02X%02X%02X", wifi_state.mac[0], wifi_state.mac[1], wifi_state.mac[2], wifi_state.mac[3], wifi_state.mac[4], wifi_state.mac[5]); snprintf(hostname, 64, "LeashDebugger%s", macstring); retval = sl_NetAppSet (SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, strlen((const char *)hostname), (unsigned char *) hostname); // Remove all 64 filters (8*8) memset(filter_mask.FilterIdMask, 0xFF, 8); retval = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (uint8_t*)&filter_mask, sizeof(_WlanRxFilterOperationCommandBuff_t)); if(retval<0) { RETURN_ERROR(retval, "WIFI filter fail"); } retval = sl_Stop(SL_STOP_TIMEOUT); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL stop fail"); } wifi_state.status = 0; return RET_SUCCESS; }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return None // //**************************************************************************** void ConnectToNetwork() { char ucAPSSID[32]; unsigned short len, config_opt; // staring simplelink g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); unsigned char macAddressVal[SL_MAC_ADDR_LEN]; unsigned char macAddressLen = SL_MAC_ADDR_LEN; sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(unsigned char *)macAddressVal); Report("CC3200 LaunchPad MAC Address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n\r", macAddressVal[0],macAddressVal[1],macAddressVal[2],macAddressVal[3],macAddressVal[4],macAddressVal[5]); // Device is in AP Mode and Force AP Jumper is not Connected if(((g_uiSimplelinkRole == ROLE_AP) || (g_uiSimplelinkRole == ROLE_P2P)) && g_uiDeviceModeConfig == ROLE_STA ) { //Switch to STA Mode sl_WlanSetMode(ROLE_STA); sl_Stop(SL_STOP_TIMEOUT); g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //Device is in STA Mode and Force AP Jumper is Connected if(((g_uiSimplelinkRole == ROLE_STA) || (g_uiSimplelinkRole == ROLE_P2P)) && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode sl_WlanSetMode(ROLE_AP); sl_Stop(SL_STOP_TIMEOUT); g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //No Mode Change Required if(g_uiSimplelinkRole == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server while (!(g_usMCNetworkUstate & MCU_IP_ALLOC)) { } char iCount=0; //Read the AP SSID memset(ucAPSSID,'\0',AP_SSID_LEN_MAX); len = AP_SSID_LEN_MAX; config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char*) ucAPSSID); //Blink LED 3 times to Indicate AP Mode for(iCount=0;iCount<3;iCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } } else { //waiting for the device to Auto Connect while (((!(g_usMCNetworkUstate & MCU_AP_ASSOC)) || !(g_usMCNetworkUstate & MCU_IP_ALLOC))&& g_ucConnectTimeout < AUTO_CONNECTION_TIMEOUT_COUNT) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); g_ucConnectTimeout++; } //Couldn't connect Using Auto Profile if(g_ucConnectTimeout == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); g_ucConnectTimeout &= ~MCU_AP_ASSOC; g_ucConnectTimeout &= ~MCU_IP_ALLOC; //Connect Using Smart Config SmartConfigConnect(); //Waiting for the device to Auto Connect while (!(g_usMCNetworkUstate & MCU_AP_ASSOC) || !(g_usMCNetworkUstate & MCU_IP_ALLOC)) { MAP_UtilsDelay(500); } } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); g_iInternetAccess = ConnectionTest(); if (g_iInternetAccess == 0) { Report("Successful connection to the Internet\r\n"); osi_SyncObjSignal(&semaphore_Connected); //signal "Connected" semaphore so mqtt task can continue } else { Report("Could not obtain connection to the Internet\r\n"); } } }