Skip to content

Instantly share code, notes, and snippets.

@Jotschi
Created February 2, 2020 19:59
Show Gist options
  • Save Jotschi/1f79c3211435d1d9278a4d572e467ef5 to your computer and use it in GitHub Desktop.
Save Jotschi/1f79c3211435d1d9278a4d572e467ef5 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "SSID";
const char* password = "PW";
const int BUTTON_PIN = 0;
const int FART_PIN = 14;
byte mac[6];
ESP8266WebServer server(80);
static unsigned long last_interrupt_time = 0;
ICACHE_RAM_ATTR void buttonPress() {
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 100) {
fart();
}
last_interrupt_time = interrupt_time;
}
// Delay safe for interrupt
void InterruptDelay(int milli)
{
int Milliseconds = millis() + 50;
while (Milliseconds > millis());
}
void fart() {
digitalWrite(FART_PIN, HIGH);
InterruptDelay(60);
digitalWrite(FART_PIN, LOW);
InterruptDelay(60);
digitalWrite(FART_PIN, HIGH);
}
void setupPins() {
pinMode(FART_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPress, RISING);
}
void initWLAN() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Change MAC
byte *mac = (byte*)malloc(6);
WiFi.macAddress(mac);
mac[0] = 0x20;
mac[1] = 0x11;
mac[2] = 0xCA;
mac[3] = 0xCA;
mac[4] = 0x24;
mac[5] = 0x24;
wifi_set_macaddr(STATION_IF, mac);
free(mac);
WiFi.mode(WIFI_STA);
WiFi.hostname("brainfarter");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
printMac();
}
void printMac() {
Serial.print("MAC: " + getMac());
}
String getMac() {
WiFi.macAddress(mac);
return String(mac[0], HEX) +
':' +
String(mac[1], HEX) +
':' +
String(mac[2], HEX) +
':' +
String(mac[3], HEX) +
':' +
String(mac[4], HEX) +
':' +
String(mac[5], HEX);
}
void handleNotFound() {
server.send(404, "text/plain", "Not found");
}
void setupServer() {
server.on("/fart", []() {
fart();
server.send(200, "text/plain", "Fart");
});
server.on("/mac", []() {
server.send(200, "text/plain", getMac());
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void setup() {
Serial.begin(115200);
initWLAN();
setupPins();
setupServer();
}
void loop() {
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment