Exemplo n.º 1
0
bool wificmd(String cmd, String verify) {
    ESPserial.println("AT+"+cmd);
    if(verify != "") {
        if(ESPserial.find("OK")) {
            return true;
        }
        return false;
    }
    return true;
}
Exemplo n.º 2
0
int getState () {
    if( !wificmd("CIPSTART=\"TCP\",\"example.com\",8080", "OK")) { //start a TCP connection.
        return 0;
    }
    delay(1000);
    String getRequest = "STATE";

    ESPserial.print("AT+CIPSEND=");
    ESPserial.println(getRequest.length());
    delay(500);

    if(ESPserial.find(">")) {
        clearWifi();
        //Serial.println("Sending..");
        ESPserial.print(getRequest);

        if( ESPserial.find("SEND OK")) {
            //Serial.println("Packet sent");
            String tmpResp = "";
            while (ESPserial.available()) {
                tmpResp = ESPserial.readString();
            }
            // close the connection
            if(wificmd("CIPCLOSE", "OK")) {
                if (tmpResp.substring(11) == "1") {
                    return 1;
                } else {
                    return 0;
                }
            } else {
                Serial.println("Error closing connection");
            }
        } else {
            Serial.println("Error sending data");
        }
    }
    return 0;
}
void loop()
{
  if(esp8266.available()) // check if the esp is sending a message 
  {
    /*
    while(esp8266.available())
    {
      // The esp has data so display its output to the serial window 
      char c = esp8266.read(); // read the next character.
      Serial.write(c);
    } */
    
    if(esp8266.find("+IPD,"))
    {
     delay(1000);
 
     int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
     
     String webpage = "<h1>Hello</h1>&lth2>World!</h2><button>LED1</button>";
 
     String cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipsend,1000,DEBUG);
     sendData(webpage,1000,DEBUG);
     
     webpage="<button>LED2</button>";
     
     cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipsend,1000,DEBUG);
     sendData(webpage,1000,DEBUG);
 
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";
     
     sendData(closeCommand,3000,DEBUG);
    }
  }
}