Hi guys,
Using SoftwareSerial to manipulate some wireless transceivers (SX1276) and can't quite grasp what I'm doing wrong. I'm sending a payload that's usually at or around 40 characters once a second at 9600bps. I'm having trouble figuring out how to send numbers (in my case, always integers). They are always received as "", "?", or not at all, unless I send it as a string (like trx.write("1023")) but I'm not sure how that helps in the context of sending integer variables.
Transmitter code:
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Adafruit_ssd1306syp.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme(8, 9, 10, 11); // BME280 || VIN = 5V, GND = GND, 3Vo = N/C, CS = D8, SDI = D9, SDO = D10, SCK = D11
SoftwareSerial trx(4,5); // SX1276 || BLU/RXD = D5, YEL/TXD = D4, BLK/GND = GND, RED/VCC = 5V
Adafruit_ssd1306syp display(7,6); // OLED DISPLAY || VCC = 3.3V, GND = GND, SDA = D7, SCL = D6
// TRANSDUCER || RED = 5V, BLK = GND, WHT = A2
const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char protocol[5] = "AAAC", channel[5];
unsigned short freqID, netID;
void setup() {
Serial.begin(9600);
bme.begin();
trx.begin(9600);
trx.flush();
setChannelID();
display.initialize();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10,0);
display.println("TRX-1276");
display.update();
}
void loop() {
txData();
delay(1000);
}
void txData() {
short pressure = (short)analogRead(A2);
short temperature = (short)(bme.readTemperature() + .5);
short humidity = (short)(bme.readHumidity() + .5);
short barometric = (short)(((bme.readPressure() * 0.000145037738) * 100) + .5);
short voltage = (short)1023;
long runtime = (long)(millis()/1000);
trx.write('I'); // COMMUNICATION ID
Serial.print('I');
for(short i = 0; i < 4; i++){
trx.write(channel[i]);
Serial.print(channel[i]);
}
trx.write(freqID);
Serial.print(freqID);
trx.write(netID);
Serial.print(netID);
trx.write('K'); // KP VERSION NAME
Serial.print('K');
for(short i = 0; i < 4; i++){
trx.write(protocol[i]);
Serial.print(protocol[i]);
}
trx.write('P'); // ADC-READ PRESSURE
Serial.print('P');
if(pressure <= 0){
trx.write('0');
Serial.print('0');
} else if(pressure >= 1023){
trx.write("1023");
Serial.print("1023");
} else {
trx.write(pressure);
Serial.print(pressure);
}
trx.write('T'); // DEGREES CENTIGRADE
Serial.print('T');
if(temperature <= -99){
trx.write("-99");
Serial.print("-99");
} else if(temperature >= 99){
trx.write("99");
Serial.print("99");
} else {
trx.write(temperature);
Serial.print(temperature);
}
trx.write('H'); // RELATIVE HUMIDITY
Serial.print('H');
if(humidity <= 0){
trx.write('0');
Serial.print('0');
} else if(humidity >= 99){
trx.write("99");
Serial.print("99");
} else {
trx.write(humidity);
Serial.print(humidity);
}
trx.write('B'); // BAROMETRIC PSI
Serial.print('B');
if(barometric <= 0){
trx.write('0');
Serial.print('0');
} else if(barometric >= 9999){
trx.write("9999");
Serial.print("9999");
} else {
trx.write(barometric);
Serial.print(barometric);
}
trx.write('V'); // BATTERY VOLTAGE
Serial.print('V');
if(voltage <= 0){
trx.write('0');
Serial.print('0');
} else if(voltage >= 1023){
trx.write("1023");
Serial.print("1023");
} else {
trx.write(voltage);
Serial.print(voltage);
}
trx.write('R'); // TOTAL RUNTIME
Serial.print('R');
if(runtime <= 0){
trx.write('0');
Serial.print('0');
} else if(runtime >= 86401){
trx.write("86401");
Serial.print("86401");
} else {
trx.write(runtime);
Serial.print(runtime);
}
trx.write('E'); // END TRANSMISSION
Serial.println('E');
return;
}
void setChannelID() {
randomSeed(analogRead(0));
channel[0] = alpha[random(1,26)]; // Soft ID 1
channel[1] = alpha[random(0,26)]; // Soft ID 2
channel[2] = alpha[random(0,26)]; // Soft ID 3
channel[3] = alpha[random(0,26)]; // Soft ID 4
freqID = char(random(1,10)); // Freq ID
netID = char(random(1,10)); // Net ID
return;
}
Receiver code:
#include <SoftwareSerial.h>
SoftwareSerial trx(4,5);
void setup() {
Serial.begin(9600);
trx.begin(9600);
}
void loop() {
if(trx.available() > 0){
Serial.print(char(trx.read())); // Convert from ASCII ID to char and print
delay(1); // For stability
}
}
Typical receiver output:
INBGYKAAACPhTHB⸮V1023RE
What's strange is that I can print those number values into the Serial console just fine, not "write()" them. Any guidance is appreciated.
TY&BR,
Chris