0

I'm building an automated watering system using a NodeMCU module and a 128X64 OLED display (I2C interface) to present information. I'm trying to get the OLED to refresh the current system status and time on the display. To simplify the display activity on the OLED I wrote the below function that receives a phrase pointer and should output the phrase and current time-stamp to the display.

For some reason I can't figure out, each time I call the PrintToLCD() function within the loop() segment, nothing is output to the display. I do see relevant information on the serial monitor.

Please advise.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <DS3231.h>
#include <Wire.h>
#include <SoftwareSerial.h> 
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(OLED_RESET);

void setup() {
  Serial.begin(9600);
  Serial.println();

......some setup code.......

  // initialize Oled display with the I2C addr 0x3D (for the 128x64)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false);
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splash screen
  // internally, this will display the splash screen.
  display.display();
  delay(500);
  // Clear the display buffer.
  display.clearDisplay();
}

void loop() {
  // Get UTC time
  //--------------------------------------------------------
  if (timeStatus() != timeNotSet) {
    if (now() != prevDisplay) {
      //update the display only if time has changed
      prevDisplay = now();
      digitalClockDisplay(timeString);
      PrintToLCD(timeString,0,30);
      //***this works fine and prints to display***
    }
  }

......some business logic code.......

  PrintToLCD("Current watering Valve",0,30);
  //This does not display any output
}

void PrintToLCD(const String& phrase, int XOffset, int YOffset) {
  display.setCursor(XOffset,YOffset);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println(phrase);
  display.println(" ");
  digitalClockDisplay(timeString);
  display.println(timeString);
  display.display();
  delay(1000);
}

void digitalClockDisplay(String timeString) {
  // digital clock display of the time
  String sHour=String(hour());
  String sMinute=String(minute());
  String sSec=String(second());
  String sDay=String(day());
  String sMonth=String(month());
  String sYear=String(year());
  timeString=String(sDay + "/" + month() + "/" + year() + "  "+ hour()+ 
":"+minute()+":"+second());
  if (minute()<10) {
    timeString=String(sDay + "/" + sMonth + "/" + year() + "  "+ hour()+ ":0" + minute()+":"+second());
  } else if (second()<10) {
    timeString=String(sDay + "/" + sMonth + "/" + year() + "  "+ hour()+ ":"+minute()+":0"+second());
  }
  Serial.println("Function: digitalClockDisplay");
  Serial.println(timeString);
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Eliav
  • 1
  • 4
  • It could be related to hardware. Recently there are a lot of questions related to embedded projects and I'm not quite sure SO is the best place for such questions since problems can have various causes not related to the code itself. – Gnqz Oct 04 '17 at 14:10
  • Hi Thanks for your answer. I do think I'm doing something wrong since once outside the loop all works fine. Might be related to the fact I'm sending a pointer to the PrintToLCD display. I'm still playing around to find a solution. – Eliav Oct 04 '17 at 16:00

1 Answers1

0

After some research it turns out that the NODEMCU (specifically the ESP8266) has a clock speed of 80MHZ. That is obviously to fast for refreshing an OLED display. The correct way is to use the Simple Timer library to set synchronous timers for refreshing the information on the display.

    SimpleTimer timer;         
    timer.setInterval(10000, refreshDisplay); 

define a refreshDisplay function:

  void refreshDisplay()
    {
    display.clearDisplay();
    display.setCursor(0,0);
    display.setTextSize(1);
    display.setTextColor(WHITE);
    digitalClockDisplay();
    display.println(...print something to display...);
    display.display();
    }
Eliav
  • 1
  • 4