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 computer
  delay(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 HIGH
  delay(500);              // wait for a 0.5  second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the  pin 13 LOW
  delay(500);              // wait for a 0.5 second
  i_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 network
  WiFi.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 broker
  client.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 subscribe
  client.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

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值