(17-10-2020, 08:05)Agregacik napisał(a):(16-10-2020, 20:32)Jarewa0606 napisał(a): SoftwareSerial bazuje na przerwaniach, wiec twój projekt to wielka lipa i jedyna możliwość to zmiana na ardunio megaPowiem tak, wcale nie musi być wielką lipą. Mam taki projekt z 3 SoftwareSerial i płytce nano i działa bardzo dobrze. Wszystko zależy co się chce osiągnąć. W moim przypadku poszczególne SoftwareSerial działają po kolei, czyli w danej chwili tylko jeden pyta i oczekuje odpowiedzi, inne śpią i nie oczekuję od nich działania w tle. Jeżeli ktoś oczekuje, że w tym samym czasie jak działa jeden SoftwareSerial drugi będzie zbierał w buforze dane to się może przeliczyć. Po zainicjowaniu każdego SoftwareSerial czyszczę bufor odbiorczy i dopiero wysyłam zapytania do podpiętego pod niego urządzenie.
Do autora wątku, pokaż poprawiony kod.
Podsyłam poprawiony kod:
Kod:#include <SPI.h> // library for SPI devices (uSD board)
#include <TinyGPS.h> // library for GPS
#include <SoftwareSerial.h> // library for serial comm. with GPS
#include <SD.h> // library for the uSD board
#include <Wire.h> // library for comm. with I2C sensors
#include "DHT.h" // library for DHT temp and humidity sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define DHTPIN A0
const byte chipSelect = 10; // the SD card CS pin is wired to pin 10
TinyGPS gps; // make the tinyGPS object
SoftwareSerial ss(1, 0); // Assign the SoftwareSerial TX and RX pins for the GPS
DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial sgsm(2, 3);
int state = 0;
const int pin = 4;
float gpslat, gpslon;
void setup()
{
// only the GPS uses this
Serial.begin(9600); // this is for displaying data on a serial monitor
// Initialise the DHT sensor
pinMode(chipSelect, OUTPUT); // The CS pin on the SD card
if (!SD.begin(chipSelect)) // see if the card is present and can be initialized
{ while(1); } // Wait forever since we can't write data
}
void loop()
//GPS I KARTA SD
{
ss.begin(9600);
dht.begin();
bool newData = false;
for (unsigned long start = millis(); millis() - start < 1000;) // gather GPS data for 1 second
{
while (ss.available()) // while the GPS data is coming via software serial
{
char c = ss.read(); // read the characters into c
if (gps.encode(c)) newData = true; // Did a new valid sentence come in?
}
}
String dataString = ""; // start with an empty dataString
if (newData) // if a new valid GPS sentence was read
{
float flat, flon, falt; // these next 9 lines parse the GPS data
unsigned long age;
int year;
byte month, day, hour, minute, second, hundredths;
gps.f_get_position(&flat, &flon, &age);
falt = gps.f_altitude();
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
char sz[32];
sprintf(sz, "%02d/%02d/%02d,%02d:%02d:%02d,", month, day, year, hour, minute, second);
File dataFile = SD.open("DANE.csv", FILE_WRITE); // write to this file
dataFile.print("Data i Czas: ");
dataFile.print(sz);
dataFile.print(" Długosc: ");// write date time data to SD card
dataFile.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6); // write GPS data to SD card
dataFile.print(",");
Serial.print(" Długość: ");
Serial.print(flon, 6);
dataFile.print(" Szerokosc: ");
dataFile.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6); // write GPS data to SD card
dataFile.print(",");
Serial.print(" Szerokość: ");
Serial.print(flat, 6);
dataFile.print(" Wysokosc: ");
dataFile.print(falt == TinyGPS::GPS_INVALID_ALTITUDE ? 0.0 : falt, 1); // write GPS data to SD card
dataFile.print(" m,");
Serial.print(" Wysokość: ");
Serial.print(falt, 1);
millis();
//TEMPERATURA i WILGOTNOSC
float temperature; // Temperature variable
float DHTh = dht.readHumidity(); // Read DHT sensor
Serial.print(" Wilgotnosc: ");
Serial.print(DHTh, 2);
dataFile.print(" Wilgotnosc: ");
dataFile.print(DHTh, 2); // write humidity data to SD card
dataFile.print("%,");
float DHTt = dht.readTemperature(); // Read temperature as Celsius (the default)
Serial.print(" Temperatura: ");
Serial.println(DHTt, 2);
dataFile.print(" Temperatura: ");
dataFile.print(DHTt, 2); // write temperature data to SD card and end line
dataFile.println(" st. C ");
dataFile.flush();
dataFile.close();
} // endif (newData)
millis(); // write data to SD card only every few seconds
while (ss.available())
{
//WYSYŁANIE DANYCH SMSEM
sgsm.begin(9600);
int c = ss.read();
if (gps.encode(c))
{
gps.f_get_position(&gpslat, &gpslon);
}
}
if (sgsm.available() > 0) {
String c = sgsm.readString();
c.trim();
if (c.indexOf("GET-GPS") > 0) {
sgsm.print("\r");
millis();
sgsm.print("AT+CMGF=1\r");
millis();
/*Replace XXXXXXXXXX to 10 digit mobile number &
ZZ to 2 digit country code*/
sgsm.print("AT+CMGS=\"+48790208796\"\r");
millis();
//The text of the message to be sent.
sgsm.print("Szerokosc: ");
sgsm.println(gpslat, 6);
sgsm.print("Dlugosc: ");
sgsm.println(gpslon, 6);
millis();
sgsm.write(0x1A);
millis();
}
}
}
Pozbyłem się jednego SoftwareSerial sgps bo używa on tej samej biblioteki co ss więc w kodzie zmieniłem sgps na ss. Po skompilowaniu i wgraniu nadal nic się nie dzieje.
Ocena wątku:
Balon meteo z GPS, czujnikiem temperatury i wilgotności oraz modułem karty SD
|
Przeglądający: 2 gości