|
楼主 |
发表于 2020-11-3 21:49
|
显示全部楼层
#include <WiFi.h>
#include <WebServer.h> //引入相应库
const char *ssid = "";
const char *password = "";
int t=0;
byte Sta[4];
int latchPin = 15;
int clockPin = 2;
int dataPin = 4;
hw_timer_t * timer = NULL;
WebServer server(80); //声明WebServer对象
void handleArg1() //回调函数
{
String arg = server.pathArg(0);
int a=arg.toInt();
if(a==100){Sta[0]=0;
Sta[1]=0;
Sta[2]=0;
Sta[3]=0;
t=10;
}
else if(a==101){Sta[0]=255;
Sta[1]=255;
Sta[2]=255;
Sta[3]=255;
t=10;
}
String c= String(Sta[0])+","+ String(Sta[1])+","+ String(Sta[2])+","+ String(Sta[3]);
server.send(200, "text/plain", c);
}
void IRAM_ATTR onTimer(){
if(t>=10){
digitalWrite(latchPin, LOW);
shiftOut(dataPin,clockPin,MSBFIRST,Sta[3]);
shiftOut(dataPin,clockPin,MSBFIRST,Sta[2]);
shiftOut(dataPin,clockPin,MSBFIRST,Sta[1]);
shiftOut(dataPin,clockPin,MSBFIRST,Sta[0]);
digitalWrite(latchPin, HIGH);
t=0;
}
else{t++;}
}
void handleArg2() //回调函数
{
String arg0 = server.pathArg(0);
String arg1 = server.pathArg(1);
int a=arg0.toInt();
int s=arg1.toInt();
if(a<8&s<=1)
{ bitWrite(Sta[0], a, s);
t=10;
//String c= String(Sta);
// server.send(200, "text/plain", c);
}
else if(a<16&s<=1)
{ bitWrite(Sta[1], a-8, s);
t=10;
}
else if(a<24&s<=1)
{ bitWrite(Sta[2], a-16, s);
t=10;
}
else if(a<32&s<=1)
{ bitWrite(Sta[3], a-24, s);
t=10;
}
else if(a==200)
{ unsigned char hexdata[6]={170,7,2,0,0,0};
hexdata[4]=s;
hexdata[5]=hexdata[0]+hexdata[1]+hexdata[2]+hexdata[3]+hexdata[4];
Serial2.write(hexdata, 6);
}
else if(a==201)
{ unsigned char hexdata[7]={170,22,3,2,0,0,0};
hexdata[5]=s;
hexdata[6]=hexdata[0]+hexdata[1]+hexdata[2]+hexdata[3]+hexdata[4]+hexdata[5];
Serial2.write(hexdata, 7);
}
else if(a==202)
{ unsigned char hexdata[5]={170,24,1,0,0};
hexdata[3]=s;
hexdata[4]=hexdata[0]+hexdata[1]+hexdata[2]+hexdata[3];
Serial2.write(hexdata, 5);
}
else if(a==203)
{ unsigned char hexdata[5]={170,19,1,0,0};
hexdata[3]=s;
hexdata[4]=hexdata[0]+hexdata[1]+hexdata[2]+hexdata[3];
Serial2.write(hexdata, 5);
}
String c= String(Sta[0])+","+ String(Sta[1])+","+ String(Sta[2])+","+ String(Sta[3]);
server.send(200, "text/plain", c);
//IRAM_ATTR onTimer();
}
void setup()
{
Serial.begin(115200);
Serial2.begin(9600);
Serial.println();
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
server.on("/{}", handleArg1); //注册链接与回调函数
server.on("/{}/{}", handleArg2);
server.begin(); //启动服务器
Serial.println("Web server started");
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 500000, true);
timerAlarmEnable(timer);
}
void loop()
{
server.handleClient(); //处理来自客户端的请求
} |
|