oled显示网络时间, wifi链接网络
# include "time.h"
String week[ 8 ] = {
"Sun" , "Mon" , "Tues" , "Wednes" , "Thur" , "Fri" , "Sat"
} ;
void printLocalTime ( Adafruit_SSD1306 & display) {
struct tm timeinfo;
if ( ! getLocalTime ( & timeinfo) ) {
Serial. println ( "Failed to obtain time" ) ;
return ;
}
Serial. println ( & timeinfo, "%A, %Y-%m-%d %H:%M:%S" ) ;
char display_str[ 30 ] = { 0 } ;
sprintf ( display_str, "%04d-%02d-%02d %02d:%02d:%02d %01d" ,
timeinfo. tm_year+ 1900 ,
timeinfo. tm_mon+ 1 ,
timeinfo. tm_mday,
timeinfo. tm_hour,
timeinfo. tm_min,
timeinfo. tm_sec, timeinfo. tm_wday) ;
display. clearDisplay ( ) ;
display. setCursor ( 0 , 0 ) ;
display. println ( display_str) ;
display. display ( ) ;
}
const char * ntpServer = "pool.ntp.org" ;
const long gmtOffset_sec = 28800 ;
const int daylightOffset_sec = 0 ;
void ntp_get_date_init ( ) {
configTime ( gmtOffset_sec, daylightOffset_sec, ntpServer) ;
}
# include <Arduino.h>
# include <WiFi.h>
# include <Adafruit_SSD1306.h>
# include "ntp_get_date.h"
Adafruit_SSD1306 display = Adafruit_SSD1306 ( 128 , 32 , & Wire) ;
const char * id= "Wifi账号" ;
const char * psw= "wifi密码" ;
void setup ( ) {
Serial. begin ( 115200 ) ;
if ( ! display. begin ( SSD1306_SWITCHCAPVCC, 0x3C ) ) {
Serial. println ( F ( "SSD1306 allocation failed" ) ) ;
for ( ; ; )
;
}
display. display ( ) ;
delay ( 500 ) ;
display. setTextSize ( 1 ) ;
display. setTextColor ( WHITE) ;
display. setRotation ( 0 ) ;
display. clearDisplay ( ) ;
WiFi. begin ( id, psw) ;
while ( WiFi. status ( ) != WL_CONNECTED) {
delay ( 500 ) ;
Serial. println ( "connection..." ) ;
}
display. setCursor ( 0 , 0 ) ;
display. println ( "wifi connect to Wifi007 success" ) ;
display. display ( ) ;
Serial. println ( "wifi connect to Wifi007 success" ) ;
ntp_get_date_init ( ) ;
}
void loop ( ) {
printLocalTime ( display) ;
delay ( 500 ) ;
}
mqtt订阅和发布信息
服务端 docker安装emqx
docker run -dit --name emqx -p 18083 :18083 -p 1883 :1883 -p 8083 :8083 -p 8084 :8084 emqx/emqx:latest
浏览器登录: http://192.168.3.12:18083/
admin 密码是public
18083 管理控制端口
1883 mqtt协议端口
8083 MQTTwebSocket端口
https://blog.csdn.net/weixin_43869518/article/details/127558282
客户端 esp32c3 xiao
# include <Arduino.h>
# include <WiFi.h>
# include <PubSubClient.h>
const char * id= "wifi账号" ;
const char * psw= "wifi密码" ;
const char * mqtt_server = "192.168.3.12" ;
const uint16_t mqtt_port = 1883 ;
WiFiClient espClient;
PubSubClient mqttClient ( espClient) ;
void callback ( char * topic, byte* payload, unsigned int length) ;
void reconnect ( ) ;
void setup ( ) {
Serial. begin ( 115200 ) ;
WiFi. mode ( WIFI_STA) ;
WiFi. begin ( id, psw) ;
while ( WiFi. status ( ) != WL_CONNECTED) {
delay ( 500 ) ;
Serial. println ( "connection..." ) ;
}
Serial. println ( "wifi connect to Wifi007 success" ) ;
mqttClient. setServer ( mqtt_server, mqtt_port) ;
mqttClient. setCallback ( callback) ;
randomSeed ( micros ( ) ) ;
}
# define MSG_BUFFER_SIZE ( 50 )
char msg[ MSG_BUFFER_SIZE] ;
int value = 0 ;
unsigned long lastMsg = 0 ;
void loop ( ) {
if ( ! mqttClient. connected ( ) ) {
reconnect ( ) ;
}
mqttClient. loop ( ) ;
unsigned long now = millis ( ) ;
if ( now - lastMsg > 2000 ) {
lastMsg = now;
value = random ( 1 , 50 ) ;
snprintf ( msg, MSG_BUFFER_SIZE, "%ld" , value) ;
Serial. print ( "Publish message: " ) ;
Serial. println ( msg) ;
mqttClient. publish ( "test/topic2" , msg) ;
}
}
void callback ( char * topic, byte* payload, unsigned int length) {
Serial. print ( "Message arrived [" ) ;
Serial. print ( topic) ;
Serial. print ( "] " ) ;
for ( int i = 0 ; i < length; i++ ) {
Serial. print ( ( char ) payload[ i] ) ;
}
Serial. println ( ) ;
}
void reconnect ( ) {
while ( ! mqttClient. connected ( ) ) {
Serial. print ( "Attempting MQTT connection..." ) ;
if ( mqttClient. connect ( "ESP32Client" ) ) {
Serial. println ( "connected" ) ;
mqttClient. publish ( "test/topic2" , "hello world" ) ;
mqttClient. subscribe ( "test/topic1" ) ;
mqttClient. subscribe ( "test/topic2" ) ;
} else {
Serial. print ( "failed, rc=" ) ;
Serial. print ( mqttClient. state ( ) ) ;
Serial. println ( " try again in 5 seconds" ) ;
delay ( 5000 ) ;
}
}
}