Read from RX/TX Pins

Hello,

I have been working on the Sparkfun Photon WeatherShield and I have attached a Gieger Counter to the USART pins. I know that I have to use Serial 1 to see the data sent. But what do I write so that I can read from that pin.
I have written-

Serial.begin(9600);
Serial1.begin(9600,SERIAL_8E1);

if(Serial1.available()){
    client.publish("IFF ","IF"); 
    sensorstring=(char)Serial1.read();
    client.publish("Radiation: ", sensorstring);
 }
 //snprintf(payload, sizeof(payload), "Radiation : %f ", radiation);
 client.publish("Radiation: Second Try", sensorstring );

Can somebody please help me out!!
I need this to be done fast.

Then you should probably provide as much info as possible to start with.
Omitting what your actual trouble with above code is and posting a contextless code snippet makes things slower for yourself.

You need a way for the Photon to read strings properly from Serial:

Have a look a this:


String readString = "";

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}

void loop()
{

  while (Serial1.available()) // While receiving characters over serial...
  {
    delay(3); // Necessary delay
    char c = Serial1.read(); // Read the character
    readString += c; // Add the character to the string
  }

  readString.trim();

  if (readString.length() > 0) // If a string has been read...
  {
    Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
    parseCommand(readString); // Do something with the string...
    readString = ""; // Clear the string
  }
}

Hello,

Thank you for your response-

This is the data which is coming via the RX pin. I have read this via my terminal but even after following your method, I am not able to read anything. The data is empty.

Please help me out!

Data coming in-
mightyohm.com Geiger Counter 1.00

CPS, 2, CPM, 2, uSv/hr, 0.01, SLOW

CPS, 0, CPM, 2, uSv/hr, 0.01, SLOW

CPS, 1, CPM, 3, uSv/hr, 0.01, SLOW

I had given all the code which I had written for the RX/TX pins-

This is the new code which I am running but I am still having the data as empty/null.

Do we have to initialize RX/TX pins before reading from Serial1? Or does the data automatically transmit to Serial1?

while (Serial1.available()) // While receiving characters over serial...
  {
    delay(3); // Necessary delay
    char c = Serial1.read(); // Read the character
    readString += c; // Add the character to the string
    //snprintf(payload, sizeof(payload), "Radiation Speed: ", readString);
    client.publish("TRY1: ", readString);
  }

  readString.trim();
  
  if (readString.length() > 0) // If a string has been read...
  {
    Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
    
    client.publish("TRY2: ", readString);
    readString = ""; // Clear the string
  }

What does that mean? What data is empty?
You say

Did you now read this data or are you not able to read anything?
That’s rather confusing :confused:

I’d say that delay(3) in that loop is rather long.
You could go with Serial1.readStringUntil() instead, since you know what data you’re looking for.


That’s assuming that there is no issue burried in supposedly unrelated areas of your code.

That’s done via Serial1.begin().

That depends on your wiring and your datasource (sensor).
Some sensors transmit unasked others need a request command.

Okay, so 2 scenarios-

  1. When I connect my Geiger counter to my computer via a FTDI I am able to get information transmitted on Putty.
  2. When I connect my Geiger Counter to Photon via the RX pin, I am trying to read that data, it is coming as null/empty.

What can I do?

You’ve got TX of the sensor hooked up to RX on the Photon and vice versa?

Try this most simple sketch

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);
}

void loop() {
  static uint32_t ms;
  while(Serial1.available()) {
    Serial.write((uint8_t)Serial1.read());
  }

  if(millis()-ms > 1000) {
    ms = millis();
    Serial.print(".");
  }
}

and see what your serial terminal gives you then.

BTW, why were you using 8E1 for Serial1 while the sensor seems to require 8N1 (which is default for Serial1 anyway)?

1 Like

Hello,

Thank you for the reply. I tried those things and I get these two errors. Please help me out. And yes I made it to the default for Serial1.
weather_station.cpp:127:17: error: cannot convert ‘USARTSerial::available’ from type ‘int (USARTSerial::)()’ to type ‘bool’
// attach external interrupt pins to IRQ functions
weather_station.cpp:128:40: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
attachInterrupt(RAIN, rainIRQ, FALLING);
^

I forgot the () at the end of Serial1.available - it should be

  while(Serial1.available()) 

And the other one needs a type cast

 Serial.write((uint8_t)Serial1.read());

I’ve updated the code above accordingly

1 Like

Thanks for the quick reply.
Getting this-
weather_station.cpp:130:11: error: ‘unit8_t’ was not declared in this scope
delay(10000);
^

Because it’s uint8_t and not unit8_t :wink:

But here the error message was quite clear about the reason

2 Likes

Thank you that worked, but I am still not receiving what I am expecting-
Input-

static uint32_t ms;
String res="";
while(Serial1.available()) {
res+=(uint8_t)Serial1.read();

}
client.publish(“TRY”, res);
if(millis()-ms > 1000) {
ms = millis();
Serial.print(".");
}

Output-
TRY 000000000000000000000000000000000000000000000000000000000000000

Whereas I should have got-
TRY Mightyohm.org…(it goes on)

The reason why I suggested Serial.write()ing the received data over client.publish()ing it was to cut out one possible cause of that issue, especially since I’m missing the info what that client actually is. You only revealed part of your code and not the bit that actually shows where you instantiate the client object.

3 Likes

I tried using serial.write() but it is giving me non-supported characters but the periods appear. What do you think I should do? Is there any other method to read from serial1?

What if you use Serial.print() instead?

Serial.write() gives you back what you receive, Serial.print() will “translate”.
So when you expect readable characters via Serial1.read() but get gibberish back via Serial.write() I’d say you’ve either set the wrong protocol parameters or your wiring is messing things up.

Can you post your current code again?

Serial.print() doesn’t seem to work either.

I checked everything again and everything looks good on the wiring part. Here is the code and different options that I have tried using. Nothing is working.

static uint32_t ms;
  while(Serial1.available()) {
    Serial.write((uint8_t)Serial1.read());
  }

  if(millis()-ms > 1000) {
    ms = millis();
    Serial.print(".");
  }
  /*Serial1.println("TEST"); 
  static uint32_t ms;
  String res;
  int cnt=0;
  char chr;
  while(cnt<100){ 
  while(Serial1.available()) {
    chr=Serial1.read();
    res+=(String)chr;
    //client.publish("TRY", res);
//    Serial.println(Serial1.read());
    cnt++;
  }
  }
  client.publish("TRY", res);
  res="";
  if(millis()-ms > 1000) {
    ms = millis();
    Serial.print(".");
  }*/
  
  
  /*String part1,part2,part3,part4,part1val,part2val,part3val;
 while (Serial1.available()){
     char c= Serial1.read();
     if (c=="\n"){
         part1="CPS";
         part1val;
         part2="CPM";
         part2val;
         part3="uSv/hr";
         part3val;
         part4;
        
        int comma=res.indexOf(",");
        part1val= res.substring(comma+1,res.indexOf(", CPM"));
        part2val= res.substring(res.indexOf(part2)+4,res.indexOf(part2)+6);
        part3val= res.substring(res.indexOf(part3)+7,res.indexOf(part3)+11);
        part4=res.substring(res.indexOf(part3)+11,res.length());
        res="";
        
     }
     else{
         res+=c;
     }
     */
     
    /*
    char res;
    int chr;

   while (Serial1.available()){
        res=Serial1.read();
        Serial.print("--");
        Serial.println(res);
        //res+=(String)chr;
    }
    */
    
    
    /*
    char storage[7];
    int count=0;
    String str="";
    while ((str = strtok(res, ",", &res)) != "\n"){ // delimiter is the comma
        storage[count]=str;
        count++;
    }*/

That’s only a part of your code, I’d like to see all of it - including setup() and all definitions/declarations.

1 Like