Commit a99cb712 authored by Markéta Jedličková's avatar Markéta Jedličková
Browse files

Main code for pro version added

parent 9b03747a
/*
This code was designed specifically for usage with the openCanSat kit (https://opencansat.eu)
This program is free software; you can redistribute it
and/or modify it under the terms of the GNU General
Public License as published by the Free Software
Foundation; either version 3 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General
Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
Licence can be viewed at
http://www.gnu.org/licenses/gpl-3.0.txt
Please maintain this license information along with authorship
and copyright notices in any redistribution of this code
*/
#include "Arduino.h"
#include <Adafruit_BME280.h> // include Adafruit BME280 library
#include <Adafruit_BMP280.h> // include Adafruit BMP280 library
#include <Adafruit_INA219.h> // include INA219
#include <SD.h> // include Arduino SD library
#include "../../libraries/Open_Cansat_GPS/src/Open_Cansat_GPS.h"
#include <RFM69.h> // include RFM69 library
#include <SPI.h>
// Local
#define PC_BAUDRATE 56700
#define MS_DELAY 0 // Number of milliseconds between data sending and LED signalization
#define Serial SerialUSB
// RFM69
#define NETWORKID 0 // Must be the same for all nodes (0 to 255)
#define MYNODEID 1 // My node ID (0 to 255)
#define TONODEID 2 // Destination node ID (0 to 254, 255 = broadcast)
#define FREQUENCY RF69_433MHZ // Frequency set up
// BMP280
#define BMP280_ADDRESS_OPEN_CANSAT 0x76
// BME280 SETTING
#define BME280_ADDRESS_OPEN_CANSAT 0x77
#define SEALEVELPRESSURE_HPA 1013.25
// SD card
#define sd_cs_pin 35 // set SD's chip select pin (according to the circuit)
// RFM69 modul
#define chip_select_pin 43 //radio chip select
#define interupt_pin 9 //radio interrupt
// create object 'rf69' from the library, which will
// be used to access the library methods by a dot notation
RFM69 radio(chip_select_pin, interupt_pin, true);
// define our own struct data type with variables; used to send data
typedef struct
{
int messageId;
String datetime;
double longitude;
double latitude;
uint8_t num_of_satelites;
float temperature;
float pressure;
float altitude;
float humidity_bme280;
float voltage_shunt;
float voltage_bus;
float current_mA;
float voltage_load;
int16_t rssi;
} messageOut;
messageOut data; //create the struct variable
// create object 'bmp' from the library, which will
// be used to access the library methods by a dot notation
Adafruit_BMP280 bmp;
// create object 'bme' from the library, which will
// be used to access the library methods by a dot notation
Adafruit_BME280 bme;
// create object 'ina219' from the library with address 0x40
// (according to the circuit, which will be used to access the
// library methods by a dot notation
Adafruit_INA219 ina219(0x40);
// create object 'gps' from the library
OpenCansatGPS gps;
// SD card file
File file; // SD library variable
// LEDS
#define D13_led_pin 42 // D13 LED
#define M_led_pin 36 // MLED
// Local variables
int idCounter = 1;
bool isBmeOk = true;
bool isBmpOk = true;
bool isSdOk = true;
bool isRadioOk = true;
void setup()
{
Serial.begin(PC_BAUDRATE);
delay(2000);
// wait for the Arduino serial (on your PC) to connect
// please, open the Arduino serial console (right top corner)
// note that the port may change after uploading the sketch
// COMMENT OUT FOR USAGE WITHOUT A PC!
while(!Serial);
Serial.println("openCanSat LITE");
Serial.print("Node ");
Serial.print(MYNODEID,DEC);
Serial.println(" ready");
// begin communication with the BMP280 on the previously specified address
// print an error to the serial in case the sensor is not found
if (!bmp.begin(BMP280_ADDRESS_OPEN_CANSAT))
{
isBmpOk = false;
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
return;
}
// begin communication with the BME280 on the previously specified address
// print an error to the serial in case the sensor is not found
if (!bme.begin(BME280_ADDRESS_OPEN_CANSAT))
{
isBmeOk = false;
Serial.println("Could not find a valid BME280 sensor, check wiring!");
return;
}
// begin communication with the INA219
ina219.begin();
// begin communication with gps
gps.begin();
// begin communication with the SD card using the previously specified pin
// print an error to the serial in case the SD card is not found
if (!SD.begin(sd_cs_pin))
{
isSdOk = false;
Serial.println("SD card initialization failed!");
return;
}
if(isSdOk)
{
// Keep only last measurement
if(SD.exists("data.txt"))
{
Serial.println("data.txt removed");
SD.remove("data.txt");
}
Serial.println("SD card initialized.");
}
// Uncomment when you want to see debug prints from GPS library
// gps.debugPrintOn(57600);
if(radio.initialize(FREQUENCY, MYNODEID, NETWORKID))
{
isRadioOk = false;
Serial.println("RFM69HW initialization failed!");
return;
}
else
{
radio.setHighPower(true); // Always use this for RFM69HW
}
pinMode(D13_led_pin, OUTPUT);
}
void loop()
{
data.messageId = idCounter;
Serial.println("MessageId = " + static_cast<String>(data.messageId));
data.temperature = 0;
data.pressure = 0;
data.altitude = 0;
if(isBmpOk)
{
data.temperature += bmp.readTemperature();
data.pressure += bmp.readPressure();
data.altitude += bmp.readAltitude(SEALEVELPRESSURE_HPA);
}
if(isBmeOk)
{
data.temperature += bme.readTemperature();
data.pressure += bme.readPressure() / 100.0F;
data.altitude += bme.readAltitude(SEALEVELPRESSURE_HPA);
data.humidity_bme280 = bme.readHumidity();
}
if(isBmpOk and isBmeOk)
{
data.temperature /= 2;
data.pressure /= 2;
data.altitude /= 2;
}
Serial.println("Temperature = " + static_cast<String>(data.temperature) + " *C");
Serial.println("Pressure = " + static_cast<String>(data.pressure) + " Pa");
Serial.println("Approx altitude = " + static_cast<String>(data.altitude) + " m");
Serial.println("Humidity = " + static_cast<String>(data.humidity_bme280) + " %");
// read values from INA219 into structure
data.voltage_shunt = ina219.getShuntVoltage_mV();
data.voltage_bus = ina219.getBusVoltage_V();
data.current_mA = ina219.getCurrent_mA();
data.voltage_load = data.voltage_bus + (data.voltage_shunt / 1000);
Serial.println("Shunt Voltage: " + static_cast<String>(data.voltage_shunt) + " mV");
Serial.println("Bus Voltage: " + static_cast<String>(data.voltage_bus) + " V");
Serial.println("Current: " + static_cast<String>(data.current_mA) + " mA");
Serial.println("Load Voltage: " + static_cast<String>(data.voltage_load) + " V");
// Initialize GPS
data.datetime = "";
data.latitude = 0;
data.longitude = 0;
data.num_of_satelites = 0;
// save start time in millisec
uint32_t start = millis();
if (gps.scan(250))
{
data.datetime = gps.getDateTimeString();
data.latitude = gps.getLat();
data.longitude = gps.getLon();
data.num_of_satelites = gps.getNumberOfSatellites();
Serial.println(String("Time to find fix: ") + (millis() - start) + String("ms"));
Serial.println(String("Datetime: ") + data.datetime);
Serial.println(String("Lat: ") + String(data.latitude, 7));
Serial.println(String("Lon: ") + String(data.longitude, 7));
Serial.println(String("Num of sats: ") + String(data.num_of_satelites));
Serial.println();
}
else
{
Serial.println("Gps have no satelit to fix.");
}
// write to the file and print info to serial
// print an error to the serial in case it does not succeed
file = SD.open("data.txt", FILE_WRITE); // open test.txt for write
if (file)
{
file.println("A " + static_cast<String>(data.altitude) +
" I " + static_cast<String>(data.current_mA) +
" Time " + static_cast<String>(data.datetime) +
" H " + static_cast<String>(data.humidity_bme280) +
" Lat " + static_cast<String>(data.latitude) +
" Lon " + static_cast<String>(data.longitude) +
" I " + static_cast<String>(data.messageId) +
" NoS " + static_cast<String>(data.num_of_satelites) +
" P " + static_cast<String>(data.pressure) +
" T " + static_cast<String>(data.temperature) +
" Vb " + static_cast<String>(data.voltage_bus) +
" Vl " + static_cast<String>(data.voltage_load) +
" Vs " + static_cast<String>(data.voltage_shunt));
Serial.println("Writing to file data.txt finished.");
file.close();
}
// RFM69HW
data.rssi = 0;
if(isRadioOk)
{
data.rssi = radio.RSSI;
Serial.println("Signal = " + static_cast<String>(radio.RSSI));
radio.send(TONODEID, (const void*)&data, sizeof(data));
}
Serial.println();
heartBeat();
idCounter ++;
delay(MS_DELAY);
}
// function for LED signalization
void heartBeat()
{
pinMode(M_led_pin, OUTPUT);
digitalWrite(D13_led_pin, HIGH);
digitalWrite(M_led_pin, HIGH);
delay(MS_DELAY);
digitalWrite(D13_led_pin, LOW);
pinMode(M_led_pin, INPUT);
delay(MS_DELAY);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment