乐鑫技术交流
直播中

一曲作罢

9年用户 853经验值
擅长:可编程逻辑 电源/新能源 MEMS/传感技术
私信 关注
[问答]

ESP8266不与互联网通信是为什么?

我的原始帖子在我编辑后消失了,所以在这里我再次输入所有内容......
  我没有真正的 Arduino 或 ESP,我正在使用 tinkerCAD 并且在那个设置中,我有一个 Arduino Uno R3 连接到一个 LCD,一个温度传感器、一个按钮和一个 ESP8266,用于将温度读数发送到 ThingSpeak 物联网云。
Arduino 的
连接到 ESP8266 并以 115200 波特进行通信。我正在使用 NMOSFET (2N7000) 电平转换器将 Arduino 的 5V TX 转换为 ESP 的 3.3V RX。
代码:全选Arduino   ESP8266
GND         GND
                +3.3V from power supply, not the Arduino 3.3V output (current restrictions)
RX (1)      TX
TX (2)      RX via NMOS level shifter
                Enable  to 3.3V supply
                Reset    to 3.3V supply


  软件初始化 ESP,将其设置为站模式,连接到 WiFi。状态会在 LCD 上更新,这样我就知道什么时候出了问题。然后实际处理包括:
1. 读取温度传感器
2. 打开与 ThingSpeak 云的单个 TCP 连接
3. 发送 GET 请求以更新我的 ThingSpeak 数据通道
4. 关闭与 ThingSpeak 的 TCP 连接除了#3 之外
的所有步骤都有效正确。基本上我发送了 AT+CIPSEND= 并且我应该从 ESP 收到一个“>”但是它从来没有发生过,它实际上没有对 CIPSEND 做出任何回应。之后,我提交了实际的 GET 请求,但我也没有收到 ESP 的回复。

我还尝试使用没有长度的 CIPSEND 并使用 CTRL+Z 终止 GET 数据包,但也没有结果。
我还尝试在 CIPSEND=length 之后不读取 ESP 响应,而是在延迟后简单地拍摄 GET,但也没有,我的数据包永远无法访问 Internet。
我还尝试使用 Webhook.site 查看我的请求是否到达那里(尽管我的 ThingSpeak 数据是正确的)但也没有。
那么,我错过了什么?我已经检查并使用了很多代码,但似乎没有任何效果。已经花了整整两天尝试一切都无济于事。此处
相关部分。您可能会注意到一些设置 LCD 状态指示器的代码,可以给我即时的视觉反馈(我没有辅助串行终端)。


代码:全选// ------------------- ESP8266 -----------------

/*
*   Sets up the ESP8266, sets it to Station Mode and Connects WiFi AP
*   REF: https://room-15.github.io/blog/2015/03/26/esp8266-at-command-reference/
*/
void setupESP8266()                                   
{
     lcdWiFiStatus(false);
    if(DEBUG){
      lcdBootStatus("E?");
    }
    esp8266Module.flush();
    esp8266Module.println(F("AT+RST"));
    delay(7000);

    if (esp8266Module.find("OK"))
    {
      if(DEBUG){
        PASSED('E');
      }  
      esp8266Module.flush();
      changeStationMode();       // put ESP8266 in Station Mode
      delay(5000);
      esp8266Module.flush();
      connectToWiFi();               // connect to WiFi network
    }
    else
    {
      if(DEBUG){
        FAIL('E');
      }
    }
}

/*
*   Sets ESP8266 to station mode
*/
bool changeStationMode()
{
    esp8266Module.println(F("AT+CWMODE=1")); // 1=Station, 2=AP, 3=AP+Station
    if (esp8266Module.find("OK"))
    {
      if(DEBUG){
        PASSED('S');   // switched to Station Mode
      }  
      return true;
    }
    else if(esp8266Module.find("NO CHANGE")){
      if(DEBUG){
        PASSED('S');
      }  
      return true;
    }
    else
    {
      if(DEBUG){
        FAIL('S');   // error switching to Station Mode
      }  
      return false;
    }
}

/*
*  Connects ESP8266 to wifi access point
*/
bool connectToWiFi()
{
  bool connected;
  if(DEBUG){
    lcdBootStatus("W?");
  }  
  String cmd = F("AT+CWJAP="");
  cmd += network;
  cmd += F("","");
  cmd += password;
  cmd += F(""");
  esp8266Module.println(cmd);
  delay(15000);
  
  if (esp8266Module.find("OK"))
  {
    if(DEBUG){
      PASSED('W');   // successful connection to WiFi AP
    }
    connected = true;
  }
  else
  {
    if(DEBUG){
      FAIL('W');   // Could not connect to Access Point
    }  
    connected = false;
  }
  
  lcdWiFiStatus(connected);
}

/*
*   Send data over WiFi to the Internet.
*   Display the Transmit (arrow up) icon during sent.
*   Blink the Receive icon (arrow down) while waiting for response.
*   NOTE: THIS IS NOT WORKING PROPERLY - NO RESPONSES FROM ESP8266
*/
String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  lcdSerialStatus(CHR_TX);      // LCD indicator for Tx
  esp8266Module.print(command); // send the request to the esp8266
  long int time = millis();
  const int BLINK_RELOAD = 100;
  int blink = BLINK_RELOAD;
  bool blinkOn = true;
  
  lcdSerialStatus(CHR_RX);
  while ( (time + timeout) > millis()) {
    while (esp8266Module.available()) {
      // output to the serial window
      char c = esp8266Module.read(); // read the next character.
      response += c;
    }
         
    if (--blink == 0)
    {
      blink = BLINK_RELOAD;
      if (blinkOn) {
        lcdSerialStatus(CHR_NONE);
      } else {
        lcdSerialStatus(CHR_RX);
      }
      blinkOn = !blinkOn;
    }
  }
  
  lcdSerialStatus(CHR_NONE);
  return response;
}

// ------------------- /ESP8266 -----------------
// -------------------  GENERAL -----------------
bool openTcpConnection(String host, int port)
{
  String openCmd = "AT+CIPSTART="TCP","" + host + ""," + port;
  esp8266Module.println(openCmd);
  delay(5000);        // Wait a little for the ESP to respond

  // response should have been CONNECT
  bool error = esp8266Module.find("Error");
   
  return !error;
}

// ------------------- /GENERAL -----------------
// ------------------- THINGSPEAK -----------------

/*
*   Open connection to Cloud over WiFi to ThingSpeak API
*/
bool openCloudConnection()
{
  String openCmd = "AT+CIPSTART="TCP","" + cfgIoT.Host
                + ""," + cfgIoT.HttpPort;
  esp8266Module.println(openCmd);
  delay(5000);        // Wait a little for the ESP to respond

  // response should have been CONNECT
  bool error = esp8266Module.find("Error");
   
  return !error;
}

/*
*   Close connection to Cloud service over WiFi
*   http://www.pridopia.co.uk/pi-doc/ESP8266ATCommandsSet.pdf
*/
void closeCloudConnection()
{   // for multiple connections AT+CIPCLOSE=
   esp8266Module.println("AT+CIPCLOSE");
     delay(50);   // response CLOSEDrnOK
    lcdCloudStatus(false);
}


int thingSpeakApiUpdateGet(int temperature)
{  
  int retCode = 0;
  //openTcpConnection("46.4.105.116", 80);
  //sendRequest("GET /a3355acb-5c03-49b8-92a9-8b4610a12a54 HTTP/1.1","webhook.site");
   
  // send request
  lcdSerialStatus(CHR_TX);
  String req;
  req = "GET /update?api_key=" + MY_CHANNEL_WRITE_API_KEY
       + "&field1=" + String(temperature, DEC)
       + " HTTP/1.1rnHost: " + "api.thingspeak.com" + "rnrn";
  
  String cipSend = "AT+CIPSEND=";
  //cipSend += 4;      // connection ID if CMUX=1 we aren't using that
  //cipSend += ", ";
  cipSend += String(req.length());
  cipSend += "rn";
  String rsp = sendData(cipSend, 1000, DEBUG); // should receive > but it never does!
  lcdPrintAt(0,0,rsp);
  
  rsp = sendData(req, 10000, DEBUG);
  lcdPrintAt(0,0,rsp);
  lcdSerialStatus(CHR_NONE);
  
  return retCode;
}



更多回帖

发帖
×
20
完善资料,
赚取积分