Code used to work but now bricks device

I recently wrote some code based on a previous example that displayed an NTP based clock on an SSD1306 oled display. It used to work perfectly but now I upload the code and it bricks my Sparkcore with a pulsing green LED after the initial start up screen. Anyone got any idea what has changed that might cause this? I’m pretty clueless about coding so don’t know where to begin but here’s my code, don’t laugh at it!


// NTP clock by Fred 8/1/16


// This #include statement was automatically added by the Particle IDE.
#include <SparkTime.h>



// black GND 
// red 3v3      so do not need the protective resistors below
// white SDA    D0 needs resistors 1.5K to 10K since 5V serial
// yellow SCL   D1 needs resistors 1.5K to 10K since 5V serial




#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"

#define OLED_RESET D4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


UDP UDPClient;
SparkTime rtc;

unsigned long currentTime;
unsigned long lastTime = 0UL;
String timeStr; //time
String dateStr; //date
String dayStr;  //day of week

void setup()   {                
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)

    display.clearDisplay();   // clears the screen and buffer
  
    display.setTextSize(0);           // from 1-9 sensible actually can be bigger but useless
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0,0); 
    display.println("NTP clock");
    display.setTextColor(BLACK, WHITE); // 'inverted' text
    display.setCursor(0,20);       // 128,64 pixels
    display.println("Starting!");
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0,40); 
    display.println("Frednet (c)");
    display.display();
    delay(4000);
    
// Select NTP server
    rtc.begin(&UDPClient, "pool.ntp.org"); // was ntp2c.mcc.ac.uk
    rtc.setTimeZone(0); // gmt offset
}

void loop(){
    display.clearDisplay();
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0,0);
    
    
    currentTime = rtc.now();
    if (currentTime != lastTime) {
      byte sec = rtc.second(currentTime);
      if (sec == 1) {
          
	// Build Date String
	dateStr = "";
	dateStr += rtc.dayString(currentTime);      // Day number
	dateStr += " ";
	dateStr += rtc.monthNameShortString(currentTime);// Month name
	dateStr += " "; 
	dateStr += rtc.yearShortString(currentTime);    //Year short number
	
	// Build Time String
	timeStr = "";
	timeStr += rtc.hour12String(currentTime);   // Hours
	timeStr += ":";
	timeStr += rtc.minuteString(currentTime);   // Minutes
	timeStr += " ";	
	timeStr += rtc.AMPMString(currentTime);     // AM or PM
	
	// Build Day String
	dayStr = "";
	dayStr = rtc.dayOfWeekString(currentTime);
	
	display.setTextSize(2); 
	display.setCursor(0,0);
	display.println(timeStr);
	
	display.setTextSize(2);
	display.setCursor(0,20);
	display.println(dateStr);
	
	display.setTextSize(2);
	display.setCursor(0,40);
	display.println(dayStr);
	display.display();
	}
	lastTime = currentTime;
	}
	
}

You can just switch to use the native Time Library now, I’d guess.

The Particle Cloud can furnish what you need…

I’ve just tried the adafruit_SSD1306 library and that sees to be the cause rather than the sparktime library, is anyone else experiencing trouble with it?

Did you try adding the pull-up resistors on SDA and SCL? Sometimes the displays have them and sometimes not, but they are always required for Particle use.

1 Like

Yes, they’re on there and the hardware is exactly as it was. The only thing I have done is re-flash the code I previously used which worked. I have noticed that the SSD_1306 library example fails to build though, has something happened to my system?
Here’s the error

spark_wiring_random.o (symbol from plugin): In function `randomSeed(unsigned int)':
(.text+0x0): multiple definition of `random(int)'
ssd1306_128x64_i2c.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
../build/module.mk:222: recipe for target 'target/workspace.elf' failed
make: *** [target/workspace.elf] Error 1

As stated in so many other threads dealing with the SSD displays I’ll repeat it here too

The multiple definition of random(int) is quite clear. You need to remove the implementation of that function from the project source, since it’s now (for a long time) already implemented in the system framework.

1 Like

Thanks for the reply and excuse my ignorance but are you saying that the library is the problem?
Any chance you could explain how to fix it or point me to a link that explains it in more basic terms?
I haven’t much experience of coding and I’m struggling enough as it is without coping with broken libraries!

You said

And I provided the solution to that

(in the sample code that is)

Ok, I’ve done that and the example library ssd1306-128x64-i2c.ino example now compiles and flashes successfully. Unfortunately my code still doesn’t work and there is no references to random(int) in it to remove.
I get the initial screen output but after that I just get a green pulsing led which only a factory reset will fix.
I noticed an earlier post that said to omit the adafruit_GFX library as that causes an error due to already being defined, are there any other things Im omitting?

this suggests you code (or a lib) keeps the flow trapped somewhere.
Adding some debug outputs (on screen, LED or USB) might help you narrowing down the potentially offending code lines.

And for the start try what @BulldogLowell already said

If there is no burning need to use the 3rd party lib, stick with the stock options.

try compiling it using a Photon for the target device. I noticed that trying to use the examples in the Particle port of that library using the IDE… wouldn’t compile at first using a Particle Core that was running an older firmware as the target device

I would try to flash your core with a bare minimum sketch and make sure that you’ve loaded the Core’s firmware to the latest version and a working sketch. (it shows the current version in the IDE)

then try to compile for your target device and flash your project…

@BulldogLowell, since the Core firmware is monolithic, what is flashed always contains the system firmware and user code. The “safest” approach is to flash tinker to the device, targeting a newer or newest firmware version. Once that is proven to work, the user app can be flashed over with the same firmware version used for tinker.

1 Like

Ok, looks like the graphics display isn’t the problem, the line that causes the green LED is this

    currentTime = rtc.now();

All that I was like trying to do was create a simple clock that used NTP to ensure it was always correct. Would the native Time Library achieve this and, if so, what’s it called and how do I use it? I assumed Sparktime library was the standard library, if it isn’t, then are there some examples somewhere of how to use the native library?
Thanks to everyone so far who has helped, I will get this working eventually! Sparkcore seems to be a little harder than Arduino to get to grips with.

Everything you need to know about the time class is explained in the Reference

2 Likes