EPS8266 Wemos D1 Mini-自动重启

时间:2020-05-02 18:01:41

标签: arduino esp8266

我已经制作了一个简单的草图,其中显示了我的订阅者人数,并且可以正常使用。我为此准备了一个木盒子,所以我将其与内部的Wemos拧在一起。当我断开USB和弦并将其重新插入草图时,似乎重新启动,但点矩阵上未显示任何内容。但是,如果按复位按钮,它将完全重新启动,并且Matrix再次显示数字。

当我运行Arduino串行监视器并拔出并重新插入时,它会运行草图,但矩阵上没有任何显示。进了我的脑袋。

我不确定代码是否会有所帮助,但是我当然可以在需要时发布它。

问题:重新连接电源后,是否有任何方法可以编写完全重新启动的代码?由于它将被卡在一个木盒子里,所以如果在重新打开电源后重新启动,那就太好了,所以我不必每次都打开盒子并按下复位键。

感谢您的帮助。谢谢!

代码:

// Libraries included

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include "JsonStreamingParser.h"
#include <ArduinoJson.h>

#include "InstagramStats.h"
#include <YoutubeApi.h>

#include "theSwedishMaker.h"


const uint16_t WAIT_TIME = 6000; //Time between fecthing data.


// Define the typ of hardware and the pins used. 

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

#define CLK_PIN   12
#define DATA_PIN  15
#define CS_PIN    13

// Hardware SPI connection
//MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary output pins
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);



#define API_KEY "********" //Your Youtube API-Key 
#define CHANNEL_ID "Youtube Channel ID" // Your youtube channel ID. 

String userName = "TheSwedishMaker"; // Your Instagram Username


WiFiClientSecure client; //For ESP8266 boards
InstagramStats instaStats(client);

YoutubeApi api(API_KEY, client);

// =======================================================================
// Configuration of router settings
// =======================================================================

const char* ssid     = "NETWORKNAME";      // SSID of local network
const char* password = "*******";                // Password on network

void setup() 
{
  P.begin();
  P.setFont(fontSubs);

  // Connecting to wifi
  Serial.begin(115200);
  Serial.print("Connecting WiFi ");
  WiFi.begin(ssid, password);
  P.print(" WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("."); delay(500);
  }
  Serial.println("");
  Serial.print("Connected: "); Serial.println(WiFi.localIP());

  client.setInsecure();
}


void loop(void)
{

  Serial.println("Getting data ...");
  P.print("fetching");
  int cnt = 0;
  //int yt1;
  long yt1,yt2,insta;
    while(1) {
    if(!cnt--) {
     cnt = 50;
     if(api.getChannelStatistics(CHANNEL_ID))
      yt1 = api.channelStats.subscriberCount; 
      yt2 = api.channelStats.viewCount;
      InstagramUserStats response = instaStats.getUserStats(userName);
      insta = response.followedByCount;    
    }

    // FORMATO
       String subsCount = num_format(yt1);
       String viewCount = num_format(yt2);
       String instaFollowers = num_format(insta);

    // PRINT
    Serial.println(subsCount);
    P.print("*" + subsCount); 
    delay(3500);
   // P.print("*" + viewCount); //Uncomment if your want to show viewcount.
   // delay(3500); //Uncomment if you want to show viewcount.
    P.print("&" + instaFollowers); 
    delay(5000);
    }}


String num_format(long num){
     String num_s;
     long num_original = num;
     if (num>99999 && num<=999999){
        num = num / 1000;
        long fraction = num_original%1000;
        String num_fraction = String(fraction);
        String decimal = num_fraction.substring(0,1);
        num_s = String(num) + "." + decimal + "K";          
    }
    else if(num>999999){
        num = num / 1000000;
        long fraction = num_original%1000000;
        String num_fraction = String(fraction);
        String decimal = num_fraction.substring(0,1);
        if (num_original<100000000){
          num_s = " " + String(num) + "." + decimal + "M";      
        }
        else{
        num_s = String(num) + "." + decimal + "M";
        }
    }
    else{
        int num_l = String(num).length();
        char num_f[15];
        int blankDigits = 6 - num_l;
        for(int i = 0; i < blankDigits; i++){
          num_f[i] = ' '; 
        }
        num_f[blankDigits] = '\0';
        num_s = num_f + String(num);
    }
    return num_s;
}    

0 个答案:

没有答案