LoRaWAN-Signal-Mapping/iot/main/main.ino

91 lines
2.7 KiB
Arduino
Raw Permalink Normal View History

#include "settings.h"
#include <Arduino.h>
#ifdef ENABLE_MEMORY_DIAGNOSTICS
// #include <MemoryFree.h>
#define DISPLAY_FREE_MEMORY() Serial.println(freeMemory(), DEC);
#else
#define DISPLAY_FREE_MEMORY() // noop
#endif
#include "random.h"
// BAD PRACTICE: For some extremely strange reason, the Arduino IDE doesn't pick up random.cpp like it does our other source files - so we've got to explicitly include it here. If we had control over the build process (which we don't), we've use a Makefile here that handled this better.
#include "random.cpp"
#include "gps.h"
#include "peripheral.h"
2019-06-24 13:31:33 +00:00
#include "storage.h"
#include "power.h"
#include "transmission.h"
void setup() {
Serial.begin(BAUD_PC);
Serial.println(F("[main] Starting"));
random_begin();
power_init();
DISPLAY_FREE_MEMORY();
GPSLocation gps_data = gps_fetch();
DISPLAY_FREE_MEMORY();
// Serial.print(F("[main] Location ")); Serial.print(gps_data.lat); Serial.print(F(", ")); Serial.println(gps_data.lng);
uint32_t id = random_get();
// Serial.print(F("[main] Id "));
// Serial.println(id);
// Activate microSD card breakout board on the SPI bus
peripheral_register(PIN_SPI_CS_RFM95);
peripheral_register(PIN_SPI_CS_SD);
peripheral_unsilence(PIN_SPI_CS_SD);
DISPLAY_FREE_MEMORY();
store_reading(id, gps_data);
DISPLAY_FREE_MEMORY();
#ifdef SD_DEBUG_ENABLED
store_debug(gps_data.time, 19 - 1); // Don't print the null byte
DISPLAY_FREE_MEMORY();
#endif
// ------------------------------------------------------------------------
// Activate the RFM95
peripheral_switch(PIN_SPI_CS_SD, PIN_SPI_CS_RFM95);
// Initialise an array to store the message in,but make sure it's at least 16 bytes long (because of AES restrictions at the other end)
uint8_t message_size = sizeof(uint32_t) + sizeof(float)*2;
if(message_size < 16) message_size = 16;
uint8_t message[message_size];
// Initialise the message to a known state (i.e. null bytes)
for(uint8_t i = 0; i < message_size; i++)
message[i] = 0;
const uint8_t* bytes_id = reinterpret_cast<uint8_t const *>(&id);
const uint8_t* bytes_lat = reinterpret_cast<uint8_t const *>(&(gps_data.lat));
const uint8_t* bytes_lng = reinterpret_cast<uint8_t const *>(&(gps_data.lng));
2019-07-02 13:07:11 +00:00
// for(int i = 0; i < sizeof(uint32_t); i++) {
// }
// The size of a float == the size of a uint32_t
for(int i = 0; i < sizeof(float); i++) {
2019-07-02 13:07:11 +00:00
message[i] = bytes_id[i];
message[sizeof(uint32_t) + i] = bytes_lat[i];
message[sizeof(uint32_t) + sizeof(float) + i] = bytes_lng[i];
}
#ifdef ENABLE_RADIO
2019-07-02 13:07:11 +00:00
// Serial.println(F("[main] Transmitting"));
transmit_init();
transmit_send(message, message_size);
#endif
power_off(); // Doesn't return
}
void loop() {
}