Monday, August 17, 2020

NTP server

first, get NTP client library for ESP32, and save in Arduino library folder 

https://randomnerdtutorials.com/esp32-ntp-client-date-time-arduino-ide/

Then set time, GMT +5.5 to view Sri Lanka time 

Then upload the code and view serial monitor using Arduino IDE, that should indicate as follows 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <WiFi.h>

#include <NTPClient.h>

#include <WiFiUdp.h>


// Replace with your network credentials

const char* ssid     = "Your SSD"; // 

const char* password = "Your Password";


// Define NTP Client to get time

WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP);


// Variables to save date and time

String formattedDate;

String dayStamp;

String timeStamp;


void setup() {

  // Initialize Serial Monitor

  Serial.begin(115200);

  Serial.print("Connecting to ");

  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  // Print local IP address and start web server

  Serial.println("");

  Serial.println("WiFi connected.");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());


// Initialize a NTPClient to get time

  timeClient.begin();

  // Set offset time in seconds to adjust for your timezone, for example:

  // GMT +1 = 3600

  // GMT +8 = 28800

  // GMT -1 = -3600

  // GMT 0 = 0

  timeClient.setTimeOffset(19800);

}

void loop() {

  while(!timeClient.update()) {

    timeClient.forceUpdate();

  }

  // The formattedDate comes with the following format:

  // 2018-05-28T16:00:13Z

  // We need to extract date and time

  formattedDate = timeClient.getFormattedDate();

  Serial.println(formattedDate);


  // Extract date

  int splitT = formattedDate.indexOf("T");

  dayStamp = formattedDate.substring(0, splitT);

  Serial.print("DATE: ");

  Serial.println(dayStamp);

  // Extract time

  timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);

  Serial.print("HOUR: ");

  Serial.println(timeStamp);

  delay(1000);

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Unix Time 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <TimeLib.h>

unsigned long offset_days = 3;    // 3 days
unsigned long t_unix_date1, t_unix_date2;

void setup() {
  Serial.begin(115200);
  t_unix_date1 = 1564398600;
  Serial.print("t_unix_date1: ");
  Serial.println(t_unix_date1);
  offset_days = offset_days * 86400;    // convert number of days to seconds
  t_unix_date2 = 1564398600 + offset_days;
  Serial.print("t_unix_date2: ");
  Serial.println(t_unix_date2);
  printf("Date1: %4d-%02d-%02d %02d:%02d:%02d\n", year(t_unix_date1), month(t_unix_date1), day(t_unix_date1), hour(t_unix_date1), minute(t_unix_date1), second(t_unix_date1));
  printf("Date2: %4d-%02d-%02d %02d:%02d:%02d\n", year(t_unix_date2), month(t_unix_date2), day(t_unix_date2), hour(t_unix_date2), minute(t_unix_date2), second(t_unix_date2));
}

void loop() {



}

Sunday, August 16, 2020

OLED interface with ESP8266

This code used to interface with OLED, 

1.54 inch White OLED Display Module 128x64 SPI Interface OLED ...

bit map code was generated using http://javl.github.io/image2cpp/ 


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 #include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels


// Declaration for SSD1306 display connected using software SPI (default case):

#define     OLED_MOSI        D7

#define     OLED_CLK          D5

#define     OLED_DC             D2

#define     OLED_CS             D8

#define     OLED_RESET      D3

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,

  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);




const unsigned char myBitmap [] PROGMEM = {

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x07, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x03, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0x80, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x60, 0x03, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xfe, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xf8, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x01, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xf0, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xc0, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x81, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0x00, 0x00, 0x01, 0x80, 0xc0, 0x0e, 0x00, 0x0e, 0x00, 0x61, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0x87, 0xf8, 0x7f, 0xc0, 0xff, 0xf8, 0x71, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xfc, 0x00, 0x00, 0x7f, 0x9f, 0xfc, 0xff, 0xe3, 0xf0, 0xf8, 0x31, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xf8, 0x00, 0x00, 0x1f, 0x90, 0xff, 0x07, 0xf3, 0xe0, 0x70, 0x19, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xf8, 0x00, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf3, 0xf0, 0x20, 0x19, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xfc, 0x00, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf3, 0xf8, 0x00, 0x1d, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xfc, 0x00, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf1, 0xff, 0x80, 0x1f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xfe, 0x00, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf0, 0xff, 0xe0, 0x1f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0x80, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf0, 0x1f, 0xf8, 0x1f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xc0, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf0, 0x03, 0xf8, 0x1f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xf0, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf0, 0x00, 0xfc, 0x3f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0x80, 0x7e, 0x03, 0xf0, 0x00, 0xf8, 0x3f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0x00, 0x1f, 0xc0, 0xff, 0x03, 0xf8, 0x01, 0xf8, 0x7f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xc0, 0x7f, 0xf1, 0xff, 0x8f, 0xfe, 0x03, 0xe0, 0xef, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x30, 0x00, 0x00, 0x0e, 0x03, 0x9f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0f, 0x1f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x3c, 0x3f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0xc0, 0x00, 0x0f, 0x00, 0x7f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x70, 0x00, 0x7c, 0x00, 0x7f, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x1c, 0x0f, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xc1, 0xc0, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x1e, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x01, 0xf0, 0x3f, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x7e, 0x7f, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 

0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff

};


void setup() {

  Serial.begin(9600);


  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally

  if(!display.begin(SSD1306_SWITCHCAPVCC)) {

    Serial.println(F("SSD1306 allocation failed"));

    for(;;); // Don't proceed, loop forever

  }


  // Show initial display buffer contents on the screen --

  // the library initializes this with an Adafruit splash screen.

  //display.display();

  //delay(2000); // Pause for 2 seconds


  // Clear the buffer

  display.clearDisplay();

  testscrolltext();


  // Clear the buffer.

  display.clearDisplay();


  // Display bitmap

  display.drawBitmap(0, 0,  myBitmap, 128, 64, BLACK, WHITE);

  display.display();


}


void loop() {

}


void testscrolltext(void) {

  display.clearDisplay();


  display.setTextSize(2); // Draw 2X-scale text

  display.setTextColor(WHITE);

  display.setCursor(0, 0);

  display.println(F("Asanka"));

  display.println(F("Lakmal"));

  display.println(F("Morawaka"));

  display.display();      // Show initial text

  delay(100);


  // Scroll in various directions, pausing in-between:

  display.startscrollright(0x00, 0x0F);

  delay(2000);

  display.stopscroll();

  delay(1000);

  display.startscrollleft(0x00, 0x0F);

  delay(2000);

  display.stopscroll();

  delay(1000);

  display.startscrolldiagright(0x00, 0x07);

  delay(2000);

  display.startscrolldiagleft(0x00, 0x07);

  delay(2000);

  display.stopscroll();

  delay(1000);

}

Search This Blog