ESP8266带2个I2C的OLED屏幕示例程序

438 篇文章 1641 订阅 ¥19.90 ¥99.00
253 篇文章 516 订阅 ¥19.90 ¥99.00
49 篇文章 23 订阅
本文介绍了如何使用ESP8266微控制器通过I2C接口连接并驱动两个OLED显示屏的示例程序,涵盖了开发环境、所需库以及具体的代码示例。
摘要由CSDN通过智能技术生成

ESP8266带2个I2C的OLED屏幕示例程序


  • 开发板:nodemcu1.0
  • 开发环境:Arduino IDE
  • 使用的库ESP8266_and_ESP32_OLED_driver_for_SSD1306_displays
  • 库版本4.2.0,下载链接
    示例代码
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 by ThingPulse, Daniel Eichhorn
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documen
以下是一个ESP8266控制的4针OLED,使用I2C协议连到网络,显示天气和时钟的代码示例: ```c++ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <TimeLib.h> #include <NTPClient.h> #include <WiFiUdp.h> #include <ArduinoJson.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define OLED_ADDRESS 0x3C Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); const char *ssid = "your_wifi_ssid"; const char *password = "your_wifi_password"; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP); const char *host = "api.openweathermap.org"; const char *apiKey = "your_openweathermap_api_key"; const char *city = "your_city"; const char *country = "your_country_code"; void setup() { Serial.begin(115200); Wire.begin(); display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS); // Connect to WiFi network WiFi.begin(ssid, password); Serial.println("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting..."); } Serial.println("Connected to WiFi"); // Initialize time client timeClient.begin(); timeClient.setTimeOffset(3600); // Set time zone offset to GMT+1 // Clear display display.clearDisplay(); } void loop() { // Update time timeClient.update(); int hour = timeClient.getHours(); int minute = timeClient.getMinutes(); int second = timeClient.getSeconds(); // Get weather data WiFiClient client; if (!client.connect(host, 80)) { Serial.println("Connection failed"); return; } String url = "/data/2.5/weather?q=" + String(city) + "," + String(country) + "&appid=" + String(apiKey); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(500); String response = ""; while (client.available()) { char c = client.read(); response += c; } // Parse weather data DynamicJsonDocument doc(1024); deserializeJson(doc, response); String description = doc["weather"][0]["description"].as<String>(); int temperature = doc["main"]["temp"].as<int>() - 273; // Convert to Celsius int humidity = doc["main"]["humidity"].as<int>(); // Display time display.setCursor(0, 0); display.setTextColor(WHITE); display.setTextSize(1); display.print(hour); display.print(":"); if (minute < 10) { display.print("0"); } display.print(minute); display.print(":"); if (second < 10) { display.print("0"); } display.println(second); // Display weather display.setCursor(0, 10); display.print(description); display.setCursor(0, 20); display.print(temperature); display.print("C "); display.print(humidity); display.println("%"); // Update display display.display(); // Wait for next update delay(1000); } ``` 在这个示例中,我们使用了一个名为```Adafruit_SSD1306```的库来控制OLED显示器。我们还使用了```TimeLib```库来获取当前时间,并使用```NTPClient```库来获取网络时间。我们通过```WiFiClient```连到OpenWeatherMap API来获取天气数据,并使用```ArduinoJson```库解析JSON响应。最后我们将时间和天气信息显示OLED上。 请注意,您需要将```your_wifi_ssid```和```your_wifi_password```替换为您的WiFi凭据,将```your_openweathermap_api_key```替换为您的OpenWeatherMap API密钥,将```your_city```替换为您所在的城市,将```your_country_code```替换为您所在的国家代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值