Serial and Particle.variable possible conflict

Hi,
I have been using serial 1 and Particle.variable with no problem then I started using the serial monitor and serial 1 stopped working.

I have discovered if i rem out my two Particle.variable statements then serial 1 starts working again.
I switched to Serial 5 and same problem.
I tried moving the Particle.variable statement to before and after the serial begin statements but still cant get serial 1 or 5 working when using Particle.variable.

I disabled the USB serial by not writing code line Serial.begin(9600); and then serial 5 and Particle Variable works.
Also the USB Serial still works.

So basically when setting Serial.begin() messes up serial 5 and particle publish.

I am using Particle Electron with firmware 0.6.0

I’ve not realy had such problems - at least to that consistent.
There are situations where this can happen, depending on the rest of your code.
Hammering one interface too fiercly or disabling interrupts are able to interfere.

If you have a minimal test code that exhibits this behaviour we could have a look.

With the code as is the Particle Variables do not appear to be visible.
If I REM out the //Serial.begin(9600); the USB Serial works and the Particle Variables are visible.
Correction now they have gone again.

I am wondering if it is an issue your end with the Particle.variable function.
Perhaps your server is not receiving the variables?

I just flashed same code again this time with Serial.begin(9600 OTA
Same thing no Particle.variable
Device is online (Breathing Light Blue)
Now I REM Serial5.begin(9600, SERIAL_8N1); and the Particle Variables reappear
Just to test I enable Serial 5 again and no Particle Variables
And again REM Serial5.begin(9600, SERIAL_8N1); FLash OTA and Particle Variables come back.

So it is a link with the serial and particle variables.
I also tried // also tried Serial5.begin(9600); and still particle variables not visible.

#include "Serial5/Serial5.h"

String gps = "GPS";
String xyz = "XYZ";

// GP-2017 GPS VARS
String inWord;
char inByte;


void setup() {
    
    // give me time to connect serial for debug
    delay(5000);
    Serial.begin(9600);
    // GPS Serial
    // if enabled pasrticle.variable not published
    // when disabled particle.variable is published
    //Serial5.begin(9600, SERIAL_8N1);
    // also tried
    Serial5.begin(9600);
    Serial.println("OK");
    
    // cloud variables seems to stop serial 1 + 5 working
    // GPS
    Particle.variable("STU", gps);
    // XYZ
    Particle.variable("XYZ", xyz);
    
}

void loop() {
    readGpsSensor();
    Serial.println(gps);
    Serial.println(xyz);
    delay(2000);
} // end loop

// read gps sensor
void readGpsSensor() {
    //gps = "";
    // maybe turn off lock led if no serial
    while (Serial5.available() > 0) {
        //Serial.print(".");
        inByte = Serial5.read();
        
        if (inByte == '\n') {
            // END OF LINE
            
            // check is data we want
            // you can change this to get any data line values
            if (inWord.startsWith("$GPRMC")) {
                // put data string in variable
                //gps = inWord.trim();
                gps = inWord;
                // clear the inword variable
                inWord = "";
                
                // does the GPS Receiver have lock?
                // get th char at pos 17 and test to see if it is an A i.e VALID V = INVALID
                //char lock = gps.charAt(17);
                //if (lock == 'A') {
                    // YES Switch on Lock LED
                    //digitalWrite(LockLED, HIGH);
                //} else {
                    // NO turn off lock led
                    //digitalWrite(LockLED, LOW);  
                //}
                
                
            } else {
                // clear the inword variable as not the data we want
                inWord = "";
            }
            
        } else {
            // build data string
            inWord += inByte;   
        }
    } // end if serial
} // end function

Try placing your functions and variables as soon as possible in your setup, before that delay.

2 Likes

I have also just tried all the other serialm ports in turn and they all appear to prevent Particle Variable being published.

If disable , REM out Serial.begin(9600); the particle variable reappears and the serial port works

This is what is hurting you. The message from the device to the cloud is sent asynchronously to the calling of setup() and having a long delay before calling Particle.variable or Particle.function is known to make those registrations miss that initial message.

I just removed delay completely and now have both Serial and Serial 1 begin enabled and particle variables are visible.
So this is all down to an initial delay function?

Arrrrrr been days on this lol

Thanks bko
Can I cure it by placing the Particle.Variable function before the delay?

Yes, do the Particle cloud registrations first and then work with the serial ports and you should be fine.

Yes Thanks again
Modified setup and now all ok

void setup() {
// GPS
Particle.variable(“STU”, gps);
// XYZ
Particle.variable(“XYZ”, xyz);
Serial.begin(9600);
// GPS Serial
Serial1.begin(9600);
// give me time to connect serial for debug
delay(5000);
Serial.println(“OK”);
}

1 Like