123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #include <Arduino.h>
- #include "LittleFS.h"
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <WebSocketsServer.h>
- const char *SSID = "S88Sniffer";
- const char *PASSWORD = "12345678";
- const int MAXMODULECOUNT = 16; // 16 * 8 inputs
- const bool DEBUGOUTPUT = false;
- /*
- PINOUT PCB
- D7 = 7 - Reset
- D6 = 6 - Load
- D2 = 2 - Data
- D4 = 4 - Clock
- Switched D4 to D1
- create wire link on PCB
- */
- ESP8266WebServer httpServer(80);
- WebSocketsServer webSocket = WebSocketsServer(81);
- int clockPin = D1;
- int dataPin = D2;
- int loadPin = D6;
- int resetPin = D7;
- int clockCounter = 0;
- long loopCounter = 0;
- long moduleCounter = 0;
- unsigned int data[MAXMODULECOUNT];
- bool loadSensors = false;
- bool resetTriggered = false;
- bool loadTriggered = false;
- bool somthingChanged = false;
- bool interuptsSet = false;
- unsigned long lastClockChange;
- unsigned long loadStartedAt;
- String changes = "";
- void ICACHE_RAM_ATTR s88Clock()
- {
- delayMicroseconds(16); //Delay makes reading output signal more reliable.
- int val = digitalRead(dataPin);
- int oldVal = bitRead(data[moduleCounter], clockCounter);
- if (val != oldVal)
- {
- changes += ";"+String(moduleCounter)+"|"+String(clockCounter)+":"+String(val);
- //somthingChanged = true;
- }
- bitWrite(data[moduleCounter], clockCounter, val);
- if ((clockCounter + 1) == 8)
- {
- moduleCounter++;
- }
- clockCounter = (clockCounter + 1) % 8;
- }
- void ICACHE_RAM_ATTR s88Load()
- {
- clockCounter = 0;
- moduleCounter = 0;
- loopCounter++;
- loadTriggered = true;
- }
- void ICACHE_RAM_ATTR s88Reset()
- {
- resetTriggered = true;
- }
- void handleData()
- {
- String resp = "";
- for (int i = 0; i < MAXMODULECOUNT; i++)
- {
- //resp += (data[i]);
- for (int j = 0; j < 8; j++)
- {
- resp += ((String(i) + "." + String(j) + ":" + String(bitRead(data[i], j))) + "~");
- }
- }
- httpServer.send(200, "text/html", resp);
- }
- void handleSocketTest()
- {
- webSocket.broadcastTXT("update");
- httpServer.send(200, "text/html", "OK");
- }
- void handleConfigs()
- {
- if (httpServer.arg("key") != "")
- {
- String key = httpServer.arg("key");
- if (httpServer.arg("value") != "")
- {
- File f = LittleFS.open(String("/config-" + key), "w");
- String value = httpServer.arg("value");
- f.write(value.c_str());
- httpServer.send(200, "text/html", "OK");
- f.close();
- }
- else
- {
- File f = LittleFS.open(String("/config-" + key), "r");
- httpServer.send(200, "text/html", f.readString());
- f.close();
- }
- }
- else
- {
- httpServer.send(400, "text/html", "must give a key");
- }
- }
- void setEmulation(bool emulationActive)
- {
- if (emulationActive)
- {
- pinMode(clockPin, OUTPUT);
- pinMode(loadPin, OUTPUT);
- pinMode(resetPin, OUTPUT);
- }
- else
- {
- pinMode(clockPin, INPUT);
- pinMode(loadPin, INPUT);
- pinMode(resetPin, INPUT);
- }
- }
- void runEmulation()
- {
- if(loadStartedAt+500 > micros()){
- digitalWrite(loadPin,HIGH);
- return;
- }
- if(loadStartedAt+1000 > micros()){
- digitalWrite(loadPin,LOW);
- return;
- }
- if (lastClockChange + 300 < micros())
- {
- lastClockChange = micros();
- digitalWrite(clockPin, !digitalRead(clockPin));
- }
- if(moduleCounter == MAXMODULECOUNT){
- loadStartedAt = micros();
- s88Load();
- }
- }
- void setupInterupts(){
- interuptsSet = true;
- attachInterrupt(digitalPinToInterrupt(clockPin), s88Clock, RISING);
- attachInterrupt(digitalPinToInterrupt(loadPin), s88Load, RISING);
- attachInterrupt(digitalPinToInterrupt(resetPin), s88Reset, RISING);
- }
- void setup()
- {
- pinMode(dataPin, INPUT);
- setEmulation(false);
- WiFi.softAP(SSID, PASSWORD);
- // put your setup code here, to run once:
- if (!LittleFS.begin())
- {
- return;
- }
- httpServer.on("/data", handleData);
- httpServer.on("/test", handleSocketTest);
- httpServer.on("/config", handleConfigs);
- httpServer.serveStatic("/index.html", LittleFS, "/index.html", "max-age=43200");
- httpServer.serveStatic("/", LittleFS, "/index.html", "max-age=43200");
- httpServer.serveStatic("/script.js", LittleFS, "/script.js", "max-age=43200");
- httpServer.serveStatic("/reset.css", LittleFS, "/reset.css", "max-age=43200");
- httpServer.serveStatic("/style.css", LittleFS, "/style.css", "max-age=43200");
- httpServer.begin();
- webSocket.begin();
- //setEmulation(true);
- setupInterupts();
- }
- void loop()
- {
- if (resetTriggered)
- {
- resetTriggered = false;
- }
- if (loadTriggered)
- {
- loadTriggered = false;
- }
- if (somthingChanged)
- {
- somthingChanged = false;
- webSocket.broadcastTXT("update");
- }
- if(changes != ""){
- webSocket.broadcastTXT(changes);
- changes = "";
- }
- httpServer.handleClient();
- webSocket.loop();
- }
|