ESP32的Serial2没有响应(NEO 6M GPS) [英] Serial2 of ESP32 not responding( NEO 6M GPS)

查看:213
本文介绍了ESP32的Serial2没有响应(NEO 6M GPS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个自动驾驶汽车项目,我有一个NEO 6M GPS模块,我在使用ESp32作为开发板,该模块与Arduino和Nodemcu可以很好地工作。但不支持ESP32,原因是它不支持软件序列,因此我从 https://www.youtube.com/watch?v=GwShqW39jlE&feature=emb_title

I am doing an Autonomous Car project and I have a NEO 6M GPS module, I am using an ESp32 as the board, the module works fine with Arduino and Nodemcu. but not with ESP32, the reason being it not supporting software serial, so I took help from https://www.youtube.com/watch?v=GwShqW39jlE&feature=emb_title

我添加了硬件序列,但仍然没有输出我提供了以下代码

I added hardware serial but still, there is no output I have provided the code below

#include<HardwareSerial.h>//No extra libray installed
#define RXD2 16
#define TXD2 17

HardwareSerial gps_serial(2);

void setup() {
  Serial.begin(115200);
  Serial.printf("LETS START");
 gps_serial.begin(115200, SERIAL_8N1, RXD2, TXD2);
}

void loop() {
  while (gps_serial.available()) {
   Serial.print(char(gps_serial.read()));  // read from gps, write to serial debug port
  Serial.print("I am here");
  }
}

串行监视器上的输出为 LETS START
下面还提供了在其他板上均可使用的其他代码,这些代码经过编辑后可在ESP32中使用

The output on the serial monitor is LETS START The other code which worked on other boards is also provided below, edited to work in ESP32

#include <WiFi.h>
//#include <WiFiClient.h>
#include <WiFiServer.h>

#include <TinyGPS++.h> // library for GPS module
//#include <SoftwareSerial.h>
//#include <ESP8266WiFi.h>
TinyGPSPlus gps;  // The TinyGPS++ object
//SoftwareSerial ss(4, 5); // The serial connection to the GPS device
HardwareSerial Serial2(2);


const char* ssid = "***********"; //ssid of your wifi
const char* password = "***********"; //password of your wifi
float latitude , longitude;
int year , month , date, hour , minute , second;
String date_str , time_str , lat_str , lng_str;
int pm;
WiFiServer server(80);

void setup()
{  Serial2.begin(115200);
  Serial.begin(115200);
  //ss.begin(9600);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password); //connecting to wifi
  while (WiFi.status() != WL_CONNECTED)// while wifi not connected
  {
    delay(500);
    Serial.print("."); //print "...."
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());  // Print the IP address
}


void loop()
{
  while (Serial2.available() > 0) //while data is available
    if (gps.encode(Serial2.read())) //read gps data
    {
      if (gps.location.isValid()) //check whether gps location is valid
      {
        latitude = gps.location.lat();
        lat_str = String(latitude , 6); // latitude location is stored in a string
        longitude = gps.location.lng();
        lng_str = String(longitude , 6); //longitude location is stored in a string
      }
      if (gps.date.isValid()) //check whether gps date is valid
      {
        date_str = "";
        date = gps.date.day();
        month = gps.date.month();
        year = gps.date.year();
        if (date < 10)
          date_str = '0';
        date_str += String(date);// values of date,month and year are stored in a string
        date_str += " / ";

        if (month < 10)
          date_str += '0';
        date_str += String(month); // values of date,month and year are stored in a string
        date_str += " / ";
        if (year < 10)
          date_str += '0';
        date_str += String(year); // values of date,month and year are stored in a string
      }
      if (gps.time.isValid())  //check whether gps time is valid
      {
        time_str = "";
        hour = gps.time.hour();
        minute = gps.time.minute();
        second = gps.time.second();
        minute = (minute + 30); // converting to IST
        if (minute > 59)
        {
          minute = minute - 60;
          hour = hour + 1;
        }
        hour = (hour + 5) ;
        if (hour > 23)
          hour = hour - 24;   // converting to IST
        if (hour >= 12)  // checking whether AM or PM
          pm = 1;
        else
          pm = 0;
        hour = hour % 12;
        if (hour < 10)
          time_str = '0';
        time_str += String(hour); //values of hour,minute and time are stored in a string
        time_str += " : ";
        if (minute < 10)
          time_str += '0';
        time_str += String(minute); //values of hour,minute and time are stored in a string
        time_str += " : ";
        if (second < 10)
          time_str += '0';
        time_str += String(second); //values of hour,minute and time are stored in a string
        if (pm == 1)
          time_str += " PM ";
        else
          time_str += " AM ";
      }
    }

 WiFiClient client = server.available(); // Check if a client has connected
  if (!client)
  {
    return;
  }
  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n <!DOCTYPE html> <html> <head> <title>GPS DATA</title> <style>";
  s += "a:link {background-color: YELLOW;text-decoration: none;}";
  s += "table, th, td </style> </head> <body> <h1  style=";
  s += "font-size:300%;";
  s += " ALIGN=CENTER> GPS DATA</h1>";
  s += "<p ALIGN=CENTER style=""font-size:150%;""";
  s += "> <b>Location Details</b></p> <table ALIGN=CENTER style=";
  s += "width:50%";
  s += "> <tr> <th>Latitude</th>";
  s += "<td ALIGN=CENTER >";
  s += lat_str;
  s += "</td> </tr> <tr> <th>Longitude</th> <td ALIGN=CENTER >";
  s += lng_str;
  s += "</td> </tr> <tr>  <th>Date</th> <td ALIGN=CENTER >";
  s += date_str;
  s += "</td></tr> <tr> <th>Time</th> <td ALIGN=CENTER >";
  s += time_str;
  s += "</td>  </tr> </table> ";

  s += "</body> </html>";

  client.print(s); // all the values are send to the webpage
  delay(100);
}

我在运行串行监视器时确实获得了IP地址,但是当我访问该地址,该字段为空白。
因此,我了解的是与GPS模块的串行通信无法正常工作。我在网上尝试了许多其他解决方案,但根本没有用。

I do get the IP address on the serial monitor when I run it but when I access that address the fields are blank. So what I understand is that the Serial communication with the GPS module is not working. I tried many other solutions on net but no use at all.

我正在使用ESP32 DevKit V1,30针版本

推荐答案

不知道确切使用哪个ESP32模块,有两个显示挡块


NEO-6M GPS模块需要5 V,ESP32引脚可提供最大3.6V电压-通常为3.3V

一些ESP32模块会阻塞某些引脚,以将其用于SD卡,相机,LCD或其他板上功能。因此,请查看您的esp32开发板规格书。



如果HWSerial2存在,则代码应该正确
如果您使用的ESP32版本低于1.03,则必须定义HWSerial,ESP32 1.04以上,这是没有必要的更多

Not knowing which ESP32 Module you use exactly, there are two show stoppers

NEO-6M GPS Module needs 5 V, ESP32 pins deliver max 3.6V - normaly 3.3V
Some ESP32 modules block certain pins to use them for SD card, camera, lcd or other on board features. So look up the datasheet of your esp32 board variant.

The code should be ok if HWSerial2 exists If you use ESP32 version below 1.03 you have to define HWSerial,
ESP32 1.04 up this is not necessary any more

#define RXD2 16
#define TXD2 17
...
HardwareSerial Serial2(2);

...
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);

,并为GSM模块提供5V电源。

and 5V power is supplied to the GSM module.

编辑2:感谢hcheung问最基本的事情

解决方案是OP在GPS上具有从RX到RX和TX到TX的功能,而不是从RX ESP引脚到RX的功能。
这样问题就解决了

Edit 2: Credit to hcheung asking the most basic thing
The solution was the OP had RX to RX and TX to TX, instead of RX ESP pin to RX on GPS and vice versa. So problem solved

这篇关于ESP32的Serial2没有响应(NEO 6M GPS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆