用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

点击进入授权页面

只需一步,快速开始

  • QQ空间
  • 回复
  • 收藏
回帖奖励 5 金币 回复本帖可获得 1 金币奖励! 每人限 1 次(中奖概率 30%)

该用户从未签到

这是一段运行在Arduino Nano(ATmega328p版本)的程序,目的是通过HC05蓝牙模块读取WIFI地址和用户标识号,在ESP8266 Wifi模块上输入Wifi信息连接,但是程序跑到“Php_Connected();”(Line 607)的时候串口输出异常的字符串,然后单片机就死机了。我使用了MemoryFree.h这个库来检测可用内存,但是显示死机前还剩下1300+b的内存,应该不会缺少运行内存才对,编译的时候也显示只是占用了40%的Flash和29%的SRAM,拜托各路大神来帮忙解决这个问题,谢谢!!!
[C] 纯文本查看 复制代码
//提供MQ-135,MQ-136传感器数据(本地不进行转换计算),服务器计算NH3和H2S数值
//type=toilet_monitor
#include <SoftwareSerial.h>
#include <EEPROM.h>
#include <avr/pgmspace.h>
#include <MemoryFree.h>
//对外通信
SoftwareSerial ESP8266(2,3); // RX, TX
SoftwareSerial HC05(10,11); // RX, TX
//其他零件接口
#define MQ135 A0
#define MQ136 A7
//定义传感器参数
int MQ135_RAW_VALUE;
int MQ136_RAW_VALUE;
//定义运行步骤状态
enum PROGRESS_STATE{INTERLIZING_ESP8266,INTERLIZING_HC05,
WIFI_CONNECTING,WIFI_CONNECTED,PHP_CONNECTED,PHP_DISCONNECTED};
PROGRESS_STATE progress_state=WIFI_CONNECTING;
//定义用户标识/用户MAC地址/WIFI SSID/WIFI BSSID/WIFI密码
/*EPPROM用法
 * 大小:1024B(1024位char)
 * USER_ID:0~49(共50位)
 * USER_MAC:50~99(共50位)
 * WIFI_SSID:100~199(共100位)
 * WIFI_BSSID:200~299(共100位)
 * WIFI_PASSWORD:300~399(共100位)
 */

String USER_ID="";
String USER_MAC="";
String WIFI_SSID="";
String WIFI_BSSID="";
String WIFI_PASSWORD="";
//定义需要访问的网址
const char WEBSITE_VERIFY_ID[] PROGMEM="http://example.com/mail/ar_get_userid.php?";
const char WEBSITE_SEND_DATA[] PROGMEM="http://[color=rgb(54, 54, 54)][backcolor=transparent]example.com[/backcolor][/color]/mail/ar_arduino2server.php?";
//ESP8266定义返回参数表
byte ESP8266_RETURN=-1;
byte ESP8266_TRY_TIME=0;
const char AT_REQUEST_0[] PROGMEM="1";
const char AT_REQUEST_1[] PROGMEM="OK";
const char* const AT_REQUEST[] PROGMEM ={AT_REQUEST_0,AT_REQUEST_1};

const char AT_RST_REQUEST_0[] PROGMEM="1";
const char AT_RST_REQUEST_1[] PROGMEM="ready";
const char* const AT_RST_REQUEST[] PROGMEM={AT_RST_REQUEST_0,AT_RST_REQUEST_1}; 

const char AT_CWJAP_REQUEST_0[] PROGMEM="2";
const char AT_CWJAP_REQUEST_1[] PROGMEM="No AP\r\n\r\nOK";
const char AT_CWJAP_REQUEST_2[] PROGMEM="WIFI CONNECTED\r\nWIFI GOT IP\r\n\r\nOK";
const char* const AT_CWJAP_REQUEST[] PROGMEM={AT_CWJAP_REQUEST_0,AT_CWJAP_REQUEST_1,AT_CWJAP_REQUEST_2};            //"OK"<->"WIFI CONNECTED\nWIFI GOT IP\nOK"

const char AT_CWQAP_REQUEST_0[] PROGMEM="2";
const char AT_CWQAP_REQUEST_1[] PROGMEM="OK\r\nWIFI DISCONNECT";
const char AT_CWQAP_REQUEST_2[] PROGMEM="ERROR";
const char* const AT_CWQAP_REQUEST[] PROGMEM={AT_CWQAP_REQUEST_0,AT_CWQAP_REQUEST_1,AT_CWQAP_REQUEST_2};  

const char AT_CWJAP_ACCESS_0[] PROGMEM="2";
const char AT_CWJAP_ACCESS_1[] PROGMEM="WIFI CONNECTED\r\nWIFI GOT IP\r\n\r\nOK";
const char AT_CWJAP_ACCESS_2[] PROGMEM="FAIL";
const char* const AT_CWJAP_ACCESS[] PROGMEM={AT_CWJAP_ACCESS_0,AT_CWJAP_ACCESS_1,AT_CWJAP_ACCESS_2};              //"OK"<->"WIFI CONNECTED\nWIFI GOT IP\nOK"  

const char AT_CIPSTAMAC_REQUEST_0[] PROGMEM="1";
const char AT_CIPSTAMAC_REQUEST_1[] PROGMEM="OK";
const char* const AT_CIPSTAMAC_REQUEST[] PROGMEM={AT_CIPSTAMAC_REQUEST_0,AT_CIPSTAMAC_REQUEST_1};

const char AT_CIPSTART_ACCESS_0[] PROGMEM="2";
const char AT_CIPSTART_ACCESS_1[] PROGMEM="CONNECT\r\n\r\nOK";
const char AT_CIPSTART_ACCESS_2[] PROGMEM="ERROR";
const char* const AT_CIPSTART_ACCESS[] PROGMEM={AT_CIPSTART_ACCESS_0,AT_CIPSTART_ACCESS_1,AT_CIPSTART_ACCESS_2};              //"OK"<->"CONNECT\nOK"

const char AT_CIPSEND_ACCESS_0[] PROGMEM="3";
const char AT_CIPSEND_ACCESS_1[] PROGMEM=">";
const char AT_CIPSEND_ACCESS_2[] PROGMEM="ERROR";
const char AT_CIPSEND_ACCESS_3[] PROGMEM="CLOSED";
const char* const AT_CIPSEND_ACCESS[] PROGMEM={AT_CIPSEND_ACCESS_0,AT_CIPSEND_ACCESS_1,AT_CIPSEND_ACCESS_2,AT_CIPSEND_ACCESS_3};       //">"<->"OK\n>","ERROR"<->"link is not valid\nERROR"

const char AT_GET_REQUEST_0[] PROGMEM="3";
const char AT_GET_REQUEST_1[] PROGMEM="SEND OK";
const char AT_GET_REQUEST_2[] PROGMEM="SEND FAIL";
const char AT_GET_REQUEST_3[] PROGMEM="CLOSED";
const char* const AT_GET_REQUEST[] PROGMEM={AT_GET_REQUEST_0,AT_GET_REQUEST_1,AT_GET_REQUEST_2,AT_GET_REQUEST_3};//"OK"<->"SEND OK","FAIL"<->"SEND FAIL"

const char AT_GET_MSG_0[] PROGMEM="<START>";
const char AT_GET_MSG_1[] PROGMEM="<END>";
const char* const AT_GET_MSG[] PROGMEM={AT_GET_MSG_0,AT_GET_MSG_1};

const char AT_CIPCLOSE_0[] PROGMEM="2";
const char AT_CIPCLOSE_1[] PROGMEM="CLOSED\r\n\r\nOK";
const char AT_CIPCLOSE_2[] PROGMEM="ERROR";
const char* const AT_CIPCLOSE[] PROGMEM={AT_CIPCLOSE_0,AT_CIPCLOSE_1,AT_CIPCLOSE_2};             //"OK"<->"CLOSED\nOK"
//主函数
void setup() {
  pinMode(MQ135,INPUT);
  pinMode(MQ136,INPUT);
  Serial.begin(9600);
  Serial.println(F("INTERLIZING_SYSTEM"));
  ESP8266.begin(9600);
  HC05.begin(9600);
  //工厂模式下清零数据
  for(int i=0;i<500;i++){
      EEPROM.write(i,0);
  }
  //尝试读取USER_ID
    for(int i=0;EEPROM.read(i)!=0;i++){
      USER_ID.concat((char)EEPROM.read(i));
    }
    //尝试读取USER_MAC
    for(int i=50;EEPROM.read(i)!=0;i++){
      USER_MAC.concat((char)EEPROM.read(i));
    }
    //尝试读取WIFI_SSID
    for(int i=100;EEPROM.read(i)!=0;i++){
      WIFI_SSID.concat((char)EEPROM.read(i));
    }
    //尝试读取WIFI_BSSID
    for(int i=200;EEPROM.read(i)!=0;i++){
      WIFI_BSSID.concat((char)EEPROM.read(i));
    }
    //尝试读取WIFI_PASSWORD
    for(int i=300;EEPROM.read(i)!=0;i++){
      WIFI_PASSWORD.concat((char)EEPROM.read(i));
    }
    Serial.println(F("INTERLIZED_SYSTEM"));
    delay(100);
}
//ESP8266收发信息(返回)
String ESP8266_MsgTx2Rx(String Msg,int Delay,char*SignList[],bool DebugSign)
{
  //读取Flash的buffer
  char*Temp_Buffer;
  ESP8266.println(Msg);
  if(DebugSign){
    Serial.println(Msg);
  }
  delay(Delay);
  String ReturnMsg="";
  char LastReturnChar=0;
  Temp_Buffer=new char[1];
  //Temp_Buffer=Num
  strcpy_P(Temp_Buffer, (char*)pgm_read_word(&(SignList[0])));
  int SignListLength=atoi(Temp_Buffer);
  delete(Temp_Buffer);
  boolean ReturnFull=false;
  while(true)
  {
   //开始新一轮读取ESP8266返回的数据
   while(ESP8266.available() > 0)
   {
    char ReturnChar=(char)(ESP8266.read());
    //重复空格去除
    if(ReturnChar==' '&&LastReturnChar==' '){
      continue;
    }
    //特殊字符去除
    if((ReturnChar>=32&&ReturnChar<=126)||ReturnChar=='\r'||ReturnChar=='\n'||ReturnChar=='\0'){
      LastReturnChar=ReturnChar;
      ReturnMsg+=ReturnChar;
    }
   }
   for(int i=1;i<=SignListLength;i++){
    Temp_Buffer=new char[50];
    strcpy_P(Temp_Buffer, (char*)pgm_read_word(&(SignList[i])));
    if(DebugSign){
      Serial.print(i);
      Serial.print(" "+ReturnMsg+" < - > ");
      Serial.println(Temp_Buffer);
  }
    //Temp_Buffer=SignList[SignListLength]
    if(ReturnMsg.lastIndexOf(Temp_Buffer)!=-1){
      ReturnFull=true;
      ESP8266_RETURN=i;
      delete(Temp_Buffer);
      break;
    }
    delete(Temp_Buffer);
   }
   if(ReturnFull){
    Temp_Buffer=new char[50];
    strcpy_P(Temp_Buffer, (char*)pgm_read_word(&(SignList[ESP8266_RETURN])));
    //Temp_Buffer=SignList[ESP8266_RETURN]
    ReturnMsg.replace(Msg+"\r\n","");
    ReturnMsg.replace(Msg.substring(Msg.indexOf("+"),Msg.length()),"");
    ReturnMsg.replace(Msg.substring(Msg.indexOf("+"),Msg.indexOf("?")),"");
    ReturnMsg.replace("\r\n\r\n","\r\n");
    String SpecialReplacer="\r\n";
    SpecialReplacer.concat(Temp_Buffer);
    SpecialReplacer.concat("\r\n");
    ReturnMsg.replace(SpecialReplacer,"");
    String AddReturnMsg="";
    AddReturnMsg.concat(Temp_Buffer);
    ReturnMsg=AddReturnMsg+"_"+ReturnMsg;
    delete(Temp_Buffer);
    if(DebugSign){
      Serial.print("Returner: ");
      Serial.println(ReturnMsg);
  }
    return ReturnMsg;
   }
  }
}
//ESP8266收发信息(不返回)
void ESP8266_MsgTx2Rx(String Msg,int Delay,const char* const SignList[])
{
  //读取Flash的buffer
  char*Temp_Buffer;
  ESP8266.println(Msg);
  delay(Delay);
  String ReturnMsg="";
  char LastReturnChar=0;
  Temp_Buffer=new char[1];
  //Temp_Buffer=Num
  strcpy_P(Temp_Buffer, (char*)pgm_read_word(&(SignList[0])));
  int SignListLength=atoi(Temp_Buffer);
  delete(Temp_Buffer);
  while(true)
  {
   //开始新一轮读取ESP8266返回的数据
   while(ESP8266.available() > 0)
   {
    char ReturnChar=(char)(ESP8266.read());
    //重复空格去除
    if(ReturnChar==' '&&LastReturnChar==' '){
      continue;
    }
    //特殊字符去除
    if((ReturnChar>=32&&ReturnChar<=126)||ReturnChar=='\r'||ReturnChar=='\n'||ReturnChar=='\0'){
       LastReturnChar=ReturnChar;
       ReturnMsg+=ReturnChar;
    }
   }
   for(int i=1;i<=SignListLength;i++){
     Temp_Buffer=new char[50];
     strcpy_P(Temp_Buffer, (char*)pgm_read_word(&(SignList[i])));
     //Temp_Buffer=SignList[SignListLength]
     if(ReturnMsg.lastIndexOf(Temp_Buffer)!=-1){
        ESP8266_RETURN=i;
        delete(Temp_Buffer);
        ReturnMsg="";
        return;
     }
     delete(Temp_Buffer);
   }
  }
}

//ESP8266收信息
String ESP8266_Rx(int Delay,char Start[],char End[])
{
  //读取Flash的buffer,转换Start、End的数组
  char*Start_Temp_Buffer;
  char*End_Temp_Buffer;
  String Start_Get="";
  String End_Get="";
  delay(Delay);
  String ReturnMsg="";
  char LastReturnChar=0;
  boolean ReturnFull=false;
  while(true)
  {
   //开始新一轮读取ESP8266返回的数据
   while(ESP8266.available() > 0)
   {
    char ReturnChar=(char)(ESP8266.read());
    //重复空格去除
    if(ReturnChar==' '&&LastReturnChar==' '){
      continue;
    }
    //特殊字符去除
    if((ReturnChar>=32&&ReturnChar<=126)||ReturnChar=='\r'||ReturnChar=='\n'||ReturnChar=='\0'){
      LastReturnChar=ReturnChar;
      ReturnMsg+=ReturnChar;
    }
   }
   Start_Temp_Buffer=new char[10];
   End_Temp_Buffer=new char[10];
   strcpy_P(Start_Temp_Buffer, (char*)pgm_read_word(&(Start)));
   strcpy_P(End_Temp_Buffer, (char*)pgm_read_word(&(End)));
   Start_Get=Start_Temp_Buffer;
   End_Get=End_Temp_Buffer;
   delete(Start_Temp_Buffer);
   delete(End_Temp_Buffer);
   if(ReturnMsg.lastIndexOf(End_Get)!=-1){
     ReturnFull=true;
   }
   if(ReturnFull){
    ReturnMsg.substring(ReturnMsg.indexOf(Start_Get)+Start_Get.length(),ReturnMsg.indexOf(End_Get)-1);
    return ReturnMsg;
   }
  }
}
HC05接收信息(主要用例为监听配对信息/接收MAC地址确认)
String HC05_MsgRx()
{
 String HC05_ReturnMsg="";
 int Length=0;
 while(1)
 {
  char ReturnChar=-1;
  while (HC05.available())
  {
   //以字节流方式读取
   ReturnChar=(char)HC05.read();
   HC05_ReturnMsg.concat((char)ReturnChar);
  }
  if(ReturnChar==0)//读到'\0'时停止
  {
    return HC05_ReturnMsg;
  }
 }
}
//HC05发送信息(主要用例为发送MAC地址)
bool HC05_MsgTx(String Msg,int Delay)
{
 HC05.println(Msg);
 delay(Delay);
 String HC05_SEND_MSG_CORRECTION=HC05_MsgRx();
 if(HC05_SEND_MSG_CORRECTION.indexOf("OK")!=-1){
  return true;
 }
 else{
  return false;
 }
}

void Interlizing_Esp8266(){
  Serial.println(F("INTERLIZING_ESP8266"));
  //EPPROM中存有USER_ID等信息
  if(USER_ID.length()!=0&&WIFI_SSID.length()!=0){
    Serial.println(F("ESP8266_WIFI_BINDED"));
    progress_state=WIFI_CONNECTING;
    return;
  }
  ESP8266.listen();
  ESP8266_MsgTx2Rx("AT+CWJAP_DEF?",2000,AT_CWJAP_REQUEST);
  if(ESP8266_RETURN==1){
    //无绑定WIFI
    Serial.println(F("ESP8266_WIFI_UNBINDED"));
    progress_state=INTERLIZING_HC05;
  }
  else if(ESP8266_RETURN==2){
    //已绑定WIFI
    Serial.println(F("ESP8266_WIFI_BINDED"));
    progress_state=WIFI_CONNECTED;
  }
}

void Interlizing_Hc05(){
  //接收匹配信息
  HC05.listen();
  Serial.println(F("INTERLIZING_HC05"));
  String HC05_REQUIRE_NEW_RETURN=HC05_MsgRx();
  USER_ID=HC05_REQUIRE_NEW_RETURN.substring(HC05_REQUIRE_NEW_RETURN.indexOf(F("<USERID>"))+8,HC05_REQUIRE_NEW_RETURN.indexOf(F("</USERID>")));
  WIFI_SSID=HC05_REQUIRE_NEW_RETURN.substring(HC05_REQUIRE_NEW_RETURN.indexOf(F("<SSID>"))+6,HC05_REQUIRE_NEW_RETURN.indexOf(F("</SSID>")));
  WIFI_BSSID=HC05_REQUIRE_NEW_RETURN.substring(HC05_REQUIRE_NEW_RETURN.indexOf(F("<BSSID>"))+7,HC05_REQUIRE_NEW_RETURN.indexOf(F("</BSSID>")));
  WIFI_PASSWORD=HC05_REQUIRE_NEW_RETURN.substring(HC05_REQUIRE_NEW_RETURN.indexOf(F("<PASSWORD>"))+10,HC05_REQUIRE_NEW_RETURN.indexOf(F("</PASSWORD>")));
  HC05_REQUIRE_NEW_RETURN="";
  //匹配信息写入EPPROM
  for(int i=0;i<USER_ID.length();i++){
    EEPROM.write(i,USER_ID.charAt(i));
  }
  for(int i=0+USER_ID.length();i<50;i++){
    EEPROM.write(i,0);
  }
  for(int i=100;i<WIFI_SSID.length();i++){
    EEPROM.write(i,WIFI_SSID.charAt(i));
  }
  for(int i=100+WIFI_SSID.length();i<200;i++){
    EEPROM.write(i,0);
  }
  for(int i=200;i<WIFI_BSSID.length();i++){
    EEPROM.write(i,WIFI_BSSID.charAt(i));
  }
  for(int i=200+WIFI_BSSID.length();i<300;i++){
    EEPROM.write(i,0);
  }
  for(int i=300;i<WIFI_PASSWORD.length();i++){
    EEPROM.write(i,WIFI_PASSWORD.charAt(i));
  }
  for(int i=300+WIFI_PASSWORD.length();i<400;i++){
    EEPROM.write(i,0);
  }
  //查询用户MAC地址
  ESP8266.listen();
  USER_MAC=ESP8266_MsgTx2Rx(F("AT+CIPSTAMAC?"),200,AT_CIPSTAMAC_REQUEST,false);
  USER_MAC.replace(":","");
  //匹配信息写入EPPROM
  for(int i=50;i<USER_MAC.length();i++){
    EEPROM.write(i,USER_MAC.charAt(i));
  }
  for(int i=50+USER_MAC.length();i<100;i++){
    EEPROM.write(i,0);
  }
  //回传用户MAC地址
  HC05.listen();
  while(!HC05_MsgTx(USER_MAC,200));
  progress_state=WIFI_CONNECTING;
}

void Wifi_Connecting(){
  ESP8266.listen();
  Serial.println(F("WIFI_CONNECTING"));
  bool WIFI_CONNECTION_STATE=false;
  while(!WIFI_CONNECTION_STATE){
    ESP8266_MsgTx2Rx("AT+CWJAP_CUR=\""+WIFI_SSID+"\",\""+WIFI_PASSWORD+"\",\""+WIFI_BSSID+"\"",2000,AT_CWJAP_ACCESS);
    if(ESP8266_RETURN==1){
      //连接WIFI成功
      Serial.println(F("ESP8266_WIFI_CONNECTED"));
      progress_state=WIFI_CONNECTED;
      WIFI_CONNECTION_STATE=true;
    }
    else if(ESP8266_RETURN==1){
      //连接WIFI失败
      Serial.print(F("ESP8266_WIFI_CONNECTION_FAIL: "));
      progress_state=WIFI_CONNECTING;
    }
  }
}

void Wifi_Connected(){
  //连接服务器
  Serial.println(F("WIFI_CONNECTED"));
  ESP8266_MsgTx2Rx(F("AT+CIPSTART=\"TCP\",\"www.yiuliu.cn\",80"),500,AT_CIPSTART_ACCESS);
  if(ESP8266_RETURN==1){
    //连接服务器成功
    Serial.println(F("ESP8266_PHP_CONNECTED"));
    progress_state=PHP_CONNECTED;
  }
  else if(ESP8266_RETURN==2){
    //连接服务器失败
    Serial.print(F("ESP8266_PHP_CONNECTION_FAIL: "));
    progress_state=WIFI_CONNECTING;
    if(++ESP8266_TRY_TIME>=5){//连接服务器超时,重启
       Serial.println(F("ESP8266_PHP_CONNECTION_TIMEOUT"));
       ESP8266_MsgTx2Rx(F("AT+RST"),1000,AT_RST_REQUEST);
       progress_state=INTERLIZING_ESP8266;
    }
  }
}

void Php_Connected(){
  //发送&接收服务器信息
  Serial.println(F("PHP_CONNECTED"));
  bool SUCCESS_GET_VERIFY=false;
  if(USER_ID==""){//已配对的情况
    //获取MAC地址
    ESP8266.listen();
    USER_MAC=ESP8266_MsgTx2Rx(F("AT+CIPSTAMAC?"),200,AT_CIPSTAMAC_REQUEST,false);
    USER_MAC.replace(":","");
    //获取用户ID
    String GET_REQUEST="GET ";
    GET_REQUEST.concat(WEBSITE_VERIFY_ID);
    GET_REQUEST.concat("mac=");
    GET_REQUEST.concat(USER_MAC);
    ESP8266_MsgTx2Rx("AT+CIPSEND="+GET_REQUEST.length()+2,200,AT_CIPSEND_ACCESS);
    if(ESP8266_RETURN==1){
      //开SOCKET成功
      Serial.println(F("ESP8266_PHP_CONNECTED(GET_VERIFY)"));
      progress_state=PHP_CONNECTED;
      //使用GET指令
      Serial.println(F("GET"));
      ESP8266_MsgTx2Rx(GET_REQUEST,200,AT_GET_REQUEST);
      if(ESP8266_RETURN==1){
        //发送成功,准备接收返回值
        Serial.println(F("ESP8266_PHP_SEND_SUCCESS(GET_VERIFY)"));
        String AT_GET_MSG_RETURN=ESP8266_Rx(1000,AT_GET_MSG[0],AT_GET_MSG[1]);
        if(AT_GET_MSG_RETURN.equals(F("none"))){//被解除配对
          Serial.println(F("ESP8266_BIND_LOST(GET_VERIFY)"));
          progress_state=INTERLIZING_HC05;
          String AT_GET_REQUEST_RETURN=ESP8266_MsgTx2Rx("AT+CWQAP",200,AT_CWQAP_REQUEST,false);
          //清零数据
          for(int i=0;i<400;i++){
            EEPROM.write(i,0);
          }
          USER_ID="";
          USER_MAC="";
          WIFI_SSID="";
          WIFI_BSSID="";
          WIFI_PASSWORD="";
        }
        else{//用户身份校验成功,获得USER_ID,手动关闭连接
          SUCCESS_GET_VERIFY=true;
          String AT_CIPCLOSE_RETURN=ESP8266_MsgTx2Rx(F("AT+CIPCLOSE"),200,AT_CIPCLOSE,false);
          Serial.print(F("ESP8266_VERIFY_SUCCESS(GET_VERIFY):"));
          Serial.println(USER_ID);
          USER_ID=AT_GET_MSG_RETURN.substring(AT_GET_MSG_RETURN.indexOf(F("<USERID>"))+8,AT_GET_MSG_RETURN.indexOf(F("</USERID>")));
        }
      }
      else{
        //发送失败,SOCKET关闭
        Serial.print(F("ESP8266_PHP_SEND_FAIL(GET_VERIFY): "));
        Serial.println(ESP8266_RETURN);
        progress_state=WIFI_CONNECTED;
      }
    }
    else{
      //开SOCKET失败
      Serial.print(F("ESP8266_PHP_CONNECTION_FAIL(GET_VERIFY):"));
      Serial.println(ESP8266_RETURN);
      progress_state=WIFI_CONNECTED;
    }
  }
  else{//新配对的情况
    Serial.println(F("ESP8266_BIND_NEW_GOT(GET_VERIFY)"));
    SUCCESS_GET_VERIFY=true;
  }  
  if(SUCCESS_GET_VERIFY){//进入常规发送数据
    //采集数据环节
    MQ135_RAW_VALUE=analogRead(MQ135);
    MQ136_RAW_VALUE=analogRead(MQ136);
    //连接服务器环节
    char*Temp_Buffer;
    Temp_Buffer=new char[50];
    strcpy_P(Temp_Buffer, (char*)pgm_read_word(&(WEBSITE_SEND_DATA)));
    String GET_REQUEST="GET ";
    GET_REQUEST.concat(Temp_Buffer);
    GET_REQUEST.concat("type=toilet&user_id="+USER_ID+"&mq135_raw="+MQ135_RAW_VALUE+"&mq136_raw="+MQ136_RAW_VALUE);
    delete(Temp_Buffer);
    ESP8266_MsgTx2Rx("AT+CIPSEND="+GET_REQUEST.length()+2,200,AT_CIPSEND_ACCESS);
    if(ESP8266_RETURN==1){
      //开SOCKET成功
      Serial.println(F("ESP8266_PHP_CONNECTED(SEND_DATA)"));
      progress_state=PHP_CONNECTED;
      //使用GET指令
      ESP8266_MsgTx2Rx(GET_REQUEST,200,AT_GET_REQUEST);
      if(ESP8266_RETURN==1){
         //发送成功,准备接收返回值
         Serial.println(F("ESP8266_PHP_SEND_SUCCESS(SEND_DATA)"));
         String AT_GET_MSG_RETURN=ESP8266_Rx(1000,AT_GET_MSG[0],AT_GET_MSG[1]);
         if(AT_GET_MSG_RETURN.equals("error")){//被解除配对
            Serial.println(F("ESP8266_BIND_LOST(SEND_DATA)"));
            progress_state=INTERLIZING_HC05;
            String AT_GET_REQUEST_RETURN=ESP8266_MsgTx2Rx(F("AT+CWQAP"),200,AT_CWQAP_REQUEST,false);
            //清零数据
            for(int i=0;i<400;i++){
              EEPROM.write(i,0);
            }
            USER_ID="";
            USER_MAC="";
            WIFI_SSID="";
            WIFI_BSSID="";
            WIFI_PASSWORD="";
         }
         else{//常规发送数据成功,获得下次发送时间,手动关闭连接
            String AT_CIPCLOSE_RETURN=ESP8266_MsgTx2Rx(F("AT+CIPCLOSE"),200,AT_CIPCLOSE,false);
            int DELAY=atoi(AT_GET_MSG_RETURN.substring(AT_GET_MSG_RETURN.indexOf(F("<DALAY>"))+7,AT_GET_MSG_RETURN.indexOf(F("</DALAY>"))).c_str());
            Serial.print(F("ESP8266_SEND_DATA_SUCCESS(SEND_DATA),NEXT_SEND_DELAY"));
            Serial.println(DELAY);
            delay(DELAY);
         }
      }
      else{
        //发送失败,SOCKET关闭
        Serial.print(F("ESP8266_PHP_SEND_FAIL(SEND_DATA): "));
        Serial.println(ESP8266_RETURN);
        progress_state=WIFI_CONNECTED;
      }
    }
    else{
       //发送失败,SOCKET关闭
       Serial.print(F("ESP8266_PHP_CONNECTION_FAIL(SEND_DATA): "));
       Serial.println(ESP8266_RETURN);
       progress_state=WIFI_CONNECTED;
    }
  }
}

void loop() {
  switch(progress_state){
    //验证有无绑定WIFI
    case INTERLIZING_ESP8266:
    {
      Interlizing_Esp8266();
      Serial.print(F("Free:"));
      Serial.println(freeMemory());
      break;
    }
    case INTERLIZING_HC05:
    {
      Interlizing_Hc05();
      Serial.print(F("Free:"));
      Serial.println(freeMemory());
      break;
    }
    case WIFI_CONNECTING:
    {
      Wifi_Connecting();
      Serial.print(F("Free:"));
      Serial.println(freeMemory());
      break;
    }
    case WIFI_CONNECTED:
    {
      Wifi_Connected();
      Serial.print(F("Free:"));
      Serial.println(freeMemory());
      break;
    }
    case PHP_CONNECTED:
    {
      Php_Connected();
      Serial.print(F("Free:"));
      Serial.println(freeMemory());
      break;
    }
    default:break;
  };
}

在线等大神帮助!
不用MemoryFree.h试一下
发新帖
发表评论
高级模式  
您需要登录后才可以回帖 登录 | 立即注册  
关闭

推荐主题 上一条 /2 下一条