Ciao
Un aggiornamento
sotto l output dello slave
...
<a>
Something wrong with thermocouple!
sending DATA to HC12
#a,N, 20.94,C, 50.19,%, NAN,K,>
codice stazione errata
<b>
codice stazione errata
<c>
codice stazione corretta
...
questo l output del master, dovrei ricevere una stringa di 40 caratteri ma ne ricevo solo una parte
...
sending_SLAVE <a>
#a,N, 20.94,C, >
sizeof(stringa)40
scrivo su file data ora e dati
spedisco a BT
a, 20.94, >
sending_SLAVE <b>
sending_SLAVE <c>
...
perdo quello compreso tra la ultima virgola "," e il fine stringa ">"
grazie per ogni suggerimento
/*
SLAVE
data 230112pm
Do not power over USB. Per datasheet,
power HC12 with a supply of at least 100 mA with
a 22 uF - 1000 uF reservoir capacitor.
Upload code to two Arduinos connected to two computers.
Transceivers must be at least several meters apart to work.
TX slave
*/
#include <SPI.h> //SPI, OLED SPI
#include <Wire.h> //I2C, RTC DS1307 I2C, OLED I2C
#include <SoftwareSerial.h> //HC12
//thermocouple K type MAX31855
#include "Adafruit_MAX31855.h" //termocoppia tipo K vcc 3.3v SPI
#define MAXDO 4
#define MAXCS 5
#define MAXCLK 6
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
//---------------------libreria htu21d
// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)
#include "Adafruit_HTU21DF.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
float t; // HTU21D
float rh; //HTU21D
float ktc; //thermocouple K type
char SLAVE_START; // carattere di start trasmissione
char MASTER_START; //carattere di start stazione MASTER
char END; // carattere di end trasmissione
char sensorNum; //codice della stazione
char rx; //variabile di appoggio
const byte HC12RxdPin = 2; //HC12 "RXD" Pin on HC12
const byte HC12TxdPin = 3; //HC12 "TXD" Pin on HC12
//const byte HC12SetPin = 6; //HC12 "SET" Pin on HC12
SoftwareSerial HC12(HC12TxdPin, HC12RxdPin); // RX nano to HC-12 TX Pin, TX nano to HC-12 RX Pin
char stringa[4] = {0};
int const maxdim = 40;
char data[maxdim] = {0};
char dataK[8] = {0}; //MAX31855 thermocouple K type
char dataT[8] = {0}; //HTU21DF
char dataRH[8] = {0}; //HTU21DF
byte i, j, k, index; //contatori
void setup()
{
Serial.begin(9600); // Open serial port to computer
HC12.begin(9600); // Open serial port to HC12
pinMode(7, OUTPUT); // led di controllo sensore HTu21D connesso
digitalWrite(7, LOW); // turn the LED on (HIGH is the voltage level)
//-------------------MAX31855 thermocouple K type
delay(1000);// wait for MAX chip to stabilize
//Serial.print("Initializing sensor...");
if (!thermocouple.begin())
{
Serial.println("ERROR");
while (1);
}
else
{
Serial.println("MAX31855 thermocouple K type ready");
ktc = 0.0; //inizializzo variabili sensore
}
//----------------------------HTU21D
delay(5000); //attesa risposta
if (!htu.begin())
{
delay(5000); //attesa risposta
Serial.println("01 Couldn't find HTU21D sensor");
digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
while (1);
}
else
{
Serial.println("HTU21D ready");
t = 0.0; //inizializzo variabili sensore
rh = 0.0; //inizializzo variabili sensore
}
//--------------------------Tx sezione HC12
//pinMode(HC12SetPin, OUTPUT);
//digitalWrite(HC12SetPin, HIGH);
i = 0; //inizializzo contatore
j = 0; //inizializzo contatore
k = 0; //inizializzo contatore
index = 0; //inizializzo contatore
SLAVE_START = '<'; //carattere di start codice stazione SLAVE
MASTER_START = '#';//carattere di start stazione MASTER
END = '>'; // carattere di end trasmissione
sensorNum = 'a'; //codice della stazione usare lettere alfabeto a b c d e
delay(1000);
Serial.println("program starts"); //stampa controllo
}
void loop()
{
delay(50);
//-------------HC12
if (HC12.available())
{
rx = HC12.read(); //sono sicuro che esista un carattere dal leggere, altrimenti non sarebbe stata chiamata
if (rx == SLAVE_START) //carattere inizio trasmissione da SLAVE(i)
{
stringa[0] = rx;
index++;
//Serial.print("stringa[0] "); //stampa di controllo
//Serial.println(stringa[0]); //stampa di controllo
}
if (index > 0) //aggiungo dati al vettore
{
k = index++ - 1;
stringa[k] = rx;
}
}
if (stringa[0] == SLAVE_START && stringa[1] != sensorNum && stringa[2] == END)
{
index = 0; //azzero contatore
Serial.println("codice stazione errata"); //stampa di controllo
Serial.println(stringa); //stampa di controllo
//svuoto vettore
stringa[0] = 0;
stringa[1] = 0;
stringa[2] = 0;
stringa[3] = 0;
memset(stringa, 0, 4);
}
if (stringa[0] == SLAVE_START && stringa[1] == sensorNum && stringa[2] == END)
{
index = 0; //azzero contatore
Serial.println("codice stazione corretta"); //stampa di controllo
Serial.println(stringa); //stampa di controllo
//svuoto vettore
stringa[0] = 0;
stringa[1] = 0;
stringa[2] = 0;
stringa[3] = 0;
memset(stringa, 0, 4);
//--------------------termocoppia K type con max 31855
//basic readout test, just print the current temp
//Serial.println("Max31855 thermocouple K");
//Serial.print("Internal Temp = ");
//Serial.println(thermocouple.readInternal());
ktc = thermocouple.readCelsius();//thermocouple K type
dtostrf(ktc, 7, 2, dataK);//temperatura in un vettore
//Serial.print("Temperature Degrees F = ");
//Serial.println(thermocouple.readFahrenheit());
if (isnan(ktc))
{
Serial.println("Something wrong with thermocouple!"); //stampa di controllo
}
else
{
//Serial.print("Temperature K Degrees C = "); //stampa di controllo
//Serial.println(ktc); //stampa di controllo
}
//------------------------HTU21D
if (!htu.begin()) //check if returns are valid, if they are NaN (not a number) then something went wrong!
{
delay(5000);
Serial.println("02 Couldn't find HTU21D sensor");
digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
while (1);
}
else
{
t = htu.readTemperature(); //leggo temperatura
rh = htu.readHumidity(); //leggo umidità
dtostrf(t, 7, 2, dataT); //temperatura in un vettore scritto in dataT
dtostrf(rh, 7, 2, dataRH); //umidità in un vettore scritto in dataRH
i = 0;
data[i] = MASTER_START;
i++;
data[i] = sensorNum;
i++;
data[i] = ',';
i++;
data[i] = 'N';//contatore
i++;
data[i] = ',';
//-------------------------dataT
for (k = 0; k < 7; k++) //scrivo la temperatura HTU21D
{
i++;
data[i] = dataT[k];
}
i++;
data[i] = ',';
i++;
data[i] = 'C'; //temperatura HTU21D
i++;
data[i] = ',';
//-------------------------dataRH
for (k = 0; k < 7; k++) //scrivo la umidità HTU21D
{
i++;
data[i] = dataRH[k];
}
i++;
data[i] = ',';
i++;
data[i] = '%'; //umidita HTU21D
i++;
data[i] = ',';
//--------------------------dataK
for (k = 0; k < 7; k++) //scrivo la temperatura K type
{
i++;
data[i] = dataK[k];
}
i++;
data[i] = ',';
i++;
data[i] = 'K'; //temperatura K type
i++;
data[i] = ',';
i++;
data[i] = END; //carattere di END trasmissione
i++;
data[i] = '\0';
//--------------------------end string DATA
//-------------------------inizio trasmissione al MST master
HC12.println(data); //scrivo su HC12 mando la stringa di dati
Serial.println("sending DATA to HC12 "); //stampa di controllo
Serial.print(data); //stampa di controllo
Serial.println();
}
}
}
/*
* -master con mkr0
data 230112pm
-per ricezione dati htu21d + termocoppia K
-FROM unita' distribuite SLAVE(i) >> TO unità centrale memorizza MASTER
-comunicazione MST SLV con HC12
-MST memorizza su SD
-utilizzo rtc DS1307
-aggiunto schermo oled
-aggiunto modulo BT per traqsmissione ad app dedicata
*/
#include <SPI.h> //SPI, SD SPI, OLED SPI
#include <Wire.h> //I2C, RTC DS1307 I2C, OLED I2C
//---------------------------SERCOM BT
#include <Arduino.h>
#include "wiring_private.h"
//const byte HC12SetPin = 6; // "SET" Pin on HC12
Uart mySerial_BT (&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); // Create the new UART instance assigning it to pin 1 and 0
//--------------------------OLED I2C
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#define I2C_ADDRESS 0x3C // 0X3C+SA0 - 0x3C or 0x3D
#define RST_PIN -1 // Define proper RST_PIN if required
SSD1306AsciiWire oled; //nome display
//-------------------caratteri di comunicazione
char SLAVE_START; //carattere di start codice stazione SLAVE
char MASTER_START; //carattere di start stazione MASTER
char END; // carattere di end trasmissione
char rx;
int const maxdim = 40; //massima dimensuione del vettore dati dallo SLAVE(i)
char stringa[maxdim]; //array dei dati dallo SLAVE(i)
int const maxdim_BT = 26; //massima dimansione array a BT
char send_this[maxdim_BT]; //array dati a BT
//-------------------RTC DS1307 I2C
#include <TimeLib.h>
#include <DS1307RTC.h>
//---------------------Scheda SD SPI
#include <SD.h>
const int chipSelect = SDCARD_SS_PIN; //board mkr0
File datafile;
char logfile[12] = "datalog.txt"; //nome file di log
byte sensorNumTot; //numero totale sensori
char id_sensor[8]; //vettore codice stazione aggiungere le stazioni se serve
char sending_SLAVE[4]; //array codice SLAVE(i)
byte i, j, k, ol; //contatori
//---------------------sezione delay con millis()
unsigned long previousMillis; // will store last time
long interval; //intervallo di campionamento
void setup()
{
Serial.begin(9600); //Open serial port to computer
//-----------------------modulo HC12
Serial1.begin(9600); //Open serial port to computer modulo HC12
//-----------------------modulo BT
mySerial_BT.begin(9600); //Open serial port to computer modulo HC12
//---------------------periferiche I2C
Wire.begin(); //OLED I2C, RTC DS1307 I2C
//---------------------SERCOM BT per bluetooth
pinPeripheral(1, PIO_SERCOM); //Assign RX function to pin 1
pinPeripheral(0, PIO_SERCOM); //Assign TX function to pin 0
delay(500);
//-----------------------OLED I2C
Wire.setClock(400000L);
#if RST_PIN >= 0
{
oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
}
#else // RST_PIN >= 0
{
oled.begin(&Adafruit128x64, I2C_ADDRESS);
}
#endif // RST_PIN >= 0
oled.setFont(Adafruit5x7); //OLED I2C set font
// oled.set2X(); //OLED I2C set font
//--------------------------led allarmi
pinMode(7, OUTPUT);// led card not present
pinMode(8, OUTPUT);// led no RTC
digitalWrite(7, LOW);
digitalWrite(8, LOW);
//---------------------Scheda SD SPI
Serial.println("initialing SD card..."); //stampa di controllo
if (!SD.begin(chipSelect)) //see if the card is present and can be initialized:
{
Serial.println("Card failed, or not present 01"); //don't do anything more
//digitalWrite(7, HIGH);
while (1);
}
Serial.println("SD card initialized...");
SLAVE_START = '<'; //carattere inizio trasmissione stazione SLAVE(i)
MASTER_START = '#'; //carattere di start stazione MASTER
END = '>'; // carattere di end trasmissione per SLAVE(i) e MASTER
sensorNumTot = 3; //numero totale SLAVE(i)
id_sensor[0] = SLAVE_START; //carattere inizio trasmissione stazione SLAVE
id_sensor[1] = 'a'; //aggiungere le stazioni se serve e aggiornare indice vettore
id_sensor[2] = 'b'; //aggiungere le stazioni se serve e aggiornare indice vettore
id_sensor[3] = 'c'; //aggiungere le stazioni se serve e aggiornare indice vettore
id_sensor[4] = 'd'; //aggiungere le stazioni se serve e aggiornare indice vettore
id_sensor[5] = 'e'; //aggiungere le stazioni se serve e aggiornare indice vettore
id_sensor[6] = END; //caratte fine trasmissione
id_sensor[7] = '\0'; //carattere null
Serial.println(id_sensor); //stampa di controllo
char stringa[maxdim] = {0}; //inizializzo vettore dati
//interval = 140000 //= 7 min millis intervallo delle interrogazioni delle stazioni
//interval = 120000 //= 6 min millis intervallo delle interrogazioni delle stazioni
//interval = 100000 //= 5 min millis intervallo delle interrogazioni delle stazioni
//interval = 80000 //= 4 min millis intervallo delle interrogazioni delle stazioni
interval = 3000; //= 15 sec millis intervallo delle interrogazioni delle stazioni
previousMillis = 0;
i = 0; //contatore
j = 0; //contatore
k = 0; //contatore
ol = 0; //contatore per OLED
rx = 0; //buffer di ricezione
delay(2000);
}
void loop()
{
if (millis() - previousMillis >= interval)
{
previousMillis = millis(); //save the last time
//-------------send to SLAVE(i)
i++;
sending_SLAVE[0] = id_sensor[0]; //carattere START trasmissione
sending_SLAVE[1] = id_sensor[i]; //codice stazione SLAVE(i)
sending_SLAVE[2] = id_sensor[6]; //carattere END trasmissione
Serial.print("sending_SLAVE "); //stampa controllo
Serial.println(sending_SLAVE); //stampa controllo
Serial1.println(sending_SLAVE); //stampa su HC12
if (i >= sensorNumTot)
{
i = 0;
}
}
//-----------------receive from SLAVE(i)
delay(10);
if (Serial1.available())
{
rx = Serial1.read(); //sono sicuro che esista un carattere dal leggere, altrimenti non sarebbe stata chiamata
if (rx == MASTER_START) //carattere inizio trasmissione da SLAVE(i)
{
stringa[0] = rx; //contiene il carattere start dalla SLAVE(i)
j++;
}
if (j > 0) //aggiungo dati al vettore
{
k = j++ - 1;
stringa[k] = rx;
}
}
//-------------------------------write scheda SD SPI
if (rx == END) //carattere fine trasmissione da SLAVE(i)
{
Serial.println(stringa); //stampa controllo
Serial.print("sizeof(stringa)"); //stampa conrollo
Serial.println(sizeof(stringa)); //stampa conrollo
while (Serial1.available()) //svuoto buffer ricezione HC12
{
rx = Serial1.read(); //leggo tutto da HC12
}
j = 0; //azzero indice per loop successivo
rx = 0; //svuoto variabile
//---------------------open file su SD
datafile = SD.open("datalog.txt", FILE_WRITE); //apre file
if (datafile) //if the file is available, write to it:
{
Serial.println("scrivo su file data ora e dati"); //stampa controllo
tmElements_t tm;
//-------------------orologio RTC DS1307 I2C
if (RTC.read(tm))
{
if (tm.Hour < 10) //scrive ora
{
datafile.print('0');
}
datafile.print(tm.Hour);
datafile.print(':'); //separatore
if (tm.Minute < 10) //scrive minuto
{
datafile.print('0');
}
datafile.print(tm.Minute);
datafile.print(':'); //separatore
if (tm.Second < 10) //scrive secondo
{
datafile.print('0');
}
datafile.print(tm.Second);
datafile.print(' '); //separatore tra ora e giorno
datafile.print(tm.Day); //scrive giorno
datafile.print('/'); //separatore
datafile.print(tm.Month); //scrive mese
datafile.print('/'); //separatore
datafile.print(tmYearToCalendar(tm.Year));//scrive anno
datafile.print(',');//separatore
datafile.println(stringa); //scrive stringa dati SLAVE(i)
datafile.close();//chiude file
//-----------------scrivo su OLED
oled_screen();
//----------------send to BT
BT_(); //spedisco dati alla app dedicata tramite BT
memset(stringa, 0, sizeof(stringa)); //svuoto array
}
else
{
digitalWrite(8, HIGH); // turn the LED on
if (RTC.chipPresent())
{
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
//digitalWrite(8, HIGH); // turn the LED on
}
else
{
Serial.println("DS1307 read error! Please check the circuitry");
Serial.println();
//digitalWrite(8, HIGH); // turn the LED on
}
}
}
else
{
Serial.println("Card failed, or not present 02");// don't do anything more
//digitalWrite(7, HIGH); // turn the LED on
}
}
}
//------Attach the interrupt handler to the SERCOM BT
void SERCOM3_Handler() //modulo HC05
{
mySerial_BT.IrqHandler();
}
//----------------OLED per stampa dati
void oled_screen()
{
oled.setFont(System5x7);
oled.clear();
oled.setCursor(0, 0);
oled.print("ST");
oled.setCursor(14, 0);
oled.print(stringa[1]); //codice stazione SLAVE(i)
oled.setCursor(35, 0);
oled.print("T");
oled.setCursor(58, 0);
oled.print("HR%");
oled.setCursor(103, 0);
oled.print("K");
oled.println();
oled.println();
//---------------stampa temperatura
oled.setCursor(10, 2); // Start at top-left corner
ol = 7;
oled.print(stringa[ol]);
oled.print(stringa[ol + 1]);
oled.print(stringa[ol + 2]);
oled.print(stringa[ol + 3]);
oled.print(stringa[ol + 4]);
//--------------------stampa umidità
oled.setCursor(45, 2); // Start at top-left corner
ol = 17;
oled.print(stringa[ol]);
oled.print(stringa[ol + 1]);
oled.print(stringa[ol + 2]);
oled.print(stringa[ol + 3]);
oled.print(stringa[ol + 4]);
//-----------------stampa temperatura K
oled.setCursor(80, 2); // Start at top-left corner
ol = 27;
oled.print(stringa[ol]);
oled.print(stringa[ol + 1]);
oled.print(stringa[ol + 2]);
oled.print(stringa[ol + 3]);
oled.print(stringa[ol + 4]);
}
//-----------------send_this to BT bluetooth
void BT_()
{
send_this[0] = stringa[1];
send_this[1] = ',';
send_this[2] = stringa[5];
send_this[3] = stringa[6];
send_this[4] = stringa[7];
send_this[5] = stringa[8];
send_this[6] = stringa[9];
send_this[7] = stringa[10];
send_this[8] = stringa[11];
send_this[9] = ',';
send_this[10] = stringa[15];
send_this[11] = stringa[16];
send_this[12] = stringa[17];
send_this[13] = stringa[18];
send_this[14] = stringa[19];
send_this[15] = stringa[20];
send_this[16] = stringa[21];
send_this[17] = ',';
send_this[18] = stringa[25];
send_this[19] = stringa[26];
send_this[20] = stringa[27];
send_this[21] = stringa[28];
send_this[22] = stringa[29];
send_this[23] = stringa[30];
send_this[24] = stringa[31];
Serial.println("spedisco a BT");
Serial.println(send_this); //stampa controllo
mySerial_BT.println(send_this); //stampa su BT
}