ESP32 Arduino连接WIFI

简介

ESP32进行I2C通信的两个引脚SDA和SCL分别采用的17和16引脚。如对引脚使用有疑问,可参考文章ESP32外设管脚分配(I2C/I2S/UART等)

程序运行结果

请添加图片描述

Arduino示例程序

//  Author: Nick
//  Date:23 Sep,2021

//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


//ESP32 Arduino驱动0.91" OLED
//NODEMCU-32 V1.2
//Arduino V1.8.16
//参考链接:
//该文章采用的是串口打印信息的方式进行显示:https://blog.csdn.net/Naisu_kun/article/details/86165403

#include <WiFi.h>

#include <Wire.h>

// 引入驱动OLED0.91所需的库
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // 设置OLED宽度,单位:像素
#define SCREEN_HEIGHT 32 // 设置OLED高度,单位:像素

// 自定义重置引脚,虽然教程未使用,但却是Adafruit_SSD1306库文件所必需的
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char *ssid = "********"; //你的网络名称
const char *password = "********"; //你的网络密码

void setup()
{
  // put your setup code here, to run once:

  //设置I2C的两个引脚SDA和SCL,这里用到的是17作为SDA,16作为SCL
  Wire.setPins(/*SDA*/17,/*SCL*/16);
  //初始化OLED并设置其IIC地址为 0x3C
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  //清除屏幕
  display.clearDisplay();
  //设置字体颜色,白色可见
  display.setTextColor(WHITE);
  //设置字体大小
  display.setTextSize(1.5);
  //设置光标位置
  display.setCursor(0, 0);

  WiFi.begin(ssid, password); //连接网络

  while (WiFi.status() != WL_CONNECTED) //等待网络连接成功
  {
    delay(500);
    display.print(".");
  }

  display.println("WiFi connected!");
  display.print("IP: ");
  display.println(WiFi.localIP()); //打印IP
  display.print("Sub: ");
  display.println(WiFi.subnetMask()); //打印SubnetMask
  display.print("Gate: ");
  display.println(WiFi.gatewayIP()); //打印Gateway
  display.display();
}

void loop()
{
  // put your main code here, to run repeatedly:
}

参考链接

该文章采用的是串口打印信息的方式进行显示:https://blog.csdn.net/Naisu_kun/article/details/86165403

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先要确保你的ESP32模块已经正确地安装在Arduino IDE中。如果你还没有安装ESP32模块,请按照以下步骤安装: a. 打开Arduino IDE,进入“文件”->“首选项”。 b. 在“附加板管理器网址”中添加以下链接:https://dl.espressif.com/dl/package_esp32_index.json c. 进入“工具”->“开发板”->“开发板管理器”。 d. 在搜索栏中输入“ESP32”。 e. 选择“ESP32 by Espressif Systems”并点击“安装”。 f. 安装完成后,选择“开发板”->“ESP32 Dev Module”。 2. 连接ESP32模块到电脑上,选择正确的串口和开发板。 3. 打开“Examples”->“WiFi”->“WiFiScan”示例程序。 4. 将以下代码复制到示例程序中: #include <WiFi.h> const char* ssid = "your_SSID_name"; const char* password = "your_SSID_password"; void setup() { Serial.begin(115200); delay(1000); Serial.println("Connecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); } void loop() { // 这里写你要执行的循环代码 } 5. 将代码中的“your_SSID_name”和“your_SSID_password”替换为你的WiFi名称和密码。 6. 上传程序到ESP32模块中。 7. 打开串口监视器,你将看到ESP32模块正在连接WiFi连接成功后将输出设备的IP地址。 8. 如果你想在ESP32模块中使用WiFi功能,你可以在代码中使用WiFi库提供的函数,例如WiFi.begin()、WiFi.localIP()等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值