Skip to content

Instantly share code, notes, and snippets.

@freman
Created January 20, 2017 10:48
Show Gist options
  • Save freman/061f4bcf2e87fe772a25a49c092e38e9 to your computer and use it in GitHub Desktop.
Save freman/061f4bcf2e87fe772a25a49c092e38e9 to your computer and use it in GitHub Desktop.
Weingard on ESP-01 - reading bits from uart (as gpio), controlling buzzer/led from gpio pins
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define LED 0
#define BEEP 2
// Read 0's from GPIO_1(TX) and 1's from GPIO_3(RX)
#define PIN0 1
#define PIN1 3
#define WEINGARD_BITS 34
const char* ControlHost = "10.0.0.1";
const uint16_t ControlPort = 4210;
const uint16_t LocalUDPPort = 4210;
const uint16_t PanelID = 1;
WiFiUDP Udp;
void setup() {
WiFi.begin("notsosecretwifissid", "secretwifipassword");
pinMode(PIN0, INPUT);
pinMode(PIN1, INPUT);
// My eBay special expects these outputs to be high to turn off the attached devices
pinMode(LED, OUTPUT);
pinMode(BEEP, OUTPUT);
digitalWrite(LED, 1);
digitalWrite(BEEP, 1);
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
digitalWrite(LED, 0);
delay(250);
digitalWrite(LED, 1);
}
digitalWrite(BEEP, 0);
delay(250);
digitalWrite(BEEP, 1);
delay(250);
digitalWrite(BEEP, 0);
delay(250);
digitalWrite(BEEP, 1);
attachInterrupt(PIN0, interrupt0, FALLING);
attachInterrupt(PIN1, interrupt1, FALLING);
Udp.begin(LocalUDPPort);
}
// Interrupt for the 0 pin
void interrupt0() {
interrupt(true);
}
// Interrupt for the 1 pin
void interrupt1() {
interrupt(false);
}
bool done = false;
uint64_t rcvd = 0LL;
long lastDigit = 0;
long iteration = 0;
void interrupt(bool one) {
// Simply shift things over
rcvd = rcvd << 1;
if (one) {
rcvd += 1LL;
}
lastDigit = millis();
iteration ++;
}
unsigned long beepStarted = 0;
unsigned long beepLength = 0;
unsigned long ledStarted = 0;
unsigned long ledLength = 0;
void loop() {
unsigned long now = millis();
unsigned long since = now - lastDigit;
if (iteration == WEINGARD_BITS || (iteration > 0 && since > 1000)) {
detachInterrupt(PIN0);
detachInterrupt(PIN1);
// Add a prefix to let the server know this is manual input
if (iteration < WEINGARD_BITS && since > 1000) {
rcvd |= (125LL << 56);
}
iteration = 0;
lastDigit = 0;
// 10 bytes
// [2] id, [8] code
uint8_t string[10];
memcpy(&string, &PanelID, 2);
memcpy(&string[2], &rcvd, 8);
Udp.beginPacket(ControlHost, ControlPort);
Udp.write(string, 10);
Udp.endPacket();
rcvd = 0LL;
delay(500);
attachInterrupt(PIN0, interrupt0, FALLING);
attachInterrupt(PIN1, interrupt1, FALLING);
}
int packetSize = Udp.parsePacket();
if (packetSize) {
char incomingPacket[255]; // buffer for incoming packets
int len = Udp.read(incomingPacket, 10);
if (len > 2) {
if (incomingPacket[1] > 0) {
beepStarted = now;
beepLength = 100 * incomingPacket[1];
digitalWrite(BEEP, 0);
}
if (incomingPacket[0] > 0) {
ledStarted = now;
ledLength = 100 * incomingPacket[0];
digitalWrite(LED, 0);
}
}
}
if (beepStarted > 0 && now - beepStarted > beepLength) {
beepStarted = 0;
digitalWrite(BEEP, 1);
}
if (ledStarted > 0 && now - ledStarted > ledLength) {
ledStarted = 0;
digitalWrite(LED, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment