基于Arduino IDE 野火ESP8266模块WIiFi开发

news2024/9/20 4:53:20

一、函数介绍

头文件

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

  ESP8266WiFi.h库主要用于连接单个WiFi网络。如果需要连接到多个WiFi网络,例如在需要切换不同网络或者备用网络时,可以使用ESP8266WiFiMulti.h头文件,它是ESP8266WiFi.h的扩展
ESP8266WiFi.h头文件的内容如下:

/*
 ESP8266WiFi.h - esp8266 Wifi support.
 Based on WiFi.h from Arduino WiFi shield library.
 Copyright (c) 2011-2014 Arduino.  All right reserved.
 Modified by Ivan Grokhotkov, December 2014

 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
 */

#ifndef WiFi_h
#define WiFi_h

#include <stdint.h>

extern "C" {
#include <wl_definitions.h>
}

#include "IPAddress.h"

#include "ESP8266WiFiType.h"
#include "ESP8266WiFiSTA.h"
#include "ESP8266WiFiAP.h"
#include "ESP8266WiFiScan.h"
#include "ESP8266WiFiGeneric.h"

#include "WiFiClient.h"
#include "WiFiServer.h"
#include "WiFiServerSecure.h"
#include "WiFiClientSecure.h"
#include "BearSSLHelpers.h"
#include "CertStoreBearSSL.h"

#ifdef DEBUG_ESP_WIFI
#ifdef DEBUG_ESP_PORT
#define DEBUG_WIFI(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ##__VA_ARGS__ )
#endif
#endif

#ifndef DEBUG_WIFI
#define DEBUG_WIFI(...) do { (void)0; } while (0)
#endif

extern "C" void enableWiFiAtBootTime (void) __attribute__((noinline));

class ESP8266WiFiClass : public ESP8266WiFiGenericClass, public ESP8266WiFiSTAClass, public ESP8266WiFiScanClass, public ESP8266WiFiAPClass {
    public:

        // workaround same function name with different signature
        using ESP8266WiFiGenericClass::channel;

        using ESP8266WiFiSTAClass::SSID;
        using ESP8266WiFiSTAClass::RSSI;
        using ESP8266WiFiSTAClass::BSSID;
        using ESP8266WiFiSTAClass::BSSIDstr;

        using ESP8266WiFiScanClass::SSID;
        using ESP8266WiFiScanClass::encryptionType;
        using ESP8266WiFiScanClass::RSSI;
        using ESP8266WiFiScanClass::BSSID;
        using ESP8266WiFiScanClass::BSSIDstr;
        using ESP8266WiFiScanClass::channel;
        using ESP8266WiFiScanClass::isHidden;

        // ----------------------------------------------------------------------------------------------
        // ------------------------------------------- Debug --------------------------------------------
        // ----------------------------------------------------------------------------------------------

    public:

        void printDiag(Print& dest);

        friend class WiFiClient;
        friend class WiFiServer;

};

extern ESP8266WiFiClass WiFi;

#endif

ESP8266 Wi-Fi库是基于ESP8266 SDK开发的。
在这里插入图片描述
WiFi.mode函数

WiFi.mode()//设置模式

在这里插入图片描述

WiFi.begin函数

WiFi.begin("network-name", "pass-to-network");

WiFi.status函数

WiFi.status()//获取连接是否已经完成,连接过程可能需要几秒钟

WiFi.localIP函数

WiFi.localIP()//获取DHCP分配给ESP模块的IP地址

根据以上一个函数,可以编写一个测试代码,来进行esp8266的联网功能测试。

二、测试例程

测试代码如下:

#include <ESP8266WiFi.h>

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin("network-name", "pass-to-network");

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

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {}

如果串口输出只有越来越多的点… ,原因可能是输入的Wi-Fi网络的名称或密码就不正确。可通过PC或手机从头连接到该Wi-Fi网络,验证名称和密码。
注意:如果建立了连接,然后由于某种原因失去了连接,ESP将自动重新连接到上次使用的接入点,一旦它再次联机。这将由Wi-Fi库自动完成,无需任何用户干预

在这里插入图片描述

编写复杂点的功能,连接tcp服务器,并进行通信,代码示例如下:

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

const char* host = "djxmmx.net";
const uint16_t port = 3000;

ESP8266WiFiMulti WiFiMulti;

void setup() {
  Serial.begin(115200);

  // We start by connecting to a WiFi network
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Wait for WiFi... ");

  while (WiFiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  delay(500);
}


void loop() {
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;

  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    Serial.println("wait 5 sec...");
    delay(5000);
    return;
  }

  // This will send the request to the server
  client.println("hello from ESP8266");
  while(client.status() == ESTABLISHED)
  {
    // read back one line from server
    Serial.println("receiving from remote server");
    String line = client.readStringUntil('\r');
    if(!line.isEmpty())
    {
      Serial.println(line);
      client.println(line);
    }
    delay(3000);
  }

  // not testing 'client.connected()' since we do not need to send data here
  //while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
    //String line = client.readStringUntil('\r');
    //Serial.println(line);
 // }

  Serial.println("closing connection");
  client.stop();

  Serial.println("wait 5 sec...");
  delay(5000);
}

在这里插入图片描述

参考
https://arduino-esp8266.readthedocs.io/en/latest/index.html

https://github.com/esp8266/Arduino.git

https://arduino-esp8266.readthedocs.io/en/2.4.2/

https://www.arduino.cc/reference/en/libraries/wifi/

http://www.taichi-maker.com/homepage/iot-development/iot-dev-reference/esp8266-c-plus-plus-reference/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1535563.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

供应链投毒预警 | 恶意Py组件tohoku-tus-iot-automation开展窃密木马投毒攻击

概述 上周&#xff08;2024年3月6号&#xff09;&#xff0c;悬镜供应链安全情报中心在Pypi官方仓库&#xff08;https://pypi.org/&#xff09;中捕获1起新的Py包投毒事件&#xff0c;Python组件tohoku-tus-iot-automation 从3月6号开始连续发布6个不同版本恶意包&#xff0c…

SAP前台处理:物料计价方式:价格控制与价格确定 - 01

一、背景&#xff1a; 物料主数据中我们讲解到物料的计价方式&#xff0c;SAP应用到的主要计价方式有移动平均价和标准价格方式两种&#xff0c;但也有按照批次计价等方式&#xff0c;我们主要介绍最常用的V2移动平均价和S3的标准价格&#xff1b; 二、不同组合下的差异&…

知识蒸馏——深度学习的简化之道 !!

文章目录 前言 1、什么是知识蒸馏 2、知识蒸馏的原理 3、知识蒸馏的架构 4、应用 结论 前言 在深度学习的世界里&#xff0c;大型神经网络因其出色的性能和准确性而备受青睐。然而&#xff0c;这些网络通常包含数百万甚至数十亿个参数&#xff0c;使得它们在资源受限的环境下&…

【HM】STM32F407 HAL库 PWM

PWM简介 脉冲宽度调制&#xff08;PWM&#xff09; 是一种数字信号&#xff0c;最常用于控制电路。该信号在预定义的时间和速度中设置为高&#xff08;5v或3.3v&#xff09;和低&#xff08;0v&#xff09;。通常&#xff0c;我们将PWM的高电平称为1&#xff0c;低电平为0。 …

pyboard开发板上手

文章目录 准备开发板连接到pyboard开发板将pyboard作为U盘打开编辑main.py重启pyboard 准备开发板 本文介绍了如何使用MicroPython在pyboard开发板上运行你的第一个程序&#xff0c;所以&#xff0c;在开始下面的步骤前&#xff0c;你需要有一块pyboard开发板&#xff0c;如果…

OKR如何与个人绩效评估和激励相结合?

在现代企业管理中&#xff0c;个人绩效评估与激励是提升员工积极性、推动企业发展的关键环节。而OKR&#xff08;目标与关键成果&#xff09;作为一种高效的目标管理方法&#xff0c;通过与个人绩效评估和激励相结合&#xff0c;可以进一步提升员工的工作动力和工作效率&#x…

数据机构-2(顺序表)

线性表 概念 顺序表 示例&#xff1a;创建一个存储学生信息的顺序表 表头&#xff08;Tlen总长度&#xff0c; Clen当前长度&#xff09; 函数 #include <seqlist.c> #include <stdio.h> #include <stdlib.h> #include "seqlist.h" #include &…

C#对于文件中的文件名判断问题

C#中对于文件名的判断问题&#xff0c;我们使用bool值进行值的传递&#xff0c;首先我们使用内置方法进行文件字符串匹配的bool值回传&#xff0c;我们打印出文件名以及相对应的bool&#xff0c;即可知道文件名是否真正生效 bool isHave fileName.Contains("Hello"…

【Python + Django】表结构创建

以员工管理系统为例。 事前呢&#xff0c;我们先把项目和app创建出来&#xff0c;详细步骤可以看我同栏目的第一篇、第二篇文章。 我知道你们是不会下来找的&#xff0c;就把链接贴在下面吧&#xff1a; 【Python Django】启动简单的文本页面-CSDN博客 【Python Django】…

excel所有知识点

1要加双引号 工作表&#xff08;.xlsx) 单击右键→插入&#xff0c;删除&#xff0c;移动、重命名、复制、设置标签颜色&#xff0c;选定全部工作表 工作表的移动&#xff1a;两个表打开→右键→移动&#xff08;如果右键是灰色的&#xff0c;可能是保护工作表了&#xff09…

读算法的陷阱:超级平台、算法垄断与场景欺骗笔记17_执法工具

1. 执法工具箱 1.1. 在数据驱动的经济环境中&#xff0c;明智监管潜力无限 1.2. 多年前的司法体系与反垄断执法机构更善于发现市场漏洞&#xff0c;并设计出了直接有效的方式来化解问题 1.2.1. 大型互联网平台的权势凌驾于法律之上 1.2.1.1. 英国上议院 1.3. 反垄断执法机…

SQLiteC/C++接口详细介绍sqlite3_stmt类(九)

返回&#xff1a;SQLite—系列文章目录 上一篇&#xff1a;SQLiteC/C接口详细介绍sqlite3_stmt类&#xff08;六&#xff09; 下一篇&#xff1a; 无 33、sqlite3_column_table_name 函数 sqlite3_column_table_name 用于返回结果集中指定列所属的表的名称。如果查询中列使…

K8S Storage

概述 一般情况下&#xff0c;K8S中的Pod都不应该将数据持久化到Pod中&#xff0c;因为Pod可能被随时创建和删除&#xff08;扩容或缩容&#xff09;&#xff0c;即便是StatefulSet或Operator的Pod&#xff0c;也都不建议在Pod里存放数据&#xff0c;可以将数据持久化到Host上。…

本地丐版运行xAI grok-1的尝试(失败版)

前言 xAI开源了包含3000多亿参数的grok-1&#xff0c;想试试在本地跑。试了半天结果内存不够&#xff0c;结果以失败告终&#xff0c;结论是机器丐不了一点&#xff0c;想要跑起来内存必须要管够&#xff0c;显存应该也是需要的&#xff08;xAI好像用的8*A100 80G NvLink&…

多数据源mybatisplus对sqlserver分页查询兼容

新增配置文件 package com.ruoyi.framework.config;import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor…

两个免费的wordpress主模板

wordpress免费网站主题 蓝色高端大气上档次的wordpress免费网站主题&#xff0c;首页大图wordpress模板。 https://www.wpniu.com/themes/31.html WP免费模板 用粉色高端大气上档次的WP免费模板&#xff0c;建个网站也不错的。 https://www.wpniu.com/themes/16.html

从后端获取文件数据并导出

导出文件的公共方法 export const download (res, tools) > {const { message, hide } tools;const fileReader: any new FileReader();console.log(fileReader-res>>>, res);fileReader.onload (e) > {if (res?.data?.type application/json) {try {co…

WM8978 —— 带扬声器驱动程序的立体声编解码器(4)

接前一篇文章&#xff1a;WM8978 —— 带扬声器驱动程序的立体声编解码器&#xff08;3&#xff09; 九、寄存器概览与详解 1. 整体概览 WM8978芯片共有58个寄存器&#xff0c;整体总表如下&#xff1a; 2. 详细说明 在此&#xff0c;只介绍WM8978较为常用的那些寄存器。 &…

嵌入式典型总线及协议

在嵌入式系统中&#xff0c;各种总线和通信协议扮演着关键的角色&#xff0c;它们连接和协调系统中的各种硬件组件&#xff0c;实现数据传输和控制。本文将介绍一些典型的嵌入式总线及其通信协议&#xff0c;以及它们在嵌入式系统中的应用。 以下是我整理的关于嵌入式开发的一…

Java-SSM电影购票系统

Java-SSM电影购票系统 1.服务承诺&#xff1a; 包安装运行&#xff0c;如有需要欢迎联系&#xff08;VX:yuanchengruanjian&#xff09;。 2.项目所用框架: 前端:JSP、layui、bootstrap等。 后端:SSM,即Spring、SpringMvc、Mybatis等。 3.项目功能点: 3-1.后端功能: 1.用户管…