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-
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
}
}
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.
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
}
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);
^
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.
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?
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.
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++;
}*/