Skip to content
Snippets Groups Projects
Commit 45925cc7 authored by Zoe Pfister's avatar Zoe Pfister :speech_balloon:
Browse files

WIP: Rename Timemanager to NTPManager, move modem information logging to ConnectionManager

parent e42a15df
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!25Minor additions to host,!24Refactor of Host ESP32
......@@ -2,18 +2,18 @@
// Created by zoe on 12/19/22.
//
#include "TimeManager.h"
#include "NTPManager.h"
TimeManager::TimeManager(TinyGsmSim7000 &modem) : modem(modem) {}
NTPManager::NTPManager(TinyGsmSim7000 &modem) : modem(modem) {}
void TimeManager::syncNTP(const std::string &ntpServer, const std::string &backupNtpServer) {
void NTPManager::syncNTP(const std::string &ntpServer, const std::string &backupNtpServer) {
// create list of ntp servers
std::vector<std::string> ntpServers;
ntpServers.push_back(ntpServer);
ntpServers.push_back(backupNtpServer);
tryNtpServerSync(backupNtpServer, ntpServers);
}
void TimeManager::tryNtpServerSync(const std::string &backupNtpServer,
void NTPManager::tryNtpServerSync(const std::string &backupNtpServer,
std::vector<std::string> &ntpServers) { // try to sync ntp with each server
for (const auto &server : ntpServers) {
try {
......@@ -29,7 +29,7 @@ void TimeManager::tryNtpServerSync(const std::string &backupNtpServer,
}
}
}
void TimeManager::synchronizeNTPWithModem(const std::string &ntpServer) {
void NTPManager::synchronizeNTPWithModem(const std::string &ntpServer) {
esp_log_write(ESP_LOG_DEBUG, TAG_TIMEMANAGER, "NTP Server Syncing...\n");
long error = modem.NTPServerSync(ntpServer.c_str(), timeZone);
......@@ -51,7 +51,7 @@ void TimeManager::synchronizeNTPWithModem(const std::string &ntpServer) {
}
}
time_t TimeManager::timeToUnixEpochSeconds(const std::string &time, const std::string &format) {
time_t NTPManager::timeToUnixEpochSeconds(const std::string &time, const std::string &format) {
// 22/10/27,10:16:20+00
struct tm tm {};
time_t dateInEpoch = 0;
......@@ -78,7 +78,7 @@ time_t TimeManager::timeToUnixEpochSeconds(const std::string &time, const std::s
return dateInEpoch;
}
void TimeManager::writeModemTimeToRTC() {
void NTPManager::writeModemTimeToRTC() {
auto gsmDateTimeString = modem.getGSMDateTime(DATE_FULL);
esp_log_write(ESP_LOG_DEBUG, TAG_TIMEMANAGER, "GSM DateTime: %s\n", gsmDateTimeString.c_str());
time_t time = timeToUnixEpochSeconds(gsmDateTimeString.c_str());
......
......@@ -2,8 +2,8 @@
// Created by zoe on 12/19/22.
//
#ifndef HOST_CENTRAL_MAST_TIMEMANAGER_H
#define HOST_CENTRAL_MAST_TIMEMANAGER_H
#ifndef HOST_CENTRAL_MAST_NTPMANAGER_H
#define HOST_CENTRAL_MAST_NTPMANAGER_H
#define TINY_GSM_MODEM_SIM7000
......@@ -15,9 +15,9 @@
#include <iomanip>
#include <string>
class TimeManager {
class NTPManager {
public:
explicit TimeManager(TinyGsmSim7000 &modem);
explicit NTPManager(TinyGsmSim7000 &modem);
private:
int timeZone = 0;
......@@ -39,4 +39,4 @@ class TimeManager {
void writeModemTimeToRTC();
};
#endif // HOST_CENTRAL_MAST_TIMEMANAGER_H
\ No newline at end of file
#endif // HOST_CENTRAL_MAST_NTPMANAGER_H
\ No newline at end of file
......@@ -5,16 +5,16 @@
#ifndef HOST_CENTRAL_MAST_STRINGTOTIMECONVERSIONEXCEPTION_HPP
#define HOST_CENTRAL_MAST_STRINGTOTIMECONVERSIONEXCEPTION_HPP
#include "NTPManager.h"
#include <exception>
#include <string>
#include "TimeManager.h"
class StringToTimeConversionException : public std::exception {
public:
explicit StringToTimeConversionException(const std::string &message) : message(message) {}
const char *what() const noexcept override {
esp_log_write(ESP_LOG_ERROR, "TimeManager", "Error converting time to epoch: %s",
esp_log_write(ESP_LOG_ERROR, "NTPManager", "Error converting time to epoch: %s",
message.c_str());
return message.c_str();
}
......
......@@ -4,7 +4,6 @@
#include "ConnectionManager.h"
ConnectionManager::ConnectionManager(TinyGsm &modem, GPRSCredentials credentials, ModemPins modemPins)
: modem(modem), credentials(std::move(credentials)), modemPins(modemPins) {}
......@@ -127,6 +126,8 @@ void ConnectionManager::modemPowerOn() {
}
void ConnectionManager::modemPowerOff() {
// Try to power-off (modem may decide to restart automatically)
// To turn off modem completely, please use Reset/Enable pins
modem.sendAT("+CPOWD=1");
if (modem.waitResponse(10000L) != 1) {
esp_log_write(ESP_LOG_WARN, TAG_GSM, "Failed to power off modem\n");
......@@ -153,3 +154,25 @@ bool ConnectionManager::waitForNetwork() {
bool ConnectionManager::gprsDisconnect() {
return modem.gprsDisconnect();
}
void ConnectionManager::logModemInformation() {
bool res = modem.isGprsConnected();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "GPRS connected: %s\n", res ? "true" : "false");
String ccid = modem.getSimCCID();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "CCID: %s\n", ccid.c_str());
String imei = modem.getIMEI();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "IMEI: %s\n", imei.c_str());
String imsi = modem.getIMSI();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "IMSI: %s\n", imsi.c_str());
String cop = modem.getOperator();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Operator: %s\n", cop.c_str());
IPAddress local = modem.localIP();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Local IP: %s\n", local.toString().c_str());
int csq = modem.getSignalQuality();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Signal quality: %d\n", csq);
}
......@@ -67,6 +67,8 @@ class ConnectionManager {
void unlockSimCard();
bool waitForNetwork();
void logModemInformation();
};
#endif // HOST_CENTRAL_MAST_CONNECTIONMANAGER_H
......@@ -13,7 +13,7 @@
#include <ArduinoJson.h>
#include <Definitions.h>
#include <ESP32Time.h>
#include <TimeManager.h>
#include <NTPManager.h>
#include <TinyGsmClient.h>
#include <WiFi.h>
#include <esp_log.h>
......@@ -21,7 +21,6 @@
#include <queue>
#include <sys/unistd.h>
// TODO: TimeManager should handle this or rename TimeManager to NTPManager
ESP32Time rtc;
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
......@@ -40,7 +39,7 @@ const GPRSCredentials gprsCredentials = GPRSCredentials("m2m.public.at", "", "")
ConnectionManager connectionManager{modem, gprsCredentials,
ConnectionManager::ModemPins{PIN_DTR, PIN_TX, PIN_RX, PWR_PIN}};
TimeManager timeManager{modem};
NTPManager ntpManager{modem};
uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t broadcast = {};
......@@ -201,17 +200,16 @@ void loop() {
connectionManager.disableGPS();
try {
// may fail if already set
connectionManager.setNetworkMode(LTE_ONLY);
connectionManager.setPreferredMode(CAT_M);
} catch (const std::exception &e) {
esp_log_write(ESP_LOG_ERROR, TAG_GSM, "Error setting network mode: %s\n", e.what());
}
String name = modem.getModemName();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Modem Name: %s\n", name.c_str());
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Modem Name: %s\n", modem.getModemName().c_str());
String modemInfo = modem.getModemInfo();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Modem Info: %s\n", modemInfo.c_str());
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Modem Info: %s\n", modem.getModemInfo().c_str());
connectionManager.unlockSimCard();
......@@ -232,31 +230,12 @@ void loop() {
return;
}
bool res = modem.isGprsConnected();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "GPRS connected: %s\n", res ? "true" : "false");
connectionManager.logModemInformation();
String ccid = modem.getSimCCID();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "CCID: %s\n", ccid.c_str());
String imei = modem.getIMEI();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "IMEI: %s\n", imei.c_str());
String imsi = modem.getIMSI();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "IMSI: %s\n", imsi.c_str());
String cop = modem.getOperator();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Operator: %s\n", cop.c_str());
IPAddress local = modem.localIP();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Local IP: %s\n", local.toString().c_str());
int csq = modem.getSignalQuality();
esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Signal quality: %d\n", csq);
timeManager.syncNTP("pool.ntp.org", "at.pool.ntp.org");
ntpManager.syncNTP("pool.ntp.org", "at.pool.ntp.org");
try {
timeManager.writeModemTimeToRTC();
ntpManager.writeModemTimeToRTC();
} catch (const std::exception &e) {
esp_log_write(ESP_LOG_ERROR, TAG_GSM, "Error writing time to rtc: %s\n", e.what());
}
......@@ -276,100 +255,5 @@ void loop() {
connectionManager.modemPowerOff();
// put readings into array
// protocol address
// hardware name != sensor name
//
// connectionManager.setBand(ConnectionManager::BAND_CAT_M, 20);
//
// delay(200);
//
// connectionManager.setModemFunctionalityLevel(ModemFunctionalityLevel::FULL);
// delay(200);
// modem.disableGPS();
// delay(200);
// esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s\n", String(modem.getSignalQuality()).c_str());
//
// esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Trying to connect to network\n");
//
// Serial.println("\n\n\nWaiting for network...");
// if (!modem.waitForNetwork()) {
// delay(10000);
// return;
// }
// esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Waiting for network...\n");
// if (!connectionManager.isNetworkConnected()) {
// esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Network not connected\n");
// return;
// } else {
// esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Network connected\n");
// delay(200);
// connectionManager.gprsConnect();
// delay(200);
// delay(200);
// syncUTCTimeToRTC();
//
// // quality
// esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s\n", String(modem.getSignalQuality()).c_str());
// // make a http post request
// String url = "influxdb.qe-forte.uibk.ac.at";
// String path = "/api/v2/write?org=QE&bucket=esp32test&precision=s";
// Serial.print("Connecting to ");
// esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s\n", url.c_str());
// // Use WiFiClient class to create TCP connections
//
// while (!queue.empty()) {
//
// xSemaphoreTake(xMutex, portMAX_DELAY);
// String lineData = queue.front();
// queue.pop();
// xSemaphoreGive(xMutex);
// String method = "POST";
//
// RequestInformation requestInformation = RequestInformation(method, url, path, lineData);
//
// //"sensorName":"DRS26","timestamp":1666872216,"protocol":"I2C","value":0,"channel":0,"measurementType":"CIRCUMFERENCE_INCREMENT"
//
// String request = buildRequest(requestInformation);
// esp_log_write(ESP_LOG_VERBOSE, TAG_MAIN, "request: %s\n", request.c_str());
//
// connectionManager.connect(80, requestInformation);
// // TinyGsmClient client{modem};
// // const int httpPort = 80;
// // if (!client.connect(url.c_str(), httpPort)) {
// // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "connection failed\n");
// // return;
// // }
// //
// // client.print(request);
// //
// // // print response
// // while (client.connected()) {
// // String line = client.readStringUntil('\n');
// // if (line == "\r") {
// // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "headers received\n");
// // break;
// // }
// // }
// // client.stop();
// delay(1000);
// }
// DBG("Network connected");
// }
#if TINY_GSM_POWERDOWN
// Try to power-off (modem may decide to restart automatically)
// To turn off modem completely, please use Reset/Enable pins
// modem.sendAT("+CPOWD=1");
// if (modem.waitResponse(10000L) != 1) {
// DBG("+CPOWD=1");
// }
// modem.poweroff();
esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Poweroff.");
#endif
delay(5000);
delay(60000);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment