加快政务公开网站建设,大连网站设计九必选仟亿科技,互联网企业分类,如何制作微视频宣传片声明
本文实现了ESP8266、微信小程序、个人服务器三者互相通信,并且小程序能发消息给微信用户
本文所有代码和步骤均为亲测有效
以下代码均为从网上搜索到后本人加以改动的,并非完全原创,若作者希望删除可联系我
ESP8266与个人服务器通信
ESP8266配置
通过串口通信使用…声明
本文实现了ESP8266、微信小程序、个人服务器三者互相通信,并且小程序能发消息给微信用户
本文所有代码和步骤均为亲测有效
以下代码均为从网上搜索到后本人加以改动的,并非完全原创,若作者希望删除可联系我
ESP8266与个人服务器通信
ESP8266配置
通过串口通信使用AT指令控制ESP8266,AT指令详细讲解网上很多,自行搜索
复位:
AT+RST\r\n
连接wifi:
AT+CWJAP="601_2.4G","tddsqdhgq15"\r\n
检查ip:
AT+CIFSR\r\n
链接个人服务器,x.x.x.x为个人服务器的公网IP地址,y为个人服务器开放的通信端口:
AT+CIPSTART="TCP","x.x.x.x",y\r\n
ESP8266向个人服务器发送15个字符:
AT+CIPSEND=15\r\n
个人服务器配置
建立tcp_server.cpp文件。
#include sys/types.h
#include sys/socket.h
#include string.h
#include stdio.h
#include netinet/in.h
#include arpa/inet.h
#include stdlib.h
#include unistd.h
#include errno.h
#include linux/tcp.h#include sys/types.h
#include sys/socket.h
#include stdio.h
#include netinet/in.h
#include arpa/inet.h
#include unistd.h
#include string.h
#include stdlib.h
#include fcntl.h
#include sys/shm.h#include pthread.h#define PORT 8888
#define QUEUE_SIZE 10
#define BUFFER_SIZE 1024#define CHECK_ERROR(sts, status) {if (sts) {printf("CHECK_ERROR:%d %d %d\n%s\n", __FILE__, __FUNCTION__, __LINE__);return status;}}int total_device_num = 0;struct DEVICE_T
{int sockfd;int id;pthread_t tid;char reserved[16];
};DEVICE_T device_t[1000];//传进来的sockfd,就是互相建立好连接之后的socket文件描述符
//通过这个sockfd,可以完成 [服务器]---[8266] 互相收发数据void *strPrintf(void *args)
{int sockfd = *((int *)args);char buffer[BUFFER_SIZE];char device_id_string[10];memset(device_id_string, 0, 10);pid_t pid = pthread_self();unsigned int i = 10, j = 0;while(1){memset(buffer,0,sizeof(buffer));//printf("listening sockfd %d\n", sockfd);int len = recv(sockfd, buffer, sizeof(buffer),0);if(strcmp(buffer,"exit\n")==0){printf("child process: %d exited.\n",pid);break;}printf("sockfd:%d receive:%s\n",sockfd,buffer);if ( buffer[0] == 'd' ){for(i = 10; buffer[i] != '_'; i ++)//device_id_123_{if (i 19){printf("error:i 24\n");exit(0);}device_id_string[i - 10] = buffer[i];}printf("sockfd:%d id %d\n", sockfd,atoi(device_id_string));for (i = 0; i total_device_num; i ++){if (sockfd == device_t[i].sockfd){break;}}device_t[i].id = atoi(device_id_string);printf("\n\n\n\n↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 发生设备绑定 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\n");printf("此设备:编号 %d id %d sockfd %d\n", i, device_t[i].id, device_t[i].sockfd);printf("已经连接的设备总数: %d\n", total_device_num);printf("所有设备信息如下:\n");for (j = 0; j total_device_num; j ++){printf("device[%d] id %d sockfd %d tid %d\n", j, device_t[j].id, device_t[j].sockfd, device_t[j].tid);}printf("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n\n\n\n");}// fputs(buffer, stdout);struct tcp_info info;int len_tcp_info = sizeof(info);getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, info, (socklen_t*)len_tcp_info);int sts_getsockopt = getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, info, (socklen_t*)len_tcp_info);printf("设备:编号 %d id %d socket %d tcp状态 %d getsockopt返回值 %d\n", i, device_t[i].id, device_t[i].sockfd, info.tcpi_state, sts_getsockopt);if((info.tcpi_state != 1)|| (sts_getsockopt != 0)) //则说明未断开 else 断开{printf("已经断开链接!!\n");if (total_device_num == 1){device_t[0].id = 0;device_t[0].sockfd = 0;device_t[0].tid = 0;}else{device_t[i].id = device_t[total_device_num - 1].id;device_t[i].sockfd = device_t[total_device_num - 1].sockfd;device_t[i].tid = device_t[total_device_num - 1].tid;device_t[total_device_num - 1].id = 0;device_t[total_device_num - 1].sockfd = 0;device_t[total_device_num - 1].tid = 0;}total_device_num --;close(sockfd);pthread_exit((void **)"线程关闭\n");}else{printf("发送消息:%s\n", buffer);send(sockfd, buffer, len, 0); //将接收到数据发送回去}}close(sockfd);
}int main(int argc, char **argv)
{int err,i ;pthread_t ntid;//建立本cpp与php之间的通信,此线程一直保存err = pthread_create(ntid, NULL, PHP_and_TcpServer, NULL);if (err != 0)printf("can't create thread: %d\n", err);//定义IPV4的TCP连接的套接字描述符int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);//定义sockaddr_in 文章转载自: http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.wrysm.cn.gov.cn.wrysm.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.zwpzy.cn.gov.cn.zwpzy.cn http://www.morning.qpqcq.cn.gov.cn.qpqcq.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.rhpgk.cn.gov.cn.rhpgk.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.bpmns.cn.gov.cn.bpmns.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.zdbfl.cn.gov.cn.zdbfl.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.rykn.cn.gov.cn.rykn.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn