//===== LIBRARY DECLARATION =====
#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>
//#include <String.h>
struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};
//===== PUSHBUTTON PIN =====
Button dbluePB = {12, 0, false};
Button lbluePB = {14, 0, false};
Button dgreenPB = {27, 0, false};
Button lgreenPB = {26, 0, false};
Button orangePB = {25, 0, false};
Button yellowPB = {33, 0, false};
Button purplePB = {32, 0, false};
Button whitePB = {35, 0, false};
//variables to keep track of the timing of recent interrupts
unsigned long button_time = 0;
unsigned long last_button_time = 0;
void IRAM_ATTR isr(Button button1) {
button_time = millis();
if (button_time - last_button_time > 250){
button1.numberKeyPresses++;
button1.pressed = true;
last_button_time = button_time;
}
}
//===== TOPIC MQTT ====
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "ee.unsoed.ac.id";
const char* topicdblue = "IoTlamp/pub/darkblue";
const char* topiclblue = "IoTlamp/pub/lightblue";
const char* topicdgreen = "IoTlamp/pub/darkgreen";
const char* topiclgreen = "IoTlamp/pub/lightgreen";
const char* topicorange = "IoTlamp/pub/orange";
const char* topicyellow = "IoTlamp/pub/yellow";
const char* topicpurple = "IoTlamp/pub/purple";
const char* topicwhite = "IoTlamp/pub/white";
WiFiClient espClient;
PubSubClient mqttClient(espClient);
unsigned long lastMsg = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (mqttClient.connect(clientId.c_str())) {
Serial.println("Connected");
// Once connected, publish an announcement...
mqttClient.publish("IoTlamp/pub", "Hi Im smart lamp");
// ... and resubscribe
mqttClient.subscribe("IoTlamp/sub");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup(){
Serial.begin(115200);
setup_wifi();
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setCallback(callback);
//===== PIN MODE =====
pinMode(dbluePB.PIN, INPUT);
attachInterrupt(dbluePB.PIN, isr(dbluePB), FALLING);
pinMode(lbluePB.PIN, INPUT);
attachInterrupt(lbluePB.PIN, isr(lbluePB), FALLING);
pinMode(dgreenPB.PIN, INPUT);
attachInterrupt(dgreenPB.PIN, isr(dgreenPB), FALLING);
pinMode(lgreenPB.PIN, INPUT);
attachInterrupt(lgreenPB.PIN, isr(dgreenPB), FALLING);
pinMode(orangePB.PIN, INPUT);
attachInterrupt(orangePB.PIN, isr(orangePB), FALLING);
pinMode(yellowPB.PIN, INPUT);
attachInterrupt(yellowPB.PIN, isr(yellowPB), FALLING);
pinMode(purplePB.PIN, INPUT);
attachInterrupt(purplePB.PIN, isr(purplePB), FALLING);
pinMode(whitePB.PIN, INPUT);
attachInterrupt(whitePB.PIN, isr(whitePB), FALLING);
}
void loop(){
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
//=====COUNTER PB=======
//int countDblue = 0;
//int countLblue = 0;
//int countDgreen = 0;
//int countLgreen = 0;
//int countOrange = 0;
//int countYellow = 0;
//int countPurple = 0;
//int countWhite = 0;
//===== STRING FOR DATA =====
if (dbluePB.pressed) {
if (dbluePB.numberKeyPresses > 3){
dbluePB.numberKeyPresses = 0;
Serial.println("PB DARK BLUE = %u", String(dbluePB.numberKeyPresses));
} else{
Serial.println("PB DARK BLUE = %u", String(dbluePB.numberKeyPresses));
}
mqttClient.publish(topicdblue,String(dbluePB.numberKeyPresses).c_str());
dbluePB.pressed = false;
}
if (lbluePB.pressed) {
if (lbluePB.numberKeyPresses > 3){
lbluePB.numberKeyPresses = 0;
Serial.println("PB LIGHT BLUE = %u", String(lbluePB.numberKeyPresses));
} else{
Serial.println("PB LIGHT BLUE = %u", String(lbluePB.numberKeyPresses));
}
mqttClient.publish(topiclblue,String(lbluePB.numberKeyPresses).c_str());
lbluePB.pressed = false;
}
if (dgreenPB.pressed) {
if (dgreenPB.numberKeyPresses > 3){
dgreenPB.numberKeyPresses = 0;
Serial.println("PB DARK GREEN = %u", String(dgreenPB.numberKeyPresses));
} else{
Serial.println("PB DARK GREEN = %u", String(dgreenPB.numberKeyPresses));
}
mqttClient.publish(topicdgreen,String(dgreenPB.numberKeyPresses).c_str());
dgreenPB.pressed = false;
}
if (lgreenPB.pressed) {
if (lgreenPB.numberKeyPresses > 3){
lgreenPB.numberKeyPresses = 0;
Serial.println("PB LIGHT GREEN = %u", String(lgreenPB.numberKeyPresses));
} else{
Serial.println("PB LIGHT GREEN = %u", String(lgreenPB.numberKeyPresses));
}
mqttClient.publish(topiclgreen,String(lgreenPB.numberKeyPresses).c_str());
lgreenPB.pressed = false;
}
if (orangePB.pressed) {
if (orangePB.numberKeyPresses > 3){
orangePB.numberKeyPresses = 0;
Serial.println("PB ORANGE = %u", String(orangePB.numberKeyPresses));
} else{
Serial.println("PB ORANGE = %u", String(orangePB.numberKeyPresses));
}
mqttClient.publish(topicorange,String(orangePB.numberKeyPresses).c_str());
orangePB.pressed = false;
}
if (yellowPB.pressed) {
if (yellowPB.numberKeyPresses > 3){
yellowPB.numberKeyPresses = 0;
Serial.println("PB YELLOW = %u", String(yellowPB.numberKeyPresses));
} else{
Serial.println("PB YELLOW = %u", String(yellowPB.numberKeyPresses));
}
mqttClient.publish(topicyellow,String(yellowPB.numberKeyPresses).c_str());
yellowPB.pressed = false;
}
if (purplePB.pressed) {
if (purplePB.numberKeyPresses > 3){
purplePB.numberKeyPresses = 0;
Serial.println("PB PURPLE = %u", String(purplePB.numberKeyPresses));
} else{
Serial.println("PB PURPLE = %u", String(purplePB.numberKeyPresses));
}
mqttClient.publish(topicpurple,String(purplePB.numberKeyPresses).c_str());
purplePB.pressed = false;
}
if (whitePB.pressed) {
if (whitePB.numberKeyPresses > 3){
whitePB.numberKeyPresses = 0;
Serial.println("PB WHITE = %u", String(whitePB.numberKeyPresses));
} else{
Serial.println("PB WHITE = %u", String(whitePB.numberKeyPresses));
}
mqttClient.publish(topicwhite,String(whitePB.numberKeyPresses).c_str());
whitePB.pressed=false;
}
}