Close

Arduino code for the ESP

A project log for world clock thing

8x 1602 LCDs - 8x I2C controller - ESP8266

davedarkodavedarko 06/28/2017 at 10:080 Comments

(OTA doesn't work with current chip)

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd[8] = {
  LiquidCrystal_I2C(0x27, 16, 2),
  LiquidCrystal_I2C(0x26, 16, 2),
  LiquidCrystal_I2C(0x25, 16, 2),
  LiquidCrystal_I2C(0x24, 16, 2),
  LiquidCrystal_I2C(0x23, 16, 2),
  LiquidCrystal_I2C(0x22, 16, 2),
  LiquidCrystal_I2C(0x21, 16, 2),
  LiquidCrystal_I2C(0x20, 16, 2)
};
int LED = 0;
int sda = 4; 
int scl = 5;
int current_lcd = 0;
int current_lcd_line = 0;
const char* ssid = "XXX";
const char* password = "XXX";
const char* clientID = "octoLCD";
const char* host = "your.hostname.com";
void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  Wire.begin(sda, scl);
  for (int i=0; i<8; i++)
  {
    lcd[i].init();
  }
  for (int i=0; i<8; i++)
  {
    lcd[i].backlight();
  }
  setup_wifi();
  // Hostname defaults to esp8266-[ChipID]
  ArduinoOTA.setHostname(clientID);
  // No authentication by default
  ArduinoOTA.setPassword((const char *)"PASSWORD");
  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
}
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void loop() 
{
  ArduinoOTA.handle();
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  delay(10);
  String url = "/81602/index.php";
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);
  boolean ignore = true;
  reset_lcds();
  while (client.connected()) 
  {
    char a,b,c,d;
    if (!ignore)
    {
      String line = client.readStringUntil('\n');
      // char f = (char) client.read();
      Serial.println(line);
      draw(line);
      // lcd[0].print(a);
      // lcd[0].setCursor(0,0);
    }
    else
    {
      a = client.read();
      if (a == 13)
      {
        b = client.read();
        if (b == 10)
        {
          c = client.read();
          if (c == 13)
          {
            d = client.read();
            if (d == 10)
            {
              ignore = false;  
            }
          }
        }
      }
    }
  }
  delay(15000);
}
void draw (String line)
{
  lcd[current_lcd].print(line);
  if (current_lcd_line==1) 
  {
    current_lcd_line = 0;
    if (current_lcd<7) current_lcd++;
  }
  else 
  {
    lcd[current_lcd].setCursor(0,1);
    current_lcd_line = 1;
  }
}
void reset_lcds()
{
  current_lcd = 0;
  current_lcd_line = 0;
  for (int i=0; i<8; i++)
  {
    lcd[i].clear();
  }
}

Discussions