Pesquisar neste blog

02/12/2023

Inserindo dados na tablela com PHP + ESP32 #10

Saída gerada no phpMyAdmin4















<?php
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $database = "sensor_db";

    $conn = mysqli_connect($hostname, $username, $password, $database);

    if (!$conn) {
        die("Connection failed: ".mysqli_connect_error());
    }

    echo "Database connection is OK";
    echo ('<br>');

    $sql = "INSERT INTO dht11 (temperature, humidity) VALUES (24, 45)";

    if (mysqli_query($conn, $sql)) {
        # code...
        echo "\n";
        echo "\nNovo registro criado com sucesso !";
    }else {
        echo "\nErro: ". $sql . "<br>" .mysqli_error($conn);
    }
?>



Código em linguagem C para o ESP32:

//https://www.youtube.com/watch?v=VEN5kgjEuh8&ab_channel=AhmadLogs

#include <WiFi.h>
#include <HTTPClient.h>

String URL = "http://192.168.1.12/5 Esp32-Temperatura/test_data.php";

const char* ssid = "XXXX";
const char* password = "XXX";

int temperature = 50;
int humidity = 70;

void setup(){
  Serial.begin(115200);
  connectWiFi();
  
}

void connectWiFi(){
  WiFi.mode(WIFI_OFF);
  delay(1000);

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);
  Serial.println("Conectando no WiFi");

  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }

  Serial.print("Conectado em: "); Serial.println(ssid);
  Serial.print("IP address: "); Serial.println(WiFi.localIP());
  delay(1000);
  
}

void loop(){
  if(WiFi.status() != WL_CONNECTED){
    connectWiFi();
 }

  String postData = "Temperatura = "+String(temperature)+"&humidity="+ String(humidity);

  HTTPClient http;
  http.begin(URL);

  int httpCode = http.POST(postData);
  String payload = http.getString();
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  
  Serial.print("URL: ");Serial.print(URL);
  Serial.print("Data: ");Serial.print(postData);
  Serial.print("httpCode: ");Serial.print(httpCode);
  Serial.print("payload: ");Serial.print(payload);
  Serial.println("-------------------");
  delay(4000);
}

Nenhum comentário: