From eb26a963a149ea3e814cd8a6099841b10b925f6a Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Fri, 30 Aug 2024 14:38:24 +0800 Subject: [PATCH] Added SendLocationFromSMS_Use_TinyGPS example https://github.com/Xinyuan-LilyGO/LilyGO-T-A76XX/issues/118 --- .../.skip.T-SIM7672G | 0 .../SendLocationFromSMS_Use_TinyGPS.ino | 250 ++++++++++++++++++ .../utilities.h | 243 +++++++++++++++++ 3 files changed, 493 insertions(+) create mode 100644 examples/SendLocationFromSMS_Use_TinyGPS/.skip.T-SIM7672G create mode 100644 examples/SendLocationFromSMS_Use_TinyGPS/SendLocationFromSMS_Use_TinyGPS.ino create mode 100644 examples/SendLocationFromSMS_Use_TinyGPS/utilities.h diff --git a/examples/SendLocationFromSMS_Use_TinyGPS/.skip.T-SIM7672G b/examples/SendLocationFromSMS_Use_TinyGPS/.skip.T-SIM7672G new file mode 100644 index 0000000..e69de29 diff --git a/examples/SendLocationFromSMS_Use_TinyGPS/SendLocationFromSMS_Use_TinyGPS.ino b/examples/SendLocationFromSMS_Use_TinyGPS/SendLocationFromSMS_Use_TinyGPS.ino new file mode 100644 index 0000000..5f8588c --- /dev/null +++ b/examples/SendLocationFromSMS_Use_TinyGPS/SendLocationFromSMS_Use_TinyGPS.ino @@ -0,0 +1,250 @@ +/** + * @file SendLocationFromSMS_Use_TinyGPS.ino + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2024-08-30 + * @note SIM7670G does not support SMS and voice functions + */ + +#include "utilities.h" + + +// Define the serial console for debug prints, if needed +#define TINY_GSM_DEBUG SerialMon + +// Set serial for debug console (to the Serial Monitor, default speed 115200) +#define SerialMon Serial + +// See all AT commands, if wanted +// #define DUMP_AT_COMMANDS + + +#include + +#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib +#include +StreamDebugger debugger(SerialAT, SerialMon); +TinyGsm modem(debugger); +#else +TinyGsm modem(SerialAT); +#endif + + +#include +TinyGPSPlus gps; + +uint32_t last_check = 0; + +// It depends on the operator whether to set up an APN. If some operators do not set up an APN, +// they will be rejected when registering for the network. You need to ask the local operator for the specific APN. +// APNs from other operators are welcome to submit PRs for filling. +// #define NETWORK_APN "CHN-CT" //CHN-CT: China Telecom + + +#define SMS_TARGET "+86xxxxxxxxxx" //Change the number you want to send sms message + + + +void light_sleep(uint32_t ms) +{ + esp_sleep_enable_timer_wakeup(ms * 1000); + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + esp_light_sleep_start(); +} + +void deep_sleep(uint32_t ms) +{ + esp_sleep_enable_timer_wakeup(ms * 1000); + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + esp_deep_sleep_start(); +} + +void smartDelay(unsigned long ms) +{ + int ch = 0; + unsigned long start = millis(); + do { + while (SerialAT.available()) { + ch = SerialAT.read(); + Serial.write(ch); + gps.encode(ch); + } + } while (millis() - start < ms); +} + + + +bool loopGPS() +{ + smartDelay(1000); + + if ((gps.charsProcessed() < 50) && ((millis() - last_check) > 10000UL)) { + Serial.println("ERROR:GPS NO DATA!"); delay(1000); + } + + if (gps.date.isValid() && gps.location.isValid() && gps.time.isValid()) { + if (gps.date.year() > 2000) { + + Serial.println("The location was obtained successfully, and GPS positioning was turned off."); + /* + * After obtaining the geographic location, + * turn off NMEA sentence output, otherwise it will affect AT interaction + * */ + modem.disableNMEA(); + + // Turn Off GPS + modem.disableGPS(); + return true; + } + } + return false; +} + + +void setup() +{ + Serial.begin(115200); + // Turn on DC boost to power on the modem +#ifdef BOARD_POWERON_PIN + pinMode(BOARD_POWERON_PIN, OUTPUT); + digitalWrite(BOARD_POWERON_PIN, HIGH); +#endif + + // Set modem reset pin ,reset modem + pinMode(MODEM_RESET_PIN, OUTPUT); + digitalWrite(MODEM_RESET_PIN, !MODEM_RESET_LEVEL); delay(100); + digitalWrite(MODEM_RESET_PIN, MODEM_RESET_LEVEL); delay(2600); + digitalWrite(MODEM_RESET_PIN, !MODEM_RESET_LEVEL); + + // Turn on modem + pinMode(BOARD_PWRKEY_PIN, OUTPUT); + digitalWrite(BOARD_PWRKEY_PIN, LOW); + delay(100); + digitalWrite(BOARD_PWRKEY_PIN, HIGH); + delay(1000); + digitalWrite(BOARD_PWRKEY_PIN, LOW); + + // Set ring pin input + pinMode(MODEM_RING_PIN, INPUT_PULLUP); + + // Set modem baud + SerialAT.begin(115200, SERIAL_8N1, MODEM_RX_PIN, MODEM_TX_PIN); + + Serial.println("Start modem..."); + delay(3000); + + while (!modem.testAT()) { + delay(10); + } + + +#ifdef NETWORK_APN + Serial.printf("Set network apn : %s\n", NETWORK_APN); + modem.sendAT(GF("+CGDCONT=1,\"IP\",\""), NETWORK_APN, "\""); + if (modem.waitResponse() != 1) { + Serial.println("Set network apn error !"); + } +#endif + + // Check network registration status and network signal status + int16_t sq ; + Serial.print("Wait for the modem to register with the network."); + RegStatus status = REG_NO_RESULT; + while (status == REG_NO_RESULT || status == REG_SEARCHING || status == REG_UNREGISTERED) { + status = modem.getRegistrationStatus(); + switch (status) { + case REG_UNREGISTERED: + case REG_SEARCHING: + sq = modem.getSignalQuality(); + Serial.printf("[%lu] Signal Quality:%d\n", millis() / 1000, sq); + delay(1000); + break; + case REG_DENIED: + Serial.println("Network registration was rejected, please check if the APN is correct"); + return ; + case REG_OK_HOME: + Serial.println("Online registration successful"); + break; + case REG_OK_ROAMING: + Serial.println("Network registration successful, currently in roaming mode"); + break; + default: + Serial.printf("Registration Status:%d\n", status); + delay(1000); + break; + } + } + Serial.println(); + + Serial.printf("Registration Status:%d\n", status); + delay(1000); + + + + Serial.println("Enabling GPS/GNSS/GLONASS"); + while (!modem.enableGPS(MODEM_GPS_ENABLE_GPIO)) { + Serial.print("."); + } + Serial.println(); + Serial.println("GPS Enabled"); + + + /* + * Enable GPS OUTPUT NMEA parse from TinyGPS Library + * * */ + modem.setGPSBaud(115200); + modem.setGPSMode(3); //GPS + BD + modem.configNMEASentence(1, 1, 1, 1, 1, 1); + modem.setGPSOutputRate(1); + modem.enableNMEA(); + + last_check = millis(); +} + + +void loop() +{ + if (loopGPS() == true) { + + Serial.printf("The location was obtained successfully and sent to the %s number.\n", SMS_TARGET); + + double lon2 = gps.location.lng(); + double lat2 = gps.location.lat(); + uint16_t year2 = gps.date.year(); + uint8_t month2 = gps.date.month(); + uint8_t day2 = gps.date.day(); + uint8_t hour2 = gps.time.hour(); + uint8_t min2 = gps.time.minute(); + uint8_t sec2 = gps.time.second(); + double speed = gps.speed.value(); + + String msg_str = "Longitude:" + String(lon2, 6) + " "; + msg_str += "Latitude:" + String(lat2, 6) + "\n"; + msg_str += "UTC Date:" + String(year2) + "/"; + msg_str += String(month2) + "/"; + msg_str += String(day2) + " \n"; + msg_str += "UTC Time:" + String(hour2) + ":"; + msg_str += String(min2) + ":"; + msg_str += String(sec2); + msg_str += "\n"; + msg_str += "Speed:"; + msg_str += String(speed); + msg_str += "Km/H\n"; + + Serial.print("MESSAGE:"); Serial.println(msg_str); + + bool res = modem.sendSMS(SMS_TARGET, msg_str); + Serial.print("Send sms message "); + Serial.println(res ? "OK" : "fail"); + + // Wait print done + delay(3000); + + // Deep sleep, wake up every 60 seconds for positioning + deep_sleep(60 * 1000); //60 Second + } + + // Light sleep, reducing the allowed power consumption + light_sleep(5000); +} diff --git a/examples/SendLocationFromSMS_Use_TinyGPS/utilities.h b/examples/SendLocationFromSMS_Use_TinyGPS/utilities.h new file mode 100644 index 0000000..9f71d3e --- /dev/null +++ b/examples/SendLocationFromSMS_Use_TinyGPS/utilities.h @@ -0,0 +1,243 @@ +/** + * @file utilities.h + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2023-11-15 + * + */ + +#pragma once + +// Note: +// When using ArduinoIDE, you must select a corresponding board type. +// If you don’t know which board type you have, please click on the link to view it. +// 使用ArduinoIDE ,必须选择一个对应的板型 ,如果你不知道你的板型是哪种,请点击链接进行查看 + +// Products Link:https://www.lilygo.cc/products/t-sim-a7670e +// #define LILYGO_T_A7670 + + +// T-Call-A767X V1.0 and V1.1 have different Pinmaps and cannot be used universally. +// #define LILYGO_T_CALL_A7670_V1_0 + +// T-Call-A767X V1.0 and V1.1 have different Pinmaps and cannot be used universally. +// #define LILYGO_T_CALL_A7670_V1_1 + +//! SIM7672G and SIM7670G are exactly the same, except for the name change +// #define LILYGO_T_SIM767XG_S3 + +// #define LILYGO_T_A7608X + +// #define LILYGO_T_A7608X_S3 + +// #define LILYGO_T_A7608X_DC_S3 + + + + + +#if defined(LILYGO_T_A7670) + +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (25) +#define MODEM_TX_PIN (26) +#define MODEM_RX_PIN (27) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (4) +#define BOARD_ADC_PIN (35) +// The modem power switch must be set to HIGH for the modem to supply power. +#define BOARD_POWERON_PIN (12) +#define MODEM_RING_PIN (33) +#define MODEM_RESET_PIN (5) +#define BOARD_MISO_PIN (2) +#define BOARD_MOSI_PIN (15) +#define BOARD_SCK_PIN (14) +#define BOARD_SD_CS_PIN (13) +#define BOARD_BAT_ADC_PIN (35) +#define MODEM_RESET_LEVEL HIGH +#define SerialAT Serial1 + +#define MODEM_GPS_ENABLE_GPIO (-1) + + +#ifndef TINY_GSM_MODEM_A7670 +#define TINY_GSM_MODEM_A7670 +#endif + +// It is only available in V1.4 version. In other versions, IO36 is not connected. +#define BOARD_SOLAR_ADC_PIN (36) + + +#elif defined(LILYGO_T_CALL_A7670_V1_0) +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (14) +#define MODEM_TX_PIN (26) +#define MODEM_RX_PIN (25) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (4) +#define BOARD_LED_PIN (12) +// There is no modem power control, the LED Pin is used as a power indicator here. +#define BOARD_POWERON_PIN (BOARD_LED_PIN) +#define MODEM_RING_PIN (13) +#define MODEM_RESET_PIN (27) +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + + +#define MODEM_GPS_ENABLE_GPIO (-1) + + +#ifndef TINY_GSM_MODEM_A7670 +#define TINY_GSM_MODEM_A7670 +#endif + +#elif defined(LILYGO_T_CALL_A7670_V1_1) + +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (32) +#define MODEM_TX_PIN (27) +#define MODEM_RX_PIN (26) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (4) +#define BOARD_LED_PIN (13) +// There is no modem power control, the LED Pin is used as a power indicator here. +#define BOARD_POWERON_PIN (BOARD_LED_PIN) +#define MODEM_RING_PIN (33) +#define MODEM_RESET_PIN (5) +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + +#define MODEM_GPS_ENABLE_GPIO (-1) + +#ifndef TINY_GSM_MODEM_A7670 +#define TINY_GSM_MODEM_A7670 +#endif + +#elif defined(LILYGO_T_SIM767XG_S3) +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (9) +#define MODEM_TX_PIN (11) +#define MODEM_RX_PIN (10) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (18) +#define BOARD_LED_PIN (12) +// There is no modem power control, the LED Pin is used as a power indicator here. +#define BOARD_POWERON_PIN (BOARD_LED_PIN) +#define MODEM_RING_PIN (3) +#define MODEM_RESET_PIN (17) +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + +#define BOARD_BAT_ADC_PIN (4) +#define BOARD_SOLAR_ADC_PIN (5) +#define BOARD_MISO_PIN (47) +#define BOARD_MOSI_PIN (14) +#define BOARD_SCK_PIN (21) +#define BOARD_SD_CS_PIN (13) + +#ifndef TINY_GSM_MODEM_SIM7672 +#define TINY_GSM_MODEM_SIM7672 +#endif + +#define MODEM_GPS_ENABLE_GPIO (4) + + +#elif defined(LILYGO_T_A7608X) + +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (25) +#define MODEM_TX_PIN (26) +#define MODEM_RX_PIN (27) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (4) +#define BOARD_BAT_ADC_PIN (35) +// The modem power switch must be set to HIGH for the modem to supply power. +#define BOARD_POWERON_PIN (12) //T-A7608-V2 is onboard led +#define MODEM_RING_PIN (33) +#define MODEM_RESET_PIN (5) //T-A7608-V2 no connection +#define BOARD_MISO_PIN (2) +#define BOARD_MOSI_PIN (15) +#define BOARD_SCK_PIN (14) +#define BOARD_SD_CS_PIN (13) + +#define MODEM_RESET_LEVEL HIGH +#define SerialAT Serial1 + + +#ifndef TINY_GSM_MODEM_A7608 +#define TINY_GSM_MODEM_A7608 +#endif + +// only version v1.1 or V2 has solar adc pin +#define BOARD_SOLAR_ADC_PIN (34) + + +// 127 is defined in GSM as the AUXVDD index +#define MODEM_GPS_ENABLE_GPIO (127) + +#elif defined(LILYGO_T_A7608X_S3) + +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (7) +#define MODEM_TX_PIN (17) +#define MODEM_RX_PIN (18) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (15) +#define BOARD_BAT_ADC_PIN (4) +// The modem power switch must be set to HIGH for the modem to supply power. +// #define BOARD_POWERON_PIN (12) +#define MODEM_RING_PIN (6) +#define MODEM_RESET_PIN (16) +#define BOARD_MISO_PIN (47) +#define BOARD_MOSI_PIN (14) +#define BOARD_SCK_PIN (21) +#define BOARD_SD_CS_PIN (13) + +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + +#ifndef TINY_GSM_MODEM_A7608 +#define TINY_GSM_MODEM_A7608 +#endif + +// only version v1.1 has solar adc pin +#define BOARD_SOLAR_ADC_PIN (3) + +// 127 is defined in GSM as the AUXVDD index +#define MODEM_GPS_ENABLE_GPIO (127) + +#elif defined(LILYGO_T_A7608X_DC_S3) + +#define MODEM_DTR_PIN (5) +#define MODEM_RX_PIN (42) +#define MODEM_TX_PIN (41) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (38) +#define MODEM_RING_PIN (6) +#define MODEM_RESET_PIN (40) +#define MODEM_RTS_PIN (1) +#define MODEM_CTS_PIN (2) + +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + +#ifndef TINY_GSM_MODEM_A7608 +#define TINY_GSM_MODEM_A7608 +#endif + +// 127 is defined in GSM as the AUXVDD index +#define MODEM_GPS_ENABLE_GPIO (127) + +#else +#error "Use ArduinoIDE, please open the macro definition corresponding to the board above " +#endif + + + + + + + + +