String variables not travelling well

It appears there's a discrepancy between what the serial monitor displays and the actual serial output.

Hi folks. I'm pretty new to all this so please be gentle! I am working on a project that is exhibiting some unexpected (to me) behaviour and I hoped somebody could shine some light. I have stripped the code down to the bare minimum that demonstrates the problem.

I have a slave Nano which sends text messages to a master Nano over via PJON. This works nicely.

I get the master Nano to place the received messages into a string variable. It then prints the variable's contents to the serial monitor. This also works nicely.

I created a string variable on the master Nano for testing and populated it locally. This also prints to the serial monitor in exactly the way I expected.

BUT

...if I connect the master Nano to a Raspberry Pi running the simplest Python serial script, ITS monitor prints the master's locally generated variable and not the network-populated one.

It appears there's a discrepancy between what the serial monitor displays and the actual serial output.

I suspect that there might be something about the way the master Nano copies from a *pointer variable that is causing this, but I'm a little out of my depth. Can anyone offer some advice as to what I should try next?

Thanks for reading!

Here's the code.

The slave Nano:

#include <PJONSoftwareBitBang.h>

PJONSoftwareBitBang bus(10);

void setup() {
  bus.strategy.set_pin(12);
  bus.begin();
}

void loop() {
  bus.send(100, "remote", 9);
  bus.update();
  }

The master Nano:

#include <PJONSoftwareBitBang.h>

PJONSoftwareBitBang bus(100);

String local_var;
String remote_var;

void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {

  remote_var = payload;

};

void setup() {

  bus.strategy.set_pin(12);
  bus.begin();
  bus.set_receiver(receiver_function);
  Serial.begin(9600);
};

void loop() {
  bus.receive(1000);     
  local_var = "local ";
  Serial.print(local_var);
  Serial.println(remote_var);

};

The Pi's Python code:

import serial
ser = serial.Serial("/dev/ttyUSB0", 9600)
while True:
    cc=str(ser.readline())
    print(cc[2:][:-5])

The Master's serial monitor:

image

The Pi's Output

Welcome to the forum

String local_var;
String remote_var;

void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info)
{
    remote_var = payload;
};

This looks odd. payload appears to be a pointer to a byte but you assign its value to a String variable

Am I interpreting that correctly ?

1 Like

Yes, I think so. I have my suspicions about this, but not the knowledge to understand it (yet). I don't understand why remote_var appears to contain the correct text only as far as the Master nano is concerned.

Yes, but it seems to be legitimate:

Son of a Diddly! It turns out the Pi wasn't supplying enough power to the Nano through the USB port. I powered the Nano externally via the VIN pin and the damn thing started working.

Much kudos to u/ventus1b on Reddit for the fix.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.