Hi everyone,
I'm using an Adfruit Ultimate breakout, a HPMA 115S0 and a RFM95 Lora Radio module on an attiny1604.
after I got proper data from my gps (see problem with GPS module using an attiny 1604 [SOLVED, it was just bad weather] - #16 by alessandro_capresi - Networking, Protocols, and Devices - Arduino Forum) I wanted to add the HPMA 115S0 sensor, but since I had to add a second software serial instance I started to get problems. At the beginning I couldn't send data, so I added another serial.begin() in the loop (HPMAserial.begin(9600)) and it worked, but now the gps doesn't work anymore and I only get 0.00 for latitude and longitude.
I tried using my old sketch and I was able to get proper values for latitude and longitude.
I'm using GitHub - Electronza/HPMA115S0: Arduino library for Honeywell HPMA115S0 particle sensor as a library for the sensor.
here's the code:
#include <hpma115s0.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define gpsPort ssGPS
#include <Arduino.h>
#include "LoRaWAN.h"
#include "secconfig.h"
/******************************
DEFINIZIONI MODULO LoRa
******************************/
#define DIO0 1 //PIN FISICO 3
#define NSS 0 //PIN FISICO 2
RFM95 rfm(DIO0, NSS);
// Definisce la lunghezza del buffer dati
uint8_t Data_Length = 0x10;
// Definisce il layer LoRaWAN
LoRaWAN LoRa = LoRaWAN(rfm);
// Frame Counter per contare ogni Payload inviato
unsigned int Frame_Counter_Tx = 0;
/******************************
DEFINIZIONI MODULO GPS
******************************/
static const int Rx = 4, Tx = 5;
static const uint32_t GPSBaud = 9600;
// The serial connection to the GPS device
SoftwareSerial ssGPS(Rx, Tx);
float lati, longi;
int latid, longid;
float latif, longif;
int pm10d, pm25d;
float pm10f, pm25f;
// The TinyGPS++ object
TinyGPSPlus tinyGPS;
///******************************
// DEFINIZIONI SENSORE PARTICELLARE HPMA115S0
// ******************************/
bool my_status;
float p25;
float p10;
//// Collegamento seriale HPMA115S0
SoftwareSerial HPMAserial(2,3); // RX, TX dell' HPMA115S0
HPMA115S0 my_hpm(HPMAserial);
void setup( ) {
Serial.begin(9600);
//Inizializzo il modulo RFM
rfm.init();
/*Inizializza le chiavi di sessione ed applicazione e l'indirizzo del
nodo per il joining tra Nodo e Network Server attraverso tecnica ABP*/
LoRa.setKeys(NwkSkey, AppSkey, DevAddr);
// HPMA115S0 setting
HPMAserial.begin(9600);
delay(100);
// Stop autosend
my_status = my_hpm.stop_autosend();
delay(500);
//my_status = my_hpm.start_measurement();
delay(1000);
ssGPS.begin(GPSBaud);
}
void loop( ) {
//ssGPS.begin(GPSBaud);
ssGPS.listen();
while (ssGPS.available()){
char c=ssGPS.read();
tinyGPS.encode(c);
if (tinyGPS.location.isUpdated()){
lati=tinyGPS.location.lat();
break;
}
}
unsigned char Data[Data_Length];
//aumento il Frame Counter
Frame_Counter_Tx++;
//Provo a stampare e trasmettere distanza come parte intera e decimale --> sprintf per l'attiny ha funzionalità limitate
char *sgn1 = (lati < 0) ? "-" : "";
latid = lati;
latif = lati - latid;
int latiff = trunc(latif * 100);
//Provo a stampare e trasmettere distanza come parte intera e decimale --> sprintf per l'attiny ha funzionalità limitate
char *sgn2 = (lati < 0) ? "-" : "";
longid = longi;
longif = longi - longid;
int longiff = trunc(longif * 10000);
delay(6000);
// Dati del sensore HPMA (con media pesata)
HPMAserial.begin(9600);
float pm10=0,pm25=0;
//int i;
//for(i=0;i<10;i++)
// {
//
my_status = my_hpm.read(&p25,&p10);
delay(6000);
pm10 = pm10 + p10;
pm25 = pm25 + p25;
//
// }
// pm10=pm10/i;
// pm25=pm25/i;
//Provo a stampare e trasmettere distanza come parte intera e decimale --> sprintf per l'attiny ha funzionalità limitate
pm10d = pm10;
pm25d= pm25;
int a;
//Creo la stringa in cui stampo il buffer Data contenente la stringa di invio
Data_Length = sprintf(Data,"pm10:%d pm2.5:%d Lat:%s%d.%02d Long:%s%d.%04d ", pm10d, pm25d, sgn1, latid, latiff,sgn2, longid, longiff ); // 68 BYTE
//delay(5000);
/*Send_Data già modificato nella libreria; mancavano alcuni parametri che
permettevano la trasmissione a certi SF(Spreading Factor) e BW(Larghezze
di Banda)*/
//A fine sketch sono riportati i canali con SF e BW modificati nella libreria (sono riportati come riferimento per ricordarsene)
//txParameter viene scelto uguale a 5 perchè individua la casistica 5 per
//la trasmissione; quindi trasmetto ad un SF7 con BW di 125 kHz
int txParameter=5;
/*Controllo sulla quantità di pacchetti inviati; considerando i tempi necessari
a sistemare e recuperare il sistema sotto terra, non volevamo inviare pacchetti fino a che le batterie non si fossero drenate;
si è quindi inserito un controllo sul contatore, in modo che quando esso arriva a
2000 la trasmissione si conclude definitivamente.*/
if (Frame_Counter_Tx < 2000)
{
LoRa.Send_Data(Data, Data_Length, Frame_Counter_Tx,txParameter);
}
}
unsigned char NwkSkey[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x014, 0x15, 0x16 };
unsigned char AppSkey[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x014, 0x15, 0x16 };
unsigned char DevAddr[4] = { 0x00, 0x00, 0x00, 0x02 };