-->
Page 1 of 1

Timer countdown and countup synchronized with the server

PostPosted: Wed Jun 15, 2016 1:36 pm
by rafik73_nodemcu
Hello
I want to display on the LCD timer that counts down to zero and then counts up from zero. Timer to be synchronized with the server in the local network via wifi. I want to synchronize every 2-3 seconds. Please help. My code:

Code: Select all#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
const char* ssid     = "net";
const char* password = "password";
const char* host = "192.168.1.8";

//-----------------------------------------------------------------------------
byte bar1[8] =
{
  0b11111,
  0b11111,
  0b11111,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};
byte bar2[8] =
{
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b11111,
  0b11111,
  0b11111
};
byte bar3[8] =
{
  0b11111,
  0b11111,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b11111,
  0b11111
};
byte bar4[8] =
{
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};
byte bar5[8] =
{
  0b00000,
  0b00000,
  0b00001,
  0b00011,
  0b00011,
  0b00001,
  0b00000,
  0b00000
};
byte bar6[8] =
{
  0b00000,
  0b00000,
  0b10000,
  0b11000,
  0b11000,
  0b10000,
  0b00000,
  0b00000
};
byte bar7[8] =
{
        0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00111,
  0b00111
};
byte bar8[8] =
{
        0b11100,
  0b11100,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b11100,
  0b11100
};
//-----------------------------------------------------------------------------
//******************************************************************************
void setup() {
  Wire.begin(0,2);
   // initialize the LCD
   lcd.begin();

   // Turn on the blacklight and print a message.
   lcd.backlight();
//------------------------------------------------------------------------------
  lcd.createChar(1,bar1);
  lcd.createChar(2,bar2);
  lcd.createChar(3,bar3);
  lcd.createChar(4,bar4);
  lcd.createChar(5,bar5);
  lcd.createChar(6,bar6);
  lcd.createChar(7,bar7);
  lcd.createChar(8,bar8);
//------------------------------------------------------------------------------
  delay(100);

  // We start by connecting to a WiFi network

  Serial.println();
  lcd.setCursor(0,0);
  lcd.print("Lacze z siecia: ");
  lcd.setCursor(0,1);
  lcd.print(ssid);
 
  WiFi.begin(ssid, password);
  delay(2000);
    lcd.clear();
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
   
    lcd.print(".");
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("WIFI polaczone"); 
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Twoj adres IP:");
  lcd.setCursor(0,1);
  lcd.print(WiFi.localIP());
 
  //-----------------------
  delay(5000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("lacze z adresem:");
  lcd.setCursor(0,1);
  lcd.print(host);
  delay(3000);
  lcd.clear();
}
//*******************************************************************************
 void custom0(int col)
{ // uses segments to build the number 0
  lcd.setCursor(col, 0);
  lcd.write(4); 
  lcd.write(1);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(4); 
  lcd.write(2); 
  lcd.write(4);
}
 
void custom1(int col) //1
{
  lcd.setCursor(col,0);
  lcd.write(1);
  lcd.write(4);
  lcd.write(32);
  lcd.setCursor(col,1);
  lcd.write(2);
  lcd.write(4);
  lcd.write(2);
}
 
void custom2(int col) //2
{
  lcd.setCursor(col,0);
  lcd.write(1);
  lcd.write(3);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(4);
  lcd.write(2);
  lcd.write(2);
}
 
void custom3(int col) //3
{
  lcd.setCursor(col,0);
  lcd.write(1);
  lcd.write(3);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(2);
  lcd.write(2);
  lcd.write(4);
}
 
void custom4(int col) //4
{
  lcd.setCursor(col,0);
  lcd.write(4);
  lcd.write(2);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(32);
  lcd.write(32);
  lcd.write(4);
}
 
void custom5(int col) //5
{
  lcd.setCursor(col,0);
  lcd.write(4);
  lcd.write(3);
  lcd.write(3);
  lcd.setCursor(col, 1);
  lcd.write(2);
  lcd.write(2);
  lcd.write(4);
}
 
void custom6(int col) //6
{
  lcd.setCursor(col,0);
  lcd.write(4);
  lcd.write(3);
  lcd.write(3);
  lcd.setCursor(col, 1);
  lcd.write(4);
  lcd.write(2);
  lcd.write(4);
}
 
void custom7(int col)  //7
{
  lcd.setCursor(col,0);
  lcd.write(1);
  lcd.write(1);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(32);
  lcd.write(32);
  lcd.write(4);
}
 
void custom8(int col) //8
{
  lcd.setCursor(col, 0);
  lcd.write(4); 
  lcd.write(3);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(4); 
  lcd.write(2); 
  lcd.write(4);
}
 
void custom9(int col)  //9
{
  lcd.setCursor(col, 0);
  lcd.write(4); 
  lcd.write(3);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(2); 
  lcd.write(2); 
  lcd.write(4);
}
void custom11(int col)
{
  lcd.setCursor(col,0);
  lcd.write(5);
  lcd.write(6);
  lcd.setCursor(col, 1);
  lcd.write(5);
  lcd.write(6);
}
void custom12(int col)
{
  lcd.setCursor(col,0);
  lcd.write(4);
  lcd.write(4);
  lcd.setCursor(col, 1);
  lcd.write(32);
  lcd.write(32);
}
void custom13(int col)
{
  lcd.setCursor(col,0);
  lcd.write(32);
  lcd.write(32);
  lcd.setCursor(col, 1);
  lcd.write(32);
  lcd.write(32);
}
void printNumber(int value, int col) {
  if (value == 0) {
    custom0(col);
  } if (value == 1) {
    custom1(col);
  } if (value == 2) {
    custom2(col);
  } if (value == 3) {
    custom3(col);
  } if (value == 4) {
    custom4(col);
  } if (value == 5) {
    custom5(col);
  } if (value == 6) {
    custom6(col);
  } if (value == 7) {
    custom7(col);
  } if (value == 8) {
    custom8(col);
  } if (value == 9) {
    custom9(col);
  }     
    if (value == 11) {
    custom11(col);
  }
  if (value == 12) {
    custom12(col);
  }
  if (value == 13) {
    custom13(col);
  }
}   
//*******************************************************************************
void loop()
{
// Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 8095;
  if (!client.connect(host, httpPort)) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("connection failed");
    return;
  }
 
  // We now create a URI for the request
  String url = "/api/v3/timers/0";
  //Serial.print("Requesting URL: ");
  //Serial.println(url);
 
  // This will send the request to the server
  client.print("GET " + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "ApiCode: 8273\r\n" +
               "Content-Type: application/json\r\n" +
               "Connection: close\r\n\r\n");
  delay(400);
   
   unsigned int i = 0; //timeout counter
  int n = 1; // char counter
  char json[500] ="{";
  while (!client.find("{")){}
  while (i<600) {                       //while1
    if(client.available()) {             //if2
      char c = client.read();
      json[n]=c;
      if(c == '}') break;
      n++;
      i=0;
    }                                 //eo if2
    i++;
  }                                   //eo

  StaticJsonBuffer<400> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(json);

  if (!root.success()) {
    lcd.setCursor(0,0);
lcd.print("blad JSON");
    return;
 
  }
 
String runningIndex = root["runningIndex"];

  //Serial.print(runningIndex);   
  //Serial.println();
  //Serial.println("closing connection"); 

if (runningIndex == "-1")
{
/*
lcd.setCursor(0,0);
lcd.print("timer zatrzymany"); 
lcd.setCursor(0,1);
lcd.print("                "); 
*/
// Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 8095;
  if (!client.connect(host, httpPort)) {  //if1
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("connection failed");
    return;
  }                                        //eo if1
 
  // We now create a URI for the request
  String url = "/api/v3/datetime";
  //Serial.print("Requesting URL: ");
  //Serial.println(url);
 
  // This will send the request to the server
  client.print("GET " + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "ApiCode: 8273\r\n" +
               "Content-Type: application/json\r\n" +
               "Connection: close\r\n\r\n");
  delay(500);
   
   unsigned int i = 0; //timeout counter
  int n = 1; // char counter
  char json[500] ="{";
  while (!client.find("{")){}
  while (i<600) {                       //while1
    if(client.available()) {             //if2
      char c = client.read();
      json[n]=c;
      if(c == '}') break;
      n++;
      i=0;
    }                                 //eo if2
    i++;
  }                                   //eo while1   

  StaticJsonBuffer<500> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(json);

  if (!root.success()) {              //if3
    return;
 
  }                                    //eo if3
 
int year = root["year"];
int month = root["month"];
int day = root["day"];
int hour = root["hour"];
int minute = root["min"];
int second = root["second"];



//-------------------------------big size----------------------------------
int m1 , m2 ,s1 , s2;

if (hour > 9) {
    m1 = (hour - (hour % 10)) / 10;
    hour = hour % 10;
  } else {
    m1 = 0;
  } 
    m2 = hour;
   
    if (minute > 9) {
    s1 = (minute - (minute % 10)) / 10;
    minute = minute % 10;
  } else {
    s1 = 0;
  } 
    s2 = minute;


printNumber(m1, 0); 
printNumber(13, 3);
printNumber(m2, 4);

printNumber(11, 7); // wyświetlamy dwukropek

 
printNumber(s1, 9);
printNumber(13, 12);
printNumber(s2, 13);
//-------------------------------------------------------------------------
}
else
{
 
 
// Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 8095;
  if (!client.connect(host, httpPort)) {  //if1
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("connection failed");
    return;
  }                                        //eo if1
 
  // We now create a URI for the request
  String url = "/api/v3/timers/" + runningIndex;
  //Serial.print("Requesting URL: ");
  //Serial.println(url);
 
  // This will send the request to the server
  client.print("GET " + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "ApiCode: 8273\r\n" +
               "Content-Type: application/json\r\n" +
               "Connection: close\r\n\r\n");
  delay(250);
   
   unsigned int i = 0; //timeout counter
  int n = 1; // char counter
  char json[500] ="{";
  while (!client.find("{")){}
  while (i<600) {                       //while1
    if(client.available()) {             //if2
      char c = client.read();
      json[n]=c;
      if(c == '}') break;
      n++;
      i=0;
    }                                 //eo if2
    i++;
  }                                   //eo while1   

  StaticJsonBuffer<500> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(json);

  if (!root.success()) {              //if3
    return;
 
  }                                    //eo if3
 
 
int displayedMins = root["displayedMins"];
int displayedSecs = root["displayedSecs"];
int dS = displayedSecs;


//-------------------------------big size----------------------------------

int m1 , m2 ,s1 , s2;

if (displayedMins > 9) {
    m1 = (displayedMins - (displayedMins % 10)) / 10;
    displayedMins = displayedMins % 10;
  } else {
    m1 = 0;
  } 
    m2 = displayedMins;
   
    if (displayedSecs > 9) {
    s1 = (displayedSecs - (displayedSecs % 10)) / 10;
    displayedSecs = displayedSecs % 10;
  } else {
    s1 = 0;
  } 
    s2 = displayedSecs;


printNumber(m1, 0); 
printNumber(13, 3);
printNumber(m2, 4);

printNumber(11, 7); // wyświetlamy dwukropek

 
printNumber(s1, 9);
printNumber(13, 12);
printNumber(s2, 13);

if(m1==0 && m2==0 && dS < 31)
{
delay(250);
lcd.clear();
}
else
{
delay(250);
}
//-------------------------------------------------------------------------
}  // end of else

  } // end void loop
  //****************************************************************************


This code works, but there are delays and the timer is not displayed smoothly.