ESP01无法连接Android应用

时间:2019-04-20 07:34:24

标签: android arduino

esp01无法接收到来自Android应用的打开/关闭LED的请求,即使发送了请求,响应代码也为null。重新连接arduino系统时,所有先前失败的请求都会立即显示在串行监视器上。之前它可以正常工作,但之前也有一个延迟。我在下面附加了arduino和android代码。请帮忙。

Arduino代码-

Building wheels for collected packages: gym-retro
  Building wheel for gym-retro (setup.py): started
  Building wheel for gym-retro (setup.py): finished with status 'error'
  Complete output from command c:\users\evgeny\appdata\local\programs\python\python37-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\Evgeny\\AppData\\Local\\Temp\\pip-install-t3ctdvi3\\gym-retro\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d C:\Users\Evgeny\AppData\Local\Temp\pip-wheel-q1w5imfc --python-tag cp37:
  use_scm_version False
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win32-3.7
  creating build\lib.win32-3.7\retro
  copying retro\enums.py -> build\lib.win32-3.7\retro
  copying retro\retro_env.py -> build\lib.win32-3.7\retro
  copying retro\__init__.py -> build\lib.win32-3.7\retro
  creating build\lib.win32-3.7\retro\data
  copying retro\data\__init__.py -> build\lib.win32-3.7\retro\data
  creating build\lib.win32-3.7\retro\data\stable
  copying retro\data\stable\__init__.py -> build\lib.win32-3.7\retro\data\stable

</skipped lots of text here/>

    running build_ext
    -- Submodules seem to be missing. Attempting to clone now.
    CMake Error at CMakeLists.txt:13 (message):
      Failed to check out submodules.  Aborting.


    -- Configuring incomplete, errors occurred!
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Evgeny\AppData\Local\Temp\pip-install-t3ctdvi3\gym-retro\setup.py", line 92, in <module>
        **kwargs
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\site-packages\setuptools\__init__.py", line 131, in setup
        return distutils.core.setup(**attrs)
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\core.py", line 148, in setup
        dist.run_commands()
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\dist.py", line 966, in run_commands
        self.run_command(cmd)
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\site-packages\setuptools\command\install.py", line 61, in run
        return orig.install.run(self)
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\command\install.py", line 545, in run
        self.run_command('build')
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\command\build.py", line 135, in run
        self.run_command(cmd_name)
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "C:\Users\Evgeny\AppData\Local\Temp\pip-install-t3ctdvi3\gym-retro\setup.py", line 53, in run
        subprocess.check_call([cmake_exe, '.', '-G', 'Unix Makefiles', build_type, pyext_suffix, pylib_dir, python_executable])
      File "c:\users\evgeny\appdata\local\programs\python\python37-32\lib\subprocess.py", line 341, in check_call
        raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command '['C:\\Users\\Evgeny\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\cmake.exe', '.', '-G', 'Unix Makefiles', '', '-DPYEXT_SUFFIX:STRING=.cp37-win32.pyd', '-DPYLIB_DIRECTORY:PATH=build\\lib.win32-3.7', '-DPYTHON_EXECUTABLE:STRING=c:\\users\\evgeny\\appdata\\local\\programs\\python\\python37-32\\python.exe']' returned non-zero exit status 1.

Android代码

#include <SoftwareSerial.h>

#define DEBUG true
int led =11;
int ldr = A0;
int threshold=300;
int connectionId;
boolean iotLogic = true;
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
{
  Serial.begin(115200);
  esp8266.begin(115200); // your esp's baud rate might be different

  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);

  sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
  sendCommand("AT+CWMODE=3\r\n",2000,DEBUG); // configure as access point
  sendCommand("AT+CWJAP=\"PKS\",\"91101132\"\r\n",2000,DEBUG);
  delay(3000);
  sendCommand("AT+CIFSR\r\n",2000,DEBUG); // get ip address
  sendCommand("AT+CIPMUX=1\r\n",2000,DEBUG); // configure for multiple connections
  sendCommand("AT+CIPSERVER=1,80\r\n",2000,DEBUG); // turn on server on port 80

  Serial.println("Server Ready");
}

void loop()
{
  int sensorval=analogRead(ldr);
  if(esp8266.available()) // check if the esp is sending a message 
  {
    if(esp8266.find("+IPD,"))
    {
     delay(1000); // wait for the serial buffer to fill up (read all the serial data)
     // get the connection id so that we can then disconnect
     connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48   
    if(esp8266.find("IOT=")){// advance cursor to "pin="
     int iot= esp8266.read()-48;
     Serial.println(iot);
      if(iot==1){
        iotLogic=true;  
      }
      else if(iot==0){
        iotLogic=false;
      }
    }

    if(iotLogic){
      if(sensorval>threshold){
        digitalWrite(led,LOW);
      }
      else{
        digitalWrite(led,HIGH);
      }
    }
    else{
      esp8266.find("pin=");
      int val=esp8266.read()-48;
      Serial.println(val);
      if(val==1){
        digitalWrite(led,HIGH);

      }
      else{
        digitalWrite(led,LOW);
      }
    }


     // build string that is send back to device that is requesting pin toggle
     String content;
     content = "Pin ";
     content += led;
     content += " is ";

     if(digitalRead(led))
     {
       content += "ON";
     }
     else
     {
       content += "OFF";
     }

     sendHTTPResponse(connectionId,content);

     // make close command
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";

     sendCommand(closeCommand,1000,DEBUG); // close connection
    }
  }
}

/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
    String response = "";

    int dataSize = command.length();
    char data[dataSize];
    command.toCharArray(data,dataSize);

    esp8266.write(data,dataSize); // send the read character to the esp8266
    if(debug)
    {
      Serial.println("\r\n====== HTTP Response From Arduino ======");
      Serial.write(data,dataSize);
      Serial.println("\r\n========================================");
    }

    long int time = millis();

    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {

        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }

    if(debug)
    {
      Serial.println(response);
    }

    return response;
}

/*
* Name: sendHTTPResponse
* Description: Function that sends HTTP 200, HTML UTF-8 response
*/
void sendHTTPResponse(int connectionId, String content)
{

     // build HTTP response
     String httpResponse;
     String httpHeader;
     // HTTP Header
     httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n"; 
     httpHeader += "Content-Length: ";
     httpHeader += content.length();
     httpHeader += "\r\n";
     httpHeader +="Connection: keep-alive\r\n\r\n";
     httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
     sendCIPData(connectionId,httpResponse);
}

/*
* Name: sendCIPDATA
* Description: sends a CIPSEND=<connectionId>,<data> command
*
*/
void sendCIPData(int connectionId, String data)
{
   String cipSend = "AT+CIPSEND=";
   cipSend += connectionId;
   cipSend += ",";
   cipSend +=data.length();
   cipSend +="\r\n";
   sendCommand(cipSend,1000,DEBUG);
   sendData(data,1000,DEBUG);
}

/*
* Name: sendCommand
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendCommand(String command, const int timeout, boolean debug)
{
    String response = "";

    esp8266.print(command); // send the read character to the esp8266

    long int time = millis();

    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {

        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }

    if(debug)
    {
      Serial.print(response);
    }

    return response;
}

0 个答案:

没有答案
相关问题