Kabar Terkini

OTA BLINK basic STA AP














Coding:

 /***************** BASIC -OTA - STA - AP -  RGB CONTROLLED - Jurnalku by Fajar Himawan ********************/
#include <ESP8266WiFi.h> 
#include <ESP8266WebServer.h> 
#include <ArduinoOTA.h>

const int wifiLedPin = D0;  // set digital pin D0 as indication, the LED turn on if NodeMCU connected to WiFi as STA mode
ESP8266WebServer server(80);      // Create a webserver object that listens for HTTP request on port 80
unsigned long previousMillis = 0;
String sta_ssid = "ota";      // set Wifi networks you want to connect to
String sta_password = "12345678";  // set password for Wifi networks

void setup(){
  Serial.begin(115200);    // set up Serial library at 115200 bps
  Serial.println();
  Serial.println("*WiFi Robot Remote Control Mode - L298N 2A*");
  Serial.println("------------------------------------------------");
 
  
  pinMode(wifiLedPin, OUTPUT);  // sets the Wifi LED pin as an Output
  digitalWrite(wifiLedPin, HIGH);

  
  // set NodeMCU Wifi hostname based on chip mac address
  String chip_id =      String(ESP.getChipId(), HEX);
  int i = chip_id.length()-4;
  chip_id = chip_id.substring(i);
  chip_id = "wificar-" + chip_id;
  String hostname(chip_id);
  
  Serial.println();
  Serial.println("Hostname: "+hostname);
  // first, set NodeMCU as STA mode to connect with a Wifi network
  WiFi.mode(WIFI_STA);
  WiFi.begin(sta_ssid.c_str(), sta_password.c_str());
  Serial.println("");
  Serial.print("Connecting to: ");
  Serial.println(sta_ssid);
  Serial.print("Password: ");
  Serial.println(sta_password);
  // try to connect with Wifi network about 3 seconds
  unsigned long currentMillis = millis();
  previousMillis = currentMillis;
  while (WiFi.status() != WL_CONNECTED && currentMillis - previousMillis <= 3000) {
    delay(500);
    Serial.print(".");
    currentMillis = millis();
  }
  // if failed to connect with Wifi network set NodeMCU as AP mode
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("*WiFi-STA-Mode*");
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    digitalWrite(wifiLedPin, LOW);    // Wifi LED on when connected to Wifi as STA mode
    delay(3000);
  } else {
    WiFi.mode(WIFI_AP);
    WiFi.softAP(hostname.c_str());
    IPAddress myIP = WiFi.softAPIP();
    Serial.println("");
    Serial.println("WiFi failed connected to " + sta_ssid);
    Serial.println("");
    Serial.println("*WiFi-AP-Mode*");
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    digitalWrite(wifiLedPin, HIGH);   // Wifi LED off when status as AP mode
    delay(3000);
  }
 
  
  server.begin();                           
  
  ArduinoOTA.begin();                       // enable to receive update/uploade firmware via Wifi OTA
}
void loop() {
    ArduinoOTA.handle();          // listen for update OTA request from clients
    server.handleClient();        // listen for HTTP requests from clients
    
    
  digitalWrite(wifiLedPin, HIGH);
  delay(1000);
  digitalWrite(wifiLedPin, LOW);
  delay(1000);
     
}



__________________


LED RGB

CODING:



/*******************LED RGB - OTA - STA - AP - FADE - Jurnalku by Fajar Himawan ********************/
#include <ESP8266WiFi.h> 
#include <ESP8266WebServer.h> 
#include <ArduinoOTA.h>


const int wifiLedPin = D0;  // set digital pin D0 as indication, the LED turn on if NodeMCU connected to WiFi as STA mode

const int LedMerah = D5;
const int LedHijau = D6;
const int LedBiru = D7;


ESP8266WebServer server(80);      // Create a webserver object that listens for HTTP request on port 80

unsigned long previousMillis = 0;

String sta_ssid = "ota";      // set Wifi networks you want to connect to
String sta_password = "12345678";  // set password for Wifi networks


void setup(){
  Serial.begin(115200);    // set up Serial library at 115200 bps
  Serial.println();
  Serial.println("*WiFi Robot Remote Control Mode - L298N 2A*");
  Serial.println("------------------------------------------------");
 
  
  pinMode(wifiLedPin, OUTPUT);  // sets the Wifi LED pin as an Output
    pinMode(LedMerah, OUTPUT);
    pinMode(LedHijau, OUTPUT);
    pinMode(LedBiru, OUTPUT);

  digitalWrite(wifiLedPin, HIGH);
    digitalWrite(LedMerah, HIGH);
    digitalWrite(LedHijau, HIGH);
    digitalWrite(LedBiru, HIGH);


  
  // set NodeMCU Wifi hostname based on chip mac address
  String chip_id =      String(ESP.getChipId(), HEX);
  int i = chip_id.length()-4;
  chip_id = chip_id.substring(i);
  chip_id = "wificar-" + chip_id;
  String hostname(chip_id);
  
  Serial.println();
  Serial.println("Hostname: "+hostname);

  // first, set NodeMCU as STA mode to connect with a Wifi network
  WiFi.mode(WIFI_STA);
  WiFi.begin(sta_ssid.c_str(), sta_password.c_str());
  Serial.println("");
  Serial.print("Connecting to: ");
  Serial.println(sta_ssid);
  Serial.print("Password: ");
  Serial.println(sta_password);

  // try to connect with Wifi network about 3 seconds
  unsigned long currentMillis = millis();
  previousMillis = currentMillis;
  while (WiFi.status() != WL_CONNECTED && currentMillis - previousMillis <= 3000) {
    delay(500);
    Serial.print(".");
    currentMillis = millis();
  }

  // if failed to connect with Wifi network set NodeMCU as AP mode
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("*WiFi-STA-Mode*");
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    digitalWrite(wifiLedPin, LOW);    // Wifi LED on when connected to Wifi as STA mode
    delay(3000);
  } else {
    WiFi.mode(WIFI_AP);
    WiFi.softAP(hostname.c_str());
    IPAddress myIP = WiFi.softAPIP();
    Serial.println("");
    Serial.println("WiFi failed connected to " + sta_ssid);
    Serial.println("");
    Serial.println("*WiFi-AP-Mode*");
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    digitalWrite(wifiLedPin, HIGH);   // Wifi LED off when status as AP mode
    delay(3000);
  }
 
  
  server.begin();                           
  
  ArduinoOTA.begin();                       // enable to receive update/uploade firmware via Wifi OTA
}

void loop() {
    ArduinoOTA.handle();          // listen for update OTA request from clients
    server.handleClient();        // listen for HTTP requests from clients

  testRGB();
    
  Blink();
}

void testRGB() { // fade in and out of Red, Green, Blue

analogWrite(LedMerah, 0); // R off
analogWrite(LedHijau, 0); // G off
analogWrite(LedBiru, 0); // B off

fade(LedMerah); // R
fade(LedHijau); // G
fade(LedBiru); // B
}

//////////////////////////////////////////////////////////////////////////////////////////////////

void fade(int pin) {

for (int u = 0; u < 1024; u++) {
analogWrite(pin, u);
delay(1);
}
for (int u = 0; u < 1024; u++) {
analogWrite(pin, 1023 - u);
delay(1);
}
}


void Blink() {

    digitalWrite(LedMerah, HIGH);
  delay(1000);
  digitalWrite(LedMerah, LOW);
  delay(10);
    
  digitalWrite(LedHijau, HIGH);
  delay(1000);
  digitalWrite(LedHijau, LOW);
  delay(10);
      
  digitalWrite(LedBiru, HIGH);
  delay(1000);
  digitalWrite(LedBiru, LOW);
  delay(10);
  
  digitalWrite(LedMerah, HIGH);
  delay(1000);
  digitalWrite(LedBiru, HIGH);
  delay(1000);
  digitalWrite(LedBiru, LOW);
  delay(10);
    
  digitalWrite(LedMerah, HIGH);
  delay(1000);
  digitalWrite(LedHijau, HIGH);
  delay(1000);
  digitalWrite(LedHijau, LOW);
  delay(10);

  digitalWrite(LedBiru, HIGH);
  delay(1000);
  digitalWrite(LedHijau, HIGH);
  delay(1000);
  digitalWrite(LedHijau, LOW);
  delay(10);

}




_____________________


Fade Rgb Ota AP - webserver Controlled
Coding:

/* RGB web server with ESP8266
* analogWrite with values received from web page
* Saya tambahkan script FADE RGB LED
*
* WiFi router that creates a fixed domain: http://rgb
* web page returns POST request with 3 RGB parameters http://192.168.1.5/
* web page inspired by https://github.com/dimsumlabs/nodemcu-httpd
*/

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>

#include <ArduinoOTA.h>


const char *ssid = "The IoT Projects";
const char *password = "";
const byte DNS_PORT = 53;

const int redPin = D5;
const int greenPin = D6;
const int bluePin = D7;


long int fade_goTime;
int fade_break = 100;



IPAddress apIP(192, 168, 1, 5);
DNSServer dnsServer;
ESP8266WebServer webServer(80);





String webpage = ""
"<!DOCTYPE html><html><head><title>RGB control</title><meta name='mobile-web-app-capable' content='yes' />"
"<meta name='viewport' content='width=device-width' /></head><body style='margin: 0px; padding: 0px;'>"
"<canvas id='colorspace'></canvas></body>"
"<script type='text/javascript'>"
"(function () {"
" var canvas = document.getElementById('colorspace');"
" var ctx = canvas.getContext('2d');"
" function drawCanvas() {"
" var colours = ctx.createLinearGradient(0, 0, window.innerWidth, 0);"
" for(var i=0; i <= 360; i+=10) {"
" colours.addColorStop(i/360, 'hsl(' + i + ', 100%, 50%)');"
" }"
" ctx.fillStyle = colours;"
" ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);"
" var luminance = ctx.createLinearGradient(0, 0, 0, ctx.canvas.height);"
" luminance.addColorStop(0, '#ffffff');"
" luminance.addColorStop(0.05, '#ffffff');"
" luminance.addColorStop(0.5, 'rgba(0,0,0,0)');"
" luminance.addColorStop(0.95, '#000000');"
" luminance.addColorStop(1, '#000000');"
" ctx.fillStyle = luminance;"
" ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);"
" }"
" var eventLocked = false;"
" function handleEvent(clientX, clientY) {"
" if(eventLocked) {"
" return;"
" }"
" function colourCorrect(v) {"
" return Math.round(1023-(v*v)/64);"
" }"
" var data = ctx.getImageData(clientX, clientY, 1, 1).data;"
" var params = ["
" 'r=' + colourCorrect(data[0]),"
" 'g=' + colourCorrect(data[1]),"
" 'b=' + colourCorrect(data[2])"
" ].join('&');"
" var req = new XMLHttpRequest();"
" req.open('POST', '?' + params, true);"
" req.send();"
" eventLocked = true;"
" req.onreadystatechange = function() {"
" if(req.readyState == 4) {"
" eventLocked = false;"
" }"
" }"
" }"
" canvas.addEventListener('click', function(event) {"
" handleEvent(event.clientX, event.clientY, true);"
" }, false);"
" canvas.addEventListener('touchmove', function(event){"
" handleEvent(event.touches[0].clientX, event.touches[0].clientY);"
"}, false);"
" function resizeCanvas() {"
" canvas.width = window.innerWidth;"
" canvas.height = window.innerHeight;"
" drawCanvas();"
" }"
" window.addEventListener('resize', resizeCanvas, false);"
" resizeCanvas();"
" drawCanvas();"
" document.ontouchmove = function(e) {e.preventDefault()};"
" })();"
"</script></html>";

//////////////////////////////////////////////////////////////////////////////////////////////////

void handleRoot() {
// Serial.println("handle root..");
String red = webServer.arg(0); // read RGB arguments
String green = webServer.arg(1);
String blue = webServer.arg(2);

if((red != "") && (green != "") && (blue != ""))
{
  analogWrite(redPin, 1023 - red.toInt());
  analogWrite(greenPin, 1023 - green.toInt());
  analogWrite(bluePin, 1023 - blue.toInt());
}


Serial.print("Red: ");
Serial.println(red.toInt()); 
Serial.print("Green: ");
Serial.println(green.toInt()); 
Serial.print("Blue: ");
Serial.println(blue.toInt()); 
Serial.println();

webServer.send(200, "text/html", webpage);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////







void setup() {
    
    
   
  fade_goTime = millis(); 


pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);

delay(1000);
Serial.begin(115200);
Serial.println();

WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(ssid);

// if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS request
dnsServer.start(DNS_PORT, "rgb", apIP);

webServer.on("/", handleRoot);

webServer.begin();

testRGB();
    
    
        
ArduinoOTA.begin();                       // enable to receive update/uploade firmware via Wifi OTA
    

}

//////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {

    
dnsServer.processNextRequest();
webServer.handleClient();

ArduinoOTA.handle();          // listen for update OTA request from clients  

//if(  millis() >= fade_goTime) fade();

}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

void testRGB() { // fade in and out of Red, Green, Blue

analogWrite(redPin, 0); // R off
analogWrite(greenPin, 0); // G off
analogWrite(bluePin, 0); // B off

fade(redPin); // R
fade(greenPin); // G
fade(bluePin); // B
}

//////////////////////////////////////////////////////////////////////////////////////////////////

void fade(int pin) {

for (int u = 0; u < 1024; u++) {
analogWrite(pin, u);
delay(1);
}
for (int u = 0; u < 1024; u++) {
analogWrite(pin, 1023 - u);
delay(1);
}
}


void fade()
{
 
  static unsigned int rgbColour[3] = {255,0,0}; //Start on red
  static int incColour = 1;
  static int decColour = 0;
  static int i = -1;
      
      // cross-fade the two colours.
      i++;
      if(i > 255) {
        i=0;
        decColour ++;
        if(decColour >2) decColour = 0;      
        if (decColour == 2)  incColour = 0;
        else incColour = decColour +1;
      }

       
        rgbColour[decColour] -= 1;
        rgbColour[incColour] += 1;
       
        setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);      
       fade_goTime = millis() + fade_break;       
   }

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
 }
















No comments

Featured Post

INILAH 6 PROGRAM PRIORITAS Kemendikdasmen Versi Abdul Mu'ti, Usung Pendidikan Bermutu untuk Semua

INILAH 6 PROGRAM PRIORITAS Kemendikdasmen Versi Abdul Mu'ti, Usung Pendidikan Bermutu untuk Semua INILAH 6 PROGRAM PRIORITAS ...