Kabar Terkini

Servo + PotensioMeter + NodeMCU Esp8266


Coding 3

Servo + PotensioMeter + OTA + NodeMCU Esp8266

:



/*******

- Kendali Servo dg PotensioMeter Esp8266

for:

- AccessPoint AP

- WebServer

- OTA

iot2 - fajar himawan

********/


#include <ESP8266WiFi.h> 

#include <ESP8266WebServer.h> 

#include <ArduinoOTA.h>


#include <Servo.h>  // add servo library


Servo servo;  // create servo object to control a servo



int pot = 0;  // analog pin used to connect the potentiometer

int temp;     // variable to read the value from the analog pin





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 = "IOT-" + 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 1 seconds

  unsigned long currentMillis = millis();

  previousMillis = currentMillis;

  while (WiFi.status() != WL_CONNECTED && currentMillis - previousMillis <= 1500) {

    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(1000);

  }

 


  server.on ( "/", HTTP_handleRoot );       // call the 'handleRoot' function when a client requests URI "/"

  server.onNotFound ( HTTP_handleRoot );    // when a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"

  server.begin();                           // actually start the server

  

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






  servo.attach(2);          //D4

  servo.write(0);




}





void loop() {

    

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

    server.handleClient();        // listen for HTTP requests from clients

    

  temp = analogRead(pot);            // reads the value of the potentiometer (value between 0 and 1023)

  Serial.println(temp);

  temp = map(temp, 0, 1023, 0, 180);  // scale it to use it with the servo (value between 0 and 180)

  servo.write(temp);                 // sets the servo position according to the scaled value

  delay(50);                         // waits for the servo to get there

      

}


// function prototypes for HTTP handlers

void HTTP_handleRoot(void){

  server.send ( 200, "text/html", "" );       // Send HTTP status 200 (Ok) and send some text to the browser/client

  

  if( server.hasArg("State") ){

     Serial.println(server.arg("State"));

  }

}


void handleNotFound(){

  server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request

}


    






No comments

Featured Post

Pengelolaan Kinerja Guru (PKG) tahun 2025

📑 [PKG 2025] Berikut ini materi/panduan untuk Penilaian Pengelolaan Kinerja Guru (PKG) tahun 2025. 1. Pengelolaan Kinerja Guru 2. Pengelola...