#include <WiFi.h>
#include <HTTPClient.h>
#include <NewPing.h> // Include the NewPing library
#define WIFI_SSID "wokwi-GUEST"
#define WIFI_PASSWORD ""
#define TRIGGER_PIN 5 // Pin connected to the trigger of the ultrasonic sensor
#define ECHO_PIN 18 // Pin connected to the echo of the ultrasonic sensor
#define MAX_DISTANCE 200 // Maximum distance we want to measure (in cm)
float Temperature = 25.5;
float Humidity = 67;
int value = 100;
const char *serverUrl = "https://console.thingzmate.com/api/v1/device-types/kommakahome2/devices/thingzkit2/uplink"; // Replace with your server endpoint
String AuthorizationToken = "Bearer de49c0ef7f6e04ccb2a7ee4ce36ec719";
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Create a NewPing object
void setup() {
Serial.begin(115200);
delay(4000); // Delay to let serial settle
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", AuthorizationToken); // Authorization token
// Read distance from the ultrasonic sensor
unsigned int distance = sonar.ping_cm();
// Create JSON payload
String payload = "{\"temperature\":" + String(Temperature) +
",\"humidity\":" + String(Humidity) +
",\"Raw_Value\":" + String(value) +
",\"distance\":" + String(distance) + "}";
// Send POST request
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 code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
delay(10000); // Wait for 10 seconds before sending next request
}