Skip to content

Instantly share code, notes, and snippets.

@swiercz
Created September 17, 2018 09:53
Show Gist options
  • Save swiercz/e3860f84ede1141833c52ab816a9c560 to your computer and use it in GitHub Desktop.
Save swiercz/e3860f84ede1141833c52ab816a9c560 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <WiFiMulti.h>
#define SEC 100
// Button with LED indicator and buzzer
WiFiMulti WiFiMulti;
WiFiClient client;
const int buttonPin = 2;
const int ledPin = 25;
const int buzzerPin = 14;
hw_timer_t * timer = NULL;
volatile unsigned int timer_counter = 0;
void IRAM_ATTR onTimer() {
// 100 Hz
++timer_counter;
}
unsigned light_off_time = 0, buzz_off_time = 0;
void buzzOn(uint16_t time) {
buzz_off_time = timer_counter + 2*SEC;
Serial.println("Buzzzzzzz " + String(time));
digitalWrite(buzzerPin, HIGH);
}
void lightOn(uint16_t time) {
light_off_time = timer_counter + 1*SEC;
Serial.println("Light " + String(time));
digitalWrite(ledPin, HIGH);
}
void work() {
Serial.println("timer " + String(timer_counter));
Serial.println("ligh off " + String(light_off_time));
Serial.println("buzz off " + String(buzz_off_time));
if (light_off_time <= timer_counter) {
digitalWrite(ledPin, LOW);
}
if (buzz_off_time <= timer_counter) {
digitalWrite(buzzerPin, LOW);
}
}
volatile bool button_pressed = false;
void IRAM_ATTR handleInterrupt() {
button_pressed = true;
}
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), handleInterrupt, FALLING);
Serial.begin(9600);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 10000, true);
timerAlarmEnable(timer);
setupWifiConnection();
setupServerConnection();
}
void loop() {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
if(client.available() || client.connected()) {
String line = client.readStringUntil('\n');
if (String("Buzz").equalsIgnoreCase(line)) {
buzzOn(2000); // buzz for 1 sec
}
if (String("Light").equalsIgnoreCase(line)) {
lightOn(1000); // turn on the light for 1 sec
}
} else {
if(client.connected()) Serial.println("client connected");
if(client.available()) Serial.println("client available:" + String(client.available()));
// delay(60000);
}
} else{
Serial.println("Error in WiFi connection");
}
if(button_pressed) {
Serial.println("Pushed!");
sendSendMessageToServer("Push");
button_pressed = false;
}
work();
delay(500);
}
void setupWifiConnection() {
WiFiMulti.addAP("TooplooxNet", "dupadupa..");
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setupServerConnection() {
const uint16_t port = 21107;
const char * host = "172.30.1.76";
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
} else {
Serial.println("client connection success");
client.print("Hello Button\n");
client.setTimeout(100);
}
}
void sendSendMessageToServer(String message) {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
if(client.available() || client.connected()) {
client.print(message + "\n");
}
else {
if(client.connected()) Serial.println("client connected");
if(client.available()) Serial.println("client available:" + String(client.available()));
delay(60000);
}
} else{
Serial.println("Error in WiFi connection");
delay(30000); //Retry every 30 seconds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment