Can't get my GPS and PM sensor to work together

Hello.
I’m trying to code an air quality sensor. I need to measure the temperature, humidity, PM25, PM10 and GPS location. I can’t get my PM sensor to work with my GPS.

I am using an Arduino Uno board, a U- blox NEO -6M GPS Module and an SDS011 nova PM sensor.

I think that the PM sensor can’t work with the TinyGPS++ library because I can get all the other sensors to work without the TinyGPS++ library. Or it may be something else.

Please help, I am really new to coding.

#include <SoftwareSerial.h>
#include <SDS011.h> //PM
#include <SD.h> //SD
#include <SimpleDHT.h> //temp
#include <TinyGPS++.h>//GPS

#define PM_TX 2 //PM
#define PM_RX 3 //PM
#define SD_CS_PIN 10 //SD
#define DHTPIN 9 //TEMP

#define LED_RED A1 //lys
#define LED_GREEN A0 //lys

#define GPS_RX 7
#define GPS_TX 6

SoftwareSerial gpsCom(GPS_RX, GPS_TX);

TinyGPSPlus gps;
SDS011 sds;
File file;
SimpleDHT22 dhtSensor;

void setup() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);

sds.begin(PM_TX,PM_RX);
Serial.begin(9600);
//gpsCom.begin(9600);

SD.begin(SD_CS_PIN); //starter SD
char filename= “TESTER123.txt”; //gir filen navn

if(SD.exists(filename)){ //skriv i eksiterende fil
file = SD.open(filename, O_WRITE | O_APPEND);
Serial.println(“1”);
} else {
file = SD.open(filename, O_CREAT | O_WRITE);
Serial.println(“Filen ble lagd”);
Serial.println(" ");
}
file.flush();
}

void loop() {
delay(500)//PM
float pm25,pm10;
int error=sds.read(&pm25,&pm10);
//Serial.println(“Could not read air data _pre_if_test: “+String(error));
if(!error) {
Serial.print(pm25);
Serial.print(”/t”);
Serial.println(pm10);
}else {
Serial.println("Could not read air data: "+String(error));
}
file.flush();
delay(1000);

//TEMP
float temperature = 0.0;
float humidity = 0.0;

dhtSensor.read2(DHTPIN,&temperature, &humidity, NULL);
Serial.print(temperature);
Serial.print(“C”);
Serial.print("/t");
Serial.print(humidity);
Serial.print("%");
Serial.println();
file.flush();
delay(1000);

//GPS
gpsCom.listen();
bool gpsEncodeComplete = false;
do {
if (!gpsCom.available()) {
// No new data available.
// Immediately jump to next iteration
continue;
}
gpsEncodeComplete = gps.encode(gpsCom.read());
if (!gpsEncodeComplete) {
// Data is incomplete,
// Jump to next iteration and try again
continue;
}
} while (!gpsEncodeComplete); // Loop until gps data was successfully read and encoded from GPS module

bool gpsValid = gps.location.isValid();
bool gpsUpdated = gps.location.isUpdated();
bool isUseful = gpsValid && gpsUpdated;
if (!isUseful) {
// No valid position.
// I.e. no GPS fix.
Serial.println(“No valid GPS position”);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_RED, LOW);
return;
}

digitalWrite(LED_GREEN, HIGH);
delay(500);
digitalWrite(LED_GREEN, LOW);

Serial.print(“Time: “);
Serial.print(gps.date.day());
Serial.print(”.”);
Serial.print(gps.date.month());
Serial.print(".");
Serial.print(gps.date.year());
Serial.print(" “);
Serial.print(gps.time.hour());
Serial.print(”:");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.print(gps.time.second());
Serial.print(".");
Serial.print(gps.time.centisecond());

Serial.print("\t");

Serial.print(“Latitude: “);
Serial.print(gps.location.lat(), 6); // Latitude in degrees
Serial.print(”\t”);
Serial.print("Longitude: ");
Serial.print(gps.location.lng(), 6); // Longitude in degrees

Serial.println();

file.flush();
delay(1000);
}

riktig_kode.ino (3.1 KB)

Din jobb! (du vil få hint i Tromsø) Koden din er en god start.

(school project)
You can compare with this example

/*
   Kode for logging av 'værdata'
   Bruker støvsensor, GPS, hygrometer og termometer, Tid fra GPS.
   Logger til SD-kort hver 10.sek.
   KNs forslag
   .. jeg har ikke alle detaljer om de to lysdiodene...
   .. du burde jo brukes til 'feil'-situasjoner!
   Denne koden er ikke prøve på utstyret DU har
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
//#include <stdlib.h> // tror ikke denne trengs !!! (kn)
#include <SDS011.h>
#include <SD.h>
#include <SimpleDHT.h>
// definisjoner  'pinnebruk'
#define PM_TX 2
#define PM_RX 3
#define GPS_TX 6
#define GPS_RX 7
#define DHTPIN 5 // skal være pin 9 ?
#define SD_PIN 10
#define LED_GREEN A0
#define LED_WHITE 13 // til testing (innebygget diode)
// #define LED_WHITE A1 // (OBS: endret fra pin_A1 til A1)
#define GPSBaud  9600
#define intervall 10000 // 10 sekunder


// ****** globale variable ********************************
unsigned long nu, neste; //
char filename[] = "NVK_01.csv";  // Narvik01,02 osv er csv for "kommaseparete verdier"

// ****** definer forekomster ****************************
SimpleDHT22 dht22;
File file;
TinyGPSPlus gps;
SoftwareSerial ss(GPS_RX, GPS_TX);
SDS011 sds;

//************************
void setup()
//************************
{
  pinMode(LED_WHITE, OUTPUT); // use ??
  pinMode(LED_GREEN, OUTPUT); // use ??
  pinMode(SD_PIN, OUTPUT);
  sds.begin(PM_TX, PM_RX); //   *** OBS ***  er dette riktig rekkefølge på parameterne ??  vanlig er rx,tx
  ss.begin(GPSBaud);
  Serial.begin(9600);
  if (!SD.begin(SD_PIN)) // sjekk om alt OK med minnekortet.. Hvis IKKE OK BLINK !!!!
    while (1) // if no SD.. HANG!!
    {
      digitalWrite(LED_WHITE, 1); delay(100);
      digitalWrite(LED_WHITE, 0); delay(100);
    }
  file = SD.open(filename, O_WRITE | O_CREAT);
  Serial.print("Time, Latitude, Longitude, PM25, PM10, Temperature, Humidity\n");
  file.print("Time, Latitude, Longitude, PM25, PM10, Temperature, Humidity\n");
  file.close();
}  //  END ********** SETUP ************


//***************************
void loop()
//***************************
{
  ss.listen(); // først: snakk med gps'en
  nu = millis();  // se på klokka
  if (nu > neste) // på tide å gjøre en måling ??
  {
    file = SD.open(filename, O_WRITE | O_CREAT);
    neste += intervall; // noter når 'neste' logging skal gjøres. !! rollback etter ca 57 dager
    if (ss.available()) gps.encode(ss.read());
    if (gps.location.isValid())
    {
      digitalWrite(LED_GREEN, HIGH);
      // Date
      Serial.print("Date: ");
      Serial.print(gps.date.day());    Serial.print('/');
      Serial.print(gps.date.month());  Serial.print('/');
      Serial.print(gps.date.year());   Serial.print(" ");
      Serial.print(gps.time.hour());   Serial.print(':');
      Serial.print(gps.time.minute()); Serial.print(':');
      Serial.print(gps.time.second()); Serial.print(',');
      file.print(gps.date.day());     file.print('/');
      file.print(gps.date.month());   file.print('/');
      file.print(gps.date.year());    file.print(' ');
      file.print(gps.time.hour());    file.print(':');
      file.print(gps.time.minute());  file.print(':');
      file.print(gps.time.second());  file.print(',');
      file.flush(); // ************* ta bort - unødv. *************
      //-------------------
      // Latitude.
      Serial.print(" Latitude: ");
      Serial.print(gps.location.lat(), 6); Serial.print(',');
      //Satt inn 10.01.18
      file.print(gps.location.lat(), 6);   file.print(',');
      //-------------
      // longitude
      Serial.print(" Longitude: ");
      Serial.print(gps.location.lng(), 6); Serial.println(',');
      //Satt inn 10.01.18
      file.print(gps.location.lng(), 6);   file.print(',');
    }
    else // no data
    {
      Serial.println ("no GPS data.. ");
      file.print("0:0,0.0,0.0,");  // i fila.. hvis ikke fix for gps'en
    }
    // så: ********** støvmengden *************************************
    // sds.listen();  // denne er løst i biblioteket??
    float pm25, pm10;
    int error = sds.read(&pm25, &pm10);
    if (error == 0) // hvis alt OK
    {
      Serial.print("PM 2.5 ");
      Serial.println(pm25);
      Serial.print("PM 10 ");
      Serial.println(pm10);
      //Satt inn 10.01.18
      file.print(pm25);  file.print(',');
      file.print(pm10);  file.print(',');
    }
    else
    {
      Serial.println("no PM-data");
      file.print("-1,-1,"); // indikér missing data for PM sensor
    }
    // ******* temp/fukt ******************************************
    float temperature = 0;
    float humidity = 0;
    error = dht22.read2(DHTPIN, &temperature, &humidity, NULL);
    if (error == 0) // alt OK ?
    {
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println("C");
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.println("%");
      file.print(temperature);  file.print(',');
      file.println(humidity);
      //------------------------
      delay(500);  // hvorfor ???  ta bort
      digitalWrite(LED_GREEN, LOW);  // ********** disse diodene må ordnes..
    }
    else // temp error
    {
      Serial.println("no Temp-data");
      file.println("-1,-1"); // indikér missing data for tmp sensor (
    }
    file.close();  // file lukkes og directory oppdateres
  }// slutt 'denne' logg-linjen
} // slutt code

The SDS011 library uses SoftwareSerial. If you look at the SoftwareSerial documentation you will see that it can only listen to one port at a time. Arduino - SoftwareSerial

If you don't mind slower data rates you can alternate between receiving SDS011 updates and GPS updates. Retry the SDS011 read until it is successful. Then retry the GPS reading until THAT is successful.

If you don’t mind slower data rates you can alternate between receiving SDS011 updates and GPS updates. Retry the SDS011 read until it is successful. Then retry the GPS reading until THAT is successful.

Note that in both cases, you WILL lose data. If that matters, you can get a Mega, with 4 hardware serial ports or you can investigate alternatives to SoftwareSerial, such as AltSoftSerial and NeoSWSerial.