Hi everyone,
Requirement:
In my bags manufacturing company, over each sewing Line, RFT (Right First Time) lights are installed for visual management of hourly RFT values (Hourly Actual Output in comparison with Target Output).
Right now, workers manually Turn ON/OFF lights with respect to RFT values.
Red Light will be turned ON, when RFT value is below 90.
Yellow Light will be turned ON, when RFT value is from 91 to 95.
Green Light will be turned ON, when RFT value is above 95.
The blue bulb is for Changeover indication. QCOC (Quick Changeover Crew)
If QCOC =1 , Blue Bulb ON , If QCOC=0, Blue Bulb OFF
IT department has designed a web portal, in which data of RFT values is stored on hourly basis and is displayed on screens over each line. I want to link RFT lights (red, yellow, green, blue) with that web portal so that lights should automatically turn ON and OFF as per RFT values.
For this: I'm working on 220V AC operated RFT quality Lights (Red, Yellow, Green, Blue) with ESP 32 Wroom 32d, 5V adapter to power ESP 32 and pair of 2 channel 5V relays each connected with lights.
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// Define relay pins
const int relay1 = 5; // Pin connected to IN1 of Relay 1 (Green Bulb)
const int relay2 = 18; // Pin connected to IN2 of Relay 2 (Yellow Bulb)
const int relay3 = 19; // Pin connected to IN3 of Relay 3 (Red Bulb)
const int relay4 = 17; // Pin connected to IN3 of Relay 3 (Red Bulb)
const int wifiLED = 2; // Pin connected to Wi-Fi status LED
// Wi-Fi credentials
const char* ssid = "USERNAME";
const char* password = "PASSWORD";
// Other declarations
StaticJsonDocument<200> doc;
String host_or_IPv4 = "http://125.209.66.227:60002/";
String Destination = "";
String URL_Server = "";
String getData = "";
String payloadGet = "";
HTTPClient http;
WiFiClient client;
// Setup
void setup() {
Serial.begin(115200);
// Initialize Wi-Fi LED pin as output
pinMode(wifiLED, OUTPUT);
// Set relay pins as outputs
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// Initialize relays and Wi-Fi LED to OFF state
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(wifiLED, LOW);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// Turn Wi-Fi LED ON upon successful connection
digitalWrite(wifiLED, HIGH);
}
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// Main loop
void loop() {
// Check Wi-Fi status and update Wi-Fi LED
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(wifiLED, HIGH);
} else {
digitalWrite(wifiLED, LOW);
}
// Existing code logic remains unchanged
int id = 0; //--> ID in Database
getData = "ID=" + String(id);
Destination = "api/Production/GetRetriveRFT?LineNumber=1&type=json";
URL_Server = host_or_IPv4 + Destination;
Serial.println("----------------Connect to Server-----------------");
Serial.println("Get LED Status from Server or Database");
Serial.print("Request Link : ");
Serial.println(URL_Server);
http.begin(client, URL_Server); //--> Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCodeGet = http.GET(); //--> Send the request
payloadGet = http.getString(); //--> Get the response payload from server
Serial.println(payloadGet); //--> Print request response payload
Serial.print("Response Code : "); //--> If Response Code = 200 means Successful connection, if -1 means connection failed. For more information see here : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Serial.println(httpCodeGet); //--> Print HTTP return code
Serial.print("Returned data from Server : ");
Serial.println(payloadGet); //--> Print request response payload
DeserializationError error = deserializeJson(doc, payloadGet);
double RFT = doc["DefectPercentage"];
double QCOC = doc["QCOC"];
Serial.println(RFT);
Serial.println(QCOC);
if (RFT > 0) {
double inputValue = RFT; // Read input value from serial monitor
// Turn off all bulbs first
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
Serial.println(RFT);
if (RFT < 95) {
digitalWrite(relay3, LOW);
Serial.println("Red Bulb ON");
}
else if(RFT >= 95 && RFT < 97){
digitalWrite(relay2, LOW);
Serial.println("Yellow Bulb ON");
}
else if (RFT >= 97) {
digitalWrite(relay1, LOW); // Relay LOW means ON
Serial.println("Green Bulb ON");
}
if (QCOC = 1) {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, LOW);
Serial.println("Blue Bulb ON");
}
else
{
digitalWrite(relay4, HIGH);
Serial.println("Blue Bulb OFF");
}
Serial.println("----------------Closing Connection----------------");
http.end(); //--> Close connection
Serial.println();
Serial.println("Please wait 10 seconds for the next connection.");
Serial.println();
}
}
Problem:
The problem that I'm facing is: lights don't turn ON and OFF as per changing RFT values. For example: If RFT value is 90, Red will turn ON and if RFT value changed to 98, Red bulb stays ON, Same happens with other bulbs. There is no accuracy in implementation. I don't know where's the problem. Am I missing something in hardware or programming.





