// DS18B20 Temperature Sensor using ESP32
/*
i. -55 to 125
ii Uses 1 Wire Communication (1 data line (must be pulled HIGH) and 1 gnd wire)
iii. user configurable resolution between 9 to 12-bits
iv. 64-bit Serial Code whihc is unique to each sensor
GND, DQ (must be pulled HIGH), VDD
*/
#include <DallasTemperature.h>
#include <OneWire.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 1
const char* apiUrl1 = "https://653005906c756603295e1f51.mockapi.io/Usuarios";
int sensorPin = 16;
OneWire oneWire (sensorPin); // creates the OneWire object using a specific pin
DallasTemperature sensor (&oneWire);
void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
sensor.begin ();
}
void loop() {
// put your main code here, to run repeatedly:
sensor.requestTemperatures ();
float temp = sensor.getTempCByIndex (0);
Serial.print ("Temperature: ");
Serial.print (temp);
Serial.println ("ºC");
delay (3000);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.begin(115200);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiUrl1);
http.addHeader("Content-Type", "application/json");
// Crear un objeto JSON
StaticJsonDocument<200> jsonDocument;
jsonDocument["Temperatura corporal"] = temp;
// Convertir el objeto JSON a una cadena
String jsonData;
serializeJson(jsonDocument, jsonData);
int httpResponseCode = http.POST(jsonData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error al enviar datos: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(5000); // Esperar 10 segundos antes de la próxima solicitud
}