文章目录
声明知识补充数据上报--应用进程通过CGI与html交互实现步骤源码分析 -pthread_refresh.c处理上报的线程CGI完成对HTML的同步更新效果图声明
华清远见教育集团 专注高端IT培训 做良心教育,做专业教育,做受人尊敬的职业教育
创客学院官网:/
华清远见创客学院嵌入式课程链接:/emb
华清远见创客学院物联网课程链接:/iot
知识补充
HTTP什么是 HTTP ?
超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信。
HTTP 的工作方式是客户端与服务器之间的请求-应答协议。
web 浏览器可能是客户端,而计算机上的网络应用程序也可能作为服务器端。
举例:客户端(浏览器)向服务器提交 HTTP 请求;服务器向客户端返回响应。响应包含关于请求的状态信息以及可能被请求的内容。
两种 HTTP 请求方法:GET 和 POST
在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。
GET - 从指定的资源请求数据。
POST - 向指定的资源提交要被处理的数据。
键值对 key value 对应
键值对于键值对之间用&分割
GET 方法
请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:
/test/demo_form.php?name1=value1&name2=value2
有关 GET 请求的其他一些注释:
GET 请求可被缓存
GET 请求保留在浏览器历史记录中
GET 请求可被收藏为书签
GET 请求不应在处理敏感数据时使用
GET 请求有长度限制
GET 请求只应当用于取回数据
POST 方法
请注意,查询字符串(名称/值对)是在 POST 请求的 HTTP 消息主体中发送的:
POST /test/demo_form.php HTTP/1.1
Host:
name1=value1&name2=value2
有关 POST 请求的其他一些注释:
POST 请求不会被缓存
POST 请求不会保留在浏览器历史记录中
POST 不能被收藏为书签
POST 请求对数据长度没有要求
比较 GET 与 POST
共享内存
/Set_Mode/article/details/90111261
信号量
https://zhuzhongwei./article/details/90114792
线程
/Set_Mode/article/details/90175878
数据上报–应用进程通过CGI与html交互
在本文介绍的仓储项目中,用户通过客户端的一些控件实现对向底层硬件发送指令,这一过程是命令数据的下发;用户还可以实时查看到仓库的环境数据,这是数据的上报。总体上分为三个的对象和两条数据流向,网页端-服务器-底层实现命令数据的下发和环境数据的上报,也就实现了人机交互的过程。下面是数据上报部分框图:
目的:完成html 和 CGI 和 用户进程 的交互,实现数据上报到html端。随便从程序上加以分析这一过程。
实现步骤
实现html文件,放在boa服务器的www目录下实现.cgi程序,放在boa服务器的www目录下编写应用程序上报线程,实现与cgi进行交互在开发板上运行boa服务器,并运行应用程序测试结果源码分析 -pthread_refresh.c
处理上报的线程
通过共享内存实现与CGI那边数据的共享,为了避免使用冲突(同时对共享内存读写),有引入信号量实现互斥机制。这里还没有对底层获取数据的处理,就先使用写死的数值,模拟进行上报操作。
这里实现每隔1s对共享内存写一次数据,即上报一次。
#include "data_global.h"#include "sem.h"#define N 1024 //for share memoryextern int shmid;extern int msgid;extern int semid;extern key_t shm_key;extern key_t sem_key;extern key_t key; //msg_keyextern pthread_mutex_t mutex_client_request;extern pthread_mutex_t mutex_refresh;extern pthread_mutex_t mutex_sqlite;extern pthread_mutex_tmutex_transfer;extern pthread_mutex_tmutex_sms;extern pthread_mutex_tmutex_buzzer;extern pthread_mutex_tmutex_led;extern pthread_mutex_tmutex_camera;extern pthread_cond_t cond_client_request;extern pthread_cond_t cond_refresh;extern pthread_cond_t cond_sqlite;extern pthread_cond_tcond_transfer;extern pthread_cond_tcond_sms;extern pthread_cond_tcond_buzzer;extern pthread_cond_tcond_led;extern pthread_cond_tcond_camera;extern struct env_info_client_addr sm_all_env_info;struct shm_addr{char shm_status; //shm_status可以等于home_id,用来区分共享内存数据struct env_info_client_addr sm_all_env_info;};struct shm_addr *shm_buf;int file_env_info_struct(struct env_info_client_addr *rt_status, int home_id);/* 内存数据刷新线程 - 更新共享内存里的实时数据*/void *pthread_refresh(void *arg){//semaphore for access to resource limitsif ((sem_key = ftok("/tmp", 'i')) < 0){perror("ftok failed .\n");exit(-1);}semid = semget(sem_key, 1, IPC_CREAT | IPC_EXCL | 0666);if (semid == -1){if (errno == EEXIST){semid = semget(sem_key, 1, 0777);} else {perror("fail to semget");exit(1);}} else {init_sem (semid, 0, 1);}//share memory for env_info refresh config//这里确保与CGI程序使用的是同个 keyif ((shm_key = ftok("/tmp", 'i')) < 0){perror("ftok failed .\n");exit(-1);}shmid = shmget(shm_key, N, IPC_CREAT | IPC_EXCL | 0666);if (shmid == -1){if (errno == EEXIST){shmid = shmget(key, N, 0777);} else {perror("fail to shmget");exit(1);}}//share memapif ((shm_buf = (struct shm_addr *)shmat(shmid, NULL, 0)) == (void *) - 1){perror("fail to shmat");exit(1);}printf("pthread_refresh ......>>>>>>>\n");#if 1bzero (shm_buf, sizeof (struct shm_addr));//实现每隔1s写一次数据,确保与其他读写操作是互斥的while (1) {sem_p(semid, 0);shm_buf->shm_status = 1;file_env_info_struct(&shm_buf->sm_all_env_info, shm_buf->shm_status);sleep(1);sem_v(semid, 0);}#endif}//把数据'写死',就不用底层实现了int file_env_info_struct(struct env_info_client_addr *rt_status, int home_id){int env_info_size = sizeof(struct env_info_client_addr);//printf("env_info_size = %d.\n", env_info_size);rt_status->monitor_no[home_id].zigbee_info.temperature = 10.0;rt_status->monitor_no[home_id].zigbee_info.tempMIN = 2.0;rt_status->monitor_no[home_id].zigbee_info.tempMAX = 20.0;rt_status->monitor_no[home_id].zigbee_info.humidity = 20.0;rt_status->monitor_no[home_id].zigbee_info.humidityMIN = 10.0;rt_status->monitor_no[home_id].zigbee_info.humidityMAX = 30.0;rt_status->monitor_no[home_id].zigbee_info.reserved[0] = 0.01;rt_status->monitor_no[home_id].zigbee_info.reserved[1] = -0.01;rt_status->monitor_no[home_id].a9_info.adc = 9.0;rt_status->monitor_no[home_id].a9_info.gyrox = -14.0;rt_status->monitor_no[home_id].a9_info.gyroy = 20.0;rt_status->monitor_no[home_id].a9_info.gyroz = 40.0;rt_status->monitor_no[home_id].a9_info.aacx = 642.0;rt_status->monitor_no[home_id].a9_info.aacy = -34.0;rt_status->monitor_no[home_id].a9_info.aacz = 5002.0;rt_status->monitor_no[home_id].a9_info.reserved[0] = 0.01;rt_status->monitor_no[home_id].a9_info.reserved[1] = -0.01;return 0;}
CGI完成对HTML的同步更新
env1.cgi处理程序先通过ftok操作,获取到与应用层的线程相同的的共享内存,然后在信号量的控制下进行读操作。读到的数据写入html文件中,用来刷新用户页面看到的数据。
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <string.h>#include <errno.h>#include <sys/sem.h>#include <unistd.h>#include "sem.h"#include "cgic.h"#include "data_global.h"#define N 32#define MONITOR_NUM 1char status[2][6] = {"Close", "Open"};char fan_status[4][6] = {"Close", "One", "Two", "Three"};struct shm_addr{char shm_status;struct env_info_client_addr sm_all_env_info;};int cgiMain(){key_t key;int shmid,semid;struct shm_addr *shm_buf;if((key = ftok("/tmp",'i')) <0){perror("ftok");exit(1);}printf("key = %x\n",key);if((semid = semget(key, 1, 0666)) < 0){perror("semget");exit(1);}if((shmid = shmget(key, N, 0666 )) == -1){perror("shmget");exit(1);}if((shm_buf = (struct shm_addr*)shmat(shmid, NULL, 0)) == (void*)-1 ){perror("shmat");exit(1);}sem_p(semid,0);cgiHeaderContentType("text/html");fprintf(cgiOut, "<head><meta http-equiv=\"refresh\" content=\"1\"><style><!--body{line-height:50%}--></style> </head>");fprintf(cgiOut, "<HTML>\n");fprintf(cgiOut, "<BODY bgcolor=\"#666666\">\n");//fprintf(cgiOut, "<h1><font color=\"#FF0000\">HOME_ID #%d:</font></H2>\n ", shm_buf->shm_status);if (shm_buf->shm_status == 1){fprintf(cgiOut, "<script>function show(){var date =new Date(); var now = \"\"; now = date.getFullYear()+\"年\"; now = now + (date.getMonth()+1)+\"月\"; \ now = now + date.getDate()+\"日\"; now = now + date.getHours()+\"时\"; now = now + date.getMinutes()+\"分\";now = now + date.getSeconds()+\"秒\"; document.getElementById(\"nowDiv\").innerHTML = now; setTimeout(\"show()\",1000);} </script> \n ");fprintf(cgiOut, "<h2><font face=\"Broadway\"><font color=\"#00FAF0\">Home1 Real-time Environment Info:</font></font></H2>\n ");fprintf(cgiOut, "<h2 align=center><font color=\"#cc0033\"><body onload=\"show()\"> <div id=\"nowDiv\"></div></font></h2> \n "); fprintf(cgiOut, "<h4>ZIGBEE数据显示部分</H4>\n ");fprintf(cgiOut, "<h4>Temperature:\t%0.2f</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].zigbee_info.temperature );fprintf(cgiOut, "<h4>Humidity:\t%0.2f</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].zigbee_info.humidity);fprintf(cgiOut, "<h4>A9数据显示部分</H4>\n ");fprintf(cgiOut, "<h4>Adc:\t%0.2f</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.adc);fprintf(cgiOut, "<h4>GYROX:\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.gyrox);fprintf(cgiOut, "<h4>GYROY:\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.gyroy);fprintf(cgiOut, "<h4>GYROZ:\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.gyroz);fprintf(cgiOut, "<h4>AACX :\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.aacx);fprintf(cgiOut, "<h4>AACY :\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.aacy);fprintf(cgiOut, "<h4>AACZ :\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.aacz);fprintf(cgiOut, "<h4>A9-RESERVED[0]:\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.reserved[0]);fprintf(cgiOut, "<h4>A9-RESERVED[1]:\t%d</H4>\n ", shm_buf->sm_all_env_info.monitor_no[shm_buf->shm_status].a9_info.reserved[1]);fprintf(cgiOut, "<h4>STM32数据显示部分</H4>\n ");fprintf(cgiOut, "<h4>......</H4>\n ");}else{fprintf(cgiOut, "<h2><font face=\"Broadway\"><font color=\"#FFFAF0\">Close!</font></font></H2>\n ");}//fprintf(cgiOut, "<h3>:</H3>\n ");fprintf(cgiOut, "</BODY></HTML>\n");sem_v (semid, 0);return 0;}