切换到宽版
  • 3227阅读
  • 3回复

[AVR]ES8266模块-网络时钟-数码管显示 [复制链接]

上一主题 下一主题
离线mckk520
 

发帖
222
M币
1613
专家
9
粉丝
33
只看楼主 倒序阅读 我要置顶 楼主  发表于: 2019-01-05
,
,

,
模块直连上网获取网络时间,并且数码管显示,加网页显示,手机可WEB重置WIFI,也可以按键呼出快连程序,扫描二维码即可连接WIFI
,
,
程序

  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <ESP8266WebServer.h>
  5. #include <FS.h>
  6. #include <ESP8266WiFiMulti.h>  
  7. #include <ESP8266HTTPClient.h>  
  8. #include <string.h>
  9. #include <ESP8266mDNS.h>
  10. #include <WiFiUdp.h>
  11. ESP8266WebServer server(80);
  12. //===========   按键及输出IO定义    =====================
  13. //TM1620模块引脚定义
  14. #define DIO   15
  15. #define CLK   4
  16. #define STB   5   //这里定义了那三个脚
  17. #define KEY1   0  //GPIO 0
  18. #define LED 2    //esp模块板载LED蓝色
  19. #define bi 12    //
  20. //==========================================
  21. int  CWD=0;
  22.   unsigned long   new_time;
  23. //===========   全局数据,定义    ========
  24.    int nowificici=0;
  25.   int  SJ_NIAN ,   SJ_YUE ,   SJ_RI ,   SJ_XQ  ;
  26.   
  27.   
  28.   int       KEY_TIME=0;
  29.   
  30.   
  31.   
  32.   bool  duan_flag=0;  
  33.   //  #define    new_time    40    //=====   每50S更新一次网络时间,
  34. #define    dao_time    10    //=====   倒计时 时间,
  35. #define    newgp_time    5    //=====   股票更新时间 S
  36.    const char* wifi_gp_data = "/wifi_gp_data.txt";
  37.   
  38.     
  39.     
  40.     int GP_DAT;   // 股票100%比  股票 正 负
  41.     unsigned char  ZFU;
  42.     unsigned char GP_ZFU;  //   股票 正 负
  43.     
  44.     
  45.        String gpmingzi = "未更新";
  46.     
  47.     
  48.     
  49.     
  50.    unsigned int   ksj=0;
  51.   unsigned char  onkey=1;
  52.     
  53.     
  54.   unsigned char  LED_SMG[] = {0,0,0,0   };
  55.     
  56. bool  SPIFFSOK=0;   // 无 FLASH 内存标志
  57. unsigned char   wh;
  58. int k;
  59. int m;
  60. int dpp;
  61. unsigned char ledd;
  62. unsigned char   autoo=0x80;
  63. const byte  tab[] = {0x5F, 0x06, 0x3B, 0x2F, 0x66, 0x6D, 0x7D, 0x47,
  64.                        0x7F, 0x6F
  65.                       };//  0-9   共阴数码管
  66. //----  WEB时间需要的数据  ------
  67. unsigned char HHH=0;
  68. unsigned char MMM=0;
  69. unsigned char SSS=0;
  70. int S1=0;
  71. int M1=0;
  72. unsigned char liangdu=141;
  73. unsigned char  HTTP_KOFF=1;
  74. int diann;
  75. int m1;
  76. int m10;
  77. int h1;
  78. int h10;
  79. int bibi;
  80.   unsigned char   dsb =0x80;
  81. unsigned char  sjh;
  82. unsigned char sjm;
  83. unsigned char sjs;
  84. unsigned long sj0=0;
  85. // int k;
  86. //int m;
  87. //int dpp;
  88. //unsigned char ledd;
  89. //unsigned char   autoo;
  90. #define APIKEY "60a806f68cc9bffe" //设置apikey    60a806f68cc9bffe
  91. //------时间 数据 NED ------
  92.     const char* ssid     = "";
  93.     const char* password = "";
  94. // 北京时间网站 http://time.beijing-time.org/time.asp   直接浏览器打开输出时间信息,
  95. //这粒模拟了浏览器,抓取需要的数据,
  96.     const char* host = "time.beijing-time.org";        // 获取 HTTP网站,的IP地址用
  97.     String url = "/time.asp";        //  获取 HTTP网站主页下的连接
  98.     
  99.     
  100.     
  101.     
  102.    //===========================
  103.   
  104.   
  105.    // 股票网站   http://hq.sinajs.cn/list=sh601360
  106.     const char* host2 = "hq.sinajs.cn";        // 获取 HTTP网站,的IP地址用
  107.     String url2 = "/list=sz300104";        //  获取 HTTP网站主页下的连接
  108.   
  109.   //===========================
  110. //-----------------
  111. const char* hostt = "led4";  // esp8266,local/
  112.     String ssid2 = "";
  113.   String pass2 = "";
  114. String  nofwifi ="未连接";
  115.   int kofff=0;
  116.   
  117. int bih1;
  118. void(* resetFunc) (void) = 0; // 需要复位时这样调用: resetFunc();
  119. int KEY_NUM = 0;            //按键键值存放变量,不等于1说明有按键按下  
  120. int KEYSJ;
  121. int g = 0;
  122. int ke = 1;
  123. //==========================================
  124. //函数声明
  125. void init_TM1620(void);
  126. void TM1620_Write( unsigned char DATA);
  127. void Write_COM(unsigned char cmd);
  128.   void SPIFFS_666(void);   //==FS文件夹系统,小程序
  129. //================================
  130. //================================
  131. //=======  小程序    ===================
  132. /** IP to String? */
  133. String toStringIp(IPAddress ip) {
  134.   String res = "";
  135.   for (int i = 0; i < 3; i++) {
  136.     res += String((ip >> (8 * i)) & 0xFF) + ".";
  137.   }
  138.   res += String(((ip >> 8 * 3)) & 0xFF);
  139.   return res;
  140. }
  141. //=============================================================
  142. bool autoConfig()   //自动连接WIFI程序, ===用上一次的WIFI密码连接
  143. {
  144.   WiFi.begin();    //======注入WIFI
  145.    for (int i = 9; i >0; i--)   // 超时 2S X 5   =  10S
  146.   {
  147.     if (  WiFi.status() == WL_CONNECTED)  // 如果相等 说明连接成功 == 就退出
  148.     {
  149.        Serial.println("===================");  
  150.       WiFi.printDiag(Serial);
  151.         Serial.println("===================");  
  152.         
  153.            digitalWrite(LED, 1);    // LED
  154.           
  155.                 autoo = 0x80;
  156.     
  157.       digitalWrite(STB,  LOW);
  158.     TM1620_Write(0xc8);     //位5  ===  4
  159.     TM1620_Write( autoo | 0x40  );      //段码
  160.     digitalWrite(STB,  HIGH);  
  161.         
  162.         
  163.         
  164.   
  165.       delay(100);          
  166.     Serial.println("==== wifi AUTO  ok =====");
  167.     
  168.           Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
  169.       Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
  170.       
  171.           Serial.print("WIFI  IP = :      ");
  172.     Serial.println(WiFi.localIP());
  173.     
  174.       WiFi.setAutoConnect(true);  // 设置自动连接===================================
  175.       return true;   //连接成功了跳过循环,退出连接程序
  176.       break;
  177.     }
  178.     else  // 连接不成功 == 一直等待连接
  179.     {
  180.       
  181.     Serial.println("==== wifi  NO =====");
  182.           digitalWrite(LED, 0);    //  LED
  183.             
  184.             
  185.              autoo = 0x00;
  186.             
  187.         
  188.       digitalWrite(STB,  LOW);
  189.     TM1620_Write(0xc8);     //位5  ===  4
  190.     TM1620_Write( autoo | 0x40  );      //段码
  191.     digitalWrite(STB,  HIGH);  
  192.             
  193.                  delay(500);
  194.         
  195.         
  196.             
  197.             
  198.                         autoo = 0x80;
  199.             
  200.         
  201.       digitalWrite(STB,  LOW);
  202.     TM1620_Write(0xc8);     //位5  ===  4
  203.     TM1620_Write( autoo | 0x40  );      //段码
  204.     digitalWrite(STB,  HIGH);  
  205.             
  206.             
  207.              delay(500);        
  208.             
  209.             
  210.             
  211.             
  212.             
  213.     }
  214.   }
  215. digitalWrite(2, 0);
  216.   Serial.println("==  wifi MIMA NO  NO ===");  
  217.   Serial.println("==== wifi  smartConfig  OPEN  99S -- =====");
  218.   return false;
  219. }
  220. void smartConfig()  //快连程序
  221. {
  222.    delay(10);
  223.    WiFi.mode(WIFI_STA);
  224.   
  225.    WiFi.beginSmartConfig();
  226.   
  227.      delay(10);
  228.   int i=99;
  229.   while (i>0)   // 99S超时
  230.   {
  231.   
  232.     
  233.     
  234.         digitalWrite(STB,  LOW);
  235.     TM1620_Write(0xC8);     // 位1       ===  1
  236.     TM1620_Write( autoo | 0x3E  );      // 段码  W
  237.     digitalWrite(STB,  HIGH);
  238.   
  239.     digitalWrite(STB,  LOW);
  240.     TM1620_Write(0xC6);     // 位2   ===  2
  241.     TM1620_Write( 0x71 );      // 段码  F
  242.     digitalWrite(STB,  HIGH);
  243.   
  244.           
  245.     digitalWrite(STB,  LOW);
  246.     TM1620_Write(0xC2);     //位3  ===  dian
  247.     TM1620_Write( tab[i/10%10] );      //段码 ledd  
  248.     digitalWrite(STB,  HIGH);  
  249.       digitalWrite(STB,  LOW);
  250.     TM1620_Write(0xC0);     //位4  ===  3
  251.     TM1620_Write(   tab[i%10]  );      //段码
  252.     digitalWrite(STB,  HIGH);  
  253.     
  254.     
  255.     
  256.     
  257.     
  258.     
  259.     
  260.     
  261.     
  262.     
  263.     
  264.     
  265.       
  266.         Serial.print("==== wifi  smartConfig SOS  SOS    ");
  267.          Serial.print(i);
  268.           Serial.println(" S  ======= ");
  269.         
  270.        digitalWrite(2,0);
  271.        delay(500);
  272.       
  273.       
  274.           if (WiFi.smartConfigDone())
  275.     {
  276.       Serial.println("===   SmartConfig OK           =========================");
  277.       
  278.       
  279.       
  280.     digitalWrite(STB,  LOW);
  281.     TM1620_Write(0xc0);     // 位1       ===  1
  282.     TM1620_Write(   0x76  );      // 段码  H
  283.     digitalWrite(STB,  HIGH);
  284.   
  285.     digitalWrite(STB,  LOW);
  286.     TM1620_Write(0xc2);     // 位2   ===  2
  287.     TM1620_Write( 0x76   );      // 段码 H
  288.     digitalWrite(STB,  HIGH);
  289.           
  290.     digitalWrite(STB,  LOW);
  291.     TM1620_Write(0xc4);     //位3  ===  dian
  292.     TM1620_Write( 0xFF );      //段码 ledd
  293.     digitalWrite(STB,  HIGH);  
  294.       
  295.   
  296.       digitalWrite(STB,  LOW);
  297.     TM1620_Write(0xc6);     //位4  ===  3
  298.     TM1620_Write( 0x76    );      //段码 H
  299.     digitalWrite(STB,  HIGH);  
  300.       digitalWrite(STB,  LOW);
  301.     TM1620_Write(0xc8);     //位5  ===  4
  302.     TM1620_Write( autoo |  0x76   );      //段码  H
  303.     digitalWrite(STB,  HIGH);  
  304.       
  305.       
  306.       
  307.       
  308.       
  309.       
  310.       
  311.       
  312.       
  313.       
  314.       
  315.       
  316.       
  317.       Serial.printf("==========SSID:%s\r\n", WiFi.SSID().c_str());
  318.       Serial.printf("==========PSW:%s\r\n", WiFi.psk().c_str());
  319.       
  320.         WiFi.mode(WIFI_AP_STA );
  321.         
  322.                   Serial.println("=======  WiFi.mode  显示模式,信道,密码 ======");  
  323.       WiFi.printDiag(Serial);
  324.         Serial.println("===================");  
  325.         
  326.       WiFi.setAutoConnect(true);  // 设置自动连接
  327.       break;
  328.     }
  329.       
  330.       
  331.       
  332.       
  333.         i--;
  334.        digitalWrite(2,1);
  335.        delay(500);   // 这个地方一定要加延时,否则极易崩溃重启
  336.     
  337.           
  338.           
  339.     //  WiFi.beginSmartConfig();
  340.   }
  341. Serial.println("==== wifi NO  NO   smartConfig  30S END  =====");
  342. }
  343. //=============================================================
  344. //===============   WEB网页送显   =================================
  345. void handleRootGet() {
  346.   
  347.   String gpsz ="/list=sz";    //深圳及创业板
  348.   String gpsh ="/list=sh";   //上证
  349.       String web_ssid = server.arg("web_ssid");          // 取回Post数据
  350.       String web_pass = server.arg("web_pass");         // 取回Post数据
  351.    String web_GP_DATA = server.arg("web_GP_DATA");       // 取回Post数据
  352.      if( web_GP_DATA.length() ==6 )  // 取回Post数据==6位 = 股票的数字
  353.    {
  354.    if( (web_GP_DATA.toInt() /100000%10 )==0)  url2 = gpsz + web_GP_DATA;  
  355.    if( (web_GP_DATA.toInt() /100000%10 )==3)  url2 = gpsz + web_GP_DATA;
  356.    if( (web_GP_DATA.toInt() /100000%10 )==6)  url2 = gpsh + web_GP_DATA;  //判断是上证
  357.   
  358.    if(  SPIFFSOK==1)     // 有FLASH 内存标志
  359.     {
  360.         File x = SPIFFS.open(wifi_gp_data, "w");  // 打开SPIFFS文件系统内指定的文件
  361.              x.println(url2);    // 写入文件,
  362.              x.close();                 // 上锁 SPIFFS
  363.      }
  364.   
  365.    }
  366.      if( web_ssid.length() >=4 )  // 取回Post数据 = WIFI,AP名字大于2位数,
  367.    {
  368.       WiFi.begin(web_ssid.c_str(), web_pass.c_str());  //重新注入WIFI,修改WIFI
  369.    }
  370. //======  WEB HTTP  网页输出 代码  =============
  371. String html = "";
  372.    html += "<head> ";
  373.    html += "<meta name='viewport' content='width=device-width,initial-scale=1' charset='utf-8' />  ";
  374.     html += "<title>ESP8266_LED4设置页面</title> ";
  375.     html += "</head>";
  376.       html += "<body align='center' style='background-color:#d3d388'>";
  377.   html += "<h2>设置</h2>";
  378.   
  379.   
  380.      html +=" <p style='color:#ff04fe'>";
  381.        html +="联网状态=: ";
  382.   html +=String() +  nofwifi +"<br>";
  383.   
  384.   
  385.   // Serial.println(WiFi.localIP());    //获取本地IP
  386.   
  387.           
  388.      html += String()  + toStringIp(WiFi.localIP()) + "<br>";
  389.   
  390.     
  391.       
  392.          html +="<br>";
  393.       
  394.         html +="北京时间: ";
  395.         
  396.   html += String()+ SJ_NIAN   ;
  397.      html +="年";
  398.       
  399.     
  400.             html += String()+ SJ_YUE    ;
  401.          html +="月";
  402.         html += String()+ SJ_RI    ;
  403.          html +="日";
  404.           html +="/星期";
  405.         html += String()+ SJ_XQ   ;
  406.       
  407.    html += "<br>";
  408.         html += String()+ HHH   ;
  409.          html +="点";
  410.           html +="/";
  411.         html += String()+ MMM   ;
  412.          html +="分";
  413.      html +="/--";
  414.         html += String()+ SSS    ;
  415.          html +="秒";
  416.         
  417.   
  418.    html += "<br>";
  419.   
  420.   
  421.   
  422.   
  423.   
  424.    html +="</p>";
  425.   
  426.   
  427.    html +=" <p>重新连接WIFI网络..</p>";
  428.   html += "<form method='post' action='wifisave'  >";
  429.   html += " <p>WIFI名称<input type='text' name='web_ssid' placeholder='ssid'></p>";
  430.   html += " <p>WIFI密码<input type='text' name='web_pass' placeholder='pass'></p>";
  431.   html += "  <input type='submit' value='保存'>";
  432.   html += "</form>";
  433. /***
  434.   html +="<h1>输入股票代码<br>";
  435.    html += "<form method='post' action='wifisave'>";
  436.    html += "<input type='text' maxlength='6' name='web_GP_DATA'><input type='submit' value='确定' />";
  437.     html += "</form>";
  438.    html +="</h1>";
  439.   
  440.      html +=" <p>六位股票数字<br></p>";
  441.   html +="<br>";
  442.   
  443.   
  444.   ****/
  445.   
  446.     html +="<br>";
  447.    html +=" <a href='/'target='_top'>刷新主页</a> ";
  448.     
  449.        html +=" <p>输入正确的WIFI密码,保存后</p>";
  450.   html +=" <p>重启终端,即可更新使用</p>";
  451.     
  452.     
  453.   
  454.   
  455. html +=" <p>BY:WANGYU-TEL:15858013830</p>";
  456.    html +="</body>";
  457.   server.send(200, "text/html", html);
  458.   
  459. }
  460. //======================================
  461.                       
  462.                       
  463.                       
  464. void TM1620_Write( unsigned char DATA)                        //写数据函数
  465. {
  466.    unsigned char i;
  467.   
  468.   for (i = 0; i < 8; i++)
  469.   {
  470.     digitalWrite(CLK, LOW);
  471.      if(DATA & 0X01)
  472.      {
  473.      digitalWrite(DIO, HIGH);
  474.      }
  475.      else
  476.      {
  477.        digitalWrite(DIO, LOW);
  478.      }
  479.     digitalWrite(CLK, HIGH);
  480.     DATA>>=1;
  481.   }
  482. }
  483. void Write_COM(unsigned char cmd)                //发送命令字
  484. {
  485.   digitalWrite(STB,  LOW);
  486.   TM1620_Write(cmd);
  487.   digitalWrite(STB, HIGH);
  488. }
  489. //TM1620初始化函数
  490. void init_TM1620(void)
  491. {
  492.     
  493.     digitalWrite(STB,  LOW);
  494.   TM1620_Write(0x02);
  495.   digitalWrite(STB, HIGH);
  496.     digitalWrite(STB,  LOW);
  497.   TM1620_Write(0x44);
  498.   digitalWrite(STB, HIGH);
  499.   
  500.      digitalWrite(STB,  LOW);
  501.     TM1620_Write(liangdu);              // 亮度 (0x88-0x8f)8级亮度可调 137-143
  502.     digitalWrite(STB, HIGH);
  503.     digitalWrite(STB,  LOW);
  504.     TM1620_Write(0xC0);     // 位1       ===  1
  505.     TM1620_Write( 0x20);      // 段码
  506.     digitalWrite(STB,  HIGH);
  507.   
  508.     digitalWrite(STB,  LOW);
  509.     TM1620_Write(0xc2);     // 位2   ===  2
  510.     TM1620_Write( 0x20  );      // 段码
  511.     digitalWrite(STB,  HIGH);
  512.           
  513.     digitalWrite(STB,  LOW);
  514.     TM1620_Write(0xc4);     //位3  ===  dian
  515.     TM1620_Write(  0xFF );      //段码 ledd
  516.     digitalWrite(STB,  HIGH);  
  517.       
  518.   
  519.       digitalWrite(STB,  LOW);
  520.     TM1620_Write(0xc6);     //位4  ===  3
  521.     TM1620_Write( 0x20 );      //段码
  522.     digitalWrite(STB,  HIGH);  
  523.       digitalWrite(STB,  LOW);
  524.     TM1620_Write(0xc8);     //位5  ===  4
  525.     TM1620_Write( autoo | 0x20  );      //段码
  526.     digitalWrite(STB,  HIGH);  
  527.     
  528. }
  529. //================================
  530. //================================
  531. //=====   setup() //初始化   =====
  532. //================================
  533. //================================
  534. void setup() //初始化函数
  535. {
  536.    pinMode( bi, OUTPUT);
  537.     pinMode(LED, OUTPUT);
  538.   
  539.    pinMode(5,INPUT_PULLUP);    //定义GPIO  5 为带上拉输入引脚  
  540.     //  pinMode(3,INPUT_PULLUP);    //定义GPIO  3为带上拉输入引脚  
  541.    digitalWrite(LED, 0);
  542.   
  543.   
  544.     delay(100);  
  545. digitalWrite(bi, 0);
  546.   unsigned long koff=100000;
  547. while(koff>1)   //RXD没有低电平后延时0.3S退出
  548. {
  549. if(digitalRead(5) == 0)   //串口有低电平信号,
  550. koff=100000;
  551. delayMicroseconds(7); //9us
  552. koff--;
  553. }
  554. digitalWrite(LED, 1);
  555.   
  556.   
  557.   
  558.   
  559.   
  560.   
  561.   
  562.   
  563.   
  564.   
  565.   
  566.   
  567.   delay(100); //
  568.    Serial.begin(115200);
  569.     delay(100); //
  570. Serial.println("");
  571. Serial.println("");
  572.     
  573.      Serial.println("==== ESP8266 OK   =====");
  574.           SPIFFS.begin();
  575.   
  576.   
  577.   
  578. pinMode(KEY1,INPUT_PULLUP);    //定义KEY1为带上拉输入引脚  
  579.   
  580.   
  581.     pinMode(LED, OUTPUT);
  582.   
  583.   
  584.   pinMode(STB, OUTPUT);
  585.   pinMode(CLK, OUTPUT);
  586.   pinMode(DIO, OUTPUT); //让三个脚都是输出状态
  587.   
  588.     
  589.     digitalWrite(LED, 0);
  590.   
  591.     
  592.      digitalWrite(CLK, HIGH);
  593.       digitalWrite(STB, HIGH);
  594.        digitalWrite(DIO, HIGH);
  595.     
  596.    init_TM1620();     //初始化TM1638
  597.   
  598.       digitalWrite(bi, 0);
  599.   delay(10);
  600. //================
  601.   
  602.      //    WiFi.mode(WIFI_STA);    
  603.          WiFi.mode(WIFI_AP_STA );   // 必须采用 AP 与 Station 兼容模式
  604.     delay(100); // 等待配网
  605. WiFi.begin();    //======注入WIFI
  606.   delay(100);
  607.     
  608.      //    autoConfig();
  609.     
  610.     WiFi.setAutoConnect(true);  // 设置自动连接
  611.    //      if ( !autoConfig() )     //如自动连接不成功超时20次,就进入手动配网
  612.    //      {
  613.     
  614.     //       smartConfig();   //手动快连程序
  615.    //       }
  616.   
  617. delay(10);
  618.     //      WiFi.mode(WIFI_AP_STA );   // 必须采用 AP 与 Station 兼容模式
  619. delay(10);
  620.           Serial.println("=======  WiFi.mode  ALL=:   ");  
  621.       WiFi.printDiag(Serial);
  622.         Serial.println("===================");  
  623.     delay(1000);
  624.   
  625.      // ===============设置内网ap
  626. // ===============设置内网ap
  627. IPAddress softLocal(192,168,4,1); // DNS
  628. IPAddress softGateway(192,168,4,2); //AP
  629. IPAddress softSubnet(255,255,255,0);
  630. WiFi.softAPConfig(softLocal, softLocal, softSubnet);
  631. //String apName = ("AP168_4_1_"+(String)ESP.getChipId()); // 2 设置WIFI名称
  632. String apName = "LED4_192_168_4_1";
  633. const char *softAPName = apName.c_str();
  634. WiFi.softAP(softAPName); // 3创建wifi 名称 +密码 adminadmin
  635. IPAddress myIP = WiFi.softAPIP(); // 4输出创建的WIFI IP地址
  636.   
  637.   
  638.   
  639.   
  640.   
  641.   while (WiFi.status() != WL_CONNECTED) {
  642.     delay(500);
  643.     Serial.println("===  wifi  no  no  ===========");
  644.   }
  645.   Serial.println("===  wifi  ok  ok    ===========");
  646.    Serial.println("IP address: ");   //打印下行
  647. Serial.println(WiFi.localIP());    //获取本地IP
  648.   
  649.   
  650.    Serial.print("== WIFI IP =:   ");
  651.   Serial.println(WiFi.localIP());
  652.   
  653. IPAddress ip = WiFi.localIP();
  654.   
  655.   if (!MDNS.begin(hostt, myIP)) {
  656.     Serial.println("Error setting up MDNS responder!");
  657.     while(1) {
  658.       delay(1000);
  659.     
  660.     }
  661.   }
  662.   Serial.println("mDNS responder started");
  663.   
  664.   
  665.   Serial.println("");
  666.   Serial.print("Connected! IP address: ");
  667.   Serial.println(WiFi.localIP());
  668.   MDNS.begin(hostt);
  669.   
  670.    delay(500);
  671.   
  672.   
  673.       server.on("/", HTTP_GET, handleRootGet);
  674.        server.on("/wifisave", HTTP_POST, handleRootGet);
  675.   
  676.   server.begin();
  677.   
  678.   
  679.     MDNS.setInstanceName("Cubie's ESP8266");
  680.   MDNS.addService("http", "tcp", 80);
  681.   
  682.   
  683.   
  684.   
  685.   //   WiFi.begin(ssid, password);
  686.       Serial.println();
  687.       Serial.println(" ============  SPIFFS_666 ==================   ");
  688.       
  689.       delay(500);
  690.         
  691.         
  692.         
  693.         SPIFFS_666();
  694.         
  695.         
  696.         
  697.     
  698.       digitalWrite(LED, 1);
  699.    delay(500);
  700. //=  =============
  701.    dpp= 0x80;
  702.   
  703.    m= 1;
  704.    k=0;
  705.   
  706.              HTTP_KOFF=1;
  707.     autoo=0x80;       //auto字符
  708.     
  709.        digitalWrite(bi, 0);
  710.    S1=0;
  711.   
  712.    HHH=23;
  713.    MMM=58;
  714.    SSS=55;
  715.   
  716.     new_time=10;
  717.   
  718. }
  719. //================================
  720. //================================
  721. //=====   loop() //主函数    =====
  722. //================================
  723. //================================
  724. void loop() //主函数
  725. {
  726.         //  ====  路由器连接状态判断
  727.   if (WiFi.status() == WL_CONNECTED )    nofwifi ="连接OK";
  728.   else  nofwifi ="未连接";
  729.   
  730.   
  731.   
  732.    server.handleClient();
  733.   
  734.     
  735.     
  736.     
  737.     
  738.     
  739.     
  740.       
  741.     
  742.         if(digitalRead(KEY1) == LOW)   //有按键按下  
  743.   {  
  744.        delay(1);  
  745.    KEY_TIME++;
  746.     digitalWrite(LED, 0);
  747.     if( KEY_TIME <5000 ) duan_flag=1;
  748.     
  749.      if( KEY_TIME >5000  )
  750.   {
  751.       digitalWrite(LED,1);
  752.    duan_flag=0;
  753.     smartConfig();  //快连程序
  754.       digitalWrite(LED, 1);
  755.   }
  756.   
  757.   
  758.   }
  759.   
  760.   else
  761.   {
  762.    KEY_TIME=0;
  763. if( duan_flag==1)
  764.   {
  765.       digitalWrite(LED, 1);
  766.     new_time=0;
  767. duan_flag=0;
  768.     digitalWrite(LED, 1);
  769.   }
  770.   
  771. }
  772.     
  773.     
  774.    //   while(digitalRead(KEY1) == LOW)
  775.     
  776. //  digitalWrite(LED, 1);
  777.   
  778.   
  779.   
  780.   
  781.             
  782.         unsigned long sj1=millis();   // 取得当前时间数
  783.       if( sj1 - sj0 > 400)  ledd=0x00;       //双点
  784.       if( sj1 - sj0 > 1000)  // 1s 当前时间数大于前一个时间1S间隔
  785.   {
  786.     sj0 = sj1;
  787.      ledd=0x06;       //双点
  788.     S1++;
  789.     
  790.     if(S1==1)  digitalWrite(bi, 0);
  791.     
  792.     
  793.     if(S1>= new_time )  //=====   每10S更新一次
  794.     {
  795.       S1=0;
  796.       
  797.        digitalWrite(LED, 1);
  798.       
  799.         Serial.println("=======  GET OPEN ===========");
  800.        //    digitalWrite(bi, 1);
  801.     
  802.          int code_now = getWeatherCode();
  803.           
  804.           if (code_now == -1)
  805.            {
  806.              nowificici++;
  807.             
  808.                     Serial.print("====  nowificici  =;      ");
  809.                     Serial.println(nowificici);
  810.                     
  811.                     
  812.                     
  813.                if(nowificici>=50)
  814.                {
  815.                 
  816.                   Serial.println("========== NO WIFI  50CI  ==========");
  817.                 
  818.                     
  819.                      new_time = 8280 ;   //  23  h
  820.                     
  821.                  nowificici=0;
  822.                   HTTP_KOFF=0;
  823.                   
  824.                    S1=0;
  825.                }
  826.                delay(10);
  827.             
  828.            }
  829.           
  830.         else     //  OK  WIFI  
  831.         
  832.           {
  833.           HHH = sjh;
  834.           MMM = sjm;
  835.           SSS = sjs;
  836.           
  837.             new_time = 3000 ;   // 50  M
  838.            S1=0;
  839.           
  840.           }
  841.       
  842.     //    getWeatherCode99();
  843.      //   getWeatherCodepm25();
  844.         
  845.         
  846.         
  847.   //  getWeatherCode99();
  848. //     getWeatherCode9922();
  849.       //  getWeatherCodeled3();
  850.         
  851.       
  852.     }
  853.     
  854.     
  855.     SSS++;
  856.     if(SSS>=60)  {  SSS=0; MMM++;   }
  857.     if(MMM>=60)  {  MMM=0; HHH++;   }
  858.     if(HHH>=24)  {  HHH=0;   }
  859.     
  860.       LED_SMG[0]= tab[MMM%10 ];
  861.      LED_SMG[1]= tab[MMM/10%10 ];
  862.     
  863.      LED_SMG[2]= tab[HHH%10 ];
  864.      LED_SMG[3]= tab[HHH/10%10 ];
  865.     
  866.     
  867.   }
  868.   
  869.   
  870.   
  871.     
  872.   
  873.   
  874.     
  875.     digitalWrite(STB,  LOW);
  876.     TM1620_Write(0xc0);     // 位1       ===  1
  877.     TM1620_Write(   LED_SMG[0]  );      // 段码
  878.     digitalWrite(STB,  HIGH);
  879.   
  880.     digitalWrite(STB,  LOW);
  881.     TM1620_Write(0xc2);     // 位2   ===  2
  882.     TM1620_Write(  LED_SMG[1]  );      // 段码
  883.     digitalWrite(STB,  HIGH);
  884.           
  885.     digitalWrite(STB,  LOW);
  886.     TM1620_Write(0xc4);     //位3  ===  dian
  887.     TM1620_Write(  ledd | 0x88 );      //段码 ledd
  888.     digitalWrite(STB,  HIGH);  
  889.       
  890.   
  891.       digitalWrite(STB,  LOW);
  892.     TM1620_Write(0xc6);     //位4  ===  3
  893.     TM1620_Write(  LED_SMG[2] );      //段码
  894.     digitalWrite(STB,  HIGH);  
  895.       digitalWrite(STB,  LOW);
  896.     TM1620_Write(0xc8);     //位5  ===  4
  897.     TM1620_Write( autoo |  LED_SMG[3]  );      //段码
  898.     digitalWrite(STB,  HIGH);  
  899.   
  900.   
  901.   
  902.   
  903.   
  904.   
  905.   
  906.     
  907.    delay(1);  
  908.   
  909. }
  910. //====================================================================================
  911.   //=================        end           =================================
  912.   
  913.   
  914.   
  915.   
  916.      void SPIFFS_666(void)   //==FS文件夹系统,小程序
  917.           
  918.           {
  919.   bool ok = SPIFFS.begin();   //检查 FS文件夹系统,如果失败,返回0
  920.   if (ok)    //检查到 有内存的模块,可以执行读出 数据
  921.     {
  922.     
  923.      SPIFFSOK=1;    // 有FLASH 内存标志
  924.   File f = SPIFFS.open(wifi_gp_data, "r");
  925.    url2  = f.readStringUntil('\n');
  926.     f.close();
  927.    url2.trim();  
  928.   }
  929.   else  SPIFFSOK=0;   // 无 FLASH 内存标志
  930.   
  931.           }
  932.     
  933.   
  934. // ============HTTP 小程序=====================================================================
  935. int   getWeatherCode(){
  936.     
  937.         
  938. // 北京时间网站 http://time.beijing-time.org/time.asp   直接浏览器打开输出时间信息,
  939. //这粒模拟了浏览器,抓取需要的数据,
  940.     const char* host1 = "time.beijing-time.org";        // 获取 HTTP网站,的IP地址用
  941.     String url1 = "/time.asp";        //  获取 HTTP网站主页下的连接
  942.   WiFiClient client;
  943.       
  944.       //----------------- http to tcp
  945.    //  
  946.       const int httpPort = 80;
  947.       if (!client.connect(host1, httpPort)) {
  948.         return -1;
  949.       }
  950.       client.print(String("GET ") + url1 + " HTTP/1.1\r\n" +
  951.                    "Host: " + host1 + "\r\n" +
  952.                    "Connection: close\r\n\r\n");
  953.       delay(150);
  954.       
  955.       String str = "";
  956.       while(client.available()){
  957.         String line = client.readStringUntil('\r');
  958.         str += line;
  959.     
  960.       }
  961.     
  962.     //     Serial.println(str);  OK
  963.     
  964.         String sjall_data ="";
  965.     
  966.       String new_data ="";
  967.         String       STSJS ="";
  968.        String STSJM ="";
  969.         String STSJH ="";
  970.     
  971.      //    new_data =   str.substring(   str.indexOf("nyear")+1 ,    str.length()     ) ;
  972.        // ========================分割字符, "nyear"开始-到结束,只需要这段字符
  973.       
  974.           //      t0=new Date().getTime(); nyear=     2019; nmonth=1; nday=1; nwday=2; nhrs=14; nmin=15; nsec=54;
  975.                 
  976.                 
  977.                 
  978.        sjall_data =   str.substring(   str.indexOf("getTime")+10 ,    str.length()     ) ;
  979.       
  980.           if( sjall_data.length() <5 )  return -1;  //
  981.       new_data =   str.substring(   str.indexOf("nyear")+6 ,    str.length()     ) ;   // "nyear" 0 -- end,
  982.       
  983.      SJ_NIAN =   ( new_data.substring(  0, new_data.indexOf(":")  )   ) .toInt();    //    年获取
  984.     
  985.          new_data =   str.substring(   str.indexOf("nmonth")+7 ,    str.length()     ) ;
  986.            SJ_YUE =   ( new_data.substring(  0, new_data.indexOf(":")  )   ) .toInt();    //   月
  987.           
  988.                 new_data =   str.substring(   str.indexOf("nday")+5 ,    str.length()     ) ;
  989.                     SJ_RI =   ( new_data.substring(  0, new_data.indexOf(":")  )   ) .toInt();    // 日
  990.                     
  991.                         new_data =   str.substring(   str.indexOf("nwday")+6 ,    str.length()     ) ;
  992.                            SJ_XQ =   ( new_data.substring(  0, new_data.indexOf(":")  )   ) .toInt();    // 星期
  993. // 时
  994.   //分
  995. // 秒
  996.       
  997.       
  998.         
  999.   
  1000.        new_data =   new_data.substring(  new_data.indexOf("nhrs")+5 ,    new_data.length()     ) ;
  1001.       
  1002.   
  1003.       
  1004.       
  1005.   
  1006.      STSJH =   new_data.substring(  0, new_data.indexOf(":")  ) ;   // hhhhhhhhhhhhhhhhhhhhhhhhhhh
  1007.   
  1008.   
  1009.   
  1010.    new_data =   new_data.substring(  new_data.indexOf("nmin")+5 ,    new_data.length()     ) ;
  1011.   
  1012.     STSJM =   new_data.substring(  0, new_data.indexOf(":")  ) ;        //mmmmmmmmmmmmmmmmmmmmmmmmm
  1013.     
  1014.     
  1015.        new_data =   new_data.substring(  new_data.indexOf("nsec")+5 ,    new_data.length()     ) ;
  1016.       
  1017.       STSJS =   new_data.substring(  0, new_data.indexOf(":")  ) ;        // sssssmmmmmmmmmmmmsssssss
  1018.     
  1019.   
  1020.           
  1021. // ===================字符转INT,如果字符是纯数字,转换后的INT值就可以赋值给数码管使用,    sjm sjh
  1022.       sjm  =  STSJM.toInt();
  1023.       sjh  =  STSJH.toInt();
  1024.       sjs =  STSJS.toInt()+6;
  1025. Serial.println("====  SJ=;      ");
  1026. Serial.println(sjm);
  1027.           
  1028.       return  sjm;
  1029.     }



本文内容包含图片或附件,获取更多资讯,请 登录 后查看;或者 注册 成为会员获得更多权限
离线dujq

发帖
427
M币
481
专家
4
粉丝
51
只看该作者 1楼 发表于: 2019-01-05
请登录后查看
离线devcang

发帖
18104
M币
9149
专家
20
粉丝
97
只看该作者 2楼 发表于: 2019-01-06
回 dujq 的帖子
请登录后查看

发帖
767
M币
291
专家
3
粉丝
28
只看该作者 3楼 发表于: 2019-01-06
请登录后查看
快速回复
限80 字节
温馨提示:欢迎交流讨论,请勿发布纯表情、纯引用等灌水帖子;以免被删除
 
上一个 下一个