Read from RX/TX Pins

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[]= "";
//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("", 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);
        RGB.color(255, 255, 255);

void setup()

  pinMode(WSPEED, INPUT_PULLUP); // input from wind meters windspeed sensor
  pinMode(RAIN, INPUT_PULLUP); // input from wind meters rain gauge sensor
  // publish/subscribe
  if (client.isConnected()) {
 //Initialize the I2C sensors and ping them

 /*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);

void loop() {
static uint32_t ms;
  while(Serial1.available()) {

  if(millis()-ms > 1000) {
    ms = millis();
  static uint32_t ms;
  String res;
  int cnt=0;
  char chr;
  while(Serial1.available()) {;
    //client.publish("TRY", res);
//    Serial.println(;
  client.publish("TRY", res);
  if(millis()-ms > 1000) {
    ms = millis();
  /*String part1,part2,part3,part4,part1val,part2val,part3val;
 while (Serial1.available()){
     char c=;
     if (c=="\n"){
        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);
    char res;
    int chr;

   while (Serial1.available()){;
    char storage[7];
    int count=0;
    String str="";
    while ((str = strtok(res, ",", &res)) != "\n"){ // delimiter is the comma
 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 1",storage[1]);
 // 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);

  //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);
 Spark.publish("Temperature in F", payload);
 client.publish("Temperature in F", payload);
 snprintf(payload, sizeof(payload), "Temperature: %f C", c);
 Spark.publish("Temperature in C", payload);
 client.publish("Temperature in C", payload);


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

 snprintf(payload, sizeof(payload), "Pressure: %f mmHg", (pascals/ 133.322));
 Spark.publish("Pressure in mmHg", payload);
 client.publish("Pressure in mmHg", payload);
 snprintf(payload, sizeof(payload), "Wind Speed: %f MPH", get_wind_speed());
 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


You can make your code look much nicer by placing:



Around your code. Forum Tips and Tricks

1 Like

Thanks! I will keep that in mind next time.

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

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

void setup() {

void loop() {
  static uint32_t ms;
  while(Serial1.available()) {

  if(millis()-ms > 1000) {
    ms = millis();

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.