#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include "sendtoSlack.h"
/* 3 button box, when button is pressed, it will send a message to slack, and light up the LED stack light
Use FreeRTOS to parallelize 3 tasks for 3 lights
Include a I2C LCD to display the status of the system
Include the 4 th button to reset the lights
Include OTA to update the code
*/
const String Machine = "C1";
const int buttonPin1=14;
const int buttonPin2=12;
const int buttonPin3=13;
const int resetPin=27;
const int ledPin1=26;
const int ledPin2=25;
const int ledPin3=33;
// for toggle button function, need to keep track of the state of the button
bool callHelp_1=false;
bool isFaulty_2=false;
bool isFaulty_3=false;
LiquidCrystal_I2C lcd(0x27,20,4);
void light1(void * parameter){
while (1){
// toggle task state
Serial.println(digitalRead(buttonPin1));
// if pin1 has been hold for 2 seconds, send a message to slack and light up the LED
// 2 second confirmation for caling for help,(cancel help no wait)
if (digitalRead(buttonPin1)==HIGH && digitalRead(ledPin1)==LOW && callHelp_1==false ){
lcd.setCursor(0,0);
lcd.print("Please hold");
vTaskDelay(2000);
Serial.println(digitalRead(buttonPin1));
// Serial.print("button state: ");
// Serial.println(digitalRead(buttonPin1));
if (digitalRead(buttonPin1)==HIGH ){
digitalWrite(ledPin1,HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Calling technician");
postToSlackWEBHOOK_digital("Technician", Machine);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Help called");
callHelp_1=true;
} else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Cancelled");
continue;
}
}
// cancel help
if (digitalRead(buttonPin1)==HIGH && digitalRead(ledPin1)==HIGH && callHelp_1==true){
Serial.println("cannelling");
Serial.println(digitalRead(buttonPin1));
digitalWrite(ledPin1,LOW);
callHelp_1=false;
// spam killer
vTaskDelay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Ready to Use");
}
// // program the help light
// if(isFaulty_1 && digitalRead(ledPin1)==HIGH){
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Calling technician");
// vTaskDelay(1500);
// isFaulty_1=false;
// postToSlackWEBHOOK_digital("Technician", Machine);
// // 10s delay to prevent spamming
// vTaskDelay(5000/portTICK_PERIOD_MS);
// }
// vTaskDelay(150);
}
}
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print("WiFi ");
WifiConnect();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WiFi connected");
pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
pinMode(buttonPin3,INPUT);
pinMode(resetPin,INPUT);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
xTaskCreatePinnedToCore(
light1, /* Task function. */
"light1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
NULL, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
// put your setup code here, to run once:
vTaskDelay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Ready to Use");
}
void loop() {
// put your main code here, to run repeatedly:
}