#include <WiFi.h>
#include <PubSubClient.h>
#include <TinyGPS++.h>
const char* ssid = "HAC-kathon";
const char* password = "H@C2k25.";
const char* mqtt_server = "mqtt.eclipseprojects.io";
const char* thingsboard_server = "mqtt.thingsboard.cloud";
const char* tb_token = "n3v7mblr71fhds8763l1";
WiFiClient espClient;
PubSubClient mqttClient(espClient);
PubSubClient tbClient(espClient);
TinyGPSPlus gps;
#define SOUND_SENSOR_PIN 34
#define MQTT_TOPIC "wokwi/infrasound"
void callback(char* topic, byte* payload, unsigned int length) {
char msg[20];
strncpy(msg, (char*)payload, length);
msg[length] = '\0';
float soundFrequency = atof(msg);
// Simulated GPS data (Replace with actual GPS readings)
float latitude = 10.765 + random(0, 10) / 1000.0;
float longitude = 76.985 + random(0, 10) / 1000.0;
// Publish the detected infrasound to ThingsBoard
char tb_payload[200];
sprintf(tb_payload, "{\"sound_frequency\": %.2f, \"latitude\": %.6f, \"longitude\": %.6f}",
soundFrequency, latitude, longitude);
tbClient.publish("v1/devices/me/telemetry", tb_payload);
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setCallback(callback);
mqttClient.subscribe(MQTT_TOPIC);
tbClient.setServer(thingsboard_server, 1883);
while (!tbClient.connected()) {
tbClient.connect("ESP32", tb_token, NULL);
}
}
void loop() {
mqttClient.loop();
tbClient.loop();
delay(1000);
}