martes, 25 de abril de 2023

ESP32 Conectar a WIFI y mostrando IP en puerto serie o OLED o LCD

 

MOSTRANDO IP EN PUERTO SERIE:

#include "WiFi.h"
const char* ssid = "NombreRed";
const char* password = "Contraseña";


void setup() {
 Serial.begin(115200);
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);  

while (WiFi.status() != WL_CONNECTED) {  
  delay(500);    
  Serial.println("Connecting to WiFi..");  
 }

 Serial.println();
 Serial.print("Conectado a:\t");  
 Serial.println(WiFi.SSID());  
 Serial.print("IP address:\t");
 Serial.println(WiFi.localIP());
}
void loop() {
 Serial.print("Conectado a:\t");  
 Serial.println(WiFi.SSID());  
 Serial.print("IP address:\t");
 Serial.println(WiFi.localIP());
 delay(5000);
}





MOSTRANDO IP EN PANTALLA OLED:

#include "WiFi.h"

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

const char* ssid ="NombreRed";
const char* password ="Contraseña";

#define i2c_Address 0x3c
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1   //   QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println();
  Serial.print("Conectado a:\t");
  Serial.println(WiFi.SSID());
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());

  display.begin(i2c_Address, true); // Address 0x3C default
  display.display();
  delay(2000);
  // Clear the buffer.
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("IP:");
  display.println("");
  display.println(WiFi.localIP());
  display.display();
  delay(2000);
}
 
void loop() {
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
  delay (5000);
  }

MOSTRANDO IP EN PANTALLA LCD:

#include "WiFi.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const char espwifi_ssid[]="NombreRed";
const char espwifi_pass[]="Contraseña";
LiquidCrystal_I2C lcd(0x27,16,2);
unsigned long task_time_ms=0;

void espwifi_setup(){
  WiFi.mode(WIFI_STA);
  WiFi.begin(espwifi_ssid,espwifi_pass);
  while (WiFi.status() != WL_CONNECTED) delay(500);
}

void setup()
{
  espwifi_setup();
  lcd.init();
  lcd.noCursor();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(7, 0);
  lcd.print(String("IP:"));
  lcd.setCursor(0, 1);
  lcd.print(WiFi.localIP().toString());
}
void loop()
{
  if((millis()-task_time_ms)>=5000){
    task_time_ms=millis();
    lcd.clear();
    lcd.setCursor(7, 0);
    lcd.print(String("IP:"));
    lcd.setCursor(0, 1);
    lcd.print(WiFi.localIP().toString());
  }

}