Esp8266学习2. Node-mcu基于Arduino IDE2.0.3设置及基本操作

news/2024/10/18 12:26:45/

Esp8266学习2. Node-mcu基于Arduino IDE2.0.3设置及基本操作

  • 一、准备工作
    • 1. 下载Aruino IDE
    • 2. 准备Node-MCU开发板
  • 二、设置
    • 1. 填写开发板网址
    • 2. 开发板设置
    • 3. 连接开发板
  • 三、测试点亮LED程序
    • 1. 加载示例程序
    • 2. 编译运行
  • 四、一些基本网络操作
    • 1. 连接到热点
    • 2. 使用WiFiClient
    • 3. 创建http请求

在这里插入图片描述

一、准备工作

1. 下载Aruino IDE

这里使用 2.0.3 版本。
https://www.arduino.cc/en/Main/Software
下载后安装。

2. 准备Node-MCU开发板

二、设置

1. 填写开发板网址

点击 File-Preferences,到Additional boards manager URLs,填写:

在这里插入图片描述
https://arduino.esp8266.com/stable/package_esp8266com_index.json
点击OK。

2. 开发板设置

点击菜单 Tools-Board-Boards Manager
在这里插入图片描述
搜索ESP8266,点击Install。
在这里插入图片描述

3. 连接开发板

把开发板连接到电脑,
点击Tools-Port-选择对应的开发板端口。
在这里插入图片描述

点击Select Board:
在这里插入图片描述

选择NodeMUC 1.0开发板。
提示下载库时点击YES。

三、测试点亮LED程序

1. 加载示例程序

点击File-Examples-01.Basics-Blink,加载程序如下:

/*BlinkTurns an LED on for one second, then off for one second, repeatedly.Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZEROit is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set tothe correct LED pin independent of which board is used.If you want to know what pin the on-board LED is connected to on your Arduinomodel, check the Technical Specs of your board at:https://www.arduino.cc/en/Main/Productsmodified 8 May 2014by Scott Fitzgeraldmodified 2 Sep 2016by Arturo Guadalupimodified 8 Sep 2016by Colby NewmanThis example code is in the public domain.https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/// the setup function runs once when you press reset or power the board
void setup() {// initialize digital pin LED_BUILTIN as an output.pinMode(LED_BUILTIN, OUTPUT);
}// the loop function runs over and over again forever
void loop() {digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)delay(1000);                      // wait for a seconddigitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOWdelay(1000);                      // wait for a second
}

2. 编译运行

点击如下菜单 :
在这里插入图片描述
第一个按钮是编译,第二个按钮烧写。烧写完成控制台提示:
在这里插入图片描述
烧写后可以看到开发板的LED灯在闪烁。

四、一些基本网络操作

1. 连接到热点

引用库 <ESP8266Wifi.h>

代码:

#include <ESP8266WiFi.h>#ifndef STASSID
#define STASSID "你的WIFI账号"
#define STAPSK "你的WIFI密码"
#endif// SSID的账号与密码赋值
const char* ssid = STASSID;
const char* password = STAPSK;// 必须指定WIFI工作模式WiFi.mode(WIFI_STA);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}Serial.println("");Serial.println("WiFi connected");Serial.println("Get IP address: ");// 获取本地IP地址Serial.println(WiFi.localIP());

2. 使用WiFiClient

  • WiFiClient client : 初始化类
  • connect(host, port) : 创建连接
  • client.readStringUntil(‘\r’) : 读取一行
  • client.read() : 从缓冲区读取数据
  • client.connected() : 连接状态
  • client.println(“”) :发送数据
  • client.available() :缓冲区是否有数据
  • client.stop() : 关闭连接

3. 创建http请求

使用WiFiClient类用来管理TCP连接。

 // 使用 WiFiClient 类创建TCP连接WiFiClient client;if (!client.connect(host, port)) {Serial.println("connection failed");delay(5000);return;}// 发送数据给服务器Serial.println("sending data to server");if (client.connected()) { client.println("hello from ESP8266"); }// 定义接收的超时时间unsigned long timeout = millis();while (client.available() == 0) {if (millis() - timeout > 5000) {Serial.println(">>> Client Timeout !");client.stop();delay(60000);return;}}// 把收到的数据打印出来Serial.println("receiving from remote server");// not testing 'client.connected()' since we do not need to send data herewhile (client.available()) {char ch = static_cast<char>(client.read());Serial.print(ch);}// 关闭连接Serial.println();Serial.println("closing connection");client.stop();

http://www.ppmy.cn/news/11187.html

相关文章

mysql的binlog学习记录

文章目录什么是binlogbinlog格式StatementRowMixedbinlog使用什么是binlog MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件。简单的来说&#xff0c;binlog日志用于记录所有更新了数据或者以及潜在更新了数据&#xff08;例如&#xff0c;没有匹…

webviz安装,docker安装可正常使用与Foxglove Studio

Foxglove Studio Foxglove Studio与webviz使用起来非常类似 去可以直接使用web也可以下载安装包 Foxglove Studio不提供源码 安装包下载地

初级开发者福音:手把手教你实现数字滚动效果~

文章目录一、前言二、背景知识三、实现方案Step 1&#xff1a;分析需求Step 2&#xff1a;实现单个数字的滚动效果Step 3&#xff1a;组件接口设计Step 4&#xff1a;完善组件一、前言 前端数字滚动显示的场景很多&#xff0c;比如抽奖的时候&#xff0c;营造一种马上公布中奖…

Linux操作系统--文件管理(保姆级教程)

文件系统类型的含义 文件系统类型式指文件在存储介质上存放及存储的组织方法和数据结构。 Linux采用虚拟文件系统技术&#xff08;virtual file system)-VFS 一个世纪的文件系统想要被Linux支持&#xff0c;就必须提供一个符合VFS标准的接口&#xff0c;才能与VFS协同工作&am…

数学建模-回归分析(Stata)

注意&#xff1a;代码文件仅供参考&#xff0c;一定不要直接用于自己的数模论文中国赛对于论文的查重要求非常严格&#xff0c;代码雷同也算作抄袭 如何修改代码避免查重的方法&#xff1a;https://www.bilibili.com/video/av59423231 //清风数学建模 一、基础知识 1.简介 …

DB性能跟不上,加缓存就够了?

服务端软件开发时&#xff0c;通常会把数据存储在DB。而服务端系统遇到的第一个性能瓶颈&#xff0c;往往发生在访问DB时。 这时大部分开发会拿出“缓存”&#xff0c;通过使用Redis在DB前提供一层缓存数据&#xff0c;缓解DB压力&#xff0c;提升服务端性能。 在数据库前添加…

Oracle常用命令,DBA的基础操作,Oracle运维基础操作

用户 sys 和system都是系统管理员&#xff08;DBA&#xff09;&#xff0c;拥有最大的权限&#xff0c;密码是安装时设置的&#xff1b; scott是普通用户&#xff0c;拥有一些用于学习的表&#xff0c;初始密码是tiger。 内置角色 角色是权限的集合&#xff0c;以下是三个内…

私域运营达成目标的三个核心步骤

对于现在的企业来说&#xff0c;建立私域流量是常用的手段&#xff0c;为什么这么说呢&#xff1f;这是因为当前的市场环境下&#xff0c;同行之间的用户争夺是非常激烈的&#xff0c;在加上用户的主观能动性差&#xff0c;这就导致企业不得不想办法给自己一个稳定的用户运营环…