157 lines
4.6 KiB
Arduino
157 lines
4.6 KiB
Arduino
|
#include <ArduinoLowPower.h>
|
||
|
#include <SPI.h>
|
||
|
#include "MNLib.h"
|
||
|
|
||
|
//#define IS_CLIENT
|
||
|
//#define IS_SERVER
|
||
|
|
||
|
|
||
|
byte LoopState = 0;
|
||
|
char inByte;
|
||
|
unsigned long nextTick = 0;
|
||
|
unsigned int msTick = 2000;
|
||
|
|
||
|
|
||
|
|
||
|
// Class to manage message delivery and receipt, using the rfm95 declared above
|
||
|
//RHReliableDatagram rfManager(rfm95, CLIENT_ADDRESS);
|
||
|
|
||
|
// Internal on-chip Temperature sensor
|
||
|
TemperatureZero TempZero = TemperatureZero();
|
||
|
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
SerialUSB.begin(115200);
|
||
|
//while (!SerialUSB);
|
||
|
/*for (int i = 0; i < 5; i++)
|
||
|
{
|
||
|
delay(1000);
|
||
|
SerialUSB.println("owo");
|
||
|
}*/
|
||
|
init_mn();
|
||
|
/*for (int i = 0; i < 2; i++)
|
||
|
{
|
||
|
delay(1000);
|
||
|
SerialUSB.println("awa");
|
||
|
}
|
||
|
printStatusReport();*/
|
||
|
nextTick = millis();
|
||
|
}
|
||
|
|
||
|
|
||
|
void loop()
|
||
|
{
|
||
|
if (SerialUSB.available())
|
||
|
{
|
||
|
size_t config_size = sizeof(MNConfiguration) - sizeof(configuration.client_secret_key) - sizeof(configuration.server_secret_key); // Don't leak the secret key
|
||
|
char command = SerialUSB.read();
|
||
|
switch (command)
|
||
|
{
|
||
|
case 'C': // Print configuration for in the field debugging
|
||
|
SerialUSB.println("Configuration:");
|
||
|
SerialUSB.print("Frequency: ");
|
||
|
SerialUSB.print(configuration.modem_frequency);
|
||
|
SerialUSB.println(" MHz");
|
||
|
SerialUSB.print("Power: ");
|
||
|
SerialUSB.print(configuration.modem_power);
|
||
|
SerialUSB.println(" dBm");
|
||
|
SerialUSB.print("Client address: ");
|
||
|
SerialUSB.println(configuration.client_address);
|
||
|
SerialUSB.print("Server address: ");
|
||
|
SerialUSB.println(configuration.server_address);
|
||
|
|
||
|
for (int i = 0; i < N_DEVICES; i++)
|
||
|
{
|
||
|
SerialUSB.print("Pointer ");
|
||
|
SerialUSB.print(i);
|
||
|
SerialUSB.print(": ");
|
||
|
SerialUSB.println(configuration.devices[i]);
|
||
|
if (configuration.devices[i] != 255)
|
||
|
{
|
||
|
for(int j = 0; j < sizeof(AnalogInput); j++)
|
||
|
{
|
||
|
SerialUSB.print(reinterpret_cast<uint8_t*>(devices[i])[j]);
|
||
|
SerialUSB.print(" ");
|
||
|
}
|
||
|
SerialUSB.println();
|
||
|
devices[i]->printStatus();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
SerialUSB.println();
|
||
|
SerialUSB.print("Battery voltage: ");
|
||
|
SerialUSB.println(batteryVoltage());
|
||
|
|
||
|
SerialUSB.println();
|
||
|
SerialUSB.println(sizeof(uint16_t) + 1 * (sizeof(float) + sizeof(uint8_t) * 2));
|
||
|
SerialUSB.println(sizeof(uint16_t));
|
||
|
SerialUSB.println(1 * (sizeof(float) + sizeof(uint8_t) * 2));
|
||
|
SerialUSB.println("END");
|
||
|
break;
|
||
|
|
||
|
case 'c':
|
||
|
SerialUSB.print("N_DEVICES: ");
|
||
|
SerialUSB.println(N_DEVICES);
|
||
|
SerialUSB.print("CFGMEM: ");
|
||
|
SerialUSB.println(CFGMEM);
|
||
|
SerialUSB.print("MNConfiguration: ");
|
||
|
SerialUSB.println(sizeof(MNConfiguration) - sizeof(configuration.client_secret_key) - sizeof(configuration.server_secret_key));
|
||
|
SerialUSB.print("Device: ");
|
||
|
SerialUSB.println(sizeof(Device) - 4);
|
||
|
SerialUSB.print("AnalogInput: ");
|
||
|
SerialUSB.println(sizeof(AnalogInput) - 4);
|
||
|
SerialUSB.println("END");
|
||
|
break;
|
||
|
|
||
|
case 'r': // Read configuration (Frequency, which sensors etc.)
|
||
|
SerialUSB.write(reinterpret_cast<char*>(&configuration), config_size);
|
||
|
break;
|
||
|
|
||
|
case 'w':
|
||
|
SerialUSB.readBytes(reinterpret_cast<char*>(&configuration), config_size);
|
||
|
refreshConfig();
|
||
|
break;
|
||
|
|
||
|
case 'R': // Read configuration memory (extended configuration for each sensor)
|
||
|
SerialUSB.write(reinterpret_cast<char*>(&configuration_memory), sizeof(configuration_memory));
|
||
|
break;
|
||
|
|
||
|
case 'W':
|
||
|
SerialUSB.readBytes(reinterpret_cast<char*>(&configuration_memory), sizeof(configuration_memory));
|
||
|
refreshConfig();
|
||
|
break;
|
||
|
|
||
|
case 'k':
|
||
|
char data[sizeof(MNConfiguration)];
|
||
|
SerialUSB.readBytes(reinterpret_cast<char*>(&configuration) + config_size, sizeof(configuration.client_secret_key) + sizeof(configuration.server_secret_key));
|
||
|
SerialUSB.write(reinterpret_cast<char*>(&configuration) + config_size, sizeof(configuration.client_secret_key) + sizeof(configuration.server_secret_key));
|
||
|
|
||
|
refreshConfig();
|
||
|
break;
|
||
|
|
||
|
case 's':
|
||
|
saveMemory();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
loopSensors();
|
||
|
|
||
|
// TICK-ROUTINE
|
||
|
if (millis() > nextTick)
|
||
|
{
|
||
|
sendSensorData();
|
||
|
|
||
|
SerialUSB.println("s");
|
||
|
uint8_t data[5];
|
||
|
data[0] = MT_DeviceStatus;
|
||
|
float bv = batteryVoltage();
|
||
|
memcpy(&data[1], &bv, 4);
|
||
|
send(data, 5);
|
||
|
nextTick = nextTick + msTick;
|
||
|
digitalWrite(LED_BUILTIN, HIGH);
|
||
|
delay(2);
|
||
|
digitalWrite(LED_BUILTIN, LOW);
|
||
|
}
|
||
|
}
|