#include <WiFi.h>
#include <WiFiClient.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* Push Button Init*******************/
const int BUTTON_PIN_1 = 18; // the number of the pushbutton pin
const int BUTTON_PIN_2 = 19;
const int BUTTON_PIN_3 = 21;
const int BUZZER_PIN = 22;
int lastState1 = HIGH;
int lastState2 = HIGH;
int lastState3 = HIGH; // the previous state from the input pin
int currentState1;
int currentState2;
int currentState3; // the current reading from the input pin
int pushButton_1;
int pushButton_2;
int pushButton_3;
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "Zayn"
#define WLAN_PASS "ibrahimarkana"
/**************************** MQTT Setup ***********************************/
#define MQTT_SERVER "broker.hivemq.com"
#define MQTT_PORT 1883
#define TOPIC "dashboard/sensor001"
String MACHINE_NO = "MACHINE 01";
String DRAWING_NO = "581078-0021";
String MOLD_NO = "01";
String COSTUMER = "TOKAIRIKA";
String Json_Data = "";
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT);
Adafruit_MQTT_Publish push_sensor = Adafruit_MQTT_Publish(&mqtt, TOPIC);
void buzzer(){
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(200);
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000);
retries--;
if (retries == 0) {
while (1);
}
}
Serial.println("MQTT Connected!");
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
Serial.println();
pinMode(BUTTON_PIN_1, INPUT_PULLUP);
pinMode(BUTTON_PIN_2, INPUT_PULLUP);
pinMode(BUTTON_PIN_3, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}
/**************************** Buzzer Function ******************/
void loop() {
MQTT_connect();
// read the state of the switch/button:
currentState1 = digitalRead(BUTTON_PIN_1);
currentState2 = digitalRead(BUTTON_PIN_2);
currentState3 = digitalRead(BUTTON_PIN_3);
if(lastState1 == LOW && currentState1 == HIGH){
buzzer();
pushButton_1 = 1;
pushButton_2 = 0;
pushButton_3 = 0;
if (! push_sensor.publish(Json_Data.c_str())) {
Serial.println(F("Failed to Publish data..."));
} else {
Serial.println("Status Temperature : " + String (pushButton_1));
Serial.println("Status Timer : " + String (pushButton_2));
Serial.println("Status Material : " + String (pushButton_3));
Serial.println("");
}
}else
if(lastState2 == LOW && currentState2 == HIGH){
buzzer();
pushButton_1 = 0;
pushButton_2 = 1;
pushButton_3 = 0;
if (! push_sensor.publish(Json_Data.c_str())) {
Serial.println(F("Failed to Publish data..."));
} else {
Serial.println("Alarm Tamperature : " + String (pushButton_1));
Serial.println("Alarm Timer : " + String (pushButton_2));
Serial.println("Status Material : " + String (pushButton_3));
Serial.println(""); }
}else
if(lastState3 == LOW && currentState3 == HIGH){
buzzer();
pushButton_1 = 0;
pushButton_2 = 0;
pushButton_3 = 1;
if (! push_sensor.publish(Json_Data.c_str())) {
Serial.println(F("Failed to Publish data..."));
} else {
Serial.println("Alarm Tamperature : " + String (pushButton_1));
Serial.println("Alarm Timer : " + String (pushButton_2));
Serial.println("Status Material : " + String (pushButton_3));
Serial.println("");}
}
lastState1 = currentState1;
lastState2 = currentState2;
lastState3 = currentState3;
Json_Data = "{ \"pb_1\": \"" + String (pushButton_1) + "\"" +
", \"pb_2\": \"" + String (pushButton_2) + "\"" +
", \"pb_3\": \"" + String (pushButton_3) + "\"" +
"}";
}