Luqmaan Baboo
Published © MIT

Date, Time & Temperature Display using ☒XinaBox

Cool OLED display showing the date, time and temperature in Celsius and Fahrenheit using Xinabox ☒CHIPS based off ESP8266.

EasyFull instructions provided19 minutes86
Date, Time & Temperature Display using ☒XinaBox

Things used in this project

Hardware components

IP01
XinaBox IP01
☒CHIP USB Programmer based on FT232R From FTDI Limited
×1
CW01
XinaBox CW01
☒CHIP Wi-Fi Core based on ESP8266 Wi-Fi Module
×1
SW01
XinaBox SW01
☒CHIP Temperature, humidity and atmospheric pressure sensor based on the BME280 from Bosch.
×1
OD01
XinaBox OD01
☒CHIP 128x64 Pixel OLED Display
×1
PU01
XinaBox PU01
☒CHIP USB (Type A) Power Supply
×1
XC10
XinaBox XC10
☒CHIP Bus Connectors
×1
5V USB Power Supply
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Date_Time_Temp.ino

Arduino
Simply enter your WiFi details in their respective fields and upload to your board.
#include <xCore.h>          // include core library for XinaBox xCHIPS
#include <xOD01.h>          // include OLED display library
#include <xSW01.h>          // include the temperature sensor library
#include <ESP8266WiFi.h>    // include ESP8266WiFi functionality
#include <String.h>
// include time libraries
#include <WifiUDP.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>

xSW01 SW01;
// define NTP properties
#define ntpOffset   60 * 60      // in seconds
#define ntpInterval 60 * 1000    // in miliseconds
// insert a reliable ntp time server between the double quotes
// here i've used a google ntp time server
#define ntpAddress "time1.google.com"

// set up the NTP UDP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpAddress, ntpOffset, ntpInterval);

// temperature variable
float tempC;    // celsius
float tempF;    // fahrenheit

// your wifi details
const char* wifi_ssid = "XinaBox";             // your wifi ssid
const char* wifi_pass = "RapidIoT";           // your wifi password

// date and time variable
String date;
String clktime;

// variables containing days and months
const char * days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} ;
const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"} ;
const char * ampm[] = {"AM", "PM"} ;

void setup ()
{

  tempC = tempF = 0;    // initialize temperature to zero
  timeClient.begin();   // start the NTP UDP client
  // start serial communication
  Serial.begin(115200);
  // start i2c communication and set pins
  Wire.begin(2, 14);
  // start temperature sensor
  SW01.begin();
  // start OLED display
  OLED.begin();
  // clear OLED display
  OD01.clear();

  // establish wifi connection
  wifi_connect();
  delay(1000);
}

void loop()
{
  // run if wifi connection is established
  if (WiFi.status() == WL_CONNECTED) { 
    SW01.poll();                  // read temperature
    tempC = SW01.getTempC();      // store temp in celcius
    tempF = SW01.getTempF();      // store temp in fahrenheit
    date = "";                    // clear date variable
    clktime = "";                 // clear time variable

    // update the ntp client and get the unix utc timestamp
    timeClient.update();
    unsigned long epochTime =  timeClient.getEpochTime();

    // convert received time stamp to time_t object
    time_t utc;
    utc = epochTime;

    // utc time
    TimeChangeRule utcRule = {"UTC", Last, Sun, Mar, 1, 0};
    Timezone UTC(utcRule, utcRule);

    // format time variables
    date += days[weekday(utc) - 1];
    date += ", ";
    date += months[month(utc) - 1];
    date += " ";
    date += day(utc);
    date += ", ";
    date += year(utc);

    // format the time to 12-hour format with AM/PM and no seconds
    clktime += hourFormat12(utc);
    clktime += ":";
    if (minute(utc) < 10) // add a zero if minute is under 10
      clktime += "0";
    clktime += minute(utc);
    clktime += " ";
    clktime += ampm[isPM(utc)];

    // display time, date and tempearure on OLED
    OD01.set2X();
    OD01.print("  ");
    OD01.println(clktime);
    OD01.set1X();
    OD01.println("");
    OD01.println("");
    OD01.print("   ");
    OD01.print(tempC);
    OD01.print(" C   ");
    OD01.print(tempF);
    OD01.println(" F");
    OD01.println("");
    OD01.println(date);
  } else {    // try to restablish wifi connection if disconnected
    wifi_connect();
  }
  delay(5000);  // updates display every 5seconds
}

// establishes a wifi connection
void wifi_connect() {
  Serial.print("Connecting to ");
  Serial.print(wifi_ssid);
  WiFi.begin(wifi_ssid, wifi_pass);    // connect to wifi
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(300);
  }
  Serial.println("");
  Serial.print("Connected to WiFi at ");
  Serial.print(WiFi.localIP());
  Serial.println("");
}

Credits

Luqmaan Baboo

Luqmaan Baboo

9 projects • 2 followers
Electrical Engineering Student with a fascination for automation.
Thanks to MichaelB247.

Comments