Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3987|回復: 16
打印 上一主題 下一主題

gps 合併問題

[複製鏈接]
跳轉到指定樓層
1#
發表於 2019-1-14 16:55:06 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
以下為合併後程式碼
可是只能顯示gps,無法顯示DHT11
麻煩高手解惑~~~




#include <DHT.h>  
#include "DHT.h"
#include<SoftwareSerial.h> //匯入程式庫
SoftwareSerial GPSModule(10, 11); // TX, RX
#define dhtPin 8      
#define dhtType DHT11   
const int AOUTpin=0;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin=9;//the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
const int ledPin=13;//the anode of the LED connects to digital pin D13 of the arduino

int limit;
int value;


int updates;
int failedUpdates;
int pos;
int stringplace = 0;
String timeUp;
String nmea[15];
String labels[12] {"Time: ", "Status: ", "Latitude: ", "Hemisphere: ", "Longitude: ", "Hemisphere: ", "Speed: ", "Track Angle: ", "Date: "};


DHT dht(dhtPin, dhtType);



void setup() {
  Serial.begin(9600);
  dht.begin();

pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPin, OUTPUT);//sets the pin as an output of the

Serial.begin(57600);
GPSModule.begin(9600);

Serial.begin(115200);//sets the baud rate
Serial .println();
Serial .println("感測器啟動");
}

void loop() {
  Serial.flush();
GPSModule.flush();
while (GPSModule.available() > 0)
{
GPSModule.read();

}
if (GPSModule.find("$GPRMC,")) {
String tempMsg = GPSModule.readStringUntil('\n');
for (int i = 0; i < tempMsg.length(); i++) {
if (tempMsg.substring(i, i + 1) == ",") {
nmea[pos] = tempMsg.substring(stringplace, i);
stringplace = i + 1;
pos++;
}
if (i == tempMsg.length() - 1) {
nmea[pos] = tempMsg.substring(stringplace, i);
}
}
updates++;
nmea[2] = ConvertLat();
nmea[4] = ConvertLng();
for (int i = 0; i < 9; i++) {
Serial.print(labels[i]);
Serial.print(nmea[i]);
Serial.println("");
}

}
else {

failedUpdates++;

}
stringplace = 0;
pos = 0;
}

String ConvertLat() {
String posneg = "";
if (nmea[3] == "S") {
posneg = "-";
}
String latfirst;
float latsecond;
for (int i = 0; i < nmea[2].length(); i++) {
if (nmea[2].substring(i, i + 1) == ".") {
latfirst = nmea[2].substring(0, i - 2);
latsecond = nmea[2].substring(i - 2).toFloat();
}
}
latsecond = latsecond / 60;
String CalcLat = "";

char charVal[9];
dtostrf(latsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLat += charVal[i];
}
latfirst += CalcLat.substring(1);
latfirst = posneg += latfirst;
return latfirst;
}

String ConvertLng() {
String posneg = "";
if (nmea[5] == "W") {
posneg = "-";
}

String lngfirst;
float lngsecond;
for (int i = 0; i < nmea[4].length(); i++) {
if (nmea[4].substring(i, i + 1) == ".") {
lngfirst = nmea[4].substring(0, i - 2);
//Serial.println(lngfirst);
lngsecond = nmea[4].substring(i - 2).toFloat();
//Serial.println(lngsecond);

}
}
lngsecond = lngsecond / 60;
String CalcLng = "";
char charVal[9];
dtostrf(lngsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLng += charVal[i];
}
lngfirst += CalcLng.substring(1);
lngfirst = posneg += lngfirst;
return lngfirst;








  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("無法從DHT傳感器讀取!   ");
    return;
  }
  Serial.println();
  Serial.print("濕度: ");
  Serial.print(h);
  Serial.println("%\t");
  Serial.print("攝氏溫度: ");
  Serial.print(t);
  Serial.println("*C\t");
  Serial.print("華氏溫度: ");
  Serial.print(f);
  Serial.println("*F\n");

  delay(3000);
{
value= analogRead(AOUTpin);//reads the analaog value from the alcohol sensor's AOUT pin
limit= digitalRead(DOUTpin);//reads digital value from the alcohol sensor's DOUT pin
Serial.print("酒精值 ");
Serial.println(value);//prints the alcohol value
Serial.print("Limit: ");
Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
delay(2000);


if (limit == HIGH){
digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPin, LOW);//if threshold not reached, LED remains off

}
}


}
推薦
發表於 2019-1-14 17:24:28 | 只看該作者
本帖最後由 超新手 於 2019-1-14 17:27 編輯

因為你沒按規則合併
你把 DHT 的 loop 程式, 合併到  String ConvertLng() 裡面去了
應該放到 loop 的後半段...
  stringplace = 0;
  pos = 0;
這邊才是 loop 迴圈內

}
3#
 樓主| 發表於 2019-1-15 11:06:11 | 只看該作者
改完後變成不會顯示GPS了
麻煩再幫忙解惑
感謝~~~~


#include <DHT.h>  
#include "DHT.h"
#include<SoftwareSerial.h> //匯入程式庫
SoftwareSerial GPSModule(10, 11); // TX, RX
#define dhtPin 8      
#define dhtType DHT11   
const int AOUTpin=0;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin=9;//the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
const int ledPin=13;//the anode of the LED connects to digital pin D13 of the arduino

int limit;
int value;


int updates;
int failedUpdates;
int pos;
int stringplace = 0;
String timeUp;
String nmea[15];
String labels[12] {"Time: ", "Status: ", "Latitude: ", "Hemisphere: ", "Longitude: ", "Hemisphere: ", "Speed: ", "Track Angle: ", "Date: "};


DHT dht(dhtPin, dhtType);



void setup() {
  Serial.begin(9600);
  dht.begin();

pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPin, OUTPUT);//sets the pin as an output of the

Serial.begin(57600);
GPSModule.begin(9600);

Serial.begin(115200);//sets the baud rate
Serial .println();
Serial .println("感測器啟動");
}

void loop() {

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("無法從DHT傳感器讀取!   ");
    return;
  }
  Serial.println();
  Serial.print("濕度: ");
  Serial.print(h);
  Serial.println("%\t");
  Serial.print("攝氏溫度: ");
  Serial.print(t);
  Serial.println("*C\t");
  Serial.print("華氏溫度: ");
  Serial.print(f);
  Serial.println("*F\n");

  delay(3000);

  

value= analogRead(AOUTpin);//reads the analaog value from the alcohol sensor's AOUT pin
limit= digitalRead(DOUTpin);//reads digital value from the alcohol sensor's DOUT pin
Serial.print("酒精值 ");
Serial.println(value);//prints the alcohol value
Serial.print("Limit: ");
Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
delay(2000);


if (limit == HIGH){
digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPin, LOW);//if threshold not reached, LED remains off
}

  




  
  Serial.flush();
GPSModule.flush();
while (GPSModule.available() > 0)
{
GPSModule.read();

}
if (GPSModule.find("$GPRMC,")) {
String tempMsg = GPSModule.readStringUntil('\n');
for (int i = 0; i < tempMsg.length(); i++) {
if (tempMsg.substring(i, i + 1) == ",") {
nmea[pos] = tempMsg.substring(stringplace, i);
stringplace = i + 1;
pos++;
}
if (i == tempMsg.length() - 1) {
nmea[pos] = tempMsg.substring(stringplace, i);
}
}
updates++;
nmea[2] = ConvertLat();
nmea[4] = ConvertLng();
for (int i = 0; i < 9; i++) {
Serial.print(labels[i]);
Serial.print(nmea[i]);
Serial.println("");
}

}
else {

failedUpdates++;

}
stringplace = 0;
pos = 0;
}


String ConvertLat() {
String posneg = "";
if (nmea[3] == "S") {
posneg = "-";
}
String latfirst;
float latsecond;
for (int i = 0; i < nmea[2].length(); i++) {
if (nmea[2].substring(i, i + 1) == ".") {
latfirst = nmea[2].substring(0, i - 2);
latsecond = nmea[2].substring(i - 2).toFloat();
}
}
latsecond = latsecond / 60;
String CalcLat = "";

char charVal[9];
dtostrf(latsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLat += charVal[i];
}
latfirst += CalcLat.substring(1);
latfirst = posneg += latfirst;
return latfirst;
}


String ConvertLng() {
String posneg = "";
if (nmea[5] == "W") {
posneg = "-";
}

String lngfirst;
float lngsecond;
for (int i = 0; i < nmea[4].length(); i++) {
if (nmea[4].substring(i, i + 1) == ".") {
lngfirst = nmea[4].substring(0, i - 2);
//Serial.println(lngfirst);
lngsecond = nmea[4].substring(i - 2).toFloat();
//Serial.println(lngsecond);

}
}
lngsecond = lngsecond / 60;
String CalcLng = "";
char charVal[9];
dtostrf(lngsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLng += charVal[i];
}
lngfirst += CalcLng.substring(1);
lngfirst = posneg += lngfirst;
return lngfirst;
}
4#
發表於 2019-1-15 11:38:23 | 只看該作者
那可以顯示 濕度/溫度/酒精值嗎?

可能是 DELAY 太久
先拿掉
delay(2000); 和 delay(3000); 看看
5#
 樓主| 發表於 2019-1-15 11:52:29 | 只看該作者
本帖最後由 James 於 2019-1-15 11:53 編輯
超新手 發表於 2019-1-15 11:38
那可以顯示 濕度/溫度/酒精值嗎?

可能是 DELAY 太久


可以 但是只會顯示一次
delay拿掉結果一樣
6#
發表於 2019-1-15 13:16:12 | 只看該作者
本帖最後由 超新手 於 2019-1-15 13:22 編輯

顯示一次後, 就什麼都沒有顥示嗎?
應該不是"無法從DHT傳感器讀取"吧?
7#
 樓主| 發表於 2019-1-15 13:27:06 | 只看該作者
超新手 發表於 2019-1-15 13:16
顯示一次後, 就什麼都沒有顥示嗎?


不是  是像這樣

未命名.png (24.99 KB, 下載次數: 286)

未命名.png
8#
發表於 2019-1-15 14:31:54 | 只看該作者
本帖最後由 超新手 於 2019-1-15 14:34 編輯

1)改回去GPS 程式, 結果是正常的嗎?
2) 加兩行除錯一下
if (GPSModule.find("$GPRMC,")) {
Serial.print("#")
String tempMsg = GPSModule.readStringUntil('\n');
Serial.print("*")
9#
 樓主| 發表於 2019-1-15 14:45:44 | 只看該作者
超新手 發表於 2019-1-15 14:31
1)改回去GPS 程式, 結果是正常的嗎?
2) 加兩行除錯一下
if (GPSModule.find("$GPRMC,")) {

是可以使用的

加上後結果沒什麼改變

未命名.png (25.74 KB, 下載次數: 306)

未命名.png
10#
發表於 2019-1-15 15:32:07 | 只看該作者
再多印一些資料出來看看
  if (GPSModule.find("$GPRMC,")) {
    Serial.print("#");
    String tempMsg = GPSModule.readStringUntil('\n');
    Serial.print("*");
    Serial.println(tempMsg.length());
    for (int i = 0; i < tempMsg.length(); i++) {
      if (tempMsg.substring(i, i + 1) == ",") {
        nmea[pos] = tempMsg.substring(stringplace, i);
        stringplace = i + 1;
       serial.println(stringplace);
        serial.println(i);
        serial.println(pos);
        pos++;
        
      }
      if (i == tempMsg.length() - 1) {
        nmea[pos] = tempMsg.substring(stringplace, i);
        serial.println(stringplace);
        serial.println(i);
        serial.println(pos);   
      }
    }
11#
 樓主| 發表於 2019-1-15 15:40:34 | 只看該作者
超新手 發表於 2019-1-15 15:32
再多印一些資料出來看看
  if (GPSModule.find("$GPRMC,")) {
    Serial.print("#");

有了!!!!!
但是有一個小問題
就是前面會有很多數字

未命名.png (22.37 KB, 下載次數: 284)

未命名.png
12#
發表於 2019-1-15 15:46:52 | 只看該作者
這樣算沒東西吧?
你之前所謂GPS可以, 有真的顯示 GPS 座標嗎?
13#
 樓主| 發表於 2019-1-15 16:11:39 | 只看該作者
超新手 發表於 2019-1-15 15:46
這樣算沒東西吧?
你之前所謂GPS可以, 有真的顯示 GPS 座標嗎?

怕謝
剛沒有講 因為在地下室所以沒有數值


未命名.png (22.71 KB, 下載次數: 252)

未命名.png
14#
 樓主| 發表於 2019-1-16 10:45:58 | 只看該作者
本帖最後由 James 於 2019-1-16 10:47 編輯

想再請問一下
我把紅色部分刪除後
會剩下前面四個數字
這幾個數字是代表什麼
有辦法讓它不要顯示嗎

for (int i = 0; i < tempMsg.length(); i++) {
      if (tempMsg.substring(i, i + 1) == ",") {
        nmea[pos] = tempMsg.substring(stringplace, i);
        stringplace = i + 1;
       serial.println(stringplace);
        serial.println(i);
        serial.println(pos);
       pos++;
        
      }
      if (i == tempMsg.length() - 1) {
        nmea[pos] = tempMsg.substring(stringplace, i);
        serial.println(stringplace);
        serial.println(i);
        serial.println(pos);   
      }
    }


未命名.png (19.13 KB, 下載次數: 258)

未命名.png
15#
發表於 2019-1-16 11:18:45 | 只看該作者
所有後來新增的程式都是除錯用
根本沒用處. 本來就要刪
我看不出來為什麼加了那些除錯程式後
就會變正常


16#
 樓主| 發表於 2019-1-18 10:20:25 來自手機 | 只看該作者
了解我在研究看看
17#
 樓主| 發表於 2019-1-18 10:22:45 來自手機 | 只看該作者
超新手 發表於 2019-1-16 11:18
所有後來新增的程式都是除錯用
根本沒用處. 本來就要刪
我看不出來為什麼加了那些除錯程式後

了解 我在研究看看
非常感謝你的幫忙
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-4-28 02:37 , Processed in 0.132230 second(s), 9 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表