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[]= "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);
}

You can make your code look much nicer by placing:

```cpp



```

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() {
  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.

3 Likes