Jan Marcinowski
Published © GPL3+

rail.X: Monitoring Railroad Crossings for You

A mesh of open devices monitoring railway crossings "open-closed" status.

IntermediateWork in progress10 hours1,790

Things used in this project

Story

Read more

Schematics

Version 0.3 rail.X

Code

Version 1.3 of the code.

C/C++
This is the hardware code for rail.X
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

STARTUP(System.disable(SYSTEM_FLAG_PUBLISH_RESET_INFO));

// This #include statement was automatically added by the Particle IDE.
#include <adafruit-ina219.h>

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

// This #include statement was automatically added by the Particle IDE.
#include <DS18B20.h>

#define tilt_status C1 // attached to tilt sensor through debounce and filtering only (no XOR)

#define temp_data D6 // data from temp sensor DS1820
#define temp_power D5 // power for temp sensor
#define current_power C0

const int MAXRETRY = 4;

DS18B20  ds18b20(temp_data, true); //Sets Pin D6 for DS1820 Temp Sensor and 
FuelGauge fuel; // so that we can check the battery voltage
PMIC pmic; //Initalize the PMIC class so you can call the Power Management functions below.
Adafruit_INA219 ina219;

float celsius;
bool TiltStat;


void setup() {
    uint32_t currentFrequency;
   //Serial.begin(115200);
    pinMode(temp_power, OUTPUT);            // power for temp sensor
    pinMode(current_power, OUTPUT);
    pinMode(D7, OUTPUT);            // LED
    pinMode(tilt_status, INPUT_PULLDOWN); // sets pin as input
    
    pmic.setChargeCurrent(0,0,1,0,0,0); //Set charging current to 1024mA (512 + 512 offset)
    pmic.setInputVoltageLimit(4840);   //Set the lowest input voltage to 4.84 volts. This keeps my 5v solar panel from operating below 4.84 volts.  
    
    digitalWrite(current_power, HIGH);
    delay(200);
    ina219.begin();
    ina219.setCalibration_16V_400mA();
    
    getTiltStatus();
    getTemp();
    
    Particle.connect();
    waitUntil(Particle.connected);
    
    publishData();
    delay(500);
    System.sleep(SLEEP_MODE_DEEP,1380); //this is in sec


}


    void publishData(){

       
        float shuntvoltage = 0;
        float busvoltage = 0;
        float current_mA = 0;
        float loadvoltage = 0;
       
        shuntvoltage = ina219.getShuntVoltage_mV();
        busvoltage = ina219.getBusVoltage_V();
        current_mA = ina219.getCurrent_mA();
        loadvoltage = busvoltage + (shuntvoltage / 1000);
       
        Particle.publish("T1", String(celsius, 2) + ";" + String(TiltStat) + ";" + String(fuel.getVCell(), 2) + ";" + String(fuel.getSoC(), 0) + ";" + String(current_mA, 2) + ";" + String(busvoltage, 2));
        digitalWrite(current_power, LOW);
    }
    
    void getTemp(){
      float _temp;
      
      int   i = 0;
      digitalWrite(temp_power, HIGH); // power temp sensor ON
      delay(300);                       // wait for the temp sensor to stablize under power
      
      do {
        _temp = ds18b20.getTemperature();
      } while (!ds18b20.crcCheck() && MAXRETRY > i++); //checking for the temp sensor to give right value
    
      if (i < MAXRETRY) {
        celsius = _temp;
        digitalWrite(temp_power, LOW); // power temp sensor OFF
        //Serial.println(celsius);
      }
      else {
        celsius = NAN;
        //Serial.println("Invalid reading");
        digitalWrite(temp_power, LOW);
      }
    }
    
    void getTiltStatus(){
        TiltStat = digitalRead(tilt_status);
    }

Credits

Jan Marcinowski

Jan Marcinowski

1 project • 1 follower
I'm running a Creative Agency MEGGOT in Poland. Started tinkering for fun about 4-5 years ago. Still tinker for fun.

Comments