Added timestamp to debugMqtt
This commit is contained in:
+5
-3
@@ -53,13 +53,15 @@
|
|||||||
#define MAX_SPEED 1.0
|
#define MAX_SPEED 1.0
|
||||||
#define MAX_ROTATION 1.0
|
#define MAX_ROTATION 1.0
|
||||||
|
|
||||||
//Wlan / Mqtt config
|
//Network config
|
||||||
#define WLAN_SSID "Kleiax2"
|
#define WLAN_SSID "Kleiax2"
|
||||||
#define WLAN_PASSWORD "Punica-699"
|
#define WLAN_PASSWORD "Punica-699"
|
||||||
#define WLAN_IP 0x0101a8c0 //192.168.1.1
|
#define WLAN_IP 0x0101a8c0 //192.168.1.1
|
||||||
#define WLAN_SUBNETMASK 0x0000FFFF //255.255.0.0
|
#define WLAN_SUBNETMASK 0x0000FFFF //255.255.0.0
|
||||||
#define WLAN_GATEWAY 0x0100a8c0 //192.168.0.1
|
#define WLAN_GATEWAY 0x0100a8c0 //192.168.0.1
|
||||||
#define MQTT_SERVER 0x1300a8c0 //192.168.0.19
|
// #define MQTT_SERVER 0x1300a8c0 //192.168.0.19
|
||||||
|
#define MQTT_SERVER 0x0201a8c0 //192.168.1.2
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
#define MQTT_BUFFER_SITE 50
|
#define MQTT_BUFFER_SITE 128
|
||||||
|
#define NTP_SERVER 0x0201a8c0 //192.168.1.2
|
||||||
|
|
||||||
|
|||||||
+36
-2
@@ -5,6 +5,9 @@ Loglevel DebugMqtt::loglevel;
|
|||||||
bool DebugMqtt::isInit = false;
|
bool DebugMqtt::isInit = false;
|
||||||
char DebugMqtt::msg[MQTT_BUFFER_SITE];
|
char DebugMqtt::msg[MQTT_BUFFER_SITE];
|
||||||
char DebugMqtt::topic[MQTT_BUFFER_SITE];
|
char DebugMqtt::topic[MQTT_BUFFER_SITE];
|
||||||
|
unsigned long long int DebugMqtt::real_millis = 0;
|
||||||
|
unsigned long int DebugMqtt::last_millis = 0;
|
||||||
|
|
||||||
|
|
||||||
DebugMqtt::DebugMqtt(const char* name) {
|
DebugMqtt::DebugMqtt(const char* name) {
|
||||||
this->name = name;
|
this->name = name;
|
||||||
@@ -35,11 +38,21 @@ void DebugMqtt::sendData(Loglevel loglevel, String data){
|
|||||||
|
|
||||||
void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement) {
|
void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement) {
|
||||||
// Example String: "weather temperature=82 1465839830100400200";
|
// Example String: "weather temperature=82 1465839830100400200";
|
||||||
unsigned long int nanos = millis() * 1000000;
|
|
||||||
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "%s %s=%f %lu", measurement_name.c_str(), field_set.c_str(), measurement, nanos);
|
unsigned long long int nanos = DebugMqtt::getUpdatedRealMillis() * 1000000;
|
||||||
|
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "%s %s=%f %llu", measurement_name.c_str(), field_set.c_str(), measurement, nanos);
|
||||||
this->sendData(Loglevel::debug, DebugMqtt::msg);
|
this->sendData(Loglevel::debug, DebugMqtt::msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DebugMqtt::sendTime() {
|
||||||
|
struct tm timeinfo;
|
||||||
|
if(!getLocalTime(&timeinfo)){
|
||||||
|
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "Failed to obtain time");
|
||||||
|
}
|
||||||
|
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "%d %d %d %d %d %d %llu", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, DebugMqtt::getUpdatedRealMillis());
|
||||||
|
this->sendMsg(Loglevel::info, DebugMqtt::msg);
|
||||||
|
}
|
||||||
|
|
||||||
void DebugMqtt::init(PubSubClient *client, Loglevel loglevel) {
|
void DebugMqtt::init(PubSubClient *client, Loglevel loglevel) {
|
||||||
DebugMqtt::client = client;
|
DebugMqtt::client = client;
|
||||||
DebugMqtt::loglevel = loglevel;
|
DebugMqtt::loglevel = loglevel;
|
||||||
@@ -50,6 +63,26 @@ void DebugMqtt::changeLoglevel(Loglevel loglevel) {
|
|||||||
DebugMqtt::loglevel = loglevel;
|
DebugMqtt::loglevel = loglevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DebugMqtt::initRealMillis() {
|
||||||
|
time_t now;
|
||||||
|
struct tm timeinfo;
|
||||||
|
if(!getLocalTime(&timeinfo)){
|
||||||
|
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "Failed to obtain time in initRealMillis()");
|
||||||
|
DebugMqtt::client->publish(DebugMqtt::enum_to_string(Loglevel::error).c_str(), DebugMqtt::msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
time(&now);
|
||||||
|
DebugMqtt::last_millis = millis();
|
||||||
|
DebugMqtt::real_millis = now;
|
||||||
|
DebugMqtt::real_millis = DebugMqtt::real_millis * 1000 + DebugMqtt::last_millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long int DebugMqtt::getUpdatedRealMillis() {
|
||||||
|
DebugMqtt::real_millis = DebugMqtt::real_millis + (millis() - DebugMqtt::last_millis);
|
||||||
|
DebugMqtt::last_millis = millis();
|
||||||
|
return DebugMqtt::real_millis;
|
||||||
|
}
|
||||||
|
|
||||||
String DebugMqtt::enum_to_string(Loglevel loglevel) {
|
String DebugMqtt::enum_to_string(Loglevel loglevel) {
|
||||||
switch(loglevel){
|
switch(loglevel){
|
||||||
case Loglevel::error :
|
case Loglevel::error :
|
||||||
@@ -64,3 +97,4 @@ String DebugMqtt::enum_to_string(Loglevel loglevel) {
|
|||||||
return "INVALID ENUM";
|
return "INVALID ENUM";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,12 @@ class DebugMqtt {
|
|||||||
void sendData(Loglevel loglevel, String data);
|
void sendData(Loglevel loglevel, String data);
|
||||||
void writeToInflux(String measurement_name, String field_set, float measurement);
|
void writeToInflux(String measurement_name, String field_set, float measurement);
|
||||||
|
|
||||||
|
void sendTime();
|
||||||
|
|
||||||
static void init(PubSubClient *client, Loglevel loglevel);
|
static void init(PubSubClient *client, Loglevel loglevel);
|
||||||
static void changeLoglevel(Loglevel loglevel);
|
static void changeLoglevel(Loglevel loglevel);
|
||||||
|
static void initRealMillis();
|
||||||
|
static unsigned long long int getUpdatedRealMillis();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* name;
|
const char* name;
|
||||||
@@ -34,6 +38,9 @@ class DebugMqtt {
|
|||||||
static char msg[MQTT_BUFFER_SITE];
|
static char msg[MQTT_BUFFER_SITE];
|
||||||
static char topic[MQTT_BUFFER_SITE];
|
static char topic[MQTT_BUFFER_SITE];
|
||||||
|
|
||||||
|
static unsigned long long int real_millis;
|
||||||
|
static unsigned long int last_millis;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DEBUG_MQTT_H
|
#endif // DEBUG_MQTT_H
|
||||||
+7
-2
@@ -2,6 +2,7 @@
|
|||||||
#include <Ps3Controller.h>
|
#include <Ps3Controller.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "motorControl.h"
|
#include "motorControl.h"
|
||||||
@@ -67,7 +68,10 @@ void setup() {
|
|||||||
mqtt_client.setServer(mqtt_server, MQTT_PORT);
|
mqtt_client.setServer(mqtt_server, MQTT_PORT);
|
||||||
reconnectMqtt();
|
reconnectMqtt();
|
||||||
|
|
||||||
|
configTime(3600, 3600, "192.168.1.2");
|
||||||
|
|
||||||
DebugMqtt::init(&mqtt_client, Loglevel::debug);
|
DebugMqtt::init(&mqtt_client, Loglevel::debug);
|
||||||
|
DebugMqtt::initRealMillis();
|
||||||
|
|
||||||
Ps3.attach(callbackControllerAction);
|
Ps3.attach(callbackControllerAction);
|
||||||
Ps3.attachOnConnect(callbackControllerConnect);
|
Ps3.attachOnConnect(callbackControllerConnect);
|
||||||
@@ -91,7 +95,7 @@ void loop() {
|
|||||||
mqtt_client.loop();
|
mqtt_client.loop();
|
||||||
|
|
||||||
if (millis() - last_millis > 1000) {
|
if (millis() - last_millis > 1000) {
|
||||||
debugger.sendMsg(Loglevel::info, "loop : eine Sekinde");
|
debugger.sendTime();
|
||||||
|
|
||||||
last_millis = millis();
|
last_millis = millis();
|
||||||
}
|
}
|
||||||
@@ -116,7 +120,7 @@ void reconnectMqtt() {
|
|||||||
if (mqtt_client.connect(clientId.c_str())) {
|
if (mqtt_client.connect(clientId.c_str())) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
// Once connected, publish an announcement...
|
// Once connected, publish an announcement...
|
||||||
mqtt_client.publish("/Rover/Info", "Connected");
|
mqtt_client.publish("Rover/Info", "Connected to Mqtt-Broker");
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
Serial.print(mqtt_client.state());
|
Serial.print(mqtt_client.state());
|
||||||
@@ -206,3 +210,4 @@ void getDis(double lat1, double lon1, double lat2, double lon2) {
|
|||||||
double dy = 111.3 * (lat1 - lat2);
|
double dy = 111.3 * (lat1 - lat2);
|
||||||
double erg = sqrt(dx * dx + dy * dy);
|
double erg = sqrt(dx * dx + dy * dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user