完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
HTTP请求: #include #include #include #include // Http请求内容 static const char send_data[] = "GET /v1.0/device/13855/sensor/22988/datapoints HTTP/1.1rn" "U-ApiKey:[your apikey]rn" "Host: api.yeelink.netrnrn"; void tcpclient(const char* host_name, int port) { (void)port; char *recv_data; int sock, bytes_received; struct hostent *yeelink_host; struct in_addr yeelink_ipaddr; struct sockaddr_in yeelink_sockaddr; recv_data = rt_malloc(1024); if (recv_data == RT_NULL) { rt_kprintf("No memoryrn"); return; } // 第一步 DNS地址解析 rt_kprintf("calling gethostbyname with: %srn", host_name); yeelink_host = gethostbyname(host_name); yeelink_ipaddr.s_addr = *(unsigned long *) yeelink_host->h_addr_list[0]; rt_kprintf("Yeelink IP Address:%srn" , inet_ntoa(yeelink_ipaddr)); yeelink_sockaddr.sin_family = AF_INET; yeelink_sockaddr.sin_port = htons(80); yeelink_sockaddr.sin_addr = yeelink_ipaddr; rt_memset(&(yeelink_sockaddr.sin_zero), 0, sizeof(yeelink_sockaddr.sin_zero)); while(1) { // 第二步 创建套接字 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { rt_kprintf("Socket errorn"); rt_free(recv_data); return; } // 第三步 连接yeelink if (connect(sock, (struct sockaddr *)&yeelink_sockaddr, sizeof(struct sockaddr)) == -1) { rt_kprintf("Connect fail!n"); lwip_close(sock); rt_free(recv_data); return; } // 第4步 发送Http请求 send(sock,send_data,strlen(send_data), 0); // 第5步 获得Http响应 bytes_received = recv(sock, recv_data, 1024 - 1, 0); recv_data[bytes_received] = ' |