İçeriğe geç

hLED ESP8266 ile 2 Mosfet’li LED Kontrol Sistemi

Arduino Kodu

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <FS.h>                   //this needs to be first, or it all crashes and burns...

const int led1Pin = D1; //2 
const int led2Pin = D2; //2 
const int max_duty = 255; //1023
int dly = 10;
bool anim = false;
int id = 1;
String cid; 

float gonderimsikligi;
int isik;
int power;

const char* ssid = "WIFI SSID";
const char* password = "WIFI PAROLA";

//flag for saving data
bool shouldSaveConfig = false;


String sliderValue1 = "125";
String sliderValue2 = "125";

const char* PARAM_INPUT = "value";
const char* PARAM_ID = "id";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

WiFiClient espClient;

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>hOLOlu LED</title>
  <style>
    html {font-family: Arial; display: inline-block; text-align: center;}
  .topnav {overflow: hidden; background-color: #0A1128; }
    h1 {font-size: 1.8rem;color: white;}
    h2 {font-size: 2.3rem;}
    p {font-size: 1.9rem;}
    body {margin:0px;}
    .slider { -webkit-appearance: none; margin: 14px; width: 360px; height: 25px; background: #FFD65C;
      outline: none; -webkit-transition: .5s; transition: opacity .2s;}
    .slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 40px; height: 40px; border-radius: 50%; background: #003249; cursor: pointer;}
    .slider::-moz-range-thumb { width: 35px; height: 35px; background: #003249; cursor: pointer; } 
    .button:{ margin: 5px; text-align: center; border: 1px; float: left; }
  </style>
</head>
<body>
    <div class="topnav"><h1>hOLOlu LED Kontrol</h1></div>
  <p><span id="textSliderValue1">%SLIDERVALUE1%</span></p>
  <p><input type="range" onchange="updateSliderPWM(this)" id="pwmSlider1" min="0" max="255" value="%SLIDERVALUE1%" step="1" class="slider"></p>

  <p><span id="textSliderValue2">%SLIDERVALUE2%</span></p>
  <p><input type="range" onchange="updateSliderPWM(this)" id="pwmSlider2" min="0" max="255" value="%SLIDERVALUE2%" step="1" class="slider"></p>
  <p><input type="button" onClick="updateAnim(this)" id="A1" value="&#9728; Anim 1">
  <input type="button" onClick="updateAnim(this)" id="A2" value="&#9728; Anim 2">
  <input type="button" onClick="updateAnim(this)" id="A3" value="&#9728; Anim 3">
  <input type="button" onClick="updateAnim(this)" id="A4" value="&#9728; Anim 4">
  <input type="button" onClick="updateAnim(this)" id="A5" value="&#9728; Anim 5"></p>
  <p><input type="button" onClick="updateAnim(this)" id="A0" value="Animasyonu Durdur"></p>
<script>
function updateSliderPWM(element) {
  var sliderValue1 = document.getElementById("pwmSlider1").value;
  var sliderValue2 = document.getElementById("pwmSlider2").value;
  if (element.id == "pwmSlider1") {
    var id = "1";
    document.getElementById("textSliderValue1").innerHTML = sliderValue1;
    sliderValue = sliderValue1;
  } else { 
    var id = "2";
    document.getElementById("textSliderValue2").innerHTML = sliderValue2; 
    sliderValue = sliderValue2;}

  console.log(element.id);
  console.log(sliderValue1);
  console.log(sliderValue2);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value="+sliderValue+"&id="+id, true);
  xhr.send();
}
function updateAnim(element) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/animasyon?value="+element.id, true);
  xhr.send();
} 
</script>
</body>
</html>
)rawliteral";

// Replaces placeholder with button section in your web page
String processor(const String& var){
  //Serial.println(var);
  if (var == "SLIDERVALUE1"){
    return sliderValue1;
  }
  if (var == "SLIDERVALUE2"){
    return sliderValue2;
  }
  return String();
}

//callback notifying us of the need to save config
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}


void setup() {
  Serial.begin(115200);
//  WiFiManager wifiManager;
//  wifiManager.autoConnect("hLEDAP");
  analogWrite(led1Pin, sliderValue1.toInt());
  analogWrite(led2Pin, sliderValue2.toInt());
    
    //write_to_file("ayarlar.json", "data to save" );

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });

  // Send a GET request to <ESP_IP>/slider?value=<inputMessage>
  server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    int gid;
    
    //String sensorNumber = request->pathArg(0);

    //if (request->pathArg(0)) {
      inputMessage = request->getParam("id")->value(); // request->pathArg(1);
      gid = inputMessage.toInt();
      inputMessage = request->getParam("value")->value(); //request->pathArg(0);
      
      if (gid == 1) {
           sliderValue1 = inputMessage;
           analogWrite(led1Pin, sliderValue1.toInt()); } 
           else
           { 
            sliderValue2 = inputMessage;
            analogWrite(led2Pin, sliderValue2.toInt());}
    // }
    // else {
    //   inputMessage = "No message sent";
    // }
    Serial.println(inputMessage);
    request->send(200, "text/plain", "OK");
  });
  
    // Route for root / web page
  server.on("/animasyon", HTTP_GET, [](AsyncWebServerRequest *request){
    String inputMessage;
    // GET input1 value on <ESP_IP>/slider?value=<inputMessage>
    if (request->hasParam(PARAM_INPUT)) {
      inputMessage = request->getParam(PARAM_INPUT)->value();
      if (inputMessage =="A1") { anim=true; id=1;}
      if (inputMessage =="A2") { anim=true; id=2;}
      if (inputMessage =="A3") { anim=true; id=3;}
      if (inputMessage =="A4") { anim=true; id=4;} 
      if (inputMessage =="A5") { anim=true; id=5;}      
      if (inputMessage =="A0") { anim=false; id=0;}
    }
    else {
      inputMessage = "No message sent";
    }
    Serial.println(inputMessage);
    request->send(200, "text/plain", "OK");
  });

  AsyncElegantOTA.begin(&server,"hololu","!q2w3e4r");    // Start AsyncElegantOTA

  // Start server
  server.begin();

  cid = "hLED-";
  cid += String(ESP.getChipId(),HEX);


  //client.setServer("mqtt.mustafaozkaya.com.tr", 8883);
  //client.setCallback(callback);

  anim=true;
  id=1;

}

void loop() {
/*
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
*/

 if (anim)
 {
  if (id==1){ 

    for(int dutyCycle = 0; dutyCycle < max_duty; dutyCycle++){   
      analogWrite(led1Pin, dutyCycle);
      analogWrite(led2Pin, dutyCycle);
      delay(dly);
    }

    for(int dutyCycle = max_duty; dutyCycle > 0; dutyCycle--){
      analogWrite(led1Pin, dutyCycle);
      analogWrite(led2Pin, dutyCycle);
      delay(dly);
    }
  }

  if (id==2){ 
    
    int d = max_duty;
    for(int dutyCycle = 0; dutyCycle < max_duty; dutyCycle++){   
      d = d - 1;
      analogWrite(led1Pin, dutyCycle);
      analogWrite(led2Pin,d);
      delay(dly);
    }

    d = 0;
    for(int dutyCycle = max_duty; dutyCycle > 0; dutyCycle--){
      d = d + 1;
      analogWrite(led1Pin, dutyCycle);
      analogWrite(led2Pin, d);
      delay(dly);
    }
  }

  if (id==3){ 
      analogWrite(led1Pin, 0);
      analogWrite(led2Pin, 0);
      delay(1000);
      analogWrite(led1Pin, max_duty);
      analogWrite(led2Pin, max_duty);
      delay(1000);
    }

  if (id==4){ 
      analogWrite(led1Pin, 0);
      analogWrite(led2Pin, max_duty);
      delay(1000);
      analogWrite(led1Pin, max_duty);
      analogWrite(led2Pin, 0);
      delay(1000);
    }


  if (id==5){ 
      analogWrite(led1Pin, 0);
      analogWrite(led2Pin, max_duty);
      delay(30);
      analogWrite(led1Pin, max_duty);
      analogWrite(led2Pin, 0);
      delay(30);
      analogWrite(led1Pin, 0);
      analogWrite(led2Pin, max_duty);
      delay(30);
      analogWrite(led1Pin, max_duty);
      analogWrite(led2Pin, 0);
      delay(30);
      analogWrite(led1Pin, 0);
      analogWrite(led2Pin, max_duty);
      delay(30);
      analogWrite(led1Pin, max_duty);
      analogWrite(led2Pin, 0);
      delay(30);
      analogWrite(led1Pin, max_duty);
      analogWrite(led2Pin, max_duty);
      delay(2000);
    for(int dutyCycle = max_duty; dutyCycle > 0; dutyCycle--){
      analogWrite(led1Pin, dutyCycle);
      analogWrite(led2Pin, dutyCycle);
      delay(dly);
    }
  }

 }

}
Kategori:ArduinoESP8266

İlk Yorumu Siz Yapın

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir