#include <WiFi.h>
#include <HTTPClient.h>
#define WIFI_AP "Wokwi-GUEST"
#define WIFI_PASS ""
const char* server = "83.166.237.170";
const int port = 9090;
const char* token = "9TIJVP7fEaAnUnw4vitv";
#define RXp2 16
#define TXp2 17
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
connectToWiFi();
}
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_AP, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
}
void sendDataToThingsBoard(float temperature, float pressure) {
HTTPClient http;
// Form the URL for sending data
String url = "http://" + String(server) + ":" + port + "/api/v1/" + String(token) + "/telemetry";
// Form JSON payload with random temperature and pressure values
String payload = "{\"temperature\":" + String(temperature) +
",\"pressure\":" + String(pressure) + "}";
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if(httpResponseCode > 0) {
String response = http.getString();
Serial.println("HTTP Response code: " + String(httpResponseCode));
Serial.println(response);
} else {
Serial.print("Error sending HTTP request: ");
Serial.println(httpResponseCode);
}
http.end();
}
void loop() {
// Generate random temperature and pressure values
float temperature = random(10, 30); // Random temperature between 10°C and 30°C
float pressure = random(900, 1100); // Random pressure between 900 hPa and 1100 hPa
// Print generated values for verification
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C, Pressure: ");
Serial.print(pressure);
Serial.println(" hPa");
// Send data to ThingsBoard
sendDataToThingsBoard(temperature, pressure);
delay(5000); // Send data every 5 seconds
}