本文主要是介绍8266 Ubuntu下 arduino开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
8266 NodeMCU arduino开发
直接用usb连接8266的usb接口即可,设备中会出现/dev/ttyUSB0
, 需要将其权限设置下sudo usermod -a -G dialout $USER
. logout后生效.
下载arduino IDE,做以下设置:
- file/preferences Additional boards manager URLS设置为
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Tools//Board
设置为NodeMCU 1.0(ESP-12E Module)
Tools/Port
设置为/dev/ttyUSB0
编写下面代码
#define LED_BUILTIN 2
int i_frame = 0;
void setup() { Serial.begin(9600); // Start the Serial communication to send messages to the computerdelay(1);Serial.println("init done \n");// initialize inbuilt LED pin as an output.pinMode(LED_BUILTIN, OUTPUT);
}// loop function runs over and over again forever
void loop() {char buff[255];sprintf(buff, "%d frame------", i_frame);Serial.println(buff);// Serial.print(i_frame);// Serial.println("loop----");digitalWrite(LED_BUILTIN, HIGH); // turn the LED on by making the pin 13 HIGHdelay(500); // wait for a 0.5 seconddigitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the pin 13 LOWdelay(500); // wait for a 0.5 secondi_frame += 1;
}
点击upload即可.
注意
arduino esptool 可能会比较老导致刷入固件失败,那么可以用pip安装一个最新的版本,然后替换掉arduino用的这个版本.
pip install esptool
# 查看新安装的版本位置
pip show esptool
cd ~/.arduino15/packages/esp8266/hardware/esp8266/3.1.2/tools
mv esptool esptool_ori
cp ~/anaconda3/lib/python3.7/site-packages/esptool . -r
重新build即可.
- 其他一些命令
# 获取flash信息
esptool.py --port /dev/ttyUSB0 --baud 115200 flash_id# 手动刷入bin
esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash -fm dio 0x00000 ./0x00000.bin
mqtt示例
- 先安装mqtt 库
Sketch -> Include Library -> Manage Libraries… 输入PubSubClient, 选择Nick O’Leary版本 - 下载MQTTX
配置:
Broker: broker.emqx.io
TCP Port: 1883
Websocket Port: 8083 - 使用以下代码测试
#include <ESP8266WiFi.h>
#include <PubSubClient.h>// WiFi
const char *ssid = "2.4g"; // Enter your WiFi name
const char *password = "abc"; // Enter WiFi password// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topic = "esp8266/test";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;WiFiClient espClient;
PubSubClient client(espClient);void setup() {// Set software serial baud to 115200;Serial.begin(115200);// connecting to a WiFi networkWiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.println("Connecting to WiFi..");}Serial.println("Connected to the WiFi network");//connecting to a mqtt brokerclient.setServer(mqtt_broker, mqtt_port);client.setCallback(callback);while (!client.connected()) {String client_id = "esp8266-client-";client_id += String(WiFi.macAddress());Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {Serial.println("Public emqx mqtt broker connected");} else {Serial.print("failed with state ");Serial.print(client.state());delay(2000);}}// publish and subscribeclient.publish(topic, "hello emqx");client.subscribe(topic);
}void callback(char *topic, byte *payload, unsigned int length) {Serial.print("Message arrived in topic: ");Serial.println(topic);Serial.print("Message:");for (int i = 0; i < length; i++) {Serial.print((char) payload[i]);}Serial.println();Serial.println("-----------------------");
}void loop() {client.loop();
}
- 在mqttx中输入message后,arduino 串口会打印出消息.
12F 接线参考
https://blog.csdn.net/qq_42190295/article/details/91867943
https://zhuanlan.zhihu.com/p/346356668
fm
的设置参考官方解释:
flash-mode is qio for most ESP8266 ESP-01/07 (512 kByte modules) and dio for most ESP32 and ESP8266 ESP-12 (>=4 MByte modules). ESP8285 requires dout.
其他资料:
https://www.dfrobot.com.cn/images/upload/File/DFR0489/20170906115748b3idk3.pdf
这篇关于8266 Ubuntu下 arduino开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!