Read from RX/TX Pins


#6

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.


#7

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?


#8

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)?


#9

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);
^


#10

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


#11

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


#12

Because it’s uint8_t and not unit8_t :wink:

But here the error message was quite clear about the reason


#13

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)


#14

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.


#15

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?


#16

What if you use Serial.print() instead?


#17

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?


#18

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


#19

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++;
    }*/

#20

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


#21

Here s the whole code-

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

// This #include statement was automatically added by the Particle IDE.
#include "SparkFun_Photon_Weather_Shield_Library/SparkFun_Photon_Weather_Shield_Library.h"

char Org[] = "Calplug 2016 Weather Station";
char Disp[] = "Weather now";
char Locn[] = "Irvine";

char server[]= "m11.cloudmqtt.com";
//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
Weather sensor;

int analogPin = A0;//Wind Vane Pin on Weather Shield 
int val = 0;// variable to store the read value
int wind_speed =D3;//Wind Speed Pin on Weather Shield
float rainin =0;
int RAIN = D2;
int WSPEED = D3;
int winddir = A0;
String res;
float currentTime = 0;

int count = 0;

//Global Variables
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
long lastSecond; //The millis counter to see when a second rolls by
byte seconds; //When it hits 60, increase the current minute
byte minutes; //Keeps track of where we are in various arrays of data

String readString = "";
float windspeedmph = 0; // [mph instantaneous wind speed]
long lastWindCheck = 0;
volatile float dailyrainin = 0; // [rain inches so far today in local time]
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;
volatile unsigned long raintime, rainlast, raininterval, rain;

void rainIRQ()
// Count rain gauge bucket tips as they occur
// Activated by the magnet and reed switch in the rain gauge, attached to input D2
{
  raintime = millis(); // grab current time
  raininterval = raintime - rainlast; // calculate interval between this and last event

    if (raininterval > 10) // ignore switch-bounce glitches less than 10mS after initial edge
  {
    dailyrainin += 0.011; //Each dump is 0.011" of water

    rainlast = raintime; // set up for next event
  }
}

void wspeedIRQ()
// Activated by the magnet in the anemometer (2 ticks per rotation), attached to input D3
{
  if (millis() - lastWindIRQ > 10) // Ignore switch-bounce glitches less than 10ms (142MPH max reading) after the reed switch closes
  {
    windClicks++; //There is 1.492MPH for each click per second.
  }
}

MQTT client("m11.cloudmqtt.com", 11978, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

    if (message.equals("RED"))    
        RGB.color(255, 0, 0);
    else if (message.equals("GREEN"))    
        RGB.color(0, 255, 0);
    else if (message.equals("BLUE"))    
        RGB.color(0, 0, 255);
    else    
        RGB.color(255, 255, 255);
    delay(1000);
}

void setup()

{
  pinMode(WSPEED, INPUT_PULLUP); // input from wind meters windspeed sensor
  pinMode(RAIN, INPUT_PULLUP); // input from wind meters rain gauge sensor
 
  client.connect("m11.cloudmqtt.com","lupmqidp","0pI2dJuRvPNQ");
  // publish/subscribe
  if (client.isConnected()) {
       client.publish("CONNECTING...","CONNECTED");
       client.subscribe("/inTopic");
    }
  Serial.begin(9600);
  Serial1.begin(9600);
 //Initialize the I2C sensors and ping them
 sensor.begin();

 /*You can only receive acurate barrometric readings or acurate altitiude
 readings at a given time, not both at the same time. The following two lines
 tell the sensor what mode to use. You could easily write a function that
 takes a reading in one made and then switches to the other mode to grab that
 reading, resulting in data that contains both acurate altitude and barrometric
 readings. For this example, we will only be using the barometer mode. Be sure
 to only uncomment one line at a time. */

 sensor.setModeBarometer();//Set to Barometer Mode

 //baro.setModeAltimeter();//Set to altimeter Mode
 //These are additional MPL3115A2 functions the MUST be called for the sensor to work.
 
  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions
  attachInterrupt(RAIN, rainIRQ, FALLING);
  attachInterrupt(WSPEED, wspeedIRQ, FALLING);
  delay(10000);
}


void loop() {
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++;
    }*/
    
 /*
 char payload[255]; 
 
 //snprintf(payload, sizeof(payload), "Radiation: %s:%s, %s:%s, %s:%s, %s",part1,part1val,part2,part2val,part3,part3val,part4 );
 //Spark.publish("Radiation",payload);
 //Spark.publish("Radiation 1",storage[1]);
 delay(5000);
 // Measure Relative Humidity from the HTU21D or Si7021
 float h = sensor.getRH();
 // Measure Temperature from the HTU21D or Si7021
 float f = sensor.getTempF();
 float c=sensor.getTemp();
 // Temperature is measured every time RH is requested.
 // It is faster, therefore, to read it from previous RH
 // measurement with getTemp() instead with readTemp()
 
 //Measure Pressure from the MPL3115A2
 float pascals = sensor.readPressure();
 Serial.print((pascals/100) * 0.0295300);
 Serial.println("in.Hg");

  //If in altitude mode, you can get a reading in feet with this line:
  //float altf = sensor.readAltitudeFt();
 

 snprintf(payload, sizeof(payload), "Temperature: %f F", f);
 Serial.println(payload);
 Spark.publish("Temperature in F", payload);
 client.publish("Temperature in F", payload);
 
 delay(3000);
 snprintf(payload, sizeof(payload), "Temperature: %f C", c);
 Serial.println(payload);
 Spark.publish("Temperature in C", payload);
 client.publish("Temperature in C", payload);

 delay(3000);

 snprintf(payload, sizeof(payload), "Humidity: %f", h);
 Serial.println(payload);
 Spark.publish("Humidity", payload);
 client.publish("Humidity", payload);
 delay(3000);
 
 snprintf(payload, sizeof(payload), "Pressure: %f pascals", pascals);
 Serial.println(payload);
 Spark.publish("Pressure in pascals", payload);
 client.publish("Pressure in pascals", payload);

 delay(3000);
 
 snprintf(payload, sizeof(payload), "Pressure: %f mmHg", (pascals/ 133.322));
 Serial.println(payload);
 Spark.publish("Pressure in mmHg", payload);
 client.publish("Pressure in mmHg", payload);
 
 snprintf(payload, sizeof(payload), "Wind Speed: %f MPH", get_wind_speed());
 Serial.println(payload);
 client.publish("Wind Speed", payload);
 
 float wind_dir= analogRead(analogPin);
 char dir[100];
 if(wind_dir > 2270 && wind_dir < 2290) {
    snprintf(dir, sizeof(dir), "Direction: North");
    
 }
 if(wind_dir > 3220 && wind_dir < 3299) {
    snprintf(dir, sizeof(dir), "Direction: North East");
    
 }
 if(wind_dir > 3890 && wind_dir < 3999){
    snprintf(dir, sizeof(dir), "Direction: East");
    
 }
 if(wind_dir > 3780 && wind_dir < 3850) {
    snprintf(dir, sizeof(dir), "Direction: South East");
    
 }
 if(wind_dir > 3570 && wind_dir < 3650){
    snprintf(dir, sizeof(dir), "Direction: South");
    
 }
 if(wind_dir > 2790 && wind_dir < 2850) {
    snprintf(dir, sizeof(dir), "Direction: South West");
    
 }
 if(wind_dir > 1580 && wind_dir < 1610) {
    snprintf(dir, sizeof(dir), "Direction: West");
    
 }
 if(wind_dir > 1930 && wind_dir < 1950) {
    snprintf(dir, sizeof(dir), "Direction: North West");
    
 }
 Spark.publish("Wind Direction", dir);
 client.publish("Wind Direction", dir);
*/
}

float get_wind_speed()
{
  float deltaTime = millis() - lastWindCheck; //750ms

  deltaTime /= 1000.0; //Covert to seconds

  float windSpeed = (float)windClicks / deltaTime; //3 / 0.750s = 4

  windClicks = 0; //Reset and start watching for new wind
  lastWindCheck = millis();

  windSpeed *= 1.492; //4 * 1.492 = 5.968MPH

  return(windSpeed);
}

#22

You can make your code look much nicer by placing:

```cpp



```

Around your code. Forum Tips and Tricks


#23

Thanks! I will keep that in mind next time.


#24

Hey, could you understand what my mistake is? I am still trying to figure that part out.


#25

I’d step back from the issue and try taking things step by step again
First flash this code alone

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 since you’ve got an FTDI check this sketch against the FTDI and terminal. Send data to the Photon via one terminal and see what you receive via USB serial on another one. You should see the input from the FTDI with the dots from the sketch in between.

Then check again what your sensor gives you via the FTDI when using the same port settings as in the test above.
Does the sensor need a command sent to start transmitting or does it just blab away immediately when powered on?

When both tests are positive connect RX/TX of the sensor to TX/RX on the Photon (no other sensors) and check above sketch against that.

Without the hardware at hand it’s a bit difficult to say more. If I had one of these I’d maybe look at the signals with a logic analyser and do lots of other tests, bit via a forum thread things get rather cumbersome.