ESp32 den veri tabanına bilgi gönderma

Başlatan bbs2006, 04 Kasım 2023, 23:51:40

bbs2006

Merhaba

esp32 bağlı ledim yandıgı zaman veri tabanında durum değişkenine 1 yazıp html sayfasında lambayı yakacak şekilde bir çalışmaya ihtiyacım var.   html sayfasına buton  bastıgım zaman veritabanına 1 yazıyor. ve esp32 led yanıyor. Bu led yandıgı anda veritabanındaki durum değişkenine 1 yazıp html syafsında lambayı yakacak şekilde istiyorum.



//Variables used in the code
String LED_id = "1";                          //Just in case you control more than 1 LED    
String data_to_send = "";                     //Text data to send to the server
unsigned int Actual_Millis, Previous_Millis;
int refresh_time = 200;                       //Refresh rate of connection to website (recommended more than 1s)


//Inputs/outputs
int LED = 4;                                  //LED connected on IO2
int Relay = 2;                               //Pin for SSR control is IO12

void setup() {
  delay(10);
  Serial.begin(115200);                       //Start monitor
  pinMode(LED, OUTPUT);                       //Set pin 2 as OUTPUT
  pinMode(Relay, OUTPUT);                     //Set pin 12 as OUTPUT  
  digitalWrite(LED, LOW);                     //Start with LED turned OFF
  digitalWrite(Relay, LOW);                   //Start with SSR turned OFF
  
  WiFi.begin(ssid, password);                 //Start wifi connection
  Serial.print("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {     //Check for the connection
    delay(500);
    Serial.print(".");
  }

  Serial.print("Connected, my IP: ");
  Serial.println(WiFi.localIP());
  Actual_Millis = millis();                   //Save time for refresh loop
  Previous_Millis = Actual_Millis; 
}


void loop() {  
  //We make the refresh loop using millis() so we don't have to use delay();
  Actual_Millis = millis();
  if(Actual_Millis - Previous_Millis > refresh_time){
    Previous_Millis = Actual_Millis;  
    if(WiFi.status()== WL_CONNECTED){                         //Check WiFi connection status  
      HTTPClient http;                                        //Create new client
      data_to_send = "check_LED_status=" + LED_id;            //Create the data to send
                 //Create the data to send
      
      /*Begin new connection to website. Make sure you change "https://electronoobs.com/" to
       *the name of your website. So the next line should be:
       *http.begin("https://yourwebsite.com/NOOBIX_V2/esp32_update.php");*/           
      http.begin("https://barisbilirsoylu.net.tr/update.php");          //Indicate the destination webpage 
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");        //Prepare the header
      
      int response_code = http.POST(data_to_send);                                //Send the POST. This will giveg us a response code
      
      //If the code is higher than 0, it means we received a response
      if(response_code > 0){
        
        Serial.print("HTTP Response code: ");
        Serial.println(response_code);
        Serial.println("HTTP code " + String(response_code));                     //Print return code
  
        if(response_code == 200){  
           //String response_body= "LED_is_on";                                              //If code is 200, we received a good response and we can read the echo data
          String response_body = http.getString();                                //Save the data comming from the website
          Serial.print("Server reply: ");                                         //Print data to the monitor for debug
          Serial.println(response_body);

          //If the received data is LED_is_off, we turn OFF the SSR and LED
          if(response_body == "LED_is_off"){
            digitalWrite(LED, LOW);
            digitalWrite(Relay, LOW);
          ;    
            
          }
          //If the received data is LED_is_on, we turn ON the SSR and LED
          else if(response_body == "LED_is_on"){
                       
            digitalWrite(LED, HIGH);
            digitalWrite(Relay, HIGH);
          
          }  
        }//End of response_code = 200
      }//END of response_code > 0
      
      else{
       Serial.print("Error sending POST, code: ");
       Serial.println(response_code);
      }
      http.end();                                                                 //End the connection
    }//END of WIFI connected
    else{
      Serial.println("WIFI connection error");
    }
  }
}


















sadece geri bildirim için nasılb ir kod yazmalyım.

erdcan

Hocam butona basıldığı durumlarda veri tabanındaki ilgili tablodaki Led durumu tutan sütünü sorgulamanız gerekir.

bbs2006

Me
Alıntı yapılan: erdcan - 05 Kasım 2023, 14:00:46Hocam butona basıldığı durumlarda veri tabanındaki ilgili tablodaki Led durumu tutan sütünü sorgulamanız gerekir.
[/quot
Butona basıldıgı zaman veri tabında  status değişkeni  led yanarsa 1 yanmazasa 0 oluyor ve esp32 deki led yakıyor. Bende veri tabanına durum değişkeni ekledim. Led yandıgı zaman veri tabanındaki durumda değişkeni 1 olacak ve  html sayfasında lamba yanacak. ben bu ikinci işlemi