19-10-2020, 19:58
Nieco poprawiłem kod (nie wiem, czy można to nazwać poprawą). Dodałem przerwanie do pinu 3 od modułu GSM. Gdy będzie zmiana stanu (połączenie przychodzące lub sms) to wykonać ma się procedura wyslij_dane. Program się uruchamia, zapisuje sobie dane na kartę pamięci, w momencie wykonania połączenie de facto przerywa zapis danych ale już smsa nie wysyła.
Kod:
#include "Wire.h"
#include "SD.h"
#include "SoftwareSerial.h"
#include "TinyGPS.h"
#include "DHT.h"
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define DHTPIN A0
const int chipSelect = 10;
File dataFile;
DHT dht(DHTPIN, DHTTYPE);
TinyGPS gps;
SoftwareSerial ss(6, 7);
SoftwareSerial sgsm(3, 9);
const int pin = 9;
int state = 0;
float gpslat, gpslon;
void setup()
{
Serial.begin(115200);
Serial.print("Initializing SD card...");
pinMode(chipSelect, OUTPUT);
sgsm.begin(9600);
ss.begin(9600);
dht.begin();
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
// Open up the file we're going to log to!
dataFile = SD.open("GPStest.txt", FILE_WRITE);
if (! dataFile) {
Serial.println("error opening GPStest.txt");
// Wait forever since we cant write data
while (1) ;
}
}
void loop()
{
bool newData = false;
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (ss.available())
{
char c = ss.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData)
{
float flat, flon;
unsigned long age;
int year;
byte month, day, hour, minute, second, hundredths;
gps.f_get_position(&flat, &flon, &age);
char sz[32];
float falt = gps.f_altitude();
Serial.print("");
Serial.print("Szerokość: ");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(", ");
Serial.print("Długośc: ");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
Serial.print(", ");
Serial.print("Wysokośc: ");
Serial.print(falt == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : falt, 2);
Serial.print(", ");
dataFile.print("");
dataFile.print("Szerokosc: ");
dataFile.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
dataFile.print(", ");
dataFile.print("Dlugosc: ");
dataFile.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
dataFile.print(", ");
dataFile.print("Wysokosc: ");
dataFile.print(falt == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : falt, 2);
dataFile.print(", ");
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.println(DHTt, 2); // write temperature data to SD card and end line
attachInterrupt(digitalPinToInterrupt(3), wyslij_dane, CHANGE);
}
dataFile.flush();
delay(500);
}
void wyslij_dane()
{
bool newData = false;
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (ss.available())
{
char c = ss.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData)
{
Serial.begin(9600);
sgsm.begin(9600);
ss.begin(9600);
Serial.print("AT+CMGF=1\r");
float flat, flon;
unsigned long age;
int year;
byte month, day, hour, minute, second, hundredths;
gps.f_get_position(&flat, &flon, &age);
char sz[32];
sgsm.print("\r");
sgsm.print("AT+CMGF=1\r");
Serial.print("AT+CMGF=1\r");
/*Replace XXXXXXXXXX to 10 digit mobile number &
ZZ to 2 digit country code*/
sgsm.print("AT+CMGS=\"+48790208796\"\r");
//The text of the message to be sent.
sgsm.print("Latitude :");
sgsm.println(flat, 6);
sgsm.print("Longitude:");
sgsm.println(flon, 6);
sgsm.write(0x1A);
state = 1;
}
if (digitalRead(pin) == LOW) {
state = 0;
}
}