Skip to content

Instantly share code, notes, and snippets.

@nlgranger
Last active September 29, 2023 12:55
Show Gist options
  • Save nlgranger/9da3da3668eb2c99ab1ebfd7b33a44be to your computer and use it in GitHub Desktop.
Save nlgranger/9da3da3668eb2c99ab1ebfd7b33a44be to your computer and use it in GitHub Desktop.
A7670E sleep
/**
* @file sleep.ino
* @author Lewis He ([email protected])
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co.,
* Ltd
* @date 2023-05-24
*
*/
#define TINY_GSM_MODEM_SIM7600
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
// See all AT commands, if wanted
#undef DUMP_AT_COMMANDS
#include <TinyGsmClient.h>
#define uS_TO_S_FACTOR \
1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 30 /* Time ESP32 will go to sleep (in seconds) */
#define UART_BAUD 115200
#define MODEM_RX 27
#define MODEM_TX 26
#define MODEM_PWR 4
#define MODEM_RST 5
#define MODEM_DTR 25
#define BAT_EN 12
#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
void setup() {
Serial.begin(115200); // Set console baud rate
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(100);
if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TIMER) {
Serial.println("Resetting and powering up modem !");
pinMode(MODEM_RST, OUTPUT);
digitalWrite(MODEM_RST, LOW);
pinMode(MODEM_PWR, OUTPUT);
digitalWrite(MODEM_PWR, LOW);
delay(1000);
digitalWrite(MODEM_RST, HIGH);
delay(100);
digitalWrite(MODEM_RST, LOW);
delay(1000);
digitalWrite(MODEM_PWR, HIGH);
delay(100);
digitalWrite(MODEM_PWR, LOW);
Serial.println("Let modem do stuff");
delay(30000);
} else {
Serial.println("Wakeup modem !");
delay(15000);
// Need to cancel GPIO hold if wake from sleep
Serial.println("hold disable on dtr");
gpio_hold_dis((gpio_num_t)MODEM_DTR);
delay(10000);
// Pull down DTR to wake up MODEM
Serial.println("dtr low");
pinMode(MODEM_DTR, OUTPUT);
digitalWrite(MODEM_DTR, LOW);
delay(20000);
modem.sleepEnable(false);
}
Serial.println("Check modem online .");
while (!modem.testAT()) {
Serial.println(".");
delay(500);
}
Serial.println("Modem is online !");
Serial.println("Enable deep sleep mode!");
modem.sendAT("+CSCLK=1");
if (1 == modem.waitResponse(100, GF("OK"))) {
Serial.println("SIM76770 deep sleep enabled");
}
// Pull up DTR to put the modem into sleep
pinMode(MODEM_DTR, OUTPUT);
digitalWrite(MODEM_DTR, HIGH);
// Set DTR to keep at high level, if not set, DTR will be invalid after ESP32
// goes to sleep.
gpio_hold_en((gpio_num_t)MODEM_DTR);
gpio_deep_sleep_hold_en();
delay(10000);
// while (1) {
modem.sendAT("");
if (1 == modem.waitResponse(100, GF("OK"))) {
Serial.println("Modem is still up.");
delay(1000);
} else {
Serial.println("SIM76770 deep sleep enabled");
// break;
}
// }
Serial.println("Enter esp32 goto deepsleep!");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(200);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() {}
@neuralprocessing
Copy link

neuralprocessing commented Sep 28, 2023

Which revision of the board are you using? I tested the code above - my A7670 did not even come online.

@nlgranger
Copy link
Author

It's v1.1. I have tweaked the timing and added a reset to ensure it boots.

@nlgranger
Copy link
Author

BTW, the modem resets upon esp32 waking up from sleep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment