/*
ITL Factory - Monitoring System
Program for Woven Labels Looms
*/
#include <EspMQTTClient.h>
#include <DHT.h>
#include <max6675.h>
#define ID "ESP1" //Change client id here
#define PROXY_PIN 4
#define WARP_PIN 23
#define WEFT_PIN 22
#define POWER_PIN 21
int thermoDO = 19;
int thermoCLK = 18;
int thermoCS = 5;
int thermoDO1 = 12;
int thermoCS1 = 15;
int thermoCLK1 = 14;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
MAX6675 thermocouple1(thermoCLK1, thermoCS1, thermoDO1);
#define DHTPIN 13
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
EspMQTTClient client(
"Wokwi-GUEST",
"",
"broker.hivemq.com", // MQTT Broker server ip
"", // Can be omitted if not needed
"", // Can be omitted if not needed
ID, // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
void setup() {
Serial.begin(115200);
pinMode(PROXY_PIN, INPUT);
pinMode(WARP_PIN, INPUT);
pinMode(WEFT_PIN, INPUT);
pinMode(POWER_PIN, INPUT);
dht.begin();
// Optional functionalities of EspMQTTClient
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
client.enableLastWillMessage("Woven_Labels_ESP/lastwill", "I am going offline");
}
// This function is called once everything is connected (Wifi and MQTT)
void onConnectionEstablished()
{
// Subscribe to "Topic" and display received message to Serial
client.subscribe(ID, [](const String & payload) {
Serial.println(payload);
});
client.publish(ID, "ESP_Reboot"); //Change to device id
}
unsigned long previousMillis = 0; // will store last time temp was updated
unsigned long interval = 3000; //interval for reading temp sensors
void loop() {
{
client.loop();
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
float h1 = dht.readHumidity();
float t1 = dht.readTemperature();
float t2 = thermocouple.readCelsius();
float t3 = thermocouple1.readCelsius();
client.publish("Hum1_"ID, String(h1).c_str());
client.publish("Temp1_"ID, String(t1).c_str());
client.publish("Temp2_"ID, String(t2).c_str());
client.publish("Temp3_"ID, String(t3).c_str());
}
int PROXYstate = digitalRead(PROXY_PIN);
static int lastPROXYstate = digitalRead(PROXY_PIN);
if (lastPROXYstate != PROXYstate) {
if (PROXYstate == LOW)
{
client.publish(ID, "Proxy");
}
lastPROXYstate = PROXYstate;
}
int WARPstate = digitalRead(WARP_PIN);
static int lastWARPstate = digitalRead(WARP_PIN);
if (lastWARPstate != WARPstate) {
if (WARPstate == LOW)
{
client.publish(ID, "Warp_On");
}
else if (WARPstate == HIGH)
{
client.publish(ID, "Warp_Off");
}
lastWARPstate = WARPstate;
}
int WEFTstate = digitalRead(WEFT_PIN);
static int lastWEFTstate = digitalRead(WEFT_PIN);
if (lastWEFTstate != WEFTstate) {
if (WEFTstate == LOW)
{
client.publish(ID, "Weft");
}
lastWEFTstate = WEFTstate;
}
int POWERstate = digitalRead(POWER_PIN);
static int lastPOWERstate = digitalRead(POWER_PIN);
if (lastPOWERstate != POWERstate) {
if (POWERstate == LOW)
{
client.publish(ID, "Power_On");
}
else if (POWERstate == HIGH)
{
client.publish(ID, "Power_Off");
}
lastPOWERstate = POWERstate;
}
}