基于ARM的智能灯光控制系统设备管理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于ARM的智能灯光控制系统设备管理相关的知识,希望对你有一定的参考价值。
基于ARM的智能灯光控制系统(9)设备管理
嵌入式开发培训(阶段2)底层系统开发
智能灯光控制系统
设备管理页面程序(dev_con.c)
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "html.h"
#include "config.h"
#include "ipc.h"
void light_tr(char* name,int join,int sw,char type)
{
char type_id[2]="0";
type_id[0] = type + ‘0‘;
printf("<tr><td>%s</td>",name);
if(join==1)
printf("<td>在线</td>");
else
printf("<td>下线</td>");
if(sw==1){
printf("<td class=\"center\"><input type=\"radio\" checked=\"checked\" value=\"1\" name=\"sw_sta%s\"> 开启<input type=\"radio\" value=\"0\" name=\"sw_sta%s\">关闭</td>",type_id,type_id);
}else{
printf("<td class=\"center\"><input type=\"radio\" value=\"1\" name=\"sw_sta%s\"> 开启<input type=\"radio\" value=\"0\" name=\"sw_sta%s\" checked=\"checked\">关闭</td>",type_id,type_id);
}
printf("<td class=\"center\"><button type=\"submit\" >设置</button></td></tr>");
}
void sensor_tr(char* name,int join,int bind,char type,struct sys_all* shm_dev)
{
int i;
char selected[32] = "";
char type_id[2]="0";
type_id[0] = type + ‘0‘;
printf("<tr><td>%s</td>",name);
if(join==1)
printf("<td>在线</td>");
else
printf("<td>下线</td>");
if(bind == 0)
strcpy(selected,"selected=selected");
printf("<td><select name=\"bind%s\"><option value=\"0\" %s >解除绑定</option>",type_id,selected);
for(i=0;i<shm_dev->count_dev;i++){
strcpy(selected,"");
if(bind == shm_dev->sys_dev[i].node.type)
strcpy(selected,"selected=selected");
if(shm_dev->sys_dev[i].node.type==DEV_T_LIGHT1 || shm_dev->sys_dev[i].node.type==DEV_T_LIGHT2 || shm_dev->sys_dev[i].node.type==DEV_T_LIG_NET)
printf("<option value=\"%d\" %s >%s</option>",shm_dev->sys_dev[i].node.type,selected,shm_dev->sys_dev[i].name);
}
printf("</select></td>");
printf("<td class=\"center\"><button type=\"submit\" >设置</button></td></tr>");
}
int main(int argc, char *argv[])
{
int ret = 0;
int i,msgid;
struct sys_all* shm_dev;
char item_name[5][10];
if((msgid=get_msgid()) < 0){
ret = ERR_MSG;
}
if(msg_send(msgid,CMD_GET)==0){
if((shm_dev=(struct sys_all*)set_web_shm())==NULL){
ret = ERR_SHM;
}
}
html_head();
html_title();
html_nav();
html_table_title("设备管理" , "设备设置" , "设备管理" );
if(ret != 0){
html_return_show(ret);
html_end();
return 0;
}
printf("<form method=\"get\" action=\"/cgi-bin/dev_con_post.cgi\">");
strcpy(item_name[0],"设备名称");
strcpy(item_name[1],"在线状态");
strcpy(item_name[2],"设备开关");
strcpy(item_name[3],"提交操作");
html_table_head(4,item_name,"灯光设备");
for(i=0;i<shm_dev->count_dev;i++){
if(shm_dev->sys_dev[i].node.type==DEV_T_LIGHT1 || shm_dev->sys_dev[i].node.type==DEV_T_LIGHT2 || shm_dev->sys_dev[i].node.type==DEV_T_LIG_NET)
light_tr(shm_dev->sys_dev[i].name,shm_dev->sys_dev[i].join_sta,
shm_dev->sys_dev[i].sw_sta,shm_dev->sys_dev[i].node.type);
}
html_table_end();
strcpy(item_name[0],"设备名称");
strcpy(item_name[1],"在线状态");
strcpy(item_name[2],"关联设备");
strcpy(item_name[3],"提交操作");
html_table_head(4,item_name,"感应设备");
for(i=0;i<shm_dev->count_dev;i++){
if(shm_dev->sys_dev[i].node.type==DEV_T_RAY)
sensor_tr(shm_dev->sys_dev[i].name,shm_dev->sys_dev[i].join_sta,
shm_dev->sys_dev[i].bind_dev,shm_dev->sys_dev[i].node.type,shm_dev);
}
html_table_end();
printf("</form>");
html_end();
return 0;
}
设备管理页面处理程序(dev_con_post.c)
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "getvalue.h"
#include "html.h"
#include "config.h"
#include "ipc.h"
int main(int argc, char *argv[])
{
int ret = 0;
char *val = NULL;
char val_name[16];
char type_id[2]="0";
int i,msgid;
struct sys_all* shm_dev;
set_env(getenv("REQUEST_METHOD"),
getenv("CONTENT_LENGTH"),
getenv("QUERY_STRING"));
html_head();
html_refresh("3","/cgi-bin/dev_con.cgi");
html_title();
html_nav();
html_table_title("设备管理" , "设备设置" , "设备管理" );
if((shm_dev=(struct sys_all*)set_web_shm())==NULL){
ret = ERR_SHM;
}else{
for(i=0;i<shm_dev->count_dev;i++){
//根据配置文件实时数据,动态读取变量名join_sta+id
type_id[0] = shm_dev->sys_dev[i].node.type + ‘0‘;
strcpy(val_name,"sw_sta");
strcat(val_name,type_id);
val = get_value(val_name);
if(val != NULL)
shm_dev->sys_dev[i].sw_sta = val[0]-‘0‘;
strcpy(val_name,"bind");
strcat(val_name,type_id);
val = get_value(val_name);
if(val != NULL)
shm_dev->sys_dev[i].bind_dev = val[0]-‘0‘;
}
}
if(ret == 0){
if((msgid=get_msgid()) < 0){
ret = ERR_MSG;
}
if(msg_send(msgid,CMD_SET) < 0)
ret = ERR_MSG;
}
html_return_show(ret);
html_end();
return 0;
}
CGI处理头文件 (getvalue.h)
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define FIELD_LEN 60 /*how long each name or value can be*/
#define NV_PAIRS 15 /*how many name=value pairs can process*/
typedef struct name_value_st{
char name[FIELD_LEN + 1];
char value[FIELD_LEN + 1];
}name_value;
name_value name_val_pairs[NV_PAIRS];
int num_pairs = 0;/*pairs number*/
const char *M = NULL;
const char *L = NULL;
const char *S = NULL;
static int iread = 0;
void unescape_url(char *url);
void set_env(const char *r_mth, const char *c_len,const char *q_str);
char* get_value(const char *name);
int get_input(void);
void send_error(char *error_test);
char x2c(char *what);
void load_nv_pair(char *tmp_buffer, int nv_entry_number_to_load);
void set_env(const char *r_mth, const char *c_len,const char *q_str)
{
M = r_mth;
L = c_len;
S = q_str;
}
char* get_value(const char *name)
{
int nv_entry_number = 0;
int i = 0;
char* val = NULL;
char *tname = NULL;
if(iread == 0)
{
if ( !get_input()){
return "error";
exit(EXIT_FAILURE);
}
}
for(i = 0; i < num_pairs; i++ )
{
val = name_val_pairs[nv_entry_number].value;
tname = name_val_pairs[nv_entry_number].name;
nv_entry_number++;
if( strcmp(tname,name) == 0 )
{ break;}
else
{ val = NULL;
tname = NULL;
}
}
// printf("\r\n");
iread++;//read value times
return val;
exit(EXIT_SUCCESS);
}
int get_input(void)
{
int nv_entry_number = 0;
int got_data = 0;
char *ip_data = NULL;
int ip_length = 0;
char tmp_buffer[(FIELD_LEN * 2) + 2];
int tmp_offset = 0;
char *tmp_char_ptr = NULL;
int chars_processed = 0;
/*如果是POST方法提交,数据导入ip_data*/
// tmp_char_ptr = getenv("REQUEST_METHOD");
tmp_char_ptr = (char*)M;
if ( tmp_char_ptr)
{
if(strcmp(tmp_char_ptr, "POST") == 0)
{
// tmp_char_ptr = getenv("CONTENT_LENGTH");
tmp_char_ptr = (char*)L;
if (tmp_char_ptr){
ip_length = atoi(tmp_char_ptr);
ip_data = malloc(ip_length + 1);
if (fread(ip_data, 1, ip_length, stdin) != ip_length)
{
send_error("Bad read from stdin");
return(0);
}
ip_data[ip_length] = ‘\0‘;
got_data = 1;
}
}
}
/*如果是GET方法提交,数据导入ip_data*/
// tmp_char_ptr = getenv("REQUEST_METHOD");
tmp_char_ptr = (char*)M;
if ( tmp_char_ptr)
{
if(strcmp(tmp_char_ptr, "GET") == 0)
{
// tmp_char_ptr = getenv("QUERY_STRING");
tmp_char_ptr = (char*)S;
if (tmp_char_ptr)
{
// ip_length = atoi(tmp_char_ptr);
ip_length = strlen(tmp_char_ptr);
ip_data = malloc(ip_length + 1);
// strcpy(ip_data, getenv("QUERY_STRING"));
strcpy(ip_data, (char*)S);
ip_data[ip_length] = ‘\0‘;
got_data = 1;
}
}
}
if (!got_data){
send_error("No data received");
}
if (ip_length <= 0){
send_error("Input length <= 0");
return(0);
}
memset(name_val_pairs, ‘\0‘, sizeof(name_val_pairs));
tmp_char_ptr = ip_data;
while (chars_processed <= ip_length && nv_entry_number < NV_PAIRS)
{
tmp_offset = 0;
while (*tmp_char_ptr && *tmp_char_ptr != ‘&‘ && tmp_offset < FIELD_LEN)
{
tmp_buffer[tmp_offset] = *tmp_char_ptr;
tmp_offset++;
tmp_char_ptr++;
chars_processed++;
}
tmp_buffer[tmp_offset] = ‘\0‘;
/*decode and load the pair*/
load_nv_pair(tmp_buffer, nv_entry_number);
/*move on to the next name=value pair*/
tmp_char_ptr++;
nv_entry_number++;
}
free(ip_data);
ip_data = NULL;
return(1);
}
void send_error(char *error_text)
{
printf("Content-Type: text/html\r\n");
printf("\r\n");
printf("Woops:- %s\r\n",error_text);
}
void load_nv_pair(char *tmp_buffer, int nv_entry)
{
int chars_processed = 0;
char *src_char_ptr = NULL;
char *dest_char_ptr = NULL;
/*get the part before the ‘=‘ sign*/
src_char_ptr = tmp_buffer;
dest_char_ptr = name_val_pairs[nv_entry].name;
while (*src_char_ptr && *src_char_ptr != ‘=‘ && chars_processed < FIELD_LEN)
{
/*chang a ‘+‘ to a ‘ ‘ */
if (*src_char_ptr == ‘+‘)
{
*dest_char_ptr = ‘ ‘;
}else{
*dest_char_ptr = *src_char_ptr;
}
dest_char_ptr++;
src_char_ptr++;
chars_processed++;
}
/*skip the ‘=‘ character*/
if (*src_char_ptr == ‘=‘)
{
num_pairs++;
/*get the part after the ‘=‘ sign*/
src_char_ptr++;
dest_char_ptr = name_val_pairs[nv_entry].value;
chars_processed = 0;
while (*src_char_ptr && *src_char_ptr != ‘=‘ && chars_processed < FIELD_LEN)
{
/*chang a ‘+‘ to a ‘ ‘ */
if (*src_char_ptr == ‘+‘)
{
*dest_char_ptr = ‘ ‘;
}else{
*dest_char_ptr = *src_char_ptr;
}
dest_char_ptr++;
src_char_ptr++;
chars_processed++;
}
}
unescape_url(name_val_pairs[nv_entry].name);
unescape_url(name_val_pairs[nv_entry].value);
}
void unescape_url(char *url)
{
int x,y;
for (x=0,y=0; url[y]; ++x,++y ){
if ( (url[x] = url[y]) == ‘%‘){
url[x] = x2c(&url[y+1]);
y += 2;
}
}
url[x] = ‘\0‘;
}
/* %xx:用其十六进制ASCII码值表示的特殊字符。根据值xx将其转换成相应的ASCII字符*/
char x2c(char *what)
{
register char digit;
digit = (what[0] >= ‘A‘ ? ((what[0] & 0xdf) - ‘A‘)+10 : (what[0] - ‘0‘));
digit *= 16;
digit += (what[1] >= ‘A‘ ? ((what[1] & 0xdf) - ‘A‘)+10 : (what[1] - ‘0‘));
return(digit);
}
以上是关于基于ARM的智能灯光控制系统设备管理的主要内容,如果未能解决你的问题,请参考以下文章
资料转发分享基于STM32智能路灯灯光自动控制系统设计-基于STM32无刷电机BLDC速度控制器系统设计-基于STM32热释人体感应智能门禁报警系统设计-基于STM32居家加湿器控制仿真系统设计
通过物联网平台和灯光设备实现灯光的智能控制,主要实现了通过语音控制灯光颜色的变化灯光每种颜色的占比日出日落模式等等,底层封装为