暂时放个程序
//ME->>{“method”: “update”,“gatewayNo”: “01”,“userkey”: “2b64c489d5f94237bcf2e23151bb7d01”}&^!
//Ser->>{“f”:“message”,“p1”:“ok”}&^!
//ME->>{“method”: “upload”,“data”:[{“Name”:“A1C”,“Value”:“96.2”}]}&^!
//Ser->>{“f”:“message”,“p1”:“ok”}&^!
//Ser->>进入控制设备
//Ser->>{“method”:“send”,“gatewayNo”:“01”,“userkey”:“2b64c489d5f94237bcf2e23151bb7d01”,“f”:“getAllSensors”}&^!
//ME->>{“method”:“response”,“result”:{“successful”:true,“message”:“ok!”,“data”:[{“id”:“A1O”,“value”:“10”},{“id”:“A2O”,“value”:“1”}]}}&^!
//Ser->>点击开关,设定数值
//Ser->>{“method”:“send”,“gatewayNo”:“01”,“userkey”:“2b64c489d5f94237bcf2e23151bb7d01”,“f”:“updateSensor”,“p1”:“A2O”,“p2”:“0”}&^!
//ME->>{“method”:“response”,“result”:{“successful”:true,“message”:“ok!”,“data”:[{“id”:“A1O”,“value”:“10”},{“id”:“A2O”,“value”:“1”}]}}&^!
//或
//ME->>{“method”:“response”,“result”:{“successful”:true,“message”:“Write cmd successful !”}}&^!
#include "Arduino.h"
#include "WiFi.h"
const char *ssid = "CMCC-9eUG";
const char *password = "apzwqk4u";
// const char *ssid = "X";
// const char *password = "123456789";
//const char *ssid = "HUAWEI P30";
//const char *password = "0000000000";
//const IPAddress serverIP(192,168,137,1); //目标地址
//const char *serverIP="17854231380.qicp.vip";
//uint16_t serverPort = 23460; //目标服务器端口号
const char *serverIP="tcp.lewei50.com";
uint16_t serverPort = 9960; //目标服务器端口号
#define USERKEY "2b64c489d5f94237bcf2e23151bb7d01"
//const char *server_Line="{\"method\": \"update\",\"gatewayNo\": \"01\",\"userkey\": \"2b64c489d5f94237bcf2e23151bb7d01\"}&^!";
WiFiClient client; //声明一个客户端对象,用于与服务器进行连接
int A1C_num=0;
int tim1_IRQ_count = 0;
hw_timer_t *tim1 = NULL;
#define LED_PIN 35
#define LED_ON HIGH
#define LED_OFF LOW
#define Botton_PIN 0
#define Botton_ON LOW
#define Botton_OFF HIGH
int LED_Sta=LED_OFF;
double A1O_Num=0;
void LED_Set(int Sta)
{
if(Sta==0)
{
LED_Sta=0;
digitalWrite(LED_PIN,LED_OFF);
}else{
LED_Sta=1;
digitalWrite(LED_PIN,LED_ON);
}
}
//上线
String server_Line(String Decive_num)
{
String m_String_01="{\"method\": \"update\",\"gatewayNo\": \"";
String m_String_02="\",\"userkey\": \"2b64c489d5f94237bcf2e23151bb7d01\"}&^!";
String m_String=m_String_01+Decive_num+m_String_02;
return m_String;
}
//上传传感器数据
String Data_Upload(String sensor_Num,int sensor_Data)
{
char Num[10]={};
String sensor_String_01="{\"method\": \"upload\",\"data\":[{\"Name\":\"";
String sensor_String_02="\",\"Value\":\"";
String sensor_String_03="\"}]}&^!";
String sensor_String=sensor_String_01+sensor_Num+sensor_String_02+String(sensor_Data)+sensor_String_03;
return sensor_String;
}
String getAllSensors(String Sensors1_Name,int Sensors1_Num,String Sensors2_Name,int Sensors2_Num)
{
String String1="{\"method\":\"response\",\"result\":{\"successful\":true,\"message\":\"ok!\",\"data\":[{\"id\":\"";
String String2="\",\"value\":\"";
String String3="\"},{\"id\":\"";
String String4="\",\"value\":\"";
String String5="\"}]}}&^!\"";
String String_Dis=String1+Sensors1_Name+String2+String(Sensors1_Num)+String3+Sensors2_Name+String4+String(Sensors2_Num)+String5;
return String_Dis;
}
String getAllSensors(String Sensors1_Name,double Sensors1_Num,String Sensors2_Name,int Sensors2_Num)
{
String String1="{\"method\":\"response\",\"result\":{\"successful\":true,\"message\":\"ok!\",\"data\":[{\"id\":\"";
String String2="\",\"value\":\"";
String String3="\"},{\"id\":\"";
String String4="\",\"value\":\"";
String String5="\"}]}}&^!\"";
String String_Dis=String1+Sensors1_Name+String2+String(Sensors1_Num)+String3+Sensors2_Name+String4+String(Sensors2_Num)+String5;
return String_Dis;
}
String Set_CMD(void)
{
String Data="{\"method\":\"response\",\"result\":{\"successful\":true,\"message\":\"Write cmd successful !\"}}&^!";
return Data;
}
void setup()
{
delay(1000);
//IO Init
pinMode(LED_PIN,OUTPUT);
pinMode(Botton_PIN,INPUT);
//INPUT_PULLUP上拉输入
Serial.begin(115200);
//80Mhz时钟 分频为1Mhz
tim1 = timerBegin(0, 80, true); //初始化定时器 timerBegin num : 定时器编号 divider:分频数 countUp: 是否是累加模式
timerAttachInterrupt(tim1, tim1Interrupt, true); //配置定时器中断 timerAttachInterrupt *timer:目标定时器(计时器结构体指针 hw_timer_t*) void (*fn)(void) : 中断函数入口地址 是否边沿触发
//100ms
timerAlarmWrite(tim1, 100000, true); //定时器设置timerAlarmWrite *timer:目标定时器 ( 计时器结构体指针 hw_timer_t * ) alarm_value : 计数上限值 autoreload : 是否重装载.
timerAlarmEnable(tim1); //使能定时器 timerAlarmEnable
Serial.println();
WiFi.mode(WIFI_STA); //站点模式
WiFi.setSleep(false); //关闭STA模式下wifi休眠,提高响应速度
WiFi.begin(ssid, password);
Serial.println("wait Wifi");
while (WiFi.status() != WL_CONNECTED)
{
delay(2500);
Serial.print(".");
//WiFi.begin(ssid, password);
}
Serial.println("connected");
Serial.print("location IP:");
Serial.println(WiFi.localIP());
}
bool Server_Online,WIFI_Online,Server_T_Online;
int WIFI_Test_Time;
char Server_Sta=0;
int Heart_Time=0;
void loop()
{
LED_Set(LED_Sta);
if (tim1_IRQ_count > 10)
{
tim1_IRQ_count=0;
WIFI_Test_Time++;
//检测WIFI连接
if(WiFi.status() == WL_CONNECTED)
{
WIFI_Online=true;
}else{
WIFI_Online=false;
Server_Online=false;
WiFi.begin(ssid, password);
Serial.println("WIFI begin");
while (WiFi.status() != WL_CONNECTED)
{
delay(2500);
Serial.print(".");
Serial.print(WiFi.status());
WiFi.begin(ssid, password);
}
WIFI_Online=true;
Serial.println("WiFi connected");
Serial.print("location IP:");
Serial.println(WiFi.localIP());
}
//检测服务器连接
if(client.connected())
{
Server_Online=true;
Server_Sta=1;
//Serial.println("server Online");
}else{
if(client.connect(serverIP, serverPort))
{
Server_Online=true;
Server_Sta=1;
Server_T_Online=true;
Serial.println("server connect ok");
}else{
Server_Online=false;
Server_Sta=-1;
Serial.println("server connect fail");
}
}
if(Server_Online)
{
Heart_Time++;
if(Heart_Time>=23)
{
Heart_Time=0;
//Serial.println(Data_Upload("A1C",random()%100));
Serial.println("Heart A1C");
client.print(Data_Upload("A1C",random()%100));
}
}
}
if(Server_Online)
{
if(Server_T_Online)
{
Server_T_Online=false;
//Serial.println(server_Line("01"));
Serial.println("Link server");
client.print(server_Line("01"));
Server_Sta=2;
}
if(client.available())
{
if (client.available()) //如果有数据可读取
{
String line = client.readStringUntil('\n'); //读取数据到换行符
Serial.print("read>>");
Serial.println(line);
//服务器返回数据解析
if(line.indexOf("\"f\":\"message\",\"p1\":\"ok\"")!=-1) //上线或上传传感器数值
{
if(Server_Sta==2) //上线成功
{
Server_Sta=3;
//Serial.println(Data_Upload("A1C",random()%100));
Serial.println("A1C Data");
client.print(Data_Upload("A1C",random()%100));
}else{
if(Server_Sta==3)//上传传感器数值成功
{
}else{
//Serial.print("\"f\":\"message\",\"p1\":\"ok\" Server_Sta"); Serial.println(Server_Sta);
}
}
}
if(line.indexOf("\"method\":\"send\",\"gatewayNo\"")!=-1) //切换到控制器界面或开关指令下发
{
if(line.indexOf("\"f\":\"getAllSensors\"")!=-1) //切换到界面
{
Serial.print("getAllSensors : ");Serial.print("A1O");Serial.print(A1O_Num);Serial.print("A2O");Serial.println(LED_Sta);
//Serial.println(getAllSensors("A1O",A1O_Num,"A2O",LED_Sta));
client.println(getAllSensors("A1O",A1O_Num,"A2O",LED_Sta));
}else{
if(line.indexOf("\"f\":\"updateSensor\"")!=-1) //指令下发
{
Serial.print("updateSensor>>");
if(line.indexOf("\"p1\":\"A2O\"")!=-1)
{
line.remove(0,line.indexOf("A2O")); //裁切为 A2O","p2":"1"}&^!
line.remove(0,11); //裁切为 1"}&^!
line.remove(line.indexOf("\"}&^!"),5);//裁切为 1
LED_Set(line.toInt());
Serial.print("A2O");Serial.println(LED_Sta);
//Serial.println(getAllSensors("A1O",A1O_Num,"A2O",LED_Sta));
//client.print(getAllSensors("A1O",A1O_Num,"A2O",LED_Sta));
//Serial.println(Set_CMD());
client.print(Set_CMD());
}else{
if(line.indexOf("\"p1\":\"A1O\"")!=-1)
{
Serial.println("A1O");
line.remove(0,line.indexOf("A1O")); //裁切为 A1O","p2":"1"}&^!
line.remove(0,11); //裁切为 1"}&^!
line.remove(line.indexOf("\"}&^!"),5);//裁切为 1
A1O_Num=line.toDouble();
Serial.print("A1O");Serial.println(A1O_Num);
//Serial.println(getAllSensors("A1O",A1O_Num,"A2O",LED_Sta));
//client.print(getAllSensors("A1O",A1O_Num,"A2O",LED_Sta));
//Serial.println(Set_CMD());
client.print(Set_CMD());
}else{
Serial.println("Unkown");
}
}
}
}
}
line="";
}
}
}
}
//ME->>{"method": "update","gatewayNo": "01","userkey": "2b64c489d5f94237bcf2e23151bb7d01"}&^!
//Ser->>{"f":"message","p1":"ok"}&^!
//ME->>{"method": "upload","data":[{"Name":"A1C","Value":"96.2"}]}&^!
//Ser->>{"f":"message","p1":"ok"}&^!
//Ser->>进入控制设备
//Ser->>{"method":"send","gatewayNo":"01","userkey":"2b64c489d5f94237bcf2e23151bb7d01","f":"getAllSensors"}&^!
//ME->>{"method":"response","result":{"successful":true,"message":"ok!","data":[{"id":"A1O","value":"10"},{"id":"A2O","value":"1"}]}}&^!
//Ser->>点击开关,设定数值
//Ser->>{"method":"send","gatewayNo":"01","userkey":"2b64c489d5f94237bcf2e23151bb7d01","f":"updateSensor","p1":"A2O","p2":"0"}&^!
//ME->>{"method":"response","result":{"successful":true,"message":"ok!","data":[{"id":"A1O","value":"10"},{"id":"A2O","value":"1"}]}}&^!
//或
//ME->>{"method":"response","result":{"successful":true,"message":"Write cmd successful !"}}&^!
/*定时器中断服务函数*/
void tim1Interrupt()
{
tim1_IRQ_count++;
}