Skip to content

Instantly share code, notes, and snippets.

@kleinejan
Created March 7, 2018 11:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kleinejan/e9ae3b8e7ec258397085323e89a40ade to your computer and use it in GitHub Desktop.
Save kleinejan/e9ae3b8e7ec258397085323e89a40ade to your computer and use it in GitHub Desktop.
256 bit shiftregister relay machine
#include "SSD1306.h"
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <ShiftRegister74HC595.h>
long previousTime = 0;
long interval = 10000;
String payload;
int lights;
int lightsTotal = 160;
int registersTotal = 20;
int lightsOld;
int appinit = 2;
int appinitOld;
const int dataPin = D5; //d5
const int clockPin = D7; //d7
const int latchPin = D6; //d6
// Wifi
const char* ssid = "SSID";
const char* password = "PASSWORD";
//Display
String line1;
String line2;
String line3;
String line4;
String line5 = "STATIC LINE";
String line6 = "STATIC LINE";
boolean change = false;
ESP8266WiFiMulti WiFiMulti;
ShiftRegister74HC595 sr (20, dataPin, clockPin, latchPin);
HTTPClient http;
SSD1306 display(0x3c, 5, 4);
void setup() {
WiFi.mode(WIFI_STA);
// We start by connecting to a WiFi network
WiFiMulti.addAP(ssid, password);
WiFiMulti.addAP("ADDITIONAL SSID", "ADDITIONAL PASSWORD");
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
display.init();
display.setFont(ArialMT_Plain_10);
Serial.println();
Serial.println();
Serial.print("Connecting to Wifi");
Serial.println(ssid);
line1 = "Connecting to Wifi: ";
paintDisplay();
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
line3 += ".";
paintDisplay();
delay(200);
}
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - previousTime > interval) {
previousTime = currentTime;
retreiveData();
}
paintDisplay();
if (change == true) {
if (appinit == 1) {
sr.setAllLow();
}
if (appinit == 2) {
Serial.println("running");
for (int i = 0; i < lights; i++) {
sr.set(i, LOW); // set single pin HIGH
delay(50);
}
int nLight = lightsTotal - lights;
for (int i = lights; i <= lightsTotal; i++) {
sr.set(i, HIGH); // set single pin HIGH
delay(50);
}
}
if (appinit == 4) {
Chase();
}
if (appinit == 5) {
// OFF
sr.setAllHigh();
}
if (appinit == 6) {
Random();
}
change = false;
}
}
// ShiftRegister logic
void relayTest() {
sr.setAllHigh(); // set all pins HIGH
delay(500);
sr.setAllLow(); // set all pins LOW
delay(500);
for (int i = 0; i < 160; i++) {
sr.set(i, HIGH); // set single pin HIGH
delay(50);
}
}
void Chase() {
sr.setAllHigh();
for (int i = 0; i < 160; i++) {
sr.set(i, LOW); // set single pin HIGH
delay(50);
sr.set(i, HIGH); // set single pin HIGH
delay(50);
}
}
void Random() {
sr.setAllHigh();
int randomLight = random(0, lightsTotal);
sr.set(randomLight, LOW); // set single pin HIGH
delay(50);
sr.set(randomLight, HIGH); // set single pin HIGH
delay(50);
}
void retreiveData() {
DynamicJsonBuffer jsonBuffer;
WiFiMulti.run();
if (WiFiMulti.run() != WL_CONNECTED) {
Serial.println("Not connected");
}
line4 = "[Get server update]";
paintDisplay();
if (WiFiMulti.run() == WL_CONNECTED) {
// start connection and send HTTP header
http.begin("<JSON URL>"); //HTTP
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
payload = http.getString();
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
JsonObject& payLoadObject = jsonBuffer.parseObject(payload);
if (payLoadObject.containsKey("lights")) {
appinit = payLoadObject["appinit"];
lights = payLoadObject["lights"];
String title = payLoadObject["title"];
String descr = payLoadObject["description"];
line1 = "Title: " + descr;
line2 = "App init: " + String(appinit);
line3 = "Lights: " + String(lights);
line4 = "";
}
if (appinit != appinitOld || lights != lightsOld ) {
Serial.printf("Appinit: %d\n", appinit);
Serial.printf("AppinitOld: %d\n", appinitOld);
Serial.printf("Lights: %d\n", lights);
Serial.printf("LightsOld: %d\n", lightsOld);
lightsOld = lights;
appinitOld = appinit;
Serial.println("Change");
if (appinit != 0) {
change = true;
}
}
}
void paintDisplay() {
display.clear();
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 0, String(line1));
display.drawString(0, 10, String(line2));
display.drawString(0, 20, String(line3));
display.drawString(0, 30, String(line4));
display.drawString(0, 40, String(line5));
display.drawString(0, 50, String(line6));
display.setFont(ArialMT_Plain_10);
display.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment