Olá tudo bem? Somos estudantes do SENAI e estamos fazendo um nano satélite de TCC. Nós precisamos de ajuda para fazer o histórico de GPS na Wegnology.
#include <Adafruit_BMP085.h>
#include <TinyGPSPlus.h>
#include <WiFi.h>
#include <AsyncMqttClient.h>
#define WIFI_SSID "Leo"
#define WIFI_PASSWORD "trufas3geladinho"
#define MQTT_HOST "broker.app.wnology.io"
#define MQTT_PORT 1883
#define MQTT_PUB_TOPIC "wnology/652fe11fd6aaf7ef561xxxxxx/state"
char json[5000]; // Aumentei o tamanho do buffer JSON para acomodar todas as informações
Adafruit_BMP085 bmp;
TinyGPSPlus gps;
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
unsigned long previousMillis = 0;
const long interval = 1000;
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0);
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttPublish(uint16_t packetId) {
Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
delay(3000);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
WiFi.onEvent(WiFiEvent);
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setClientId("652fe11fd6aaf7ef56xxxxxxx");
mqttClient.setCredentials("02c8d12e-fb65-4637-b9c0-17087xxxxxx", "68673631810057f75f2f2ea482a8b1dc05c52d0ae3924c214d22b996axxxxxx");
connectToWifi();
}
void loop() {
while (Serial2.available() > 0)
if (gps.encode(Serial2.read()))
displayInfo();
delay(2000);
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while (true);
}
Serial.print(gps.location.lat());
String cjson = "{\"data\": {\"Temperature\": " + String(bmp.readTemperature(), 2) +
", \"Altitude\": " + String(bmp.readAltitude()) +
", \"Pressure\": " + String(bmp.readPressure() / 101325.0) +
", \"Latitude\": " + String(gps.location.lat(),6) +
", \"Longitude\": " + String(gps.location.lng(),6) + "}}";
cjson.toCharArray(json, 5000); // Aumentei o tamanho do buffer JSON
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
mqttClient.publish(MQTT_PUB_TOPIC, 1, false, json);
}
}
void displayInfo()
{
Serial.print(F("Location: "));
if (gps.location.isValid()){
Serial.print("Lat: ");
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print("Lng: ");
Serial.print(gps.location.lng(), 6);
Serial.println();
}
else
{
Serial.print(F("INVALID"));
}
}
Este é o nosso código, nós queremos que apareça a localização do satélite no dashboard.
esse é o nosso dashboard, o atributo GPS já esta criado mas não sabemos configura-lo.