0

I've made a simple sketch which displays my subscriber count and it is working fully. I have made a wooden box for it so I am going to screw it together with the Wemos inside. When I disconnect the usb chord and stick it back in the Sketch seems to reboot but nothing is displayed on the Dot Matrix. If I push the reset button however, it reboots fully and the Matrix displays the numbers again.

When I run the Arduino serial monitor and unplug and plug it back in, it runs the sketch but nothing show on the Matrix. It's doing my head in.

I'm not sure if the code would help but I can of course publish that if needed.

The question: Is there any way to code a full reboot upon reconnection to power? Since it's going to be stuck in a wooden box it would be really nice if it would reboot when the power is turned back on so I don't have to open the box and push reset every time.

Any help is appreciated. Thanks!

The code:

// 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;
}    
Pierre
  • 160
  • 2
  • 11
  • There's no such thing as a "full" reboot when connected to power. Something else is going on. It goes through the same boot process when you press reset as it does when it's reconnected to power. The only substantive difference would be that when it reset the contents of memory shouldn't be lost (but some will be overwritten). – romkey May 02 '20 at 18:20
  • hmm okay. So do you know if there's any way to prevent it from not erasing the contents of memory when disconnecting it? – Pierre May 02 '20 at 18:23
  • that would be magic, since it's losing power. :) the possible thing would be to identify why your code has a dependency on the previous contents of memory when it boots, since it shouldn't. If that's actually the problem. – romkey May 02 '20 at 18:57
  • I added the code if anyone can see the issue in code. – Pierre May 02 '20 at 19:13
  • So you don't see "fetching" on your display in the not working case? Have you tried adding a line like `P.print("booting");` right after `P.begin()`? That would let you know whether it's successfully entering your code at all on restart. – romkey May 02 '20 at 20:29
  • No I don't see fetching on the display, but when I plug it in the computer I see that it's saying connecting in the monitor so it seems to run even though nothing is showing on the display – Pierre May 03 '20 at 04:07
  • Tried that now and it resumes to print Connected and resumes getting data but nothing displays so it seems somehow it remembers what it was doing and continues but it doesn't display anything....so frustrating! :) – Pierre May 03 '20 at 14:09

0 Answers0