Arduino获取gps卫星信号时加上delay(1000)gps受到影响-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2873|回复: 2

[未解决] Arduino获取gps卫星信号时加上delay(1000)gps受到影响

[复制链接]
发表于 2021-7-5 23:26 | 显示全部楼层 |阅读模式
现在使用到的硬件是Arduino MKR1000和Arduino MKRGPS Shield两个开发板,需要获取到gps的特定数据并且通过UDP传输到其他端口。在串口监视器中显示创建wifi,gps打印以及UDP传输一切正常。
但是我现在需要loop循环中的程序每一秒在执行一次。 所在在loop循环中加入delay(1000);。然而加Delay之后串口显示器上每秒打印数据,但gps的所有数据都变为0。想请教这是为什么?或者错误出在哪里? 求指点。


#include <WiFiUdp.h>
#include <SPI.h>
#include <WiFi101.h>
//#include <Arduino_MKRGPS.h>
#include <ArduinoNmeaParser.h>
#include "arduino_secrets.h"

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int keyIndex = 0;

int idx = 0;

unsigned int localPort = 2390;

int led =  LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer server(80);

char packetBuffer[255];

float longi;
float lati;
float alti;
int sate;
float hdop;
float geoidal_separation;

WiFiUDP Udp;

void onRmcUpdate(nmea::RmcData const);
void onGgaUpdate(nmea::GgaData const);

ArduinoNmeaParser parser(onRmcUpdate, onGgaUpdate);

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
    while (!Serial)
    {
    ; // wait for serial port to connect. Needed for native USB port only
    }
  Serial.println("Access Point Web Server");

  pinMode(led, OUTPUT);      // set the LED pin mode           


    // check for the presence of the shield:
    if (WiFi.status() == WL_NO_SHIELD)
    {
      Serial.println("WiFi shield not present");
      // don't continue:
      while (true);
     }

  WiFi.config(IPAddress(10, 23, 0, 1));

    // print the network name (SSID);
    Serial.print("Creating access point named: ");
    Serial.println(ssid);

    // Create open network. Change this line if you want to create an WEP network:
    status = WiFi.beginAP(ssid, pass);
    if (status != WL_AP_LISTENING)
    {
      Serial.println("Creating access point failed");
      // don't continue
      while (true);
    }

    delay(10000);

    server.begin();

    Serial.println("Connected to wifi");
    printWiFiStatus();

    Serial.println("\nStarting connection to server...");


  Udp.begin(localPort);
}

void loop()
{  
    if (status != WiFi.status())
  {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED)
    {
      byte remoteMac[6];

      // a device has connected to the AP
      Serial.print("Device connected to AP, MAC address: ");
      WiFi.APClientMacAddress(remoteMac);
      printMacAddress(remoteMac);  //romateMac表示远端是Mac地址。
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }

    WiFiClient client = server.available();   // listen for incoming clients

   delay(1000);   

   while (Serial1.available()) {
    parser.encode((char)Serial1.read());
  }

      Serial.print("Longitude: ");
      Serial.println(longi, 7);

      Serial.print("Latitude: ");
      Serial.println(lati, 7);

      Serial.print("Altitude: ");
      Serial.println(alti);

      Serial.print("Number of satellites: ");
      Serial.println(sate);

      Serial.print("hdop: ");
      Serial.println(hdop);

      Serial.print("geoidal_separation: ");
      Serial.println(geoidal_separation);



    int packetSize = Udp.parsePacket();
    if (1)
  {
    Udp.read(packetBuffer, 255);


    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());
    Serial.println("Contents:");
    Serial.println(packetBuffer);


    Serial.println("Packet received");
  }

  unsigned int remoteIP = (10, 23, 0, 1);
  unsigned int remotePort = 8787;

  char Unterstreichung[] = "_";
  char colon[] = ".";


  idx++;
  char chidx[128];
  sprintf(chidx,"%d",idx);  // 这个%d表示int型转成char的类型


  char chlon[20];
  sprintf(chlon,"%f",longi);

  char chlat[20];
  sprintf(chlat,"%f",lati);  // 这个%d表示float型转成char的类型

  char chalt[30];
  sprintf(chalt,"%f",alti);

  char chsate[30];
  sprintf(chsate,"%d",sate);

  char chhdop[20];
  sprintf(chhdop,"%d",hdop);

  char chgeo[20];
  sprintf(chgeo,"%d",geoidal_separation);


    strcat(chidx, Unterstreichung);  // 这个语句代表拼接两个char类型的字符串。
    Serial.println(chidx);
    strcat(chidx, chlon);
    Serial.println(chidx);
    strcat(chidx, Unterstreichung);
    Serial.println(chidx);
    strcat(chidx, chlat);
    Serial.println(chidx);
    strcat(chidx, Unterstreichung);
    Serial.println(chidx);
    strcat(chidx, chalt);
    Serial.println(chidx);
    strcat(chidx, Unterstreichung);
    Serial.println(chidx);
    strcat(chidx, chsate);
    Serial.println(chidx);
    strcat(chidx, Unterstreichung);
    Serial.println(chidx);
    strcat(chidx, chhdop);
    Serial.println(chidx);
    strcat(chidx, Unterstreichung);
    Serial.println(chidx);
    strcat(chidx, chgeo);
    Serial.println(chidx);

    Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
    Udp.print(chidx);  // print传输char,float等形式,而write是以Byte形式传输的,因此python收到的是类似于ASCII码的形式才需要转码
    Udp.endPacket();


}

/**************************************************************************************
* FUNCTION DEFINITION
**************************************************************************************/

void onRmcUpdate(nmea::RmcData const rmc)
{
  Serial.print("RMC ");

  if      (rmc.source == nmea::RmcSource::GPS)     Serial.print("GPS");
  else if (rmc.source == nmea::RmcSource::GLONASS) Serial.print("GLONASS");
  else if (rmc.source == nmea::RmcSource::Galileo) Serial.print("Galileo");
  else if (rmc.source == nmea::RmcSource::GNSS)    Serial.print("GNSS");

  Serial.print(" ");
  Serial.print(rmc.time_utc.hour);
  Serial.print(":");
  Serial.print(rmc.time_utc.minute);
  Serial.print(":");
  Serial.print(rmc.time_utc.second);
  Serial.print(".");
  Serial.print(rmc.time_utc.microsecond);

  if (rmc.is_valid)
  {
    Serial.print(" : LON ");
    Serial.print(rmc.longitude);
    Serial.print(" ° | LAT ");
    Serial.print(rmc.latitude);
    Serial.print(" ° | VEL ");
    Serial.print(rmc.speed);
    Serial.print(" m/s | HEADING ");
    Serial.print(rmc.course);
    Serial.print(" °");
  }

  Serial.println();
}

void onGgaUpdate(nmea::GgaData const gga)
{
  Serial.print("GGA ");

  if      (gga.source == nmea::GgaSource::GPS)     Serial.print("GPS");
  else if (gga.source == nmea::GgaSource::GLONASS) Serial.print("GLONASS");
  else if (gga.source == nmea::GgaSource::Galileo) Serial.print("Galileo");
  else if (gga.source == nmea::GgaSource::GNSS)    Serial.print("GNSS");

  Serial.print(" ");
  Serial.print(gga.time_utc.hour);
  Serial.print(":");
  Serial.print(gga.time_utc.minute);
  Serial.print(":");
  Serial.print(gga.time_utc.second);
  Serial.print(".");
  Serial.print(gga.time_utc.microsecond);

  if (gga.fix_quality != nmea::FixQuality::Invalid)
  {
    Serial.print(" : LON ");
    Serial.print(gga.longitude, 7);
    Serial.print(" ° | LAT ");
    Serial.print(gga.latitude, 7);
    Serial.print(" ° | Num Sat. ");
    Serial.print(gga.num_satellites);
    Serial.print(" | HDOP =  ");
    Serial.print(gga.hdop);
    Serial.print(" m | Altitude ");
    Serial.print(gga.altitude);
    Serial.print(" m | Geoidal Separation ");
    Serial.print(gga.geoidal_separation);
    Serial.print(" m");

    longi = gga.longitude;
    lati = gga.latitude;
    alti = gga.altitude;
    sate = gga.num_satellites;
    hdop = gga.hdop;
    geoidal_separation = gga.geoidal_separation;
  }

  Serial.println();
}

void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac < 16) {
      Serial.print("0");
    }
    Serial.print(mac, HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}

加delay之后

加delay之后

加delay之前

加delay之前

发表于 2021-7-5 23:34 | 显示全部楼层
换成millis试试
 楼主| 发表于 2021-7-5 23:48 | 显示全部楼层
换成millis以后运行出来的结果还是0。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-29 03:48 , Processed in 0.084139 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表