#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#define IR_SENSOR_PIN D1
const char* ssid = "IOT_LAB";
const char* password = "Iot_Lab@367";
const char* ditto_host = "http://your.ditto.host";
const char* ditto_thing_id = "com.mspvlceit:lm35-sensor1";
const char* ditto_username = "ditto";
const char* ditto_password = "ditto";
void setup() {
Serial.begin(115200);
Serial.println("Setup complete");
}
void loop() {
Serial.println("Loop running");
delay(1000);
}
WiFiClient wifiClient;
HTTPClient http;
void connectToWiFi() {
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
float readTemperature() {
int adcVal = analogRead(PIN_LM35);
float milliVolt = adcVal * (ADC_VREF_mv / ADC_RESOLUTION);
return milliVolt / 10.0;
}
String createPayload(float temperature) {
char tempStr[8];
dtostrf(temperature, 6, 2, tempStr);
return String("{\"value\": ") + String(tempStr) + String("}");
}
void sendToDitto(String payload) {
if (WiFi.status() == WL_CONNECTED) {
String url = String(ditto_host) + "/api/2/things/" + String(ditto_thing_id) + "/feature/temperature/properties/value";
http.begin(wifiClient, url);
http.setAuthorization(ditto_username, ditto_password);
http.addHeader("Content-Type", "application/json");
int httpCode = http.PUT(payload);
if (httpCode > 0) {
Serial.printf("HTTP PUT status: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_NO_CONTENT) {
Serial.println("Temperature updated in Ditto");
} else {
Serial.println("Failed to update Ditto");
Serial.println(http.getString());
}
} else {
Serial.printf("HTTP PUT failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("WiFi disconnected");
}
}