arduino

#include <avr/wdt.h>  
#include <SoftwareSerial.h>


#define FPIN 13

SoftwareSerial mySerial(10, 11); // RX, TX
unsigned long lastSendTime=0;
unsigned long lastReceiveTime=0;
unsigned long SafeKey=987654321UL; // Max 4,294,967,295
String RecCache="";

void setup() {
  // put your setup code here, to run once:
  pinMode(FPIN,OUTPUT);
  digitalWrite(FPIN,LOW);
  
  Serial.begin(9600);
  delay(100);
  Serial.println("Ready!");



  //========Wifi Config=============
  initWifi();
 

}

void loop() {
  // put your main code here, to run repeatedly:
  handleTXR();
}

void  initWifi(){
  mySerial.begin(9600);
  delay(100);
    mySerial.println("AT+RST");
    delayAndRead(3000);
  
   mySerial.println("AT+CIPMUX=1");
   delayAndRead(500);
   mySerial.println("AT+CIPSERVER=1");
   delayAndRead(500); 
}

void handleTXR(){
    //handle user input cmd
  while (Serial.available()){
    mySerial.write(Serial.read()); 
  }
  
  while(mySerial.available()){
    char c= mySerial.read();
    RecCache=RecCache +(String)c;
  }
  
    int bIndex= RecCache.indexOf("$F");
  if(bIndex>=0){
    int eIndex=RecCache.indexOf("$E",bIndex);
    if(eIndex>=0){
       //Extra Data
       String data=RecCache.substring(bIndex+2,eIndex);
       RecCache=RecCache.substring(eIndex);
       lastReceiveTime=millis();
       
       handleCmd(data);
    }
  }else{
    RecCache=""; 
  }
  
  if(RecCache.length()>=512)RecCache="";
  
}

void handleCmd(String cmd){
 Serial.println("cmd->" + cmd +"."); 
 if(cmd.startsWith("HB:")){
    byte bs[4];
    long2byte(millis(),bs);
    String msg=bytes2String(bs,0,4);// (String)((char)bs[0]) +(String)((char)bs[1]) +(String)((char)bs[2]) +(String)((char)bs[3]) ;

   reply(true,"HB",msg);
 }else if(cmd.startsWith("F:")){
    fireCmd(cmd);
 }
}

void fireCmd(String cmd){
   byte safeBytes[4]={0,0,0,0};
   for(int i=0;i<4;i++){
     safeBytes[i]=(byte)cmd.charAt(2+i);
     //Serial.print(safeBytes[i],HEX);
     //Serial.print(",");
    
   }
  unsigned long sk= bytes2long(safeBytes);
  byte duration=cmd.charAt(6);
  if(duration>15) duration=15;
  if(duration<=0)duration=1;
  
  if(sk!=SafeKey){
    reply(false,"F","Safe Key not match!");
    return;
  }
  reply(true,"F","");
  digitalWrite(FPIN,HIGH);
  delay(duration * 1000);
  digitalWrite(FPIN,LOW);
}

void reply(bool isOk,String cmd,String msg){
 String rStr="";
 if(isOk){
  rStr="$FOK-" + cmd +":" +msg +"$E";
  mySerial.println("AT+CIPSEND=0," + (String)rStr.length());  
  delay(10);
  mySerial.println(rStr);
 }else{
    rStr="$FEE-" + cmd +":" +msg +"$E";
    mySerial.println("AT+CIPSEND=0," + (String)rStr.length());  
    delay(10);
    mySerial.println(rStr);
 } 
  
}

String bytes2String(byte bs[],int offset,int len){
  
   String t="";
   for(int i=offset;i<len;i++){
    t +=(String)((char)bs[i]); 
     
   }
   return t;
  
}

void cls(){
   while(mySerial.available()){
     mySerial.read();
  }
}

void delayAndRead(int waitTime){
   delay(waitTime);
   while(mySerial.available()){
     Serial.write(mySerial.read()); 
   }
}

 void long2byte(unsigned long res,byte targets[] ) {  
       
        targets[0] = (byte) (res & 0xff);// 鏈�浣庝綅  
        targets[1] = (byte) ((res >> 8) & 0xff);// 娆′綆浣�  
        targets[2] = (byte) ((res >> 16) & 0xff);// 娆¢珮浣�  
        targets[3] = (byte) (res >> 24);// 鏈�楂樹綅,鏃犵鍙峰彸绉汇��  
              
    }  
    
unsigned long bytes2long(byte buf[]) {  
        unsigned long firstByte = 0;  
        unsigned long secondByte = 0;  
        unsigned long thirdByte = 0;  
        unsigned long fourthByte = 0;  
        int index = 0;  
        firstByte = (0x000000FFU & ( buf[index+3]));  
        secondByte = (0x000000FFU & ( buf[index + 2]));  
        thirdByte = (0x000000FFU & ( buf[index + 1]));  
        fourthByte = (0x000000FFU & ( buf[index ]));  
        index = index + 4;  
        return ((unsigned long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFUL;  
    }  
View Code

相关文章:

  • 2022-12-23
  • 2021-12-07
  • 2021-12-24
  • 2022-02-17
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-13
  • 2022-02-07
  • 2022-12-23
相关资源
相似解决方案