#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "http://nanai.tech/post-esp-data.php";
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "BME280";
String sensorLocation = "Office";
#define LED 2
int pinPulsador=4;
int sensor=19;
//Declaramos el pin al que estará conectado el pulsador
// Declaramos el pin al que estará conectado el led
int pinLed=2;
int buzzer = 5;
void setup() {
Serial.begin(115200);
pinMode(buzzer, OUTPUT);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
//aca empieza el codigo antiguo del sensor
pinMode(LED, OUTPUT);
Serial.begin(115200);
Serial.println("Welcome to the Serial Playground!");
Serial.println("---------------------------------");
}
void loop() {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
if (digitalRead(pinPulsador) == LOW) {
digitalWrite(LED, LOW);
}
if (digitalRead(sensor) == HIGH) {
digitalWrite(LED, HIGH);
Serial.println("se detecto movimiento");
delay(200);
//nuevo codigo movi
digitalWrite(pinLed, HIGH);
tone(buzzer,3000);
delay(500);
noTone(buzzer);
delay(500);
String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=54.75&value2=19.54&value3=1005.14";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
delay(1000);
Serial.println("HTTP Response code: ");
Serial.println(httpResponseCode);
delay(100);
tone(buzzer,1000);
delay(200);
noTone(buzzer);
delay(1000);
Serial.println("done!");
} else {
Serial.println("no se detecto movimiento");
delay(2000);
}
if (digitalRead(pinPulsador) == HIGH) {
digitalWrite(pinLed, HIGH);
tone(buzzer,3000);
delay(500);
noTone(buzzer);
delay(500);
String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=54.75&value2=19.54&value3=1005.14";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
delay(1000);
Serial.println("HTTP Response code: ");
Serial.println(httpResponseCode);
delay(100);
tone(buzzer,1000);
delay(200);
noTone(buzzer);
delay(1000);
Serial.println("done!");
}
//de lo contrario apagamos el led
else {
digitalWrite(pinLed, LOW);
}
}
else {
Serial.println("WiFi Disconnected");
}
//Si la señal del pulsador es activa encendemos el led
delay(10);
}