数码之家

 找回密码
 立即注册
搜索
查看: 1916|回复: 19

用chatgpt简单写了一个esp8266控制点阵显示时间的程序

[复制链接]
发表于 2023-2-15 10:33:12 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用arduino ide写一个esp8266的程序。从wifi获取网络时间,东8区。esp8266使用spi协议max7219,4位8*8点阵,每秒钟刷新一下当前时间。按一个按钮显示当前日期,再按一下显示时间
以下是一个基于 Arduino IDE 的 ESP8266 程序,用于获取网络时间,并将其显示在 4 个 8x8 LED 点阵上。同时,按下一个按钮可以显示当前日期,再按一次可以显示时间。
在此代码中,我们首先引用所需的库,然后设置了ESP8266的SPI接口并初始化了Max7219。 `setup()`函数设置了ESP8266的WiFi连接,并使用`getNtpTime()`函数获取当前的网络时间。`loop()`函数用于每秒钟刷新一次时间,同时检查按钮状态以切换日期和时间的显示。 `printDigits()`函数用于将整数转换为两位数并在Max7219上显示。 `setTimeZone()`函数用于设置时区偏移,然后我们使用`getNtpTime()`函数从NTP服务器获取时间,并将其转换为本地时间。
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3. #include <SPI.h>
  4. #include <LedControl.h>
  5. #include <Time.h> // time library header

  6. // Replace with your network credentials
  7. const char* ssid = "your_SSID";
  8. const char* password = "your_PASSWORD";

  9. // NTP Servers:
  10. static const char ntpServerName[] = "pool.ntp.org";
  11. const int timeZone = 8;     // Beijing Time
  12. WiFiUDP Udp;
  13. unsigned int localPort = 8888;  // local port to listen for UDP packets

  14. time_t getNtpTime();
  15. void digitalClockDisplay();
  16. void printDigits(int digits);

  17. // Initialize the MAX7219 display
  18. const int MAX7219_PIN_CS = 2;
  19. const int MAX7219_PIN_DIN = 3;
  20. const int MAX7219_PIN_CLK = 4;
  21. const int MAX7219_NUM_DEVICES = 4;

  22. LedControl max7219 = LedControl(MAX7219_PIN_DIN, MAX7219_PIN_CLK, MAX7219_PIN_CS, MAX7219_NUM_DEVICES);

  23. // Initialize the button
  24. const int BUTTON_PIN = 5;
  25. int buttonState = HIGH;
  26. int lastButtonState = HIGH;
  27. unsigned long lastDebounceTime = 0;
  28. unsigned long debounceDelay = 50;
  29. bool showDate = false;

  30. void setup() {
  31.   // Initialize serial communication
  32.   Serial.begin(115200);

  33.   // Initialize the MAX7219 display
  34.   max7219.shutdown(0, false);
  35.   max7219.setIntensity(0, 8);
  36.   max7219.clearDisplay(0);
  37.   max7219.shutdown(1, false);
  38.   max7219.setIntensity(1, 8);
  39.   max7219.clearDisplay(1);
  40.   max7219.shutdown(2, false);
  41.   max7219.setIntensity(2, 8);
  42.   max7219.clearDisplay(2);
  43.   max7219.shutdown(3, false);
  44.   max7219.setIntensity(3, 8);
  45.   max7219.clearDisplay(3);

  46.   // Initialize the button
  47.   pinMode(BUTTON_PIN, INPUT);

  48.   // Connect to Wi-Fi network
  49.   Serial.println();
  50.   Serial.println();
  51.   Serial.print("Connecting to ");
  52.   Serial.println(ssid);
  53.   WiFi.begin(ssid, password);
  54.   while (WiFi.status() != WL_CONNECTED) {
  55.     delay(500);
  56.     Serial.print(".");
  57.   }
  58.   Serial.println("");
  59.   Serial.println("WiFi connected");

  60.   // Initialize the UDP client
  61.   Udp.begin(localPort);

  62.   // Set the time zone
  63.   setTimeZone(timeZone);
  64. }

  65. void loop() {
  66.   // Check if the button is pressed
  67.   int reading = digitalRead(BUTTON_PIN);
  68.   if (reading != lastButtonState) {
  69.     lastDebounceTime = millis();
  70.   }
  71.   if ((millis() - lastDebounceTime) > debounceDelay) {
  72.     if (reading != buttonState) {
  73.       buttonState = reading;
  74.       if (buttonState == LOW) {
  75.         showDate = !showDate;
  76.       }
  77.     }
  78.   }
  79.   lastButtonState = reading;

  80.   // Get the current time from the NTP server
  81.   time_t t = getNtpTime();
  82.   if (t != 0) {
  83.     // Convert the time to local time
  84.     t += timeZone * 3600;

  85.     // Display the date or time
  86.     if (showDate) {
  87.       int year = year(t);
  88.       int month = month(t);
  89.       int day = day(t);
  90.       max7219.clearDisplay(0);
  91.       max7219.clearDisplay(1);
  92.       max7219.clearDisplay(2);
  93.       max7219.clearDisplay(3);
  94.       max7219.setChar(0, 0, '2');
  95.       max7219.setChar(0, 1, '0');
  96.       printDigits(year % 100);
  97.       max7219.setChar(0, 4, '/');
  98.       printDigits(month);
  99.       max7219.setChar(0, 6, '/');
  100.       printDigits(day);
  101.     } else {
  102.       int hour = hour(t);
  103.       int minute = minute(t);
  104.       int second = second(t);
  105.       max7219.clearDisplay(0);
  106.       max7219.clearDisplay(1);
  107.       max7219.clearDisplay(2);
  108.       max7219.clearDisplay(3);
  109.       printDigits(hour);
  110.       max7219.setChar(0, 2, ':');
  111.       printDigits(minute);
  112.       max7219.setChar(0, 5, ':');
  113.       printDigits(second);
  114.     }

  115.   }
  116.   
  117.   // Wait for one second
  118.   delay(1000);
  119. }

  120. void printDigits(int digits) {
  121.   // utility function for digital clock display: prints preceding colon and leading 0
  122.   max7219.setChar(0, 7, '0' + digits % 10);
  123.   digits /= 10;
  124.   max7219.setChar(0, 6, '0' + digits % 10);
  125. }
  126. void setTimeZone(int timeZone) {
  127.   // Set the time zone offset in seconds
  128.   TimeChangeRule myDST = {"DST", Last, Sun, Mar, 2, 60}; // Daylight Saving Time rule
  129.   TimeChangeRule mySTD = {"STD", Last, Sun, Nov, 2, 0}; // Standard Time rule
  130.   Timezone myTZ(myDST, mySTD);
  131.   TimeChangeRule *tcr;
  132.   tcr = &myDST;
  133.   myTZ.setDST(tcr);
  134.   tcr = &mySTD;
  135.   myTZ.setSTD(tcr);
  136.   myTZ.setStartWeekday(Sunday);
  137.   myTZ.setStartWeek(1);
  138.   myTZ.setEndWeekday(Sunday);
  139.   myTZ.setEndWeek(1);
  140.   myTZ.setStartMinute(0);
  141.   myTZ.setEndMinute(0);
  142.   TimeChangeRule dstStart = myTZ.getDSTStartRule();
  143.   TimeChangeRule stdStart = myTZ.getSTDStartRule();
  144.   Timezone::setCache(dstStart, stdStart);
  145.   Timezone::setUtcOffset(timeZone * 3600);
  146. }

  147. time_t getNtpTime() {
  148.   // Get the time from the NTP server
  149.   while (Udp.parsePacket() > 0) ; // discard any previously received packets
  150.   Serial.println("Transmit NTP Request");
  151.   byte packetBuffer[48]; // NTP requests are to port 123
  152.   memset(packetBuffer, 0, 48);
  153.   packetBuffer[0] = 0b11100011; // LI, Version, Mode
  154.   packetBuffer[1] = 0; // Stratum, or type of clock
  155.   packetBuffer[2] = 6; // Polling Interval
  156.   packetBuffer[3] = 0xEC; // Peer Clock Precision
  157.   packetBuffer[12] = 49;
  158.   packetBuffer[13] = 0x4E;
  159.   packetBuffer[14] = 49;
  160.   packetBuffer[15] = 52;
  161.   Udp.beginPacket(ntpServerName, 123); // NTP requests
  162.   Udp.write(packetBuffer, 48); // send an NTP request
  163.   Udp.endPacket();
  164.   
  165.   uint32_t startTime = millis();
  166.   while (millis() - startTime < 1000) {
  167.     int size = Udp.parsePacket();
  168.     if (size >= 48) {
  169.       Udp.read(packetBuffer, 48); // read the packet into the buffer
  170.       uint32_t highWord = word(packetBuffer[40], packetBuffer[41]);
  171.       uint32_t lowWord = word(packetBuffer[42], packetBuffer[43]);
  172.       uint32_t secsSince1900 = highWord << 16 | lowWord;
  173.       const uint32_t seventyYears = 2208988800UL;
  174.       uint32_t epoch = secsSince1900 - seventyYears;
  175.       Serial.print("Received NTP Response: ");
  176.       Serial.println(epoch);
  177.       return epoch;
  178.       }
  179.   }
  180.   Serial.println("No NTP Response :-(");
  181.   return 0; // return 0 if unable to get the time
  182. }
复制代码



结论:能理解语义,写的程序还算能用,需要稍微的修改一下。
发表于 2023-2-15 10:51:43 | 显示全部楼层
程序员要下岗了
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2023-2-15 11:23:33 | 显示全部楼层
让它写一段能自动识别JPG图像中的文字和字符的程序可以么?
回复 支持 反对

使用道具 举报

发表于 2023-2-15 11:30:28 | 显示全部楼层
chatgpt就是个天才机器人
回复 支持 反对

使用道具 举报

发表于 2023-2-15 11:37:17 | 显示全部楼层
这种自动写出来的代码有bug吗?除虫方便吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-15 11:43:45 来自手机浏览器 | 显示全部楼层
waffle 发表于 2023-2-15 11:37
这种自动写出来的代码有bug吗?除虫方便吗?

大概意思都对,小毛病有一些,改改不麻烦
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-15 11:44:06 来自手机浏览器 | 显示全部楼层
vip2128 发表于 2023-2-15 11:30
chatgpt就是个天才机器人

数据有点老,新东西很少
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-15 11:44:34 来自手机浏览器 | 显示全部楼层
高级会员 发表于 2023-2-15 11:23
让它写一段能自动识别JPG图像中的文字和字符的程序可以么?

回头试试,ocr应该不难,识别度要自己调
回复 支持 反对

使用道具 举报

发表于 2023-2-15 13:25:33 | 显示全部楼层
再说一句让它优化一下,更简捷一些,它会做的更好的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-15 14:06:14 来自手机浏览器 | 显示全部楼层
kindzhon 发表于 2023-2-15 13:25
再说一句让它优化一下,更简捷一些,它会做的更好的。

像极了程序员
回复 支持 反对

使用道具 举报

发表于 2023-2-22 10:59:37 | 显示全部楼层
这么强大?能跑吗?有没有bug
回复 支持 反对

使用道具 举报

发表于 2023-2-23 18:52:13 | 显示全部楼层
未来定义高手的技能之一就是会让AI高效工作
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-23 18:58:02 来自手机浏览器 | 显示全部楼层
721236042 发表于 2023-2-22 10:59
这么强大?能跑吗?有没有bug

要改一改才能跑,功能上凑合能用
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-23 18:58:27 来自手机浏览器 | 显示全部楼层
pcdoctor 发表于 2023-2-23 18:52
未来定义高手的技能之一就是会让AI高效工作

程序员一直都是这样的。让机器干活
回复 支持 反对

使用道具 举报

发表于 2023-4-25 14:10:54 来自手机浏览器 | 显示全部楼层
gpt如果问点冷门的程序问题,它还是解决不了的
回复 支持 反对

使用道具 举报

发表于 2023-5-13 10:21:02 来自手机浏览器 | 显示全部楼层
人工智能对生产力的提升是巨大的,让以前很大一部分人只有想法没有专业学习知识的人,一秒钟就拥有了很多专业知识。等于说无形中让你一生中学习的时间延长了好多年。
回复 支持 反对

使用道具 举报

发表于 2023-7-26 08:11:01 | 显示全部楼层
有点像搜索引擎得扩展版,把以往传统的关键字搜索功能,帮你做了筛选并经过问过相同问题的很多人的验证
回复 支持 反对

使用道具 举报

发表于 2023-7-28 09:39:30 | 显示全部楼层
代码都不用敲了,只需看代码与小改参数,爽
回复 支持 反对

使用道具 举报

发表于 2023-7-29 09:14:03 | 显示全部楼层
程序员真的要下岗了,前几天想弄个判断信号频率然后对数据做软增益的代码,chatgpt上生成了一下,也是干的不错,不过调优还得有经验的人来弄。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-3-29 18:34 , Processed in 0.218400 second(s), 11 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

快速回复 返回顶部 返回列表