英文:
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 <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
//netwok
const char* ssid = "Iffaiman";
const char* password = "iffaiman313";
//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, &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() {
}
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("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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论