Kabar Terkini

Upload Coding WifiCar OTA esp8266 Tanpa Kabel

Jurnalku by Fajar Himawan

Upload Coding WifiCar OTA esp8266

Tanpa kabel - Cukup hanya dg wifi hape Android








Coding:


/******************* WiFi Robot Obstacle Avoidance - L298N 2A
modif-2
by Jurnalku
Fajar Himawan

********************/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>

// connections for drive Motor FR & BR
int enA = D1;
int in1 = D2;
int in2 = D3;
// connections for drive Motor FL & BL
int in3 = D4;
int in4 = D5;
int enB = D6;

// ultrasonic setup:
const int trigPin = D7; // trig pin connected to Arduino's pin D7
const int echoPin = D8; // echo pin connected to Arduino's pin D8
const int wifiLedPin = D0;  // set digital pin D0 as indication, the LED turn on if NodeMCU connected to WiFi as STA mode

// defines variables
int distanceCm = 0;
int distanceHold = 20;
int SPEED = 300;       // set 1023 to max speed motor

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 Obstacle Avoidance - L298N 2A*");
  Serial.println("--------------------------------------");
 
  pinMode(trigPin, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);   // Sets the echoPin as an Input
  pinMode(wifiLedPin, OUTPUT);  // sets the Wifi LED pin as an Output
  digitalWrite(wifiLedPin, HIGH);
 
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enB, OUTPUT);

  // 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 1 seconds
  unsigned long currentMillis = millis();
  previousMillis = currentMillis;
  while (WiFi.status() != WL_CONNECTED && currentMillis - previousMillis <= 1000) {
    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);
  }

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

  // Set the speed to start, from 0 (off) to 1023 (max speed)
  analogWrite(enA, SPEED);
  analogWrite(enB, SPEED);
 
  // move forward - Initial state
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

// main program loop
void loop() {
  ArduinoOTA.handle();          // listen for update OTA request from clients

  distanceCm=getDistance(); // variable to store the distance measured by the sensor 
  Serial.println(distanceCm); // print the distance that was measured

  //if the distance is less than value of distanceHold, The robot will stop and scanning for any object around it
  if(distanceCm <= distanceHold){
    // turn right
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(400);
 
    // move forward
    digitalWrite(in1, HIGH);   
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
  }
}

// returns the distance measured by the HC-SR04 distance sensor
int getDistance() {
  int echoTime;                   //variable to store the time it takes for a ping to bounce off an object
  int calcualtedDistance;         //variable to store the distance calculated from the echo time
 
  //send out an ultrasonic pulse that's 10ms long
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  echoTime = pulseIn(echoPin, HIGH);      //use the pulsein command to see how long it takes for the
                                          //pulse to bounce back to the sensor

  calcualtedDistance = echoTime / 58.26;  //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
  return calcualtedDistance;              //send back the distance that was calculated

}





No comments

Featured Post

AHT 10 SENSOR SUHU DAN KELEMBABAN

Cara mengakses sensor AHT10 Sensor Suhu dan kelembaban Sensor AHT10  adalah sensor alternatif sensor suhu dan kelembaban sel...