Adafruit OLED库导致我的esp8266崩溃

huangapple go评论52阅读模式
英文:

Adafruit Oled library causing my esp8266 to crash

问题

今天我想尝试使用Arduino语言在ESP8266上使用OLED显示屏。之前我总是使用MicroPython来操作OLED显示屏。我写了一段很长的代码,但一直显示错误。后来我决定注释掉用于OLED显示的代码。然后错误就消失了。有人可以帮我解决这个问题吗?

我的代码:

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

//网络
const char* ssid = "Iffaiman";
const char* password = "iffaiman313";
//OLED变量
#define SCREEN_WIDTH 128 // OLED显示屏宽度(以像素为单位)
#define SCREEN_HEIGHT 64 // OLED显示屏高度(以像素为单位)
//连接到I2C的SSD1306显示屏的声明(SDA,SCL引脚)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

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

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");
  display.println("Hello, world!");
  display.display();
}

void loop() {
}

错误消息:
xception (28): epc1=0x40201e0a epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

我尝试删除任何可选代码,但没有帮助。

英文:

today I wanted to try to use a oled display with esp8266 using arduino language. Before I always used micropython to use an oled display. I had written a long code but it kept showing error. Then I decided to comment out the code that I used for the oled display. Then the error disappeared. Can someone please help me solve that problem?
My code :

#include &lt;Wire.h&gt;
#include &lt;Adafruit_SSD1306.h&gt;
#include &lt;Adafruit_GFX.h&gt;

//netwok
const char* ssid = &quot;Iffaiman&quot;;
const char* password = &quot;iffaiman313&quot;;
//oled var
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);

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

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println(&quot;Connecting to WiFi...&quot;);
  }

  Serial.println(&quot;Connected to WiFi&quot;);
  display.println(&quot;Hello, world!&quot;);
  display.display();
}

void loop() {
}

The error message:
xception (28):
epc1=0x40201e0a epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

I tried to remove any optional code but it didn't help.

答案1

得分: 2

你的代码崩溃是因为你没有在display对象上调用begin()方法。这意味着它未初始化,因此其行为是未定义的。

在对显示进行任何操作之前,你需要调用begin()方法。

Serial.println("Connected to WiFi");
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  Serial.println("Can't find display");
  while (1)
    yield();
}

display.println("Hello, world!");
display.display();

SCREEN_ADDRESS应该是SSD1306的I2C地址,可以是0x3C或0x3D。

Adafruit在如何使用他们的产品和库方面发布了丰富的示例教程。当你在硬件或软件方面遇到问题时,这些都是一个很好的起点。

英文:

Your code is crashing because you're not calling the begin() method on the display object. That means it's not initialized, so its behavior is undefined.

You need to call the begin() method before doing anything else with the display.

Serial.println(&quot;Connected to WiFi&quot;);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  Serial.println(&quot;Can&#39;t find display&quot;);
  while(1)
    yield();
}

display.println(&quot;Hello, world!&quot;);
display.display();

SCREEN_ADDRESS should be the I2C address of the SSD1306, either 0x3C or 0x3D.

Adafruit publishes extensive examples and tutorials on how to use their products and libraries. These are a very good place to start when you're having problems with the hardware or software.

huangapple
  • 本文由 发表于 2023年2月19日 01:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定