ESP-01至ESP-01,使用命令

时间:2019-03-12 07:51:11

标签: esp8266 at-command

我正在尝试通过ESP-01进行两个Arduino的对话,它们仅需要发送简单的数字数据,但发送速率约为每秒10次。
为此,我不想使用任何库,所以我只想使用AT命令。
服务器工作正常,但是(我可以连接手机),但是第二个Arduino无法读取数据。
到目前为止,我有以下代码:

这是我的“服务器”代码

byte ledstatus = 0;

byte connectionId;

long int timeout = 2000; //aantal msec die de controller wacht op een reactie van de ESP01 module alvorens verder te gaan.

#define DEBUG true

void setup() {
  Serial.begin(115200); //Seriële monitor starten

  Serial1.begin(115200); //Seriële communicatie met ESP01 Baud rate standaard 115200

  delay(5000); // This gives enough time to open the monitor

  Serial.println("Starting");

  sendData("AT+RST\r\n", 2000, DEBUG); // reset ESP01 module

  Serial.println("RST");

  sendData("AT+GMR\r\n", 2000, DEBUG); // check de versie info van de firmware in de ESP01 module

  Serial.println("GMR");

  sendData("AT+CWMODE_CUR=2\r\n", 1000, DEBUG); //2= SoftAP mode

  Serial.println("CWMODE_CUR");

  sendData("AT+CWSAP_CUR=\"TimJager_GIP\",\"SuperPassword\",5,4\r\n", 10000, DEBUG); //SSID (naam SoftAP) aanmaken, WIFI wachtwoord, kanaal 5 en beveiliging WPA & WPA2

  Serial.println("CWSAP_CUR");

  sendData("AT+CWDHCP_CUR=0,1\r\n", 1000, DEBUG); //enable DHCP in SoftAP en client mode

  Serial.println("CWDHCP_CUR");

  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections

  Serial.println("CIPMUX");

  sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 80, werkt alleen als CIPMUX=1

  Serial.println("CIPSERVER");

  sendData("AT+CIPAPMAC_CUR?\r\n", 1000, DEBUG); //wat is het mac adres van de esp?

  Serial.println("CIPAPMAC_CUR");

  sendData("AT+CIPAP_CUR?\r\n", 1000, DEBUG); //toon huidig IP-adres van de ESP

  Serial.println("CIPAP_CUR");

  sendData("AT+CWLIF\r\n", 1000, DEBUG); //toon verbonden clients

  Serial.println("CWLIF");

}

float readAndMapAnalog(uint8_t pin, float in_min, float in_max, float out_min, float out_max) {
  return (analogRead(pin) - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void loop() {

  if (Serial1.available()) { //If there is incoming data from web browser to toggle led
    if (Serial1.find("+IPD,")) { //If the incoming data from ISP01 has the instruction "IPD" - form incoming message: (+CIPMUX=1)+IPD,<link ID>,<len>[,<remote IP>,<remote port>]:<data>
      Serial.println("Request ok\nRecieving data from web browser"); // display this on serial monitor

      connectionId = Serial.parseInt(); //get the link id. paseInt converts characters to numbers. We need the link ID to be able to close the correct connection

      Serial1.flush(); // Waits for the transmission of outgoing serial data to complete.

      Serial.println("HTML verzenden"); // display this on serial monitor

      //send data from BBAVR & ESP01 (Server side) to the webbrowser (Client side)

      int measurement = readAndMapAnalog(A0, 0, 1023, 0, 1023); // Get the value of the flex sensor and show it on the page

      int contentLength = String(measurement).length(); // we need to also send the data lenght that we calculate here

      //response http header

      espsend("HTTP/1.0 200 OK \r\nContent-Length:" + (String)contentLength + "\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\n\r\n");

      espsend(String(measurement));

      //close connection

      String closeCommand = "AT+CIPCLOSE="; //close the socket connection esp command

      closeCommand += connectionId; // append connection id

      closeCommand += "\r\n";

      sendData(closeCommand, 1000, DEBUG); //timeout was standaard 3000

    }

  }

}

//FUNCTION : Send data from ESP01 to a client (webbrowser)

void espsend(String d)

{

  String cipSend = " AT+CIPSEND="; // ESP01 Message format: "AT+CIPSEND=<link ID>,<length>"

  cipSend += connectionId;

  cipSend += ",";

  cipSend += d.length(); // get the length of string d

  cipSend += "\r\n"; // begin new line

  sendData(cipSend, 500, DEBUG); // Timeout was standaard 1000

  sendData(d, 500, DEBUG); // timeout was standaard 1000

}

//FUNCTION : Send data from BBAVR to ESP01 and wait certain time for a response. Display response on serial monitor.

void sendData(String command, const int timeout, boolean debug)

{

  // FIRST we send the command to the ESP01 module with these lines

  String response = ""; // declare a string with the name response and empty it.

  Serial1.flush(); // Waits for the transmission of outgoing serial data to complete.

  Serial1.print(command); // send the command to the ESP01 module

  long int timenow = millis(); // get teh number of msec since the start of the program and put it in var "timenow"

  byte end = 0;

  // SECOND we wait for a response message of the ESP01 module

  while ( (timenow + timeout) > millis()) // stay in this loop for a number of msec set bij 'timeout' value at the start of the program

  {

    while (Serial1.available()) // loop as long as there is data in the serial receive buffer

    {

      char c = Serial1.read(); // read the next character.

      response += c; // add the last character to the response string

    }

  }

  if (debug) // if debug is set to 1 - the response is printed on the serial monitor

  {

    Serial.print(response); //displays the esp response messages in arduino Serial monitor
    delay(100);
  }
}

另外,我将此作为客户端代码:

// ESP stuff
byte connectionId; // The id of the WiFi connection
//long int timeout = 2000; // The ammount of ms that the µC will wait for a reaction from the ESP-01 module before it will continue
#define DEBUG true

#define WifiAP 2 //1: thuis //2: school //3: preventie

#define firmware 2 //1:before 2017-05 //2:from 2017-05, for connecting to the wifi network

#if WifiAP==2

String ssid = "TimJager_GIP"; // Hoofdlettergevoelig, SSID van het Wifi netwerk waarin de module nu zit

String password = "SuperPassword"; // Hoofdlettergevoelig, paswoord van het huidige Wifi netwerk

#endif

String server = "192.168.4.1"; // moet correct zijn voor onze server

String uri = "/jq/status3.php"; // moet correct zijn voor onze server

String data; // in deze string wordt het te verzenden bericht opgebouwd

String response; // in deze string komt het bericht dat je ontvangt van de server

const int timeout = 5000; //timeout http request - als we niets vernemen binnen deze tijd gaan we door

unsigned int VAR1 = 0; // variabele 1 die wordt verzonden naar de cloudserver

unsigned int VAR2 = 0; // variabele 2 die wordt verzonden naar de cloudserver

void setup()

{

  Serial.begin(115200);       // Start the serial communication

  Serial1.begin(115200);      // Serial communication with the ESP-01 (Baud rate standaard 115200)

  /*while (Serial.available() == 0); // Wait for data
  while (Serial.available() != 0) Serial.read(); // Clear the buffer*/

  Serial.println("Starting ESP");

  sendData("AT+RST\r\n", 2000, DEBUG); // Reset ESP01 module

  Serial.println("RST");

  sendData("AT+GMR\r\n", 2000, DEBUG); // Check the version of the firmware in the ESP-01 module

  Serial.println("GMR");

  /*sendData("\r\n", 2000, DEBUG); // Check the version of the firmware in the ESP-01 module

  Serial.println("");*/

  sendData("AT+CWMODE_CUR=3\r\n", 2000, DEBUG); // Check the version of the firmware in the ESP-01 module

  Serial.println("AT+CWMODE_CUR=3");

  sendData("AT+CWJAP_CUR=\"" + ssid + "\",\"" + password + "\"\r\n", 20000, DEBUG); // Check the version of the firmware in the ESP-01 module

  Serial.println("AT+CWJAP_CUR=\"" + ssid + "\",\"" + password + "\"");

  sendData("AT+CIFSR\r\n", 20000, DEBUG); // Check the version of the firmware in the ESP-01 module

  Serial.println("AT+CIFSR");

  sendData("AT+CIPSTART=\"TCP\",\"" + server + "\",80\r\n", 20000, DEBUG); // Check the version of the firmware in the ESP-01 module

  Serial.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80\r\n");

  sendData("AT+CIPSEND=4\r\n", 20000, DEBUG); // Check the version of the firmware in the ESP-01 module

  Serial.println(">test");

  /*sendData("\r\n", 2000, DEBUG); // Check the version of the firmware in the ESP-01 module

    Serial.println("");

    sendData("\r\n", 2000, DEBUG); // Check the version of the firmware in the ESP-01 module

    Serial.println("");

    sendData("\r\n", 2000, DEBUG); // Check the version of the firmware in the ESP-01 module

    Serial.println("");*/
}

void loop() {

}

//FUNCTION : Send data from ESP01 to a client (webbrowser)

void espsend(String d) {

  String cipSend = " AT+CIPSEND="; // ESP01 Message format: "AT+CIPSEND=<link ID>,<length>"

  cipSend += connectionId;

  cipSend += ",";

  cipSend += d.length(); // Get the length of string d

  cipSend += "\r\n"; // Begin new line

  sendData(cipSend, 500, DEBUG); // Timeout was standaard 1000

  sendData(d, 500, DEBUG); // Timeout was standaard 1000

}

//FUNCTION : Send data from BBAVR to ESP01 and wait certain time for a response. Display response on serial monitor.

void sendData(String command, const int timeout, boolean debug) {

  // FIRST we send the command to the ESP01 module with these lines

  String response = ""; // Declare a string with the name response and empty it.

  Serial1.flush(); // Waits for the transmission of outgoing serial data to complete.

  Serial1.print(command); // Send the command to the ESP01 module

  long int timenow = millis(); // Get teh number of msec since the start of the program and put it in var "timenow"

  byte end = 0;

  // SECOND we wait for a response message of the ESP01 module

  while ( (timenow + timeout) > millis()) // Stay in this loop for a number of msec set bij 'timeout' value at the start of the program

  {

    while (Serial1.available()) // Loop as long as there is data in the serial receive buffer

    {

      char c = Serial1.read(); // Read the next character.

      response += c; // Add the last character to the response string

    }

  }

  if (debug) // If debug is set to 1 - the response is printed on the serial monitor

  {

    Serial.print(response); // Displays the esp response messages in arduino Serial monitor
    delay(100);
  }
}

很抱歉,如果代码中有任何荷兰注释,我用英语替换了其中的大多数注释,但仍有一些矿石仍是荷兰注释。

0 个答案:

没有答案