Last Contact, last time switch

Hello,

I use Node-RED with a LilyGO-T-SIM7000G (ESP32-Clone with 4G Connection).

I want to create a mobile device with a couple of relays, sensors and servos. So far it is working very well. But now I dont know, how to realize, that I can see, when a relay was switches for the last time or a servo was turned. At best I want to see, when there was the last contact to the device as it could be that there is no 4G at the devices place for some minutes.

Sorry for my bad English.

Regards
Michael from Leipzig - Germany.

Hi.

You may need to expand on how you are connecting to the relays.

The LilyGO-T-SIM..... will have an output.
I don't know if it would be USB, or what.

You will need something to plug into that LilyGO-T.... thing to decode the messages.

Mosquitto is probably the protocol you need as it has the ability to remember states of things.

But I think I'm getting ahead of where you are.

Oh yes. It is MQTT with a Mosquitto at my server.

Is it helpful to post the code of my Node-RED?

Do you mean you need to know the time of last contact, or do you you just need to take some action if, for example, ten minutes has elapsed with no contact. That can easily be done with a Trigger node.

Both would be great. E. g. Relay1 is turned on. In the dashboard there is a green LED and the message "Turned on at 2022-02-06, 07:00".

And a message if there is no contact for 10 minutes to warn the user that he can´t turn on anything at the moment.

Formatting dates in javascript can be complex, but to get you started, if you put this in a function node it will give you something useful.

msg.payload = `Turned on at ${new Date().toLocaleString()}`
return msg;

As I said, that can easily be accomplished with a Trigger node. The node-red cookbook has many useful examples of common requirements, in particular Trigger a flow if a message isn’t received after a defined time : Node-RED

Thank You, that works fine for me...

@MichaelLE i have that same module. I'm using node red with grafana and influxdb. Eventually it will tie to ittt. Could you share your example code for the esp? I've been trying to get it to connect to mosquitto... i did find a few nice layouts for the unit in node red I'll attach the links later

I´m sorry, I´m only a rookie.
Here is the code I use. I have tried a lot and now all is working fine for me.

/*
  Michi:
  ESP32-Wrover-Module, UploadSpeed 921600, Flash 80Mhz, Mode QiO, Default 4MB with Spiffs
  13.02.2022; Working!

  LilyGO-T-SIM7000G with 
    8-Relay-Module
    BME280 (Temperature, Humidity)
    DC-Motor (with L298N Driver)
    Servo SG90 (with PCA9685 Board)
    Photoresistor (Light-Sensor)
    internal SIM
    internal GPS

  Link für T-Sim7000G
  https://github.com/Xinyuan-LilyGO/LilyGO-T-SIM7000G
*/

#define TINY_GSM_MODEM_SIM7000

//TinyGPS++
#include "TinyGPS++.h"
TinyGPSPlus tinygps;

#include <Wire.h>
#include <TinyGsmClient.h>
#include <PubSubClient.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

// Servo-Board - Test
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN  150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // This is the 'maximum' pulse length count (out of 4096)
#define USMIN  600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
#define USMAX  2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates
uint8_t servonum = 0;

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands
#define SerialAT Serial1
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// Define the serial console for debug prints, if needed
// #define DUMP_AT_COMMANDS

// PERS. SETTINGS: Start ---------------------------
// set GSM PIN, if any
#define GSM_PIN "XXXX"

// Your GPRS credentials, if any
const char apn[] = "XXX"; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = "";
const char gprsPass[] = "";

// 2nd GPS Server
const char serverGPS[] = "XXX.XXX.XXX.XXX";
const int  portGPS = 10439;

// MQTT details
const char* broker = "XXX.de";                    // Public IP address or domain name
const char* mqttUsername = "";  // MQTT username
const char* mqttPassword = "";  // MQTT password

const char* topicTTGO1Relais1Input = "TTGO1/Relais1/Input";
const char* topicTTGO1Relais1Output = "TTGO1/Relais1/Output";
const char* topicTTGO1Relais2Input = "TTGO1/Relais2/Input";
const char* topicTTGO1Relais2Output = "TTGO1/Relais2/Output";
const char* topicTTGO1Relais3Input = "TTGO1/Relais3/Input";
const char* topicTTGO1Relais3Output = "TTGO1/Relais3/Output";
const char* topicTTGO1Relais4Input = "TTGO1/Relais4/Input";
const char* topicTTGO1Relais4Output = "TTGO1/Relais4/Output";
const char* topicTTGO1Relais5Input = "TTGO1/Relais5/Input";
const char* topicTTGO1Relais5Output = "TTGO1/Relais5/Output";
const char* topicTTGO1InterneLedInput = "TTGO1/InterneLed/Input";
const char* topicTTGO1InterneLedOutput = "TTGO1/InterneLed/Output";
const char* topicTTGO1Temperature = "TTGO1/Sensor/Temperature";
const char* topicTTGO1Humidity = "TTGO1/Sensor/Humidity";
const char* topicTTGO1Light = "TTGO1/Sensor/Light";
const char* topicTTGO1Latitude = "TTGO1/GPS/GpsLat";
const char* topicTTGO1Longitude = "TTGO1/GPS/GpsLong";
const char* topicTTGO1Servo1Input = "TTGO1/Servo1/Input";
const char* topicTTGO1DcMotor1Input = "TTGO1/DcMotor1/Input";
char* stateTTGO1Relais1Output;
char* stateTTGO1Relais2Output;
char* stateTTGO1Relais3Output;
char* stateTTGO1Relais4Output;
char* stateTTGO1Relais5Output;
char* stateTTGO1InterneLedOutput;
// PERS. SETTINGS: End ---------------------------


#ifdef DUMP_AT_COMMANDS
  #include <StreamDebugger.h>
  StreamDebugger debugger(SerialAT, SerialMon);
  TinyGsm modem(debugger);
#else
  TinyGsm modem(SerialAT);
#endif

TinyGsmClient client(modem, 0);     // 1st TinyGsmClient for Connection to MQTT-Broker
TinyGsmClient clientGPS(modem, 1);  // 2nd TinyGsmClient for 2nd GPS-Server
PubSubClient mqtt(client);

// TTGO T-Call pins
#define MODEM_RST               5
#define MODEM_PWKEY             4
#define MODEM_POWER_ON          23
#define MODEM_TX                27
#define MODEM_RX                26

// Normal I2C-PINs (for Servo-Board)
#define I2C_SDA                 21
#define I2C_SCL                 22

// 2nd I2C-PINs (for BME280)
#define I2C_SDA_2               18
#define I2C_SCL_2               19

#define PinRelais1              32
#define PinRelais2              33  
#define PinRelais3              34
#define PinRelais4              35
#define PinRelais5              25
#define PinInterneLed           12   // internal LED, HIGH = LED is LED off
#define PinPhotowiderstand      15
#define PinServo1               27
#define PinDcMotor1Pin1         13
#define PinDcMotor1Pin2         14

uint32_t lastReconnectAttempt = 0;

// Powersafing, not working
// I2C for SIM800 (to keep it running when powered from battery)
TwoWire I2CPower = TwoWire(0);
TwoWire I2CBME = TwoWire(1);
Adafruit_BME280 bme;

#define IP5306_ADDR          0x75
#define IP5306_REG_SYS_CTL0  0x00


float temperature;
float humidity;
long lastMsg = 0;

float lat2      = 0;
float lon2      = 0;
float speed2    = 0;
float alt2      = 0;
int   vsat2     = 0;
int   usat2     = 0;
float accuracy2 = 0;
int   year2     = 0;
int   month2    = 0;
int   day2      = 0;
int   hour2     = 0;
int   min2      = 0;
int   sec2      = 0;
long lastGps = 0;

String latnmea = "";
String lonnmea = "";

// Powersafing. Not working.
bool setPowerBoostKeepOn(int en){
  I2CPower.beginTransmission(IP5306_ADDR);
  I2CPower.write(IP5306_REG_SYS_CTL0);
  if (en) {
    I2CPower.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
  } else {
    I2CPower.write(0x35); // 0x37 is default reg value
  }
  return I2CPower.endTransmission() == 0;
}


void mqttCallback(char* topic, byte* message, unsigned int len) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < len; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // If a message is received on the topic, you check if the message is either "true" or "false". 
  // Changes the output state according to the message
  if (String(topic) == "TTGO1/Relais1/Input") {
    Serial.print("Changing output to ");
    if(messageTemp == "true"){
      Serial.println("true");
      digitalWrite(PinRelais1, HIGH);
      Serial.println("Relais1Meldung an Broker: TRUE");
      stateTTGO1Relais1Output = "true";
      mqtt.publish(topicTTGO1Relais1Output, stateTTGO1Relais1Output);
    }
    else if(messageTemp == "false"){
      Serial.println("false");
      digitalWrite(PinRelais1, LOW);
      Serial.println("Relais1Meldung an Broker: FALSE");
      stateTTGO1Relais1Output = "false";
      mqtt.publish(topicTTGO1Relais1Output, stateTTGO1Relais1Output);
    }
  }
  else if (String(topic) == "TTGO1/Relais2/Input") {
    Serial.print("Changing output to ");
    if(messageTemp == "true"){
      Serial.println("true");
      digitalWrite(PinRelais2, HIGH);
      Serial.println("Relais2Meldung an Broker: TRUE");
      stateTTGO1Relais2Output = "true";
      mqtt.publish(topicTTGO1Relais2Output, stateTTGO1Relais2Output);
    }
    else if(messageTemp == "false"){
      Serial.println("false");
      digitalWrite(PinRelais2, LOW);
      Serial.println("Relais2Meldung an Broker: FALSE");
      stateTTGO1Relais2Output = "false";
      mqtt.publish(topicTTGO1Relais2Output, stateTTGO1Relais2Output);
    }
  }
  else if (String(topic) == "TTGO1/Relais3/Input") {
    Serial.print("Changing output to ");
    if(messageTemp == "true"){
      Serial.println("true");
      digitalWrite(PinRelais3, HIGH);
      Serial.println("Relais3Meldung an Broker: TRUE");
      stateTTGO1Relais3Output = "true";
      mqtt.publish(topicTTGO1Relais3Output, stateTTGO1Relais3Output);
    }
    else if(messageTemp == "false"){
      Serial.println("false");
      digitalWrite(PinRelais3, LOW);
      Serial.println("Relais3Meldung an Broker: FALSE");
      stateTTGO1Relais3Output = "false";
      mqtt.publish(topicTTGO1Relais3Output, stateTTGO1Relais3Output);
    }
  }
  else if (String(topic) == "TTGO1/Relais4/Input") {
    Serial.print("Changing output to ");
    if(messageTemp == "true"){
      Serial.println("true");
      digitalWrite(PinRelais4, HIGH);
      Serial.println("Relais4Meldung an Broker: TRUE");
      stateTTGO1Relais4Output = "true";
      mqtt.publish(topicTTGO1Relais4Output, stateTTGO1Relais4Output);
    }
    else if(messageTemp == "false"){
      Serial.println("false");
      digitalWrite(PinRelais4, LOW);
      Serial.println("Relais4Meldung an Broker: FALSE");
      stateTTGO1Relais4Output = "false";
      mqtt.publish(topicTTGO1Relais4Output, stateTTGO1Relais4Output);
    }
  }
  else if (String(topic) == "TTGO1/Relais5/Input") {
    Serial.print("Changing output to ");
    if(messageTemp == "true"){
      Serial.println("true");
      digitalWrite(PinRelais5, HIGH);
      Serial.println("Relais5Meldung an Broker: TRUE");
      stateTTGO1Relais5Output = "true";
      mqtt.publish(topicTTGO1Relais5Output, stateTTGO1Relais5Output);
    }
    else if(messageTemp == "false"){
      Serial.println("false");
      digitalWrite(PinRelais5, LOW);
      Serial.println("Relais5Meldung an Broker: FALSE");
      stateTTGO1Relais5Output = "false";
      mqtt.publish(topicTTGO1Relais5Output, stateTTGO1Relais5Output);
    }
  }
  else if (String(topic) == "TTGO1/InterneLed/Input") {
    Serial.print("Changing output to ");
    if(messageTemp == "true"){
      Serial.println("true");
      digitalWrite(PinInterneLed, LOW);
      Serial.println("InterneLed Meldung an Broker: TRUE");
      stateTTGO1InterneLedOutput = "true";
      mqtt.publish(topicTTGO1InterneLedOutput, stateTTGO1InterneLedOutput);
    }
    else if(messageTemp == "false"){
      Serial.println("false");
      digitalWrite(PinInterneLed, HIGH);
      Serial.println("InterneLed Meldung an Broker: FALSE");
      stateTTGO1InterneLedOutput = "false";
      mqtt.publish(topicTTGO1InterneLedOutput, stateTTGO1InterneLedOutput);
    }
  }
  else if (String(topic) == "TTGO1/Servo1/Input") {
    Serial.print("Changing Servo to ");
    Serial.println(messageTemp);
    for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
          pwm.setPWM(1, 0, pulselen);
        }
      
        delay(500);
        for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
          pwm.setPWM(1, 0, pulselen);
        }

  }
  else if (String(topic) == "TTGO1/DcMotor1/Input") {
    Serial.print("Running Dc Motor");
    Serial.println(messageTemp);
    digitalWrite(PinDcMotor1Pin1, HIGH);
    digitalWrite(PinDcMotor1Pin2, LOW);
    delay(2000);
    digitalWrite(PinDcMotor1Pin1, LOW);
    digitalWrite(PinDcMotor1Pin2, LOW);
  }
}

boolean mqttConnect() {
  SerialMon.print("Connecting to ");
  SerialMon.print(broker);

  // Connect to MQTT Broker without username and password
  //boolean status = mqtt.connect("GsmClientN");

  // Or, if you want to authenticate MQTT:
  boolean status = mqtt.connect("GsmClientN", mqttUsername, mqttPassword);

  if (status == false) {
    SerialMon.println(" fail");
    ESP.restart();
    return false;
  }
  SerialMon.println(" success");
  subscribeAllTopics;
  return mqtt.connected();
}

void subscribeAllTopics()
{
  mqtt.subscribe(topicTTGO1Relais1Input, 1);  //..., 1 = QOS 1
  mqtt.subscribe(topicTTGO1Relais1Output, 1);
  mqtt.subscribe(topicTTGO1Relais2Input, 1);
  mqtt.subscribe(topicTTGO1Relais2Output, 1);
  mqtt.subscribe(topicTTGO1Relais3Input, 1);
  mqtt.subscribe(topicTTGO1Relais3Output, 1);
  mqtt.subscribe(topicTTGO1Relais4Input, 1);
  mqtt.subscribe(topicTTGO1Relais4Output, 1);
  mqtt.subscribe(topicTTGO1Relais5Input, 1);
  mqtt.subscribe(topicTTGO1Relais5Output, 1);
  mqtt.subscribe(topicTTGO1InterneLedInput, 1);
  mqtt.subscribe(topicTTGO1InterneLedOutput, 1);
  mqtt.subscribe(topicTTGO1Servo1Input, 1);
  mqtt.subscribe(topicTTGO1DcMotor1Input, 1);
}

void sendAllStates()
{
  mqtt.publish(topicTTGO1Relais1Output, stateTTGO1Relais1Output);
  mqtt.publish(topicTTGO1Relais2Output, stateTTGO1Relais2Output);
  mqtt.publish(topicTTGO1Relais3Output, stateTTGO1Relais3Output);
  mqtt.publish(topicTTGO1Relais4Output, stateTTGO1Relais4Output);
  mqtt.publish(topicTTGO1Relais5Output, stateTTGO1Relais5Output);
  mqtt.publish(topicTTGO1InterneLedOutput, stateTTGO1InterneLedOutput);
}

// Start GPS
void enableGPS(void)
{
    // Set SIM7000G GPIO4 LOW ,turn on GPS power
    // CMD:AT+SGPIO=0,4,1,1
    // Only in version 20200415 is there a function to control GPS power
    Serial.println("Turning on GPS power at: enableGPS()");
    modem.sendAT("+SGPIO=0,4,1,1");
    delay(1000);
    modem.enableGPS();
    // modem.sendAT("+CGNSTST=1"); 
}

// Servo-Board - Test
void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= SERVO_FREQ;   // Analog servos run at ~60 Hz updates
  Serial.print(pulselength); Serial.println(" us per period"); 
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit"); 
  pulse *= 1000000;  // convert input seconds to us
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

// Convert GPS-Coordinates from dd.ddddd to DDMM.MMMM for H02 Protocol like Gpsvision TK106-GPS-Tracker
String LatConvertDoubleToSpecial(double latcoord){
        int latdeg = (int)latcoord;
        double latmin = (latcoord - (int)latcoord)*60.0;
        String lattmp = "";
        lattmp = latdeg;
        lattmp = lattmp + String(latmin, 4);
        lattmp = String(lattmp);
        latnmea = lattmp;
        // Serial.println(latnmea);
        return(latnmea);
        }

String LonConvertDoubleToSpecial(double loncoord){
        int londeg = (int)loncoord;
        double lonmin = (loncoord - (int)loncoord)*60.0;
        String lontmp = "";
        lontmp = londeg;
        lontmp = lontmp + String(lonmin, 4);
        lontmp = String(lontmp);
        lonnmea = lontmp;
        // Serial.println(lonnmea);
        return(lonnmea);
        }

void setup() {
  SerialMon.begin(115200);
  delay(10);

  // Start I2C communication
  I2CPower.begin(I2C_SDA, I2C_SCL, 400000);
  I2CBME.begin(I2C_SDA_2, I2C_SCL_2, 400000);
  
  // Keep power when running from battery, Not working!
  bool isOk = setPowerBoostKeepOn(1);
  SerialMon.println(String("Keep Power when running from battery: IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));

  // Servo-Board - Test
  pwm.begin();
  pwm.setOscillatorFrequency(27000000);
  pwm.setPWMFreq(SERVO_FREQ);  // Analog servos run at ~50 Hz updates
  delay(10);

  // Set modem reset, enable, power pins
  pinMode(MODEM_PWKEY, OUTPUT);
  pinMode(MODEM_RST, OUTPUT);
  pinMode(MODEM_POWER_ON, OUTPUT);
  digitalWrite(MODEM_PWKEY, LOW);
  // digitalWrite(MODEM_RST, HIGH);
  // digitalWrite(MODEM_POWER_ON, HIGH);
  delay(1000);
  digitalWrite(MODEM_PWKEY, HIGH);

  // PinMode for Relays, Motors, Lightsensor
  pinMode(PinRelais1, OUTPUT);
  pinMode(PinRelais2, OUTPUT);
  pinMode(PinRelais3, OUTPUT);
  pinMode(PinRelais4, OUTPUT);
  pinMode(PinRelais5, OUTPUT);
  pinMode(PinInterneLed, OUTPUT);
  pinMode(PinPhotowiderstand, INPUT);
  pinMode(PinDcMotor1Pin1, OUTPUT);
  pinMode(PinDcMotor1Pin2, OUTPUT);

  SerialMon.println("Wait...");

  // Set GSM module baud rate and UART pins
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  delay(6000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  // modem.restart();
  modem.init();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem Info: ");
  SerialMon.println(modemInfo);

  // Unlock your SIM card with a PIN if needed
  if ( GSM_PIN && modem.getSimStatus() != 3 ) {
    modem.simUnlock(GSM_PIN);
  }

  
  // You might need to change the BME280 I2C address, in our case it's 0x76
  if (!bme.begin(0x76, &I2CBME)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }  

  SerialMon.print("Connecting to APN: ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    SerialMon.println(" fail");
    ESP.restart();
  }
  else {
    SerialMon.println(" OK");
  }
  
  if (modem.isGprsConnected()) {
    SerialMon.println("GPRS connected");
  }
  else {
    SerialMon.println("No GPRS");
  }
  
  // MQTT Broker setup
  mqtt.setServer(broker, 1883);
  mqtt.setCallback(mqttCallback);

  // GPS-Receiver Start and 20 sec. Warm-Up
  enableGPS();
  delay(2000L);
}

void loop() {
  
  if (!mqtt.connected()) {
    SerialMon.println("=== MQTT NOT CONNECTED ===");
    // Reconnect every 10 seconds
    uint32_t t = millis();
    if (t - lastReconnectAttempt > 10000L) {
      lastReconnectAttempt = t;
      if (mqttConnect()) {
        lastReconnectAttempt = 0;
      }
    }
    delay(100);
    return;
  }

  // Sensors: Temperature, Humidity, Light
  // every 30 seconds
  long now = millis();
  if (now - lastMsg > 30000) {
    lastMsg = now;

    // Temperature in Celsius
    temperature = bme.readTemperature();   
    // Uncomment the next line to set temperature in Fahrenheit 
    // (and comment the previous temperature line)
    //temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
    
    // Convert the value to a char array
    char tempString[8];
    dtostrf(temperature, 1, 2, tempString);
    Serial.print("Temperature: ");
    Serial.println(tempString);
    mqtt.publish(topicTTGO1Temperature, tempString);

    humidity = bme.readHumidity();    
    // Convert the value to a char array
    char humString[8];
    dtostrf(humidity, 1, 2, humString);
    Serial.print("Humidity: ");
    Serial.println(humString);
    mqtt.publish(topicTTGO1Humidity, humString);

    int valPhotowiderstand = analogRead(PinPhotowiderstand);
    // Convert the value to a char array
    char lightString[8];
    dtostrf(valPhotowiderstand, 1, 2, lightString);
    Serial.print("Light: ");
    Serial.println(lightString);
    mqtt.publish(topicTTGO1Light, lightString);

    // States aller Relais
    sendAllStates();  // Send all States in case of Broker-Restart, Loss of GPRS ...         
  }

  // GPS-Position to MQTT-Broker and 2nd Server
  // every 30 seconds
  if (now - lastGps > 30000) {
    lastGps = now;

    if (modem.getGPS(&lat2, &lon2, &speed2, &alt2, &vsat2, &usat2, &accuracy2,
                     &year2, &month2, &day2, &hour2, &min2, &sec2)) {
      // Serial.printf("modem.getGPS: lat:%f lon:%f\n", lat2, lon2);
      // Serial.printf("modem.getGPS: lat:%f lon:%f vsat:%f usat:%f year:%f hour:%f minute:%f\n", lat2, lon2, vsat2, usat2, year2, hour2, min2);

    /*
    // GPS über AT auslesen
     SerialAT.println("AT+CGNSSINF"); // sending "AT" command 
      while(!(SerialAT.available() > 0));
      Serial.print("AT+CGNSSINF: "); Serial.println(SerialAT.read());
    */

    // Test andere Zeitvariable
    const char *timestamp = modem.getGSMDateTime(DATE_FULL).c_str();
    // Serial.print("Timestamp GSM: "); Serial.println(timestamp);

    //Datum und Uhrzeit aus GSM-Modul holen
    float lat      = 0;
    float lon      = 0;
    float accuracy = 0;
    int   year     = 0;
    int   month    = 0;
    int   day      = 0;
    int   hour     = 0;
    int   min      = 0;
    int   sec      = 0;

    String strMonth;
    String strDay;
    String strHour;
    String strMin;
    String strSec;


   /*
    * Fallback für GPS, aber noch prüfen
    for (int8_t i = 15; i; i--) {
    //Serial.println("Requesting current GSM location");
      if (modem.getGsmLocation(&lat, &lon, &accuracy, &year, &month, &day, &hour,
                             &min, &sec)) {
      //Serial.print("Latitude:");
      //Serial.println(String(lat, 8));
      //Serial.print("\tLongitude:");
      //Serial.println(String(lon, 8));
      //Serial.print("Year:");
      //Serial.println(year);
      //Serial.print("\tMonth:");
      //Serial.println(month);
      //Serial.print("\tDay:");
      //Serial.println(day);
      //Serial.print("Hour:");
      //Serial.println(hour);
      //Serial.print("\tMinute:");
      //Serial.println(min);
      //Serial.print("\tSecond:");
      //Serial.println(sec);
      strMonth = String(month);
      if (strMonth.length()==1) {strMonth = String("0" + strMonth);}
      strDay = String(day);
      if (strDay.length()==1) {strDay = String("0" + strDay);}
      strHour = String(hour);
      if (strHour.length()==1){strHour = String("0" + strHour);}
      strMin = String(min);
      if (strMin.length()==1) {strMin = String("0" + strMin);}
      strSec = String(sec);
      if (strSec.length()==1) {strSec = String("0" + strSec);}
      break;
    } else {
      Serial.println("Couldn't get GSM location, retrying in 15s.");
      delay(15000L);
    }
   }
  */
    
      // Koordinaten umrechnen
      String testtmp = LatConvertDoubleToSpecial(lat2) + LonConvertDoubleToSpecial(lon2);
      // Serial.printf("test %f\n", LatConvertDoubleToSpecial(lat2), LonConvertDoubleToSpecial(lon2));
      
      // String für Umsetzer zusammensetzen
      String strUmsetzer = "*HQ,0123456789,V1," + strHour + strMin + strSec + ",V," + latnmea + ",N," 
                            + lonnmea + ",E,010.00,000,"
                            + strDay + strMonth + String(year).substring(2) + ",FFFFFBFF,262,01,ff43,0,4#";
      Serial.println(strUmsetzer);
      
      char tempStringLat[7];
      dtostrf(lat2, 1, 5, tempStringLat);
      // Serial.print("Lat fuer Uebergabe: ");
      // Serial.println(tempStringLat);
      mqtt.publish(topicTTGO1Latitude, tempStringLat);
      char tempStringLong[7];
      dtostrf(lon2, 1, 5, tempStringLong);
      // Serial.print("Long fuer Uebergabe: ");
      // Serial.println(tempStringLong);
      mqtt.publish(topicTTGO1Longitude, tempStringLong);

      /*      
      // GPS Server Umsetzer Test
      Serial.print("Connecting to ");
      Serial.println(serverGPS);
      if (!clientGPS.connect(serverGPS, portGPS)) {
        Serial.println(" fail");
        delay(10000);
        return;
      }
      Serial.println(" success");

      clientGPS.print(strUmsetzer);

      Serial.print(strUmsetzer);
      Serial.println(" :geschickt an GPS umsetzer");


      //clientGPS.stop();
      // GPS Umsetzer --- Ende      
      */
      }
    }
  
  mqtt.loop();
}

Michael

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.