Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With logging: atmega works for sensor to send, receive does not #19

Open
wants to merge 10 commits into
base: withLogging
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Range_Testing/Range_Test_Sensor/RangeTestSensor/src/RangeTestSensor.cpp
Range_Testing/Range_Test_Hub/LoRaRangeTestHub/src/LoRaRangeTestHub.cpp
Range_Testing/Range_Test_Hub/LoRaRangeTestHub/src/LoRaRangeTestHub.cpp
arduinoSketchbook/hardware/*
arduinoSketchbook/libraries/*
9 changes: 9 additions & 0 deletions Range_Testing/Range_Test_Hub/LoRaRangeTestHub/src/Webhook.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "LoRaHubLogging",
"event": "LoRaHubLogging",
"template": "google-sheets",
"url": "https://script.google.com/<<Your app here>>/exec",
"requestType": "POST",
"noDefaults": false,
"rejectUnauthorized": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

/* Interrupt_Test2
* Program to test powering down an ATmega328 and wake it up with an edge truggered interrupt.
* The interrupt used is interrupt 0, which is pin 2 on Arduino or pin 4 on
* the ATmega328.
*
* This is a low power test, based upon published workm by Nick Gammon (https://www.gammon.com.au/power).
*
* The microcontroller goes through setup() and blinks a D9 LED 3 times slowly. It enters loop() and sets up deep
* sleep mode (as deep as possible) and goes to sleep. Before doing so,interrupt 0 ia attached as falling edge truggered.
* A pushbutton is attached to interrupt 0 which grounds the pulled-up line when pressed. A falling edge (pressing the button) should
* then wake up the microcontroller which causes a triple fast blink of the LED, and continues with loop() to go back
* to deep sleep.
*
* In this test, any press of the button should cause one triple blink of the LED, regardless of how
* long that the button is pressed. After the triple blink and until the button is pressed again, the micontroller should
* be in deep sleep. TEST RESULT: verified. Deep sleep measures a few microamps using a ATmega328 not-P (accurate measurement
* is not possible with my cheap multimeter).
*
* NOTE: The initial tests performed on 8/1/24 used an internal pullup on the interrupt 0 line. Deep sleep current measured
* oen to two tenths of a microamp when the button was not pressed (contact open). When the button was pressed (contact closed),
* the deep sleep current went up to about 85 microamps. This is due to the internal pullup being about 40 Kohms.
*
* 8/5/24: I changed the code to not use the internal pullup (version 1.1). I used an external 1 Mohm resistor as the pullup, with a 0.1 uF
* capacitor between the switched line and ground. The capacitor is to filter out noise. Because the RC combination results in
* a long rise time when the switch is released, I wired the switched line to pin 1 of a 74HC14 schmitt trigger inverter input.
* I wired the output (74HC14 pin 2) to the input of the next inverter (74HC14 pin 3) and took the output of this second inverter
* (74HC14 pin 4) as the interrup signal (microcontroller pin 4). I connected thew inputs to the 4 remaining inverters to
* ground to minimize supply current draw. This reduced the deep sleep current draw to about 3.4 microamps
* when the contact is closed! NOTE: it is possible that external schmitt trigger gates are not required as the microcontroller
* may have schmitt triggers on the input pins.
*
* version 1.10, by: Bob Glicksman, 08/05/24
* (c) 2024, Bob Glicksman, Jim Schrempp, Team Practical Projects. All rights reserved.
*/

#include <avr/sleep.h>

// CONSTANTS
const int BUTTON_PIN = 2; // the pushbutton is on digital pin 2 which is chip pin 4
const int LED_PIN = 9; // the LED is on digital pin 9 which is chip pin 15



void setup() {
// pinMode(BUTTON_PIN, INPUT_PULLUP); // Interrupt 0 is pin 2 is chip pin 4
pinMode(BUTTON_PIN, INPUT); // Interrupt 0 is pin 2 is chip pin 4, external pullup with schmitt trigger is used.
pinMode(LED_PIN, OUTPUT); // the LED pin is 9, chip pin 15.
blink3(500); // indicate that setup() is over and loop() begins
pinMode(LED_PIN, INPUT); // change the LED pin for lowest power consumption while sleeping

} // end of setup()

void loop() {
// disable the adc
ADCSRA = 0;

set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();

noInterrupts(); // disable interrupts until we actually go to sleep.
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), isr, FALLING); // ready the wakeup interrupt
EIFR = bit(INTF0); // clear flag for interrupt 0

// turn off brown-out enable in software
// BODS must be set to one and and BODSE must be set to zero within 4 clock cycles
MCUCR = bit(BODS) | bit (BODSE);
// the BODS bit is automatically cleared after 3 clock cycles
MCUCR = bit(BODS);

interrupts(); // enable interrupts just before sleeping
sleep_cpu();

// This part of loop() happens only after an interrupt 0

pinMode(LED_PIN, OUTPUT);
blink3(250); // indicate that the cpu is awake
delay(10); // wait a few milliseconds before changing pinMode()
pinMode(LED_PIN, INPUT);

} // end of loop()

// simple function to blink the LED 3 times at interval "speed" (ms)
void blink3(int speed) {
for (int i = 0; i < 3; i++) {
digitalWrite(9, HIGH);
delay(speed);
digitalWrite(9, LOW);
delay(speed);
}
} // end of blink3()

// the interrupt service routine that wakes up the microcontroller
void isr () {
sleep_disable(); // cancel sleep mode for now
detachInterrupt(digitalPinToInterrupt(BUTTON_PIN)); // preclude more interrupts due to bounce, or other

} // end of isr()
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Project LowPowerTestingTx
* Author: Bob Glicksman
* Date: 5/04/24
*
* Description: This is code for a tester of LoRa module low power operation. The tester
* is based upon a Particle Photon that is set up to not need Wifi (any Arduino can be used
* in its place). In addition to the Photon, a N.O. pushbutton switch is connected to ground
* at one end and Photon pin D0 on the other. The LoRa module (RYLR998) is connected as follows:
* * Vcc to Photon 3.3v (through an ammeter to measure supply current to the LoRa module).
* * GND to Photon GND
* * Tx to Photon Rx (Serial1)
* * Rx to Photon Tx (Serial1)
* * Reset is not connected
*
* The testing concept is to produce a companion device - the "hub". The hub uses its LoRa module
* to listen for a message from the tester. When a message is received, the hub prints the message
* to the USB serial port so that receipt of a proper message can be confirmed. NOTE: the same
* hub as used for LoRa range testing is used here, but the tester does not wait to receive a
* response message before powering down.
*
* The tester is assigned device number 0 and the hub is assigned device number 1. The network
* number used for testing is 3 and the baud rate to/from the LoRa modem is 115200. Otherwise,
* the default LoRa module values are used. NOTE: the tester code does not set up these
* values. The LoRa modules are set up using a PC and an FTDI USB-serial board.
*
* The software senses pressing of the pushbutton and powers up the LoRA module (mode = 0).
* After verifying power up ("+OK" received) the tester sends a short message to a companion LoRa
* module in the hub. After an "+OK" response is received from the LoRa module (indicating that
* the message has been sent to the hub, the LoRa module is powered down (mode = 1).
*
* version 1.0; 5/04/24
*/

//#include "Particle.h"

// The following system directives are to disregard WiFi for Particle devices. Not needed for Arduino.
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

#define VERSION 1.00
#define STATION_NUM 0 // housekeeping; not used in the code

#define PUSHBUTTON D2

void setup() {
pinMode(PUSHBUTTON, INPUT_PULLUP);


Serial1.begin(115200); // the LoRa device
Serial1.setTimeout(10); // a full string is received after 10 ms of no new data from the serial buffer
delay(1000);
} // end of setup()


void loop() {
// test for button to be pressed
if(digitalRead(PUSHBUTTON) == LOW) { // button press detected
// power up the LoRa module
Serial1.println("AT+MODE=0");
waitForOK();

// send the sensor message
Serial1.println("AT+SEND=1,5,HELLO");
waitForOK();

// power down the LoRa module
Serial1.println("AT+MODE=1");
waitForOK();

}

while(digitalRead(PUSHBUTTON) == LOW); // wait for button to be released
delay(100); // wait a little while before sampling the button again

} // end of loop()

void waitForOK() {
// wait for data in the serial buffer
while(Serial1.available() <=0 ) {}

// read out the whole string (note: setTimeout ensure that the whole string is read)
String receivedData = Serial1.readString();

// we could test for "+OK" but what would we do if we got an error?
// so we are satisfied that the LoRa module responsed to the command message

return;
} // end of waitForOK()
Loading