C3-470 使用经验一例
GGA 报文输出0表示未定位RMC 报文输出A表示定位有效,经纬度输出0
额,目测这个有效说的是时间有效 本帖最后由 hi55234 于 2014-11-11 22:19 编辑
朴素的极致
#include <MemoryFree.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
#include <SoftwareSerial.h>
SoftwareSerial gps(8, 9);
char tempx;
char GPRMC;
char GPGGA;
int gpschangertest;
boolean gpschanger;
int weiduA;
unsigned long weiduB,weiduC;
int jingduA;
unsigned long jingduB,jingduC;
boolean GPGGAget=0;
boolean GPRMCget=0;
unsigned long gpstimes;
boolean konghangpanduan;
boolean gpsshuchuqiehuanB;
int jsq1=0;
int jsq2=0;
int jsq3=0;
int jsq4=0;
int jsq5=0;
int jsq6=0;
int jsq7=0;
int GPGGAlong=0;
int GPRMClong=0;
boolean jiaoyanjieguoA,jiaoyanjieguoC;
float sudu;
String jianyan="";
int yihuoyunsuan;
boolean dingweiok=0;
boolean gpsxianguangA=0;
boolean fangzhilianji=1;
int lcdxianshi=0;
/*---------------------GPS end---------------------------------*/
int gpsshuchuqiehuanA;
#include<EEPROM.h>
int EEPROMvalue;
const int chipSelect = 10;
char TrackB0="-0000.0";
char TrackB1="00.0000000";
char TrackB2="000.0000000";
char TrackB4="2000-00-00T00:00:00";
char TrackB5="0000.00";
char TrackB6="000.00";
boolean weiduxuweitiao=0;
boolean jingduxuweitiao=0;
int weiduzhanwei,jingduzhanwei;
int utc8s,utc8f,utc8m,utc8n,utc8y,utc8r;
int xiaoyue={4,6,9,11};
boolean rijinwei,yuejinwei,nianjinwei,xiaoyueok;
unsigned long fentodu;
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(16);
DallasTemperature sensors(&oneWire);
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 17
#include <Wire.h>
#include <RTClib.h>
void printDateTime(DateTime dateTime);
RTC_DS1307 RTC;
int nian,yue,ri,shi,fen,miao;
boolean shoushiqidong;
void setup() {
Wire.begin();
RTC.begin();
pinMode(14,INPUT);
pinMode(15,INPUT);
lcd.begin(16, 2);
Serial.begin(9600);
gps.begin(9600);
EEPROMvalue= EEPROM.read(0);
if(EEPROMvalue==0)gpsshuchuqiehuanB=0;
else gpsshuchuqiehuanB=1;
}
void loop() {
if(lcdxianshi<3)ruanchuankou();
else{
sensors.requestTemperatures();
jsq1=sensors.getTempCByIndex(0);
DHT11.read(DHT11PIN);
jsq2=DHT11.humidity;
for(int col=0;col<16;col++)tempx=0;
DS1307tiquxiaodui();
jsq3=jsq2*0.1;
tempx = jsq3+48;
jsq3=jsq2%10;
tempx = jsq3+48;
tempx='%';
tempx=32;
jsq3=jsq1*0.1;
tempx = jsq3+48;
jsq3=jsq1%10;
tempx = jsq3+48;
tempx='C';
tempx=32;
}
if(millis()-gpstimes>10 && !konghangpanduan){
konghangpanduan=1;
gpsxianguangA=1;
}
if(gpsxianguangA && lcdxianshi<3){
gpsxianguangA=0;
gpsdechuli();
if(!gpsshuchuqiehuanB){
Serial.println(GPGGA);
Serial.println(GPRMC);
Serial.print("freeMemory=");
Serial.println(freeMemory());
Serial.println();
}
if(jiaoyanjieguoA)jiexiGPGGA();
if(jiaoyanjieguoC){
jiexiGPRMC();
if(dingweiok && gpschanger) {
gpschanger=0;
}
}
now1602out();
for(int col=0;col<80;col++)GPRMC=0;
for(int col=0;col<80;col++)GPGGA=0;
for(int col=0;col<16;col++)tempx=0;
jsq1=0;
GPGGAget=0;
GPRMCget=0;
gpstimes=millis();
}
gpsshuchuqiehuanA =digitalRead(14);
if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
fangzhilianji=0;
gpstimes=millis();
gpsshuchuqiehuanB=!gpsshuchuqiehuanB;
EEPROM.write(0,gpsshuchuqiehuanB);
}else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();
gpsshuchuqiehuanA =digitalRead(15);
if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
fangzhilianji=0;
gpstimes=millis();
Serial.print("lcdxianshi=");
Serial.println(lcdxianshi);
if(!dingweiok){
if(lcdxianshi<3)lcdxianshi=3;
else lcdxianshi=0;
}else lcdxianshi++;
lcdxianshi=lcdxianshi%4;
}else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();
if(millis()-gpstimes>450 && !fangzhilianji){
gpstimes=millis();
fangzhilianji=1;
}
if(lcdxianshi==3) now1602out();
}
void ruanchuankou()
{
while (gps.available() > 0) {
gpstimes=millis();
konghangpanduan=0;
if(GPRMCget){
GPRMC=gps.read();
if(gpsshuchuqiehuanB)Serial.print(GPRMC);
if(GPRMC=='*'){
GPRMClong=jsq1;
GPRMCget=0;
jsq1=0;
}else if(jsq1<79)jsq1++;
}else if(GPGGAget){
GPGGA=gps.read();
if(gpsshuchuqiehuanB)Serial.print(GPGGA);
if(GPGGA=='*'){
GPGGAlong=jsq1;
GPGGAget=0;
jsq1=0;
}else if(jsq1<79)jsq1++;
}else{
tempx=gps.read();
if(gpsshuchuqiehuanB)Serial.print(tempx);
if(jsq1<6)jsq1++;
}
if(jsq1==6){
if(tempx=='M' && tempx=='C'){
GPRMCget=1;
GPGGAget=0;
for(int col=0;col<6;col++)GPRMC=tempx;
for(int col=0;col<6;col++)tempx=0;
}else if(tempx=='G' && tempx=='A'){
GPGGAget=1;
GPRMCget=0;
for(int col=0;col<6;col++)GPGGA=tempx;
for(int col=0;col<6;col++)tempx=0;
}else{
for(int col=0;col<5;col++)tempx=tempx;
jsq1=5;
}
}
}
}
void gpsdechuli()
{
for(int col=1;col<GPRMClong-3;col++){
if(col==1)yihuoyunsuan=GPRMC;
else yihuoyunsuan=yihuoyunsuan ^ GPRMC;
}
if(yihuoyunsuan==0){
jianyan="00";
}else if(yihuoyunsuan>15){
jianyan = String(yihuoyunsuan,HEX);
}else{
jianyan = "0";
jianyan += String(yihuoyunsuan,HEX);
}
jianyan.toUpperCase();
if(jianyan==GPRMC && jianyan==GPRMC ){
jiaoyanjieguoC=1;
}else{
jiaoyanjieguoC=0;
}
jianyan="";
for(int col=1;col<GPGGAlong-3;col++){
if(col==1)yihuoyunsuan=GPGGA;
else yihuoyunsuan=yihuoyunsuan ^ GPGGA;
}
if(yihuoyunsuan==0){
jianyan="00";
}else if(yihuoyunsuan>15){
jianyan = String(yihuoyunsuan,HEX);
}else{
jianyan = "0";
jianyan += String(yihuoyunsuan,HEX);
}
jianyan.toUpperCase();
if(jianyan==GPGGA && jianyan==GPGGA ){
jiaoyanjieguoA=1;
}else{
jiaoyanjieguoA=0;
}
jianyan="";
}
void jiexiGPGGA()
{
jsq2=0;
jsq3=0;
jsq4=0;
jsq5=0;
for(int col=1;col<GPGGAlong-3;col++){
if(GPGGA==',')jsq2++;
if(jsq2==10){
jsq3=col;
col=GPGGAlong-3;
}
if(jsq2<9)jsq4=col;
if(jsq2<7)jsq5=col;
}
if(GPGGA==49)dingweiok=1;
else dingweiok=0;
for(int col=0;col<8;col++)TrackB0=0;
memcpy(TrackB0,GPGGA+jsq4+2, jsq3-jsq4-2);
}
void jiexiGPRMC()
{
jsq2=0;
jsq3=0;
jsq4=0;
jsq5=0;
jsq6=0;
jsq7=0;
for(int col=1;col<GPRMClong-3;col++){
if(GPRMC==',')jsq2++;
if(jsq2==9){
jsq3=col;
col=GPRMClong-3;
}
if(jsq2<8)jsq4=col;
if(jsq2<6)jsq5=col;
if(jsq2<4)jsq6=col;
if(jsq2<2)jsq7=col;
}
if(GPRMC=='A')chinatime();
if(dingweiok){
jingweiduchuli();
hangxiangyusudu();
}else {
weiduA=0;
weiduB=0;
weiduC=0;
jingduA=0;
jingduB=0;
jingduC=0;
}
}
void chinatime()
{
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8s=jianyan.toInt()+8;
if(utc8s>23)rijinwei=1;
utc8s=utc8s%24;
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8f=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8m=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8r=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8y=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8n=jianyan.toInt();
if(rijinwei){
if(utc8y==2 && utc8r==28){
if(utc8n % 400 == 0 || utc8n % 100 != 0 && utc8n % 4 == 0)utc8r=29;
else {
utc8r=1;
yuejinwei=1;
}
}else{
for(int col=0;col<4;col++){
if(xiaoyue==utc8y)xiaoyueok=1;
}
if(xiaoyueok && utc8r==30){
utc8r=1;
yuejinwei=1;
}else if(!xiaoyueok && utc8r==31){
utc8r=1;
yuejinwei=1;
}else{
utc8r++;
}
}
}
if(yuejinwei && utc8y==12){
utc8y=1;
nianjinwei=1;
}else if(yuejinwei){
utc8y++;
}
if(nianjinwei)utc8n++;
for(int col=0;col<16;col++)tempx=0;
if(utc8n>9){
sprintf(tempx, "%d", utc8n);
memcpy(TrackB4+2,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8n+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8y>9){
sprintf(tempx, "%d", utc8y);
memcpy(TrackB4+5,tempx, 2);
} else{
TrackB4=48;
TrackB4=utc8y+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8r>9){
sprintf(tempx, "%d", utc8r);
memcpy(TrackB4+8,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8r+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8s>9){
sprintf(tempx, "%d", utc8s);
memcpy(TrackB4+11,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8s+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8f>9){
sprintf(tempx, "%d", utc8f);
memcpy(TrackB4+14,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8f+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8m>9){
sprintf(tempx, "%d", utc8m);
memcpy(TrackB4+17,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8m+48;
}
}
void jingweiduchuli()
{
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq7+4, 2);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(weiduA!=gpschangertest){
weiduA=gpschangertest;
gpschanger=1;
}
if(GPRMC==83)weiduxuweitiao=1;
if(GPRMC==87)jingduxuweitiao=1;
memcpy(TrackB1,GPRMC+jsq7+4, 2);
if(gpschangertest<10) {
TrackB1=32;
weiduzhanwei=1;
}else weiduzhanwei=2;
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq7+6, 2);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(weiduB!=gpschangertest){
weiduB=gpschangertest;
gpschanger=1;
}
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq7+9, 4);
if(jsq6-jsq7==12) memcpy(tempx,GPRMC+jsq7+9, 4);
else if(jsq6-jsq7==13)memcpy(tempx,GPRMC+jsq7+9, 5);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(weiduC!=gpschangertest){
weiduC=gpschangertest;
gpschanger=1;
}
fentodu=weiduB*100000+weiduC*10;
fentodu=fentodu/6;
for(int col=0;col<16;col++)tempx=0;
dtostrf(fentodu,6,0,tempx);
if(fentodu>99999){
memcpy(TrackB1+3,tempx, 6);
}else if(fentodu>9999){
TrackB1=48;
memcpy(TrackB1+4,tempx, 5);
}else if(fentodu>999){
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+5,tempx, 4);
}else if(fentodu>99){
TrackB1=48;
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+6,tempx, 3);
}else if(fentodu>9){
TrackB1=48;
TrackB1=48;
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+7,tempx, 2);
}else{
TrackB1=48;
TrackB1=48;
TrackB1=48;
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+8,tempx, 1);
}
if(weiduxuweitiao){
weiduxuweitiao=0;
if(weiduzhanwei==2){
for(int col=0;col<16;col++)tempx=0;
tempx=45;
memcpy(tempx+1,TrackB1, 9);
memcpy(TrackB1,tempx, 10);
}else{
TrackB1=45;
}
}
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq6+4, 3);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(jingduA!=gpschangertest){
jingduA=gpschangertest;
gpschanger=1;
}
memcpy(TrackB2,GPRMC+jsq6+4, 3);
jingduzhanwei=3;
if(gpschangertest<100) {
TrackB2=32;
jingduzhanwei=2;
}
if(gpschangertest<10) {
TrackB2=32;
jingduzhanwei=1;
}
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq6+7, 3);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(jingduB!=gpschangertest){
jingduB=gpschangertest;
gpschanger=1;
}
for(int col=0;col<16;col++)tempx=0;
if(jsq5-jsq6==13)memcpy(tempx,GPRMC+jsq6+10, 4);
else if(jsq5-jsq6==14)memcpy(tempx,GPRMC+jsq6+10, 5);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(jingduC!=gpschangertest){
jingduC=gpschangertest;
gpschanger=1;
}
jianyan="";
fentodu=jingduB*100000+jingduC*10;
fentodu=fentodu/6;
for(int col=0;col<16;col++)tempx=0;
dtostrf(fentodu,6,0,tempx);
if(fentodu>99999){
memcpy(TrackB2+4,tempx, 6);
}else if(fentodu>9999){
TrackB2=48;
memcpy(TrackB2+5,tempx, 5);
}else if(fentodu>999){
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+6,tempx, 4);
}else if(fentodu>99){
TrackB2=48;
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+7,tempx, 3);
}else if(fentodu>9){
TrackB2=48;
TrackB2=48;
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+8,tempx, 2);
}else{
TrackB2=48;
TrackB2=48;
TrackB2=48;
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+9,tempx, 1);
}
if(jingduxuweitiao){
jingduxuweitiao=0;
if(jingduzhanwei==3){
for(int col=0;col<16;col++)tempx=0;
tempx=45;
memcpy(tempx+1,TrackB2, 10);
memcpy(TrackB2,tempx, 11);
}else if(jingduzhanwei==2){
TrackB2=45;
}else{
TrackB2=45;
}
}
}
void hangxiangyusudu()
{
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq5+4,jsq4-jsq5-3);
for(int col=0;col<jsq4-jsq5-3;col++){
if(tempx==46){
if(col==1)fentodu=(tempx-48)*1000;
else if(col==2)fentodu=(tempx-48)*10000+(tempx-48)*1000;
else if(col==3)fentodu=(tempx-48)*100000+(tempx-48)*10000+(tempx-48)*1000;
if(jsq4-jsq5-col==5)fentodu=fentodu+(tempx-48)*100;
else if(jsq4-jsq5-col==6)fentodu=fentodu+(tempx-48)*100+(tempx-48)*10;
else if(jsq4-jsq5-col==7)fentodu=fentodu+(tempx-48)*100+(tempx-48)*10+tempx-48;
fentodu=fentodu*1852*0.0001;
col=jsq4-jsq5-3;
}
}
sudu=fentodu*0.01;
for(int col=0;col<8;col++)TrackB5=0;
dtostrf(sudu,1,2,TrackB5);
for(int col=0;col<7;col++)TrackB6=0;
memcpy(TrackB6,GPRMC+jsq4+2, jsq3-jsq4-2);
}
void printDateTime(DateTime dateTime) {
nian=dateTime.year(), DEC;
yue=dateTime.month(), DEC;
ri=dateTime.day(), DEC;
shi=dateTime.hour(), DEC;
fen=dateTime.minute(), DEC;
miao=dateTime.second(), DEC;
}
void DS1307tiquxiaodui()
{
DateTime now = RTC.now();
printDateTime(now);
if(dingweiok){
if(yue != utc8y) shoushiqidong=1;
if(ri != utc8r) shoushiqidong=1;
if(shi != utc8s) shoushiqidong=1;
if(fen != utc8f) shoushiqidong=1;
if(miao-utc8m>2 || utc8m-miao>2) shoushiqidong=1;
if(shoushiqidong){
shoushiqidong=0;
RTC.set(RTC_YEAR, utc8n);
RTC.set(RTC_MONTH, utc8y);
RTC.set(RTC_DAY, utc8r);
RTC.set(RTC_HOUR, utc8s);
RTC.set(RTC_MINUTE, utc8f);
RTC.set(RTC_SECOND, utc8m);
}
}
/*
jsq3=nian*0.1;
jsq3=jsq3%10;
temp2 = jsq3+48, DEC;
jsq3=nian%10;
temp2 = jsq3+48, DEC;
jsq3=yue*0.1;
temp2 = jsq3+48, DEC;
jsq3=yue%10;
temp2 = jsq3+48, DEC;
jsq3=ri*0.1;
temp2 = jsq3+48, DEC;
jsq3=ri%10;
temp2 = jsq3+48, DEC;
*/
jsq3=shi*0.1;
tempx = jsq3+48, DEC;
jsq3=shi%10;
tempx = jsq3+48, DEC;
tempx=':';
jsq3=fen*0.1;
tempx = jsq3+48, DEC;
jsq3=fen%10;
tempx = jsq3+48, DEC;
tempx=':';
jsq3=miao*0.1;
tempx= jsq3+48, DEC;
jsq3=miao%10;
tempx = jsq3+48, DEC;
}
void now1602out()
{
/*GPS模式下的显示情况说明
char TrackB0="-0000.0";
char TrackB1="00.0000000";
char TrackB2="000.0000000";
char TrackB4="2000-00-00T00:00:00";
char TrackB5="0000.00";
char TrackB6="000.00";
lcdxianshi=0显示内容如下
LAT.00.0000000
0123456789012345
LON. 000.0000000
lcdxianshi=1显示内容如下
ASL(G): -0000.0M
0123456789012345
YAW(GPS): 000.00
lcdxianshi=2显示内容如下
00-00T00:00:00
0123456789012345
speed:000.00km/h
I'm so sorry but
0123456789012345
now the GPS lost
*/
if(!dingweiok && lcdxianshi<3){
lcd.setCursor(0, 0);
lcd.print("I'm so sorry but");
lcd.setCursor(0, 1);
lcd.print("now the GPS lost");
}else{
if(lcdxianshi==0){
lcd.setCursor(0, 0);
lcd.print("LAT.");
lcd.print(TrackB1);
lcd.setCursor(0, 1);
lcd.print("LON. ");
lcd.print(TrackB2);
}else if(lcdxianshi==1){
lcd.setCursor(0, 0);
lcd.print("ASL(G): ");
lcd.print(TrackB0);
lcd.print("M ");
lcd.setCursor(0, 1);
lcd.print("YAW(GPS): ");
lcd.print(TrackB6);
lcd.print(" ");
}else if(lcdxianshi==2){
for(int col=0;col<16;col++)tempx=0;
for(int col=0;col<14;col++)tempx=TrackB4;
lcd.setCursor(0, 0);
lcd.print("");
lcd.print(tempx);
lcd.setCursor(0, 1);
lcd.print("speed:");
lcd.print(TrackB5);
lcd.print("km/h ");
}else if(lcdxianshi==3){
lcd.setCursor(0, 0);
lcd.print("now lcdxianshi=3");
lcd.setCursor(0, 1);
lcd.print(tempx);
}
}
}
带记忆、能校时,gps不接也行
#include <MemoryFree.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
#include <SoftwareSerial.h>
SoftwareSerial gps(8, 9);
char tempx;
char GPRMC;
char GPGGA;
int gpschangertest;
boolean gpschanger;
int weiduA;
unsigned long weiduB,weiduC;
int jingduA;
unsigned long jingduB,jingduC;
boolean GPGGAget=0;
boolean GPRMCget=0;
unsigned long gpstimes;
boolean konghangpanduan;
boolean gpsshuchuqiehuanB;
int jsq1=0;
int jsq2=0;
int jsq3=0;
int jsq4=0;
int jsq5=0;
int jsq6=0;
int jsq7=0;
int GPGGAlong=0;
int GPRMClong=0;
boolean jiaoyanjieguoA,jiaoyanjieguoC;
float sudu;
String jianyan="";
int yihuoyunsuan;
boolean dingweiok=0;
boolean gpsxianguangA=0;
boolean fangzhilianji=1;
int lcdxianshi=0;
/*---------------------GPS end---------------------------------*/
int gpsshuchuqiehuanA;
#include<EEPROM.h>
int EEPROMvalue;
const int chipSelect = 10;
char TrackB0="-0000.0";
char TrackB1="00.0000000";
char TrackB2="000.0000000";
char TrackB4="2000-00-00T00:00:00";
char TrackB5="0000.00";
char TrackB6="000.00";
boolean weiduxuweitiao=0;
boolean jingduxuweitiao=0;
int weiduzhanwei,jingduzhanwei;
int utc8s,utc8f,utc8m,utc8n,utc8y,utc8r;
int xiaoyue={4,6,9,11};
boolean rijinwei,yuejinwei,nianjinwei,xiaoyueok;
unsigned long fentodu;
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(10);
DallasTemperature sensors(&oneWire);
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 11
#include <Wire.h>
#include <RTClib.h>
void printDateTime(DateTime dateTime);
RTC_DS1307 RTC;
int nian,yue,ri,shi,fen,miao;
boolean shoushiqidong;
int gp2y1010dust=2;
int gp2y1010led=15;
int dustVal=0;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
int dusti=0;
float dustValPM25,averagedustVal;
int AQI;
int mySensVals ;
unsigned long time1=millis();
void setup() {
pinMode(gp2y1010led,OUTPUT);
lcd.begin(16, 2);
Wire.begin();
RTC.begin();
pinMode(12,INPUT);
pinMode(14,INPUT);
Serial.begin(9600);
gps.begin(9600);
EEPROMvalue= EEPROM.read(0);
if(EEPROMvalue==0)gpsshuchuqiehuanB=0;
else gpsshuchuqiehuanB=1;
EEPROMvalue= EEPROM.read(10);
if(EEPROMvalue==0)lcdxianshi=0;
else if(EEPROMvalue==1)lcdxianshi=1;
else if(EEPROMvalue==2)lcdxianshi=2;
else if(EEPROMvalue==3)lcdxianshi=3;
else {
EEPROM.write(10,0);
lcdxianshi=0;
}
}
void loop() {
if(lcdxianshi<3)ruanchuankou();
else{
fenchentou();
sensors.requestTemperatures();
jsq1=sensors.getTempCByIndex(0);
DHT11.read(DHT11PIN);
jsq2=DHT11.humidity;
for(int col=0;col<16;col++)tempx=0;
DS1307tiquxiaodui();
jsq3=jsq2*0.1;
tempx = jsq3+48;
jsq3=jsq2%10;
tempx = jsq3+48;
tempx='%';
tempx=32;
jsq3=jsq1*0.1;
tempx = jsq3+48;
jsq3=jsq1%10;
tempx = jsq3+48;
tempx='C';
tempx=32;
}
if(millis()-gpstimes>10 && !konghangpanduan){
konghangpanduan=1;
gpsxianguangA=1;
}
if(gpsxianguangA && lcdxianshi<3){
gpsxianguangA=0;
gpsdechuli();
if(!gpsshuchuqiehuanB){
Serial.println(GPGGA);
Serial.println(GPRMC);
Serial.print("freeMemory=");
Serial.println(freeMemory());
Serial.println();
}
if(jiaoyanjieguoA)jiexiGPGGA();
if(jiaoyanjieguoC){
jiexiGPRMC();
if(dingweiok && gpschanger) {
gpschanger=0;
}
}
now1602out();
for(int col=0;col<80;col++)GPRMC=0;
for(int col=0;col<80;col++)GPGGA=0;
for(int col=0;col<16;col++)tempx=0;
jsq1=0;
GPGGAget=0;
GPRMCget=0;
gpstimes=millis();
}
gpsshuchuqiehuanA =digitalRead(12);
if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
fangzhilianji=0;
gpstimes=millis();
gpsshuchuqiehuanB=!gpsshuchuqiehuanB;
EEPROM.write(0,gpsshuchuqiehuanB);
}else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();
gpsshuchuqiehuanA =digitalRead(14);
if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
fangzhilianji=0;
gpstimes=millis();
if(!dingweiok){
if(lcdxianshi<3)lcdxianshi=3;
else lcdxianshi=0;
}else lcdxianshi++;
lcdxianshi=lcdxianshi%4;
EEPROM.write(10,lcdxianshi);
}else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();
if(millis()-gpstimes>450 && !fangzhilianji){
gpstimes=millis();
fangzhilianji=1;
}
if(lcdxianshi==3) now1602out();
}
void fenchentou()
{
if (millis() - time1 > 1000){
time1=millis();
/*粉尘头控制代码*/
digitalWrite(gp2y1010led,LOW);
delayMicroseconds(delayTime);
dustVal=analogRead(gp2y1010dust);
delayMicroseconds(delayTime2);
digitalWrite(gp2y1010led,HIGH);
/*邻采样10次平均averagedustVal的计算*/
mySensVals =dustVal;
dusti++;
dusti=dusti%10;
averagedustVal=0;
for(int col=0;col<10;col++)averagedustVal=averagedustVal+mySensVals;
averagedustVal=averagedustVal*0.1;
/*计算pm2.5的数值 电压阀值以上用:(v*0.172-0.0999)*1000,阀值以下用线性,参考国外帖子,认为输出最低17,对应粉尘5000*/
if(averagedustVal>184.5)dustValPM25=0.83984375*averagedustVal-99.9;
else dustValPM25=averagedustVal*0.298373984;
/*AQI的计算,美国标准*/
if(dustValPM25<15.4) AQI=(50-0)/(15.4-0)*(dustValPM25-0)+0;
else if(dustValPM25<40.4) AQI=(100-51)/(40.4-15.5)*(dustValPM25-15.5)+51;
else if(dustValPM25<65.4) AQI=(150-101)/(65.4-40.5)*(dustValPM25-40.5)+101;
else if(dustValPM25<150.4) AQI=(200-151)/(150.4-65.5)*(dustValPM25-65.5)+151;
else if(dustValPM25<250.4) AQI=(300-201)/(250.4-150.5)*(dustValPM25-150.5)+201;
else if(dustValPM25<350.4) AQI=(400-301)/(350.4-250.5)*(dustValPM25-250.5)+301;
else AQI=(500-401)/(500.4-350.5)*(dustValPM25-350.5)+401;
lcd.setCursor(0, 0);
lcd.print(dustValPM25);
lcd.print("ug AQI:");
lcd.print(AQI);
lcd.print(" ");
}
if (millis() < time1){
time1=millis();
}
}
void ruanchuankou()
{
while (gps.available() > 0) {
gpstimes=millis();
konghangpanduan=0;
if(GPRMCget){
GPRMC=gps.read();
if(gpsshuchuqiehuanB)Serial.print(GPRMC);
if(GPRMC=='*'){
GPRMClong=jsq1;
GPRMCget=0;
jsq1=0;
}else if(jsq1<79)jsq1++;
}else if(GPGGAget){
GPGGA=gps.read();
if(gpsshuchuqiehuanB)Serial.print(GPGGA);
if(GPGGA=='*'){
GPGGAlong=jsq1;
GPGGAget=0;
jsq1=0;
}else if(jsq1<79)jsq1++;
}else{
tempx=gps.read();
if(gpsshuchuqiehuanB)Serial.print(tempx);
if(jsq1<6)jsq1++;
}
if(jsq1==6){
if(tempx=='M' && tempx=='C'){
GPRMCget=1;
GPGGAget=0;
for(int col=0;col<6;col++)GPRMC=tempx;
for(int col=0;col<6;col++)tempx=0;
}else if(tempx=='G' && tempx=='A'){
GPGGAget=1;
GPRMCget=0;
for(int col=0;col<6;col++)GPGGA=tempx;
for(int col=0;col<6;col++)tempx=0;
}else{
for(int col=0;col<5;col++)tempx=tempx;
jsq1=5;
}
}
}
}
void gpsdechuli()
{
for(int col=1;col<GPRMClong-3;col++){
if(col==1)yihuoyunsuan=GPRMC;
else yihuoyunsuan=yihuoyunsuan ^ GPRMC;
}
if(yihuoyunsuan==0){
jianyan="00";
}else if(yihuoyunsuan>15){
jianyan = String(yihuoyunsuan,HEX);
}else{
jianyan = "0";
jianyan += String(yihuoyunsuan,HEX);
}
jianyan.toUpperCase();
if(jianyan==GPRMC && jianyan==GPRMC ){
jiaoyanjieguoC=1;
}else{
jiaoyanjieguoC=0;
}
jianyan="";
for(int col=1;col<GPGGAlong-3;col++){
if(col==1)yihuoyunsuan=GPGGA;
else yihuoyunsuan=yihuoyunsuan ^ GPGGA;
}
if(yihuoyunsuan==0){
jianyan="00";
}else if(yihuoyunsuan>15){
jianyan = String(yihuoyunsuan,HEX);
}else{
jianyan = "0";
jianyan += String(yihuoyunsuan,HEX);
}
jianyan.toUpperCase();
if(jianyan==GPGGA && jianyan==GPGGA ){
jiaoyanjieguoA=1;
}else{
jiaoyanjieguoA=0;
}
jianyan="";
}
void jiexiGPGGA()
{
jsq2=0;
jsq3=0;
jsq4=0;
jsq5=0;
for(int col=1;col<GPGGAlong-3;col++){
if(GPGGA==',')jsq2++;
if(jsq2==10){
jsq3=col;
col=GPGGAlong-3;
}
if(jsq2<9)jsq4=col;
if(jsq2<7)jsq5=col;
}
if(GPGGA==49)dingweiok=1;
else dingweiok=0;
for(int col=0;col<8;col++)TrackB0=0;
memcpy(TrackB0,GPGGA+jsq4+2, jsq3-jsq4-2);
}
void jiexiGPRMC()
{
jsq2=0;
jsq3=0;
jsq4=0;
jsq5=0;
jsq6=0;
jsq7=0;
for(int col=1;col<GPRMClong-3;col++){
if(GPRMC==',')jsq2++;
if(jsq2==9){
jsq3=col;
col=GPRMClong-3;
}
if(jsq2<8)jsq4=col;
if(jsq2<6)jsq5=col;
if(jsq2<4)jsq6=col;
if(jsq2<2)jsq7=col;
}
if(GPRMC=='A')chinatime();
if(dingweiok){
jingweiduchuli();
hangxiangyusudu();
}else {
weiduA=0;
weiduB=0;
weiduC=0;
jingduA=0;
jingduB=0;
jingduC=0;
}
}
void chinatime()
{
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8s=jianyan.toInt()+8;
if(utc8s>23)rijinwei=1;
utc8s=utc8s%24;
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8f=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8m=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8r=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8y=jianyan.toInt();
jianyan = String(GPRMC);
jianyan += GPRMC;
utc8n=jianyan.toInt();
if(rijinwei){
if(utc8y==2 && utc8r==28){
if(utc8n % 400 == 0 || utc8n % 100 != 0 && utc8n % 4 == 0)utc8r=29;
else {
utc8r=1;
yuejinwei=1;
}
}else{
for(int col=0;col<4;col++){
if(xiaoyue==utc8y)xiaoyueok=1;
}
if(xiaoyueok && utc8r==30){
utc8r=1;
yuejinwei=1;
}else if(!xiaoyueok && utc8r==31){
utc8r=1;
yuejinwei=1;
}else{
utc8r++;
}
}
}
if(yuejinwei && utc8y==12){
utc8y=1;
nianjinwei=1;
}else if(yuejinwei){
utc8y++;
}
if(nianjinwei)utc8n++;
for(int col=0;col<16;col++)tempx=0;
if(utc8n>9){
sprintf(tempx, "%d", utc8n);
memcpy(TrackB4+2,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8n+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8y>9){
sprintf(tempx, "%d", utc8y);
memcpy(TrackB4+5,tempx, 2);
} else{
TrackB4=48;
TrackB4=utc8y+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8r>9){
sprintf(tempx, "%d", utc8r);
memcpy(TrackB4+8,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8r+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8s>9){
sprintf(tempx, "%d", utc8s);
memcpy(TrackB4+11,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8s+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8f>9){
sprintf(tempx, "%d", utc8f);
memcpy(TrackB4+14,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8f+48;
}
for(int col=0;col<16;col++)tempx=0;
if(utc8m>9){
sprintf(tempx, "%d", utc8m);
memcpy(TrackB4+17,tempx, 2);
}else{
TrackB4=48;
TrackB4=utc8m+48;
}
}
void jingweiduchuli()
{
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq7+4, 2);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(weiduA!=gpschangertest){
weiduA=gpschangertest;
gpschanger=1;
}
if(GPRMC==83)weiduxuweitiao=1;
if(GPRMC==87)jingduxuweitiao=1;
memcpy(TrackB1,GPRMC+jsq7+4, 2);
if(gpschangertest<10) {
TrackB1=32;
weiduzhanwei=1;
}else weiduzhanwei=2;
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq7+6, 2);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(weiduB!=gpschangertest){
weiduB=gpschangertest;
gpschanger=1;
}
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq7+9, 4);
if(jsq6-jsq7==12) memcpy(tempx,GPRMC+jsq7+9, 4);
else if(jsq6-jsq7==13)memcpy(tempx,GPRMC+jsq7+9, 5);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(weiduC!=gpschangertest){
weiduC=gpschangertest;
gpschanger=1;
}
fentodu=weiduB*100000+weiduC*10;
fentodu=fentodu/6;
for(int col=0;col<16;col++)tempx=0;
dtostrf(fentodu,6,0,tempx);
if(fentodu>99999){
memcpy(TrackB1+3,tempx, 6);
}else if(fentodu>9999){
TrackB1=48;
memcpy(TrackB1+4,tempx, 5);
}else if(fentodu>999){
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+5,tempx, 4);
}else if(fentodu>99){
TrackB1=48;
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+6,tempx, 3);
}else if(fentodu>9){
TrackB1=48;
TrackB1=48;
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+7,tempx, 2);
}else{
TrackB1=48;
TrackB1=48;
TrackB1=48;
TrackB1=48;
TrackB1=48;
memcpy(TrackB1+8,tempx, 1);
}
if(weiduxuweitiao){
weiduxuweitiao=0;
if(weiduzhanwei==2){
for(int col=0;col<16;col++)tempx=0;
tempx=45;
memcpy(tempx+1,TrackB1, 9);
memcpy(TrackB1,tempx, 10);
}else{
TrackB1=45;
}
}
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq6+4, 3);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(jingduA!=gpschangertest){
jingduA=gpschangertest;
gpschanger=1;
}
memcpy(TrackB2,GPRMC+jsq6+4, 3);
jingduzhanwei=3;
if(gpschangertest<100) {
TrackB2=32;
jingduzhanwei=2;
}
if(gpschangertest<10) {
TrackB2=32;
jingduzhanwei=1;
}
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq6+7, 3);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(jingduB!=gpschangertest){
jingduB=gpschangertest;
gpschanger=1;
}
for(int col=0;col<16;col++)tempx=0;
if(jsq5-jsq6==13)memcpy(tempx,GPRMC+jsq6+10, 4);
else if(jsq5-jsq6==14)memcpy(tempx,GPRMC+jsq6+10, 5);
jianyan =tempx;
gpschangertest=jianyan.toInt();
if(jingduC!=gpschangertest){
jingduC=gpschangertest;
gpschanger=1;
}
jianyan="";
fentodu=jingduB*100000+jingduC*10;
fentodu=fentodu/6;
for(int col=0;col<16;col++)tempx=0;
dtostrf(fentodu,6,0,tempx);
if(fentodu>99999){
memcpy(TrackB2+4,tempx, 6);
}else if(fentodu>9999){
TrackB2=48;
memcpy(TrackB2+5,tempx, 5);
}else if(fentodu>999){
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+6,tempx, 4);
}else if(fentodu>99){
TrackB2=48;
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+7,tempx, 3);
}else if(fentodu>9){
TrackB2=48;
TrackB2=48;
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+8,tempx, 2);
}else{
TrackB2=48;
TrackB2=48;
TrackB2=48;
TrackB2=48;
TrackB2=48;
memcpy(TrackB2+9,tempx, 1);
}
if(jingduxuweitiao){
jingduxuweitiao=0;
if(jingduzhanwei==3){
for(int col=0;col<16;col++)tempx=0;
tempx=45;
memcpy(tempx+1,TrackB2, 10);
memcpy(TrackB2,tempx, 11);
}else if(jingduzhanwei==2){
TrackB2=45;
}else{
TrackB2=45;
}
}
}
void hangxiangyusudu()
{
for(int col=0;col<16;col++)tempx=0;
memcpy(tempx,GPRMC+jsq5+4,jsq4-jsq5-3);
for(int col=0;col<jsq4-jsq5-3;col++){
if(tempx==46){
if(col==1)fentodu=(tempx-48)*1000;
else if(col==2)fentodu=(tempx-48)*10000+(tempx-48)*1000;
else if(col==3)fentodu=(tempx-48)*100000+(tempx-48)*10000+(tempx-48)*1000;
if(jsq4-jsq5-col==5)fentodu=fentodu+(tempx-48)*100;
else if(jsq4-jsq5-col==6)fentodu=fentodu+(tempx-48)*100+(tempx-48)*10;
else if(jsq4-jsq5-col==7)fentodu=fentodu+(tempx-48)*100+(tempx-48)*10+tempx-48;
fentodu=fentodu*1852*0.0001;
col=jsq4-jsq5-3;
}
}
sudu=fentodu*0.01;
for(int col=0;col<8;col++)TrackB5=0;
dtostrf(sudu,1,2,TrackB5);
for(int col=0;col<7;col++)TrackB6=0;
memcpy(TrackB6,GPRMC+jsq4+2, jsq3-jsq4-2);
}
void printDateTime(DateTime dateTime) {
nian=dateTime.year(), DEC;
yue=dateTime.month(), DEC;
ri=dateTime.day(), DEC;
shi=dateTime.hour(), DEC;
fen=dateTime.minute(), DEC;
miao=dateTime.second(), DEC;
}
void DS1307tiquxiaodui()
{
DateTime now = RTC.now();
printDateTime(now);
if(dingweiok){
if(yue != utc8y) shoushiqidong=1;
if(ri != utc8r) shoushiqidong=1;
if(shi != utc8s) shoushiqidong=1;
if(fen != utc8f) shoushiqidong=1;
if(miao-utc8m>2 || utc8m-miao>2) shoushiqidong=1;
if(shoushiqidong){
shoushiqidong=0;
RTC.set(RTC_YEAR, utc8n);
RTC.set(RTC_MONTH, utc8y);
RTC.set(RTC_DAY, utc8r);
RTC.set(RTC_HOUR, utc8s);
RTC.set(RTC_MINUTE, utc8f);
RTC.set(RTC_SECOND, utc8m);
}
}
/*
jsq3=nian*0.1;
jsq3=jsq3%10;
temp2 = jsq3+48, DEC;
jsq3=nian%10;
temp2 = jsq3+48, DEC;
jsq3=yue*0.1;
temp2 = jsq3+48, DEC;
jsq3=yue%10;
temp2 = jsq3+48, DEC;
jsq3=ri*0.1;
temp2 = jsq3+48, DEC;
jsq3=ri%10;
temp2 = jsq3+48, DEC;
*/
jsq3=shi*0.1;
tempx = jsq3+48, DEC;
jsq3=shi%10;
tempx = jsq3+48, DEC;
tempx=':';
jsq3=fen*0.1;
tempx = jsq3+48, DEC;
jsq3=fen%10;
tempx = jsq3+48, DEC;
tempx=':';
jsq3=miao*0.1;
tempx= jsq3+48, DEC;
jsq3=miao%10;
tempx = jsq3+48, DEC;
}
void now1602out()
{
/*GPS模式下的显示情况说明
char TrackB0="-0000.0";
char TrackB1="00.0000000";
char TrackB2="000.0000000";
char TrackB4="2000-00-00T00:00:00";
char TrackB5="0000.00";
char TrackB6="000.00";
lcdxianshi=0显示内容如下
LAT.00.0000000
0123456789012345
LON. 000.0000000
lcdxianshi=1显示内容如下
ASL(G): -0000.0M
0123456789012345
YAW(GPS): 000.00
lcdxianshi=2显示内容如下
00-00T00:00:00
0123456789012345
speed:000.00km/h
I'm so sorry but
0123456789012345
now the GPS lost
*/
if(!dingweiok && lcdxianshi<3){
lcd.setCursor(0, 0);
lcd.print("I'm so sorry but");
lcd.setCursor(0, 1);
lcd.print("now the GPS lost");
}else{
if(lcdxianshi==0){
lcd.setCursor(0, 0);
lcd.print("LAT.");
lcd.print(TrackB1);
lcd.setCursor(0, 1);
lcd.print("LON. ");
lcd.print(TrackB2);
}else if(lcdxianshi==1){
lcd.setCursor(0, 0);
lcd.print("ASL(G): ");
lcd.print(TrackB0);
lcd.print("M ");
lcd.setCursor(0, 1);
lcd.print("YAW(GPS): ");
lcd.print(TrackB6);
lcd.print(" ");
}else if(lcdxianshi==2){
for(int col=0;col<16;col++)tempx=0;
for(int col=0;col<14;col++)tempx=TrackB4;
lcd.setCursor(0, 0);
lcd.print("");
lcd.print(tempx);
lcd.setCursor(0, 1);
lcd.print("speed:");
lcd.print(TrackB5);
lcd.print("km/h ");
}else if(lcdxianshi==3){
lcd.setCursor(0, 1);
lcd.print(tempx);
}
}
}
额,不绑定QQ不能发帖了,那就回帖吧:
gps 取时间,太久没玩了
#include <stdlib.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
String returnStr = "";
SoftwareSerial mySerial(10, 11); // RX, TX
LiquidCrystal_I2C lcd(0x27,16,2);//i2c地址
void setup() {
Serial.begin(19200);
mySerial.begin(19200);
lcd.init(); // lcd初始化
lcd.backlight();//开背光
}
void loop() { // run over and over
while(mySerial.available() > 0){ //gps的 $GPRMC 读取为字符串returnStr PS:gps设定为只输出$GPRMC
returnStr += char(mySerial.read());
delay(2);
}
//处理$GPRMC,长度变量是速度、方向,长度不固定,但是时间、定位标记、经纬度、以及最后的日期长度是固定的具体看GPS文档
if (returnStr.length() > 0){
Serial.print(returnStr);//可有可无,调试才需要
String str0 = returnStr.substring (18,19);//截取gps定位标记
if(str0 == "A"){ //是否已定位
int str_len = returnStr.length();//因为速度、方向长度不固定,所以取总长,倒扣取日期【太久没玩、C++菜鸟,不会分割】
String str_ymd = returnStr.substring (str_len-13,str_len-7);//取日期
int GMT_8_hour =(returnStr.substring (7,9).toInt() + 8 )% 24 ; // hour + 8 模运算,不过日期没改,还是UTC,喜欢折腾可以日进位,但平白增加运算量,感觉不值
String str1 = ":"+ returnStr.substring (9,11) +":"+ returnStr.substring (11,13)+ " " + str_ymd.substring (5,6) +"-"+ str_ymd.substring (2,4) +"-"+str_ymd.substring (0,2); //:minute:second year-month-day
//LCD 16*2 位置少 年只取个位
lcd.setCursor(0,0); // LCD0 @hour:minute:second month-day @11:11:11 7-05-09
lcd.print(GMT_8_hour);
lcd.print(str1);
lcd.setCursor(0,1);
lcd.print(" ");//清屏是非常慢的,还是直接覆盖靠谱
}
else{
lcd.setCursor(0,0);
lcd.print("GPS Lost ");
lcd.setCursor(0,1);
lcd.print(" ");
}
returnStr = "";
}
}
........代码这么长 弘毅 发表于 2017-5-9 13:03
........代码这么长
原来放了太多功能,也太追求细节,现在马虎一下一样过
#include <stdlib.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
String returnStr = "";
SoftwareSerial mySerial(10, 11); // RX, TX
LiquidCrystal_I2C lcd(0x26,16,2);//i2c地址
int liuliangji=12;
int diancifa=13;
unsigned long time1=millis();
boolean jiaohua = 0;
boolean chongfupanduan=0;
boolean test1=1;
int jsqLLx=0;
float shishiliuliang=0;
float leijiliuliang=0;
////////////////////////////////////////
void setup() {
Serial.begin(19200);
mySerial.begin(19200);
lcd.init(); // lcd初始化
lcd.backlight();//开背光
pinMode(liuliangji,INPUT); //流量计接12
pinMode(diancifa, OUTPUT); //电磁阀接13
digitalWrite(diancifa, HIGH);//关闭电磁阀
}
void loop() { // run over and over
while(mySerial.available() > 0){ //gps的 $GPRMC 读取为字符串returnStr PS:gps设定为只输出$GPRMC
returnStr += char(mySerial.read());
delay(2);
}
//处理$GPRMC,长度变量是速度、方向,长度不固定,但是时间、定位标记、经纬度、以及最后的日期长度是固定的具体看GPS文档
if (returnStr.length() > 0){
Serial.print(returnStr);//可有可无,调试才需要
String str0 = returnStr.substring (18,19);//截取gps定位标记
if(str0 == "A"){ //是否已定位
int str_len = returnStr.length();//因为速度、方向长度不固定,所以取总长,倒扣取日期【太久没玩、C++菜鸟,不会分割】
String str_ymd = returnStr.substring (str_len-13,str_len-7);//取日期
int GMT_8_hour =(returnStr.substring (7,9).toInt() + 8 )% 24 ; // hour + 8 模运算,不过日期没改,还是UTC,喜欢折腾可以日进位,但平白增加运算量,感觉不值
int GMT_8_minute = returnStr.substring (9,11).toInt();
String str1 = ":"+ returnStr.substring (9,11) +":"+ returnStr.substring (11,13)+ " " + str_ymd.substring (5,6) +"-"+ str_ymd.substring (2,4) +"-"+str_ymd.substring (0,2); //:minute:second year-month-day
//LCD 16*2 位置少 年只取个位
if (GMT_8_hour == 18 && GMT_8_minute == 55){
jiaohua = 1;
}
if (GMT_8_minute % 2 == 0){
jiaohua = 1;
}
lcd.setCursor(0,0); // LCD0 @hour:minute:second month-day @11:11:11 7-05-09
lcd.print(GMT_8_hour);
lcd.print(str1);
lcd.setCursor(0,1);
lcd.print(" ");//清屏是非常慢的,还是直接覆盖靠谱
}
else{
lcd.setCursor(0,0);
lcd.print("GPS Lost ");
lcd.setCursor(0,1);
lcd.print(" ");
}
returnStr = "";
}
if(jiaohua){
lcd.setCursor(9,0);//LCD 处理
lcd.print("Endless");
lcd.setCursor(0,1);
lcd.print(" ");
unsigned long time0=millis();
unsigned long time1=millis(); //内部循环数秒
digitalWrite(diancifa, LOW); //开(电磁阀)闸放水
while(jiaohua){ //一旦激活浇花,就是死循环,除非满足条件,置0跳出
if(millis()-time0>200000){ //超时跳出
jiaohua = 0;
digitalWrite(diancifa, HIGH);//关闭电磁阀
leijiliuliang = 0;
}
if(leijiliuliang>10){ //流量超出
jiaohua = 0;
digitalWrite(diancifa, HIGH);//关闭电磁阀
leijiliuliang = 0;
}
if(millis()-time1>1000) {
time1=millis();//每一秒还原一次
shishiliuliang=jsqLLx/7.5;//计算实时流量(公式:频率=7.5*流量(L/min))
leijiliuliang=leijiliuliang+(shishiliuliang/60);//(每秒积分)计数累计流量
lcd.setCursor(0,1);
lcd.print(shishiliuliang);
lcd.print("L/m ");
lcd.print(leijiliuliang);
lcd.print("L ");
jsqLLx=0;//清空每秒流量
}else{
int liuliangjidianping =digitalRead(liuliangji);//读取流量计的电平
if(liuliangjidianping == HIGH && !chongfupanduan){ //高电平 且 本次没计数,就计数
jsqLLx++;
chongfupanduan=!chongfupanduan;//本次已计数,则标记已计数
}
if(liuliangjidianping == LOW && chongfupanduan)chongfupanduan=!chongfupanduan;//低电平,且 计数标记为1,则重置计数标记
}
}
}
}
12v 外接供电,目前只控制一个电磁阀,多控制几个,就能定量浇灌了
PS:墙角信号差,GPS难以定位,算了,直接取时间就行
#include <stdlib.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
String returnStr = "";
SoftwareSerial mySerial(10, 11); // RX, TX
LiquidCrystal_I2C lcd(0x23,16,2);//i2c地址
int liuliangji=12;
int diancifa=13;
int Manual_switch=9;
boolean jiaohua = 0;
boolean chongfupanduan=0;
int jsqLLx=0;
float shishiliuliang=0;
float leijiliuliang=0;
////////////////////////////////////////
void setup() {
Serial.begin(19200);
mySerial.begin(19200);
lcd.init(); // lcd初始化
lcd.backlight();//开背光
pinMode(Manual_switch,INPUT); //手动开启9
pinMode(liuliangji,INPUT); //流量计接12
pinMode(diancifa, OUTPUT); //电磁阀接13
digitalWrite(diancifa, HIGH);//关闭电磁阀
}
void loop() { // run over and over
while(mySerial.available() > 0){ //gps的 $GPRMC 读取为字符串returnStr PS:gps设定为只输出$GPRMC
returnStr += char(mySerial.read());
delay(2);
}
//处理$GPRMC,长度变量是速度、方向,长度不固定,但是时间、定位标记、经纬度、以及最后的日期长度是固定的具体看GPS文档
if (returnStr.length() > 0){
int str_len = returnStr.length();//因为速度、方向长度不固定,所以取总长,倒扣取日期【太久没玩、C++菜鸟,不会分割】
String str_ymd = returnStr.substring (str_len-13,str_len-7);//取日期
int GMT_8_hour =(returnStr.substring (7,9).toInt() + 8 )% 24 ; // hour + 8 模运算,不过日期没改,还是UTC,喜欢折腾可以日进位,但平白增加运算量,感觉不值
int GMT_8_minute = returnStr.substring (9,11).toInt();
String str1 = ":"+ returnStr.substring (9,11) +":"+ returnStr.substring (11,13)+ " " + returnStr.substring (18,19) + " "+ str_ymd.substring (2,4) +"-"+str_ymd.substring (0,2) + " "; //:minute:second A month-day
lcd.setCursor(0,0); // LCD0 @hour:minute:second month-day @11:11:11 A 05-09
lcd.print(GMT_8_hour);
lcd.print(str1);
//lcd.setCursor(0,1);
//lcd.print(" ");//清屏是非常慢的,还是直接覆盖靠谱
if (GMT_8_hour == 19 && GMT_8_minute == 55){
jiaohua = 1;
}
returnStr = "";
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
//手动开启程序
int Manual_switch_OK =digitalRead(Manual_switch);
if(Manual_switch_OK == HIGH )jiaohua = 1;
///////////////////////////////////////////////////////////////////////////////////////////////////////
if(jiaohua){
lcd.setCursor(9,0);//LCD 处理
lcd.print("Endless");
//lcd.setCursor(0,1);
//lcd.print(" ");
unsigned long time0=millis();
unsigned long time1=millis(); //内部循环数秒
digitalWrite(diancifa, LOW); //开(电磁阀)闸放水
while(jiaohua){ //一旦激活浇花,就是死循环,除非满足条件,置0跳出
if(millis()-time0>3600000){ //超时跳出
jiaohua = 0;
digitalWrite(diancifa, HIGH);//关闭电磁阀
leijiliuliang = 0;
}
if(leijiliuliang>50){ //流量超出
jiaohua = 0;
digitalWrite(diancifa, HIGH);//关闭电磁阀
leijiliuliang = 0;
}
if(millis()-time1>1000) {
time1=millis();//每一秒还原一次
shishiliuliang=jsqLLx/7.5;//计算实时流量(公式:频率=7.5*流量(L/min))
leijiliuliang=leijiliuliang+(shishiliuliang/60);//(每秒积分)计数累计流量
lcd.setCursor(0,1);
lcd.print(shishiliuliang);
lcd.print("L/m ");
lcd.print(leijiliuliang);
lcd.print("L ");
jsqLLx=0;//清空每秒流量
}else{
int liuliangjidianping =digitalRead(liuliangji);//读取流量计的电平
if(liuliangjidianping == HIGH && !chongfupanduan){ //高电平 且 本次没计数,就计数
jsqLLx++;
chongfupanduan=!chongfupanduan;//本次已计数,则标记已计数
}
if(liuliangjidianping == LOW && chongfupanduan)chongfupanduan=!chongfupanduan;//低电平,且 计数标记为1,则重置计数标记
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
}
python 字符串异或校验,修改gps输出项用的
str_x = 'PSRF109,NMEA19200,NULL38400,GGA1,GLL1,GSA1,GSV1,RMC1,VTG1,USER1'
xx=''
for i in range(0,len(str_x),1):
if i==0:
xx = ord(str_x)
else:
xx = xx ^ ord(str_x)
print(hex(xx))
页:
[1]