Part Number:CC3100BOOSTOther Parts Discussed in Thread:CC3100
想用CC3100连接到openweathermap.org上获取实时天气,但输出结果显示该http链接已移除(301 Moved Permanently),需要采用https来获取天气信息。
请问如何修改代码才能改为https连接呢?
代码如下:
#define REQUEST "GET /data/2.5/weather?q=London,uk&APPID=1234567890abcdef1234567890abcdef&units=metric HTTP/1.1\r\nUser-Agent: Keil\r\nHost:api.openweathermap.org\r\nAccept: */*\r\n\r\n" int main(void){int32_t retVal; SlSecParams_t secParams;char *pConfig = NULL; INT32 ASize = 0; SlSockAddrIn_t Addr;initClk();// PLL 50 MHzUART_Init();// Send data to PC, 115200 bpsLED_Init();// initialize LaunchPad I/OUARTprintf("Weather App\n");retVal = configureSimpleLinkToDefaultState(pConfig); // set policiesif(retVal < 0)Crash(4000000);retVal = sl_Start(0, pConfig, 0);if((retVal < 0) || (ROLE_STA != retVal) ) Crash(8000000);secParams.Key = PASSKEY;secParams.KeyLen = strlen(PASSKEY);secParams.Type = SEC_TYPE; // OPEN, WPA, or WEPsl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);while((0 == (g_Status&CONNECTED)) || (0 == (g_Status&IP_AQUIRED))){_SlNonOsMainLoopTask();}UARTprintf("Connected\n");while(1){strcpy(HostName,"openweathermap.org");retVal = sl_NetAppDnsGetHostByName(HostName,strlen(HostName),&DestinationIP, SL_AF_INET);if(retVal == 0){Addr.sin_family = SL_AF_INET;Addr.sin_port = sl_Htons(80);Addr.sin_addr.s_addr = sl_Htonl(DestinationIP);// IP to big endianASize = sizeof(SlSockAddrIn_t);SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);if( SockID >= 0 ){retVal = sl_Connect(SockID, ( SlSockAddr_t *)&Addr, ASize);}if((SockID >= 0)&&(retVal >= 0)){strcpy(SendBuff,REQUEST);sl_Send(SockID, SendBuff, strlen(SendBuff), 0);// Send the HTTP GETsl_Recv(SockID, Recvbuff, MAX_RECV_BUFF_SIZE, 0);// Receive responsesl_Close(SockID);LED_GreenOn();UARTprintf("\r\n\r\n");UARTprintf(Recvbuff); UARTprintf("\r\n");}}while(Board_Input()==0){}; // wait for touchLED_GreenOff();} }
输出如下:
Weather App Connected HTTP/1.1 301 Moved Permanently Server: openresty/1.9.7.1 Date: Fri, 18 Mar 2022 22:15:03 GMT Content-Type: text/html Content-Length: 190 Connection: keep-alive Location: https://agromonitoring.com/data/2.5/weather?q=AustinERRORexas&APPID=1234567890abcdef1234567890abcdef&units=metric <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>openresty/1.9.7.1</center> </body> </html>
Kevin Qiu1:
你需要自己注册账户获取APPID替换1234567890abcdef1234567890abcdef才能访问天气服务器
参考:e2e.ti.com/…/1883261
,
Yihao Guo:
已经更换过了,是一样的结果。
,
Yihao Guo:
你好,我看了你提供的参考,除了替换APPID,还要把HostName
strcpy(HostName,"openweathermap.org");改为
strcpy(HostName,"api.openweathermap.org");问题已解决,十分感谢!
,
Kevin Qiu1:
好的,问题解决就好