netmon-multinode/MultiNodeTest/MultiNodeTest.ino

225 lines
6.3 KiB
Arduino
Raw Normal View History

2022-04-24 03:37:42 +02:00
#include <ArduinoLowPower.h>
#include <SPI.h>
#include "MNLib.h"
#define IS_CLIENT
//#define IS_SERVER
byte LoopState = 0;
char inByte;
unsigned long nextTick;
unsigned int msTick = 500;
// RFM95 connected to Pin 1 for SS an Pin 0 for interrupt
RH_RF95 rfm95(RF_SS_PIN, RF_IRQ_PIN);
// 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()
{
initialize(true);
}
// from RadioHead sample
uint8_t data[] = "Hello World!";
// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
void loop()
{
/*
SerialUSB.println("Sending to rf95_reliable_datagram_server");
// Send a message to manager_server
if (rfManager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
{
SerialUSB.println("...got an ACK");
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (rfManager.recvfromAckTimeout(buf, &len, 2000, &from))
{
SerialUSB.print("got reply from : 0x");
SerialUSB.print(from, HEX);
SerialUSB.print(": ");
SerialUSB.println((char*)buf);
}
else
{
SerialUSB.println("No reply, is rf95_reliable_datagram_server running?");
}
}
else
SerialUSB.println("sendtoWait failed");
*/
// TICK-ROUTINE
if (LoopState && millis() > nextTick)
{
switch (LoopState) {
case 1: // Read and print Currents
SerialUSB.print(float(analogRead(A1)) / DIVIDER_mA, 3);
SerialUSB.print("\t");
SerialUSB.print(float(analogRead(A2)) / DIVIDER_mA, 3);
SerialUSB.print("\t");
SerialUSB.println(float(analogRead(A3)) / DIVIDER_mA, 3);
break;
case 2: // Read and print Voltages
SerialUSB.print(float(analogRead(A1)) / DIVIDER_V, 3);
SerialUSB.print("\t");
SerialUSB.print(float(analogRead(A2)) / DIVIDER_V, 3);
SerialUSB.print("\t");
SerialUSB.println(float(analogRead(A3)) / DIVIDER_V, 3);
break;
case 3: // Read and print all GPIO and DigINPUTs
SerialUSB.print(digitalRead(8));
SerialUSB.print("\t");
SerialUSB.print(digitalRead(9));
SerialUSB.print("\t \t");
SerialUSB.print(digitalRead(4));
SerialUSB.print("\t");
SerialUSB.print(digitalRead(3));
SerialUSB.print("\t \t \t");
SerialUSB.print(digitalRead(38));
SerialUSB.print("\t");
SerialUSB.print(digitalRead(2));
SerialUSB.print("\t \t");
SerialUSB.print(digitalRead(5));
SerialUSB.print("\t");
SerialUSB.println(digitalRead(11));
break;
case 4: // Read and Print Battery Voltage
SerialUSB.println(float(analogRead(BATMON_PIN)) / 918, 3);
break;
case 5: // Read and Print internal Temperature
SerialUSB.println(TempZero.readInternalTemperature());
break;
}
nextTick = millis() + msTick;
}
// SERIAL CLI
if (SerialUSB.available() > 0) {
inByte = SerialUSB.read();
SerialUSB.read();
if (LoopState == 1)
digitalWrite(LOOPEN_PIN, LOW);
//else if(LoopState == 4)
// msTick = 500;
switch (inByte) {
case 'c':
SerialUSB.println("Currents in mA (switches in lower pos)");
LoopState = 1;
digitalWrite(LOOPEN_PIN, HIGH);
break;
case 'v':
SerialUSB.println("Voltages in V (switches in upper pos)");
LoopState = 2;
break;
case 'd':
SerialUSB.println("GPIO and Digital INPUTS");
LoopState = 3;
break;
case 'b':
SerialUSB.println("Battery Voltage in V");
//msTick = 5000;
LoopState = 4;
break;
case 't':
SerialUSB.println("Searching for DS18B20-Sensors, connected to the four 1-Wire pins...");
SerialUSB.println("On-Chip Temperature in °C");
LoopState = 5;
break;
case 's':
SerialUSB.println("Setting Radio Module and MCU in sleep mode for 10 seconds...");
rfm95.sleep();
LowPower.deepSleep(10000);
break;
default:
LoopState = 0;
PrintSR();
break;
}
}
/*
delay(5000);
rfm95.sleep();
delay(5000);
LowPower.deepSleep(10000);
*/
}
void PrintSR() {
// Status Report: Dumps a bunch of usefull information to SerialUSB
// Toggle LED
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
SerialUSB.println("**************************************");
SerialUSB.println("********** NetMon MultiNode **********");
SerialUSB.println("**************************************");
SerialUSB.println();
SerialUSB.println(__FILE__);
SerialUSB.print(__DATE__);
SerialUSB.print(" / ");
SerialUSB.println(__TIME__);
SerialUSB.println();
SerialUSB.println("********** RFM95 LoRa Module *********");
//SerialUSB.print("Client-ID: "); SerialUSB.print(rfManager.thisAddress());
SerialUSB.print("\tServer-ID: "); SerialUSB.print(configuration.server_address);
//SerialUSB.print("\tRFM-Version: "); SerialUSB.println(rfm95.getDeviceVersion());
SerialUSB.print("Modem-Config: "); SerialUSB.print(MODEM_CONFIG);
SerialUSB.print("\tFrequency: "); SerialUSB.print(MODEM_FREQ);
SerialUSB.print("\tPower: "); SerialUSB.println(MODEM_POWER);
SerialUSB.print("Last RSSI: "); SerialUSB.print(rfm95.lastRssi());
SerialUSB.print("\tFreq-Error: "); SerialUSB.print(rfm95.frequencyError());
SerialUSB.print("\tLast SNR: "); SerialUSB.println(rfm95.lastSNR());
SerialUSB.println();
SerialUSB.println("********** ATSAMD21G18A MCU *********");
SerialUSB.print("Internal Temperature (°C): ");
SerialUSB.println(TempZero.readInternalTemperature());
SerialUSB.print("Battery Voltage (V): ");
SerialUSB.println(float(analogRead(BATMON_PIN)) / 918);
if (digitalRead(LED_BUILTIN))
SerialUSB.println("Built-In LED ON");
else
SerialUSB.println("Built-In LED OFF");
if (digitalRead(LOOPEN_PIN))
SerialUSB.println("LoopSupply ENABLED (12...15 V)");
else
SerialUSB.println("LoopSupply DISABLED");
SerialUSB.println();
SerialUSB.println("Send c, v, d, b or t to plot Analog and Digital readings.");
SerialUSB.println("Send s for 10 seconds of sleep mode.");
// Toggle LED back to previous state
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}