定义全局变量
#define GPRS_PRO_SEQ_LEN 2
#define GPRS_PRO_DEV_ID_LEN 7
#define GPRS_PRO_CMD_LEN 2
#define GPRS_PRO_DEVICE_ID 7
#define GPRS_PRO_BASE_LEN 17 //首先对协议中要使用的数据进行宏定义,包括流水号,ID字节长度等。
#define MEGA
#define SSID "Itead_1(Public)"
#define PASSWORD "27955416" //定义wifi名和使用密码
#include "dht11.h"
dht11 DHT11; //引入dht11库函数
#include "uartWIFI.h"
#include <SoftwareSerial.h>
WIFI wifi; //引入wifi声明(源程序)
协议结构体定义
typedef struct
{
u16 seq_num;
u16 len;
u8 dev_id[GPRS_PRO_DEVICE_ID];
u16 command_id;
u8 *pro_data;
}gprs_pro_cmd_t;
设定透传模式,实现在网络上的通信
void setup()
{
wifi.begin();
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if(!b)
{
DebugSerial.println("Init error");
}
delay(8000); //确保模块有足够的时间获得IP地址
String ipstring = wifi.showIP();
DebugSerial.println(ipstring); //显示模块的IP地址
delay(5000);
wifi.confMux(1);
delay(100);
if(wifi.confServer(1,8080))
DebugSerial.println("Server is set up");
} //完成透传模式的设定
设置索引函数
static void gprs_pro_menue_handler(gprs_pro_cmd_t *com)
{
u8 cmd_index=0;
while(Gprs_protocol_menu[cmd_index].command_id != 0xFFFF)
{
if(com->command_id == Gprs_protocol_menu[cmd_index].command_id)
{
Gprs_protocol_menu[cmd_index].cmd_handler(com);
break;
}
cmd_index ++;
}
}//接收数据
void rec_data(){
int rece_data = wifi.ReceiveMessage(buf);
String tempstr = String(rece_data);
if (strcmp(rece_data, "@@") == 0)
{
data = pro_protocol_analysis((u8*)&rece_data,tempstr.length)
}
}
DHT11的函数设定
void pro_avtice_temperature(void){
gprs_pro_cmd_t local_com;
memset((u8*)&local_com,0,sizeof(gprs_pro_cmd_t));
local_com.pro_data = malloc(128);
local_com.command_id = 0x0004;
local_com.seq_num = 0x0001;
local_com.dev_id = 0x30313233343536;//{0x30,0x31,0x32,0x33,0x34,0x35,0x36};
//温度值读取:
DHT11.read(7);
local_com.pro_data = (u8*)&DHT11.temperature;
local_com.len = 2;
protocol_datapack(&local_com);
}//温度读取函数
void pro_avtice_TeConnect(void){
gprs_pro_cmd_t local_com; //结构体
memset((u8*)&local_com,0,sizeof(gprs_pro_cmd_t));
local_com.pro_data = malloc(128);
local_com.command_id = 0x0002;//终端登录
local_com.seq_num = 0x0001;
local_com.dev_id = 0x30313233343536;//{0x30,0x31,0x32,0x33,0x34,0x35,0x36};
local_com.pro_data = 0x324235....;
local_com.len = 74;
protocol_datapack(&local_com);
}//终端登录函数
心率传感函数设定
void gprs_pro_active_PingReq(void) //心跳
{
gprs_pro_cmd_t local_com;
memset((u8*)&local_com,0,sizeof(gprs_pro_cmd_t));
local_com.command_id = 0x0003;
local_com.seq_num = 0x0001;
local_com.dev_id = 0x30313233343536;
gprs_pro_datapack(&local_com);
}