Membuat Coding untuk CUBE AC Relay board dengan Arduino dan Cara Uploadnya

Seperti janji saya sebelumnya, saya akan mencontohkan cara membuat coding untuk CUBE AC Relay Boards dan menguploadnya melalui OTA update tanpa harus melepas ESP8266-01 dari Module AC relay.



Mari kita langsung lihat example code untuk CUBE AC Relay Board :


 /*  
  * CUBE AC Relay Board Example Program  
  * Created By: Cahyo hertanto  
  * Using WiFiManager Libray FromTzapu, and Arduino OTA  
  * Have Fun with CUBE-AC Relay Borad :)  
 */  
 #include <ESP8266WiFi.h>   
 #include <ESP8266WebServer.h>  
 #include <DNSServer.h>  
 #include <WiFiClient.h>  
 #include <WiFiManager.h>  
 #include <WiFiUdp.h>  
 #include <ArduinoOTA.h>  
 // Declaring Pin for Relay  
 int rly=0;  
 int sw=2;  
 // Add Webserver Functional Library  
 ESP8266WebServer server(80);  
 String rootPage="";  
 void setup() {  
  // put your setup code here, to run once:  
  pinMode(rly,OUTPUT);  
  pinMode(sw,INPUT);  
  Serial.begin(115200);  
  while (!Serial){  
   delay(50);  
  }  
  WiFiManager wifiManager;  
  IPAddress _ip = IPAddress(192, 168, 1, 50);  
  IPAddress _gw = IPAddress(192, 168, 1, 1);  
  IPAddress _sn = IPAddress(255, 255, 255, 0);  
  wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);  
  if (!wifiManager.autoConnect("CUBE-Relay", "12345678")) {  
   Serial.println("failed to connect, we should reset as see if it connects");  
   delay(3000);  
   ESP.reset();  
   delay(5000);  
  }  
  //OTA support here  
  ArduinoOTA.onStart([]() {  
   Serial.println("Start");  
  });  
  ArduinoOTA.onEnd([]() {  
   Serial.println("\nEnd");  
  });  
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {  
   Serial.printf("Progress: %u%%\r", (progress / (total / 100)));  
  });  
  ArduinoOTA.onError([](ota_error_t error) {  
   Serial.printf("Error[%u]: ", error);  
   if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");  
   else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");  
   else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");  
   else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");  
   else if (error == OTA_END_ERROR) Serial.println("End Failed");  
  });  
  ArduinoOTA.begin();  
  // Webserver Function here  
  rootPage = "<h1>ESP8266 Relay Control</h1><p>Relay 1 <a href=\"pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"pin=OFF1\"><button>OFF</button></a></p>";   
  server.on("/", []() {  
   server.send(200, "text/html", rootPage);  
  });  
  server.on("/pin=ON1", []() {  
   server.send(200, "text/html", rootPage);  
   Serial.println("GPIO0 = HIGH");  
   digitalWrite(rly, HIGH);  
   delay(1000);  
  });  
  server.on("/pin=OFF1", []() {  
   server.send(200, "text/html", rootPage);  
   Serial.println("GPIO0 = LOW");  
   digitalWrite(rly, LOW);  
   delay(1000);  
  });  
  server.onNotFound ( handleNotFound );  
  server.begin();  
 }  
 void loop() {  
  // put your main code here, to run repeatedly:  
  ArduinoOTA.handle();  
  server.handleClient();  
 }  
 void handleNotFound() {  
  String message = "File Not Found\n\n";  
  message += "URI: ";  
  message += server.uri();  
  message += "\nMethod: ";  
  message += ( server.method() == HTTP_GET ) ? "GET" : "POST";  
  message += "\nArguments: ";  
  message += server.args();  
  message += "\n";  
  for ( uint8_t i = 0; i < server.args(); i++ ) {  
   message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";  
  }  
  server.send ( 404, "text/plain", message );  
 }  

Pada example ini configurasi dilakukan dengan menggunakan WiFiManager via Captive Portal hasil karya Tzapu. Selain fasilitas Webserver pada example ini dilengkapi dengan Arduino OTA support yang memungkinkan mengupload hasil coding anda tanpa harus melepas ESP8266-01 dari Module CUBE AC Relay Board.

Semoga Bermanfaat, Have Fun With CUBE AC Relay Boards














Membuat Coding untuk CUBE AC Relay board dengan Arduino dan Cara Uploadnya Membuat Coding untuk CUBE AC Relay board dengan Arduino dan Cara Uploadnya Reviewed by cahyohertanto on March 13, 2018 Rating: 5

No comments:

close