Probems with Serial1


#1

Hey Guys here Is my code I am having communicating with a LoRa CLick2 device …I confirmed it communicates by hooking an Arduino to it …But the particl doesn’t seem to want to send the commands please have a look at the code and let me know whats up…

// This #include statement was automatically added by the Particle IDE.
#define LTCADDR B1100111//Table 1 both LOW (7bit address)
char B1100111;
byte ADCvinMSB, ADCvinLSB, curSenseMSB, curSenseLSB, AinVMSB, AinVLSB;
unsigned int ADCvin, ADCcur, AinV;
float inputVoltage, ADCvoltage, current10, current1, current0p1, current0p01;

int     set_port  = 1;
int incomingByte = 0;
// Some standard ports that depend on the layout of the Marvin
long    defaultBaudRate = 57600;
int     reset_port = 5;
int     RN2483_power_port = 6; //Note that an earlier version of the Marvin doesn't support seperate power to RN2483
int     led_port = 7;


//*** Set parameters here BEGIN ---->
String  set_nwkskey = "923d858b2afe5cc5dc768b8b46a8932a";
String  set_appskey = "e84d3cdd6b16bcb81bba3e0eb8e728a2";
String  set_devaddr = "0272dff9";
String  set_appeui = "fedcba9876543223";




void setup() {

  Serial.begin(9600);
  Serial1.begin(57600);
  Particle.process();
  pinMode(led_port, OUTPUT); // Initialize LED port  
  Wire.begin();


}




void loop() {
    
 
        
  char message[120];
  //================================

  Wire.beginTransmission(LTCADDR);//first get Input Voltage - 80V max
  Wire.write(0x1E);
  Wire.endTransmission(false);
  Wire.requestFrom(LTCADDR, 2, true);
  delay(1);
  ADCvinMSB = Wire.read();
  ADCvinLSB = Wire.read();
  ADCvin = ((unsigned int)(ADCvinMSB) << 4) + ((ADCvinLSB >> 4) & 0x0F);//formats into 12bit integer
  inputVoltage = ADCvin * 0.025; //25mV resolution

  Wire.beginTransmission(LTCADDR);//get ADC Input 2V max
  Wire.write(0x28);
  Wire.endTransmission(false);
  Wire.requestFrom(LTCADDR, 2, true);
  delay(1);
  AinVMSB = Wire.read();
  AinVLSB = Wire.read();
  AinV = ((unsigned int)(AinVMSB) << 4) + ((AinVLSB >> 4) & 0x0F);//12 bit format
  ADCvoltage = AinV * 0.5E-3; //500uV resolution

  Wire.beginTransmission(LTCADDR);//get sense current
  Wire.write(0x14);
  Wire.endTransmission(false);
  Wire.requestFrom(LTCADDR, 2, true);
  delay(1);
  curSenseMSB = Wire.read();
  curSenseLSB = Wire.read();
  ADCcur = ((unsigned int)(curSenseMSB) << 4) + ((curSenseLSB >> 4) & 0x0F);//12 bit format
  //gets voltage across, 25uV resolution, then this converts to voltage for each sense resistor
  current10 = ADCcur * (25E-3) / 10.0; //10mA max, unit is mA
  current1 = ADCcur * (25E-3) / 1.0; //100mA max, unit is mA
  current0p1 = ADCcur * (25E-3) / 0.1; //1A max, unit is mA
  current0p01 = ADCcur * (25E-6) / 0.01;//10A max, unit is A

  //Print everything out
  Serial.print("Vin:25mV/80V>");
  Serial.print(inputVoltage, 2);
  Serial.print("V 10ohm:2.5uA/10mA>");
  Serial.print(current0p01, 3);
  Serial.print("A ADC:0.5mV/2V>");
  Serial.print(ADCvoltage, 4);
  Serial.println("V");



  delay(1000);


  send_LoRa_data(set_port, String(inputVoltage,0)); //send temp / hum as rounded int over lora
  delay(10000);                                  
  send_LoRa_data(set_port, String(current0p01,0));       //send_LoRa_data(set_port, String(tempdec) + "F" + String(humdec)); //send temp / hum as 4 digit integer (decimals included)
  delay(10000); 
  //send_LoRa_data(set_port, String("123"));
  //delay(10000); 
  blinky();
  delay(1000);
  read_data_from_LoRa_Mod();

}

void InitializeSerials(int baudrate)
{
  delay(1000);
  print_to_console("Serial ports initialised");
}

void initializeRN2483(int pwr_port, int rst_port)
{
  //Enable power to the RN2483
  pinMode(pwr_port, OUTPUT);
  digitalWrite(pwr_port, HIGH);
  print_to_console("RN2483 Powered up");
  delay(1000);
  
  //Disable reset pin
  pinMode(rst_port, OUTPUT);
  digitalWrite(rst_port, HIGH);

  //Configure LoRa module
  send_LoRa_Command("sys reset");
  read_data_from_LoRa_Mod();

  send_LoRa_Command("radio set crc off");
  read_data_from_LoRa_Mod();
  delay(1000);
  send_LoRa_Command("mac set nwkskey " + set_nwkskey);
  read_data_from_LoRa_Mod();
  delay(1000);
  send_LoRa_Command("mac set appskey " + set_appskey);
  read_data_from_LoRa_Mod();
  delay(1000);
  send_LoRa_Command("mac set devaddr " + set_devaddr);
  read_data_from_LoRa_Mod();
  delay(1000);

  send_LoRa_Command("mac set adr on");
  read_data_from_LoRa_Mod();
  delay(1000);
  send_LoRa_Command("mac save");
  read_data_from_LoRa_Mod();
  delay(1000);
  send_LoRa_Command("mac join abp");
  read_data_from_LoRa_Mod();
  delay(1000);
}

void print_to_console(String message)
{
  Serial.println(message);
}

void read_data_from_LoRa_Mod()
{
  if (Serial1.available()) {
    String inByte = Serial1.readString();
    Serial.println(inByte);
  }

}

void send_LoRa_Command(String cmd)
{
  print_to_console("Now sending: " + cmd);
  Serial1.printf(cmd);
  Serial1.readStringUntil('\n');
  delay(500);
}

void send_LoRa_data(int tx_port, String rawdata)
{
  send_LoRa_Command("mac tx cnf " + String(tx_port) + String(" ") + rawdata);
}


void blinky()
{
  digitalWrite(led_port, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                     // wait for a second
  digitalWrite(led_port, LOW);    // turn the LED off by making the voltage LOW
  delay(500);                     // wait for a second
  digitalWrite(led_port, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                     // wait for a second
  digitalWrite(led_port, LOW);    // turn the LED off by making the voltage LOW
  delay(500);  
}

#2

This is not how you write binary literals in C/C++. This might work on Arduino, but the correct syntax would be 0b1100111.

If it worked that way it would also be very obscure to have a variable char B1100111; that had the name of a binary literal.
That’s also the reason why you don’t get a build error when using LTCADDR because it just inserts the variable name in the respective calls, but the content of that variable is not the binary address you expect to pass to the functions.


#3

Yea that’s not my problem …The problem is with serial1 it’s not transmitting or receiving the I2C works fine


#4

That’s surprising tho’

But in that case you may want to cut unrelated distractions from the code - having unaddressed issues present will just diverge attention from what you really want answered.
Also having one part of a project potentially not work correctly makes debugging other issues more difficult.

One rule of thumb for debugging hard to locate issues is to isolate the one particular one and focus on that one and only one at a time.

Also the symptom description isn’t very revealing

Also how have you wired the device?
Is the 3.3V TX signal accepted by the device?
Can you check the signal output (e.g. with Logic Analyzer or oscilloscope)?

BTW, there is one other distraction. We see this in your code

But there actually is no #include statement to be found.


#5

good point …I checked with a meter the device is getting 3.3 and ground …I don’t have an oscilloscope though it would be handy …


#6

What do I type before and after the code to make it go into its own window


#7

Some tips can be found here


#8

ok here is what I have for programming

int     set_port  = 1;
int incomingByte = 0;
// Some standard ports that depend on the layout of the Marvin
long    defaultBaudRate = 57600;
int     reset_port = 5;
int     RN2483_power_port = 6; //Note that an earlier version of the Marvin doesn't support seperate power to RN2483
int     led_port = 7;


//*** Set parameters here BEGIN ---->
String  set_nwkskey = "923d858b2afe5cc5dc768b8b46a8932a";
String  set_appskey = "e84d3cdd6b16bcb81bba3e0eb8e728a2";
String  set_devaddr = "0272dff9";
String  set_appeui = "fedcba9876543223";




void setup() {

  Serial.begin(57600);
  Serial1.begin(57600,SERIAL_9N1);
  Particle.process();
  pinMode(led_port, OUTPUT); // Initialize LED port  
  Wire.begin();


}

#9
void loop() {
  char message[120];
  //================================

//  send_LoRa_data(set_port, String(inputVoltage,0)); //send temp / hum as rounded int over lora
//  delay(10000);                                  
//  send_LoRa_data(set_port, String(current0p01,0));       //send_LoRa_data(set_port, String(tempdec) + "F" + String(humdec)); //send temp / hum as 4 digit integer (decimals included)
//  delay(10000); 
  send_LoRa_data(set_port, String("123"));
  Serial.println(message);
  read_data_from_LoRa_Mod();
  delay(10000); 
  blinky();
  delay(1000);
  read_data_from_LoRa_Mod();

}

void InitializeSerials(int baudrate)
{
  delay(1000);
  print_to_console("Serial ports initialised");
}

void initializeRN2483(int pwr_port, int rst_port)
{
  //Enable power to the RN2483
  pinMode(pwr_port, OUTPUT);
  digitalWrite(pwr_port, HIGH);
  print_to_console("RN2483 Powered up");
  delay(1000);
  
  //Disable reset pin
  pinMode(rst_port, OUTPUT);
  digitalWrite(rst_port, HIGH);

  //Configure LoRa module
  send_LoRa_Command("sys reset");
  read_data_from_LoRa_Mod();

  send_LoRa_Command("radio set crc off");
  read_data_from_LoRa_Mod();

  send_LoRa_Command("mac set nwkskey " + set_nwkskey);
  read_data_from_LoRa_Mod();

  send_LoRa_Command("mac set appskey " + set_appskey);
  read_data_from_LoRa_Mod();

  send_LoRa_Command("mac set devaddr " + set_devaddr);
  read_data_from_LoRa_Mod();


  send_LoRa_Command("mac set adr on");
  read_data_from_LoRa_Mod();

  send_LoRa_Command("mac save");
  read_data_from_LoRa_Mod();

  send_LoRa_Command("mac join abp");
  Serial.println(incomingByte);

}

void print_to_console(String message)
{
  Serial.println(message);
}

void read_data_from_LoRa_Mod()
{
  if (Serial1.available()) {
    String inByte = Serial1.readString();
    Serial.println(inByte);
  }

}

void send_LoRa_Command(String cmd)
{
  print_to_console("Now sending: " + cmd);
  Serial1.print(cmd);
  Serial1.readStringUntil('\n');
  delay(500);
}

void send_LoRa_data(int tx_port, String rawdata)
{
  send_LoRa_Command("mac tx cnf " + String(tx_port) + String(" ") + rawdata);
}


void blinky()
{
  digitalWrite(led_port, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                     // wait for a second
  digitalWrite(led_port, LOW);    // turn the LED off by making the voltage LOW
  delay(500);                     // wait for a second
  digitalWrite(led_port, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                     // wait for a second
  digitalWrite(led_port, LOW);    // turn the LED off by making the voltage LOW
  delay(500);  
}

#10

if anyone can help with this it would be awesome


#11

Now, I have no experience with your particular LoRa module, but it occurs to me that here:

  send_LoRa_Command("mac set adr on");
  read_data_from_LoRa_Mod();

  send_LoRa_Command("mac save");
  read_data_from_LoRa_Mod();

  send_LoRa_Command("mac join abp");
  Serial.println(incomingByte);

coupled with this function:

void read_data_from_LoRa_Mod()
{
  if (Serial1.available()) {
    String inByte = Serial1.readString();
    Serial.println(inByte);
  }
}

that you are transmitting commands from which you are expecting to immediately (within just a few microseconds) receive a response.

this expectation of semi-synchronous serial communication would not be typical. Usually it would take a (relatively) very long time for a response, from the point of view of your 120Mhz photon or electron.

I’d try to first (as suggested above by the esteemed @ScruffR) further isolate your Serial1 communication and test to see just how long the LoRa module takes to respond to a command.

Side note:

void read_data_from_LoRa_Mod()

functions that take no parameters and return no values also make for a tough debug.

try to get a response asynchronously from one command (not tested of course):


void loop(void) {
  auto xmitReset = [](uint32_t millis_interval) {
    static uint32_t lastMillis = 0;
    if (millis() - lastMillis > millis_interval) {
      lastMillis = millis();
      send_LoRa_Command("sys reset");
    }
  };
  xmitReset(5000);
  
  if (Serial1.available()) {
    String inString = Serial1.readString();
    Serial.println(inString);
  }
}  

#12

Yea ok …I’ll keep fighting with tomorrow thanks


#13

right.

One other thing, instead of:

void print_to_console(String message)
{
  Serial.println(message);
}

you can just:

USBSerial& console = Serial;

void setup() {
    console.begin(9600);
    console.print("Hello World");
}