Add sleep

This commit is contained in:
2022-04-24 21:05:49 +02:00
parent 17b6e0487b
commit d1c052ac90
6 changed files with 236 additions and 139 deletions

@ -2,7 +2,7 @@
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
#include <SPI.h>
#include <TemperatureZero.h>
#include <TemperatureZero.h>´
#define CLIENT_ADDRESS 1
@ -36,7 +36,7 @@ RHReliableDatagram rfManager(rfm95, CLIENT_ADDRESS);
TemperatureZero TempZero = TemperatureZero();
void setup()
void setup()
{
// Pins with internal functions
pinMode(BOOSTEN_PIN, OUTPUT);
@ -44,75 +44,84 @@ void setup()
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Acivating Motor and Digital Outputs, but at LOW level
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
// explcitly activate inputs, to avoid conflicts
pinMode(38, INPUT);
pinMode(2, INPUT);
pinMode(5, INPUT);
pinMode(11, INPUT);
/*// Acivating Motor and Digital Outputs, but at LOW level
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
// Configuring ADC
analogReference(AR_INTERNAL2V23);
analogReadResolution(12);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
SerialUSB.begin(9600);
//while (!SerialUSB) ; // Wait for serial port to be available
// explcitly activate inputs, to avoid conflicts
pinMode(38, INPUT);
pinMode(2, INPUT);
pinMode(5, INPUT);
pinMode(11, INPUT);
if (!rfManager.init())
// Configuring ADC
analogReference(AR_INTERNAL2V23);
analogReadResolution(12);
SerialUSB.begin(9600);
//while (!SerialUSB) ; // Wait for serial port to be available
if (!rfManager.init())
SerialUSB.println("init failed");
rfm95.setModemConfig(MODEM_CONFIG);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
rfm95.setModemConfig(MODEM_CONFIG);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 2 to 20 dBm:
rfm95.setTxPower(MODEM_POWER, false);
rfm95.setFrequency(MODEM_FREQ);
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 2 to 20 dBm:
rfm95.setTxPower(MODEM_POWER, false);
rfm95.setFrequency(MODEM_FREQ);
// You can optionally require this module to wait until Channel Activity
// Detection shows no activity on the channel before transmitting by setting
// the CAD timeout to non-zero:
// rfm95.setCADTimeout(10000);
// You can optionally require this module to wait until Channel Activity
// Detection shows no activity on the channel before transmitting by setting
// the CAD timeout to non-zero:
// rfm95.setCADTimeout(10000);
// on-chip teperature sensor
TempZero.init();
// activate all five "outputs" for a short period during startup (left to right)
digitalWrite(BOOSTEN_PIN, HIGH);
delay(1000);
digitalWrite(BOOSTEN_PIN,LOW);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10,LOW);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12,LOW);
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6,LOW);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7,LOW);
// on-chip teperature sensor
TempZero.init();
// Print Status Report
PrintSR();
// activate all five "outputs" for a short period during startup (left to right)
digitalWrite(BOOSTEN_PIN, HIGH);
delay(1000);
digitalWrite(BOOSTEN_PIN, LOW);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
// switch of LED after startup
// Print Status Report
PrintSR();
// switch of LED after startup
digitalWrite(LED_BUILTIN, LOW);
rfm95.sleep();*/
digitalWrite(LED_BUILTIN, LOW);
for (int i = 0; i < 10; i++)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
@ -125,17 +134,40 @@ uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
void loop()
{
while (!USBDevice.connected())
{
LowPower.sleep(1000);
USBDevice.init();
}
for (int i = 0; i < 10; i++)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
/*if (USBDevice.connected())
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);*/
return;
digitalWrite(BOOSTEN_PIN, HIGH);
delay(2000);
digitalWrite(BOOSTEN_PIN, LOW);
delay(2000);
LowPower.deepSleep(4000);
return;
/*
SerialUSB.println("Sending to rf95_reliable_datagram_server");
SerialUSB.println("Sending to rf95_reliable_datagram_server");
// Send a message to manager_server
if (rfManager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
{
// 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;
uint8_t from;
if (rfManager.recvfromAckTimeout(buf, &len, 2000, &from))
{
SerialUSB.print("got reply from : 0x");
@ -147,29 +179,29 @@ void loop()
{
SerialUSB.println("No reply, is rf95_reliable_datagram_server running?");
}
}
else
}
else
SerialUSB.println("sendtoWait failed");
*/
// TICK-ROUTINE
if(LoopState && millis() > nextTick)
if (LoopState && millis() > nextTick)
{
switch (LoopState){
switch (LoopState) {
case 1: // Read and print Currents
SerialUSB.print(float(analogRead(A1))/DIVIDER_mA, 3);
SerialUSB.print(float(analogRead(A1)) / DIVIDER_mA, 3);
SerialUSB.print("\t");
SerialUSB.print(float(analogRead(A2))/DIVIDER_mA, 3);
SerialUSB.print(float(analogRead(A2)) / DIVIDER_mA, 3);
SerialUSB.print("\t");
SerialUSB.println(float(analogRead(A3))/DIVIDER_mA, 3);
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(float(analogRead(A1)) / DIVIDER_V, 3);
SerialUSB.print("\t");
SerialUSB.print(float(analogRead(A2))/DIVIDER_V, 3);
SerialUSB.print(float(analogRead(A2)) / DIVIDER_V, 3);
SerialUSB.print("\t");
SerialUSB.println(float(analogRead(A3))/DIVIDER_V, 3);
SerialUSB.println(float(analogRead(A3)) / DIVIDER_V, 3);
break;
case 3: // Read and print all GPIO and DigINPUTs
@ -191,11 +223,11 @@ void loop()
break;
case 4: // Read and Print Battery Voltage
SerialUSB.println(float(analogRead(BATMON_PIN))/918, 3);
SerialUSB.println(float(analogRead(BATMON_PIN)) / 918, 3);
break;
case 5: // Read and Print internal Temperature
SerialUSB.println(TempZero.readInternalTemperature());
SerialUSB.println(TempZero.readInternalTemperature());
break;
}
@ -208,7 +240,7 @@ void loop()
inByte = SerialUSB.read();
SerialUSB.read();
if(LoopState == 1)
if (LoopState == 1)
digitalWrite(BOOSTEN_PIN, LOW);
//else if(LoopState == 4)
// msTick = 500;
@ -234,7 +266,7 @@ void loop()
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;
@ -246,24 +278,24 @@ void loop()
default:
LoopState = 0;
PrintSR();
break;
}
break;
}
}
/*
delay(5000);
rfm95.sleep();
delay(5000);
LowPower.deepSleep(10000);
*/
/*
delay(5000);
rfm95.sleep();
delay(5000);
LowPower.deepSleep(10000);
*/
}
void PrintSR(){
void PrintSR() {
// Status Report: Dumps a bunch of usefull information to SerialUSB
// Toggle LED
// Toggle LED
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
SerialUSB.println("**************************************");
@ -290,20 +322,20 @@ void PrintSR(){
SerialUSB.println("********** ATSAMD21G18A MCU *********");
SerialUSB.print("Internal Temperature (°C): ");
SerialUSB.println(TempZero.readInternalTemperature());
SerialUSB.println(TempZero.readInternalTemperature());
SerialUSB.print("Battery Voltage (V): ");
SerialUSB.println(float(analogRead(BATMON_PIN))/918);
SerialUSB.println(float(analogRead(BATMON_PIN)) / 918);
if(digitalRead(LED_BUILTIN))
if (digitalRead(LED_BUILTIN))
SerialUSB.println("Built-In LED ON");
else
SerialUSB.println("Built-In LED OFF");
if(digitalRead(BOOSTEN_PIN))
if (digitalRead(BOOSTEN_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.");