This article introduces how to use BW21-CBV-Kit together with its expansion board to implement a web-based washing machine control panel using the HTTP protocol. It simulates remote control of washing machine functions such as start, pause, and stop, and displays the current status in real time on an OLED screen.
Project OverviewCommands are sent from a web page via HTTP to remotely control the washing machine's operational state, while the OLED display provides real-time status updates.
Content includes:
- Hardware connection
- Flowchart
- Project code
- Effect demonstration
As this project simulates washing machine control, only an OLED screen is used on the hardware side.
If you want to control actual peripherals, corresponding GPIO operations can be added in the relevant code sections.
Project CodeOpen the Arduino IDE, create a new project, and add the following code:
#include <WiFi.h>#include <WiFiClient.h>#include <WiFiServer.h>#include <Wire.h>#include <Adafruit_OLED_libraries/Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128#define SCREEN_HEIGHT 64#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Washer stateenum WasherState {
IDLE,
WASHING,
RINSING,
SPINNING,
DONE,
PAUSED
};
const char* stateNames[] = {
"Standby",
"Washing",
"Rinsing",
"Spinning",
"Completed",
"Paused"
};
WasherState currentState = IDLE;int remainingTime = 0; // secondunsigned long lastUpdateTime = 0;
// WiFi credentialschar ssid[] = "xxx";char password[] = "xxx";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
// Initialize OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("HTTP server started");
updateDisplay();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New client connected");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
// Send HTTP headers
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Send HTML content
client.print("<!DOCTYPE html><html><head><title>Washer Control</title>");
client.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.print("<style>body{font-family:Arial;text-align:center;margin:0 auto;padding:20px;max-width:400px;}");
client.print(".btn{background-color:#4CAF50;border:none;color:white;padding:15px 32px;text-align:center;");
client.print("text-decoration:none;display:inline-block;font-size:16px;margin:4px 2px;cursor:pointer;border-radius:8px;width:150px;}");
client.print(".status{font-size:24px;margin:20px 0;padding:10px;background-color:#f0f0f0;border-radius:8px;}");
client.print(".btn-container{display:flex;flex-direction:column;align-items:center;gap:10px;}</style></head>");
client.print("<body><h1>Washer Control Panel</h1>");
client.print("<div class=\"status\">Current Status: ");
client.print(stateNames[currentState]);
client.print("</div><div class=\"btn-container\">");
client.print("<a href=\"/start\"><button class=\"btn\">Start</button></a>");
client.print("<a href=\"/pause\"><button class=\"btn\">Pause</button></a>");
client.print("<a href=\"/stop\"><button class=\"btn\">Stop</button></a>");
client.print("</div></body></html>");
break;
} else {
// Process requests
if (currentLine.startsWith("GET /start")) {
handleStart();
} else if (currentLine.startsWith("GET /pause")) {
handlePause();
} else if (currentLine.startsWith("GET /stop")) {
handleStop();
}
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
Serial.println("Client disconnected");
}
// Update washer state
if (currentState != IDLE && currentState != DONE && currentState != PAUSED) {
if (millis() - lastUpdateTime >= 1000) { // Update every second
lastUpdateTime = millis();
remainingTime--;
// State transition logic
if (remainingTime <= 0) {
currentState = DONE;
} else if (currentState == WASHING && remainingTime <= 20) {
currentState = RINSING;
} else if (currentState == RINSING && remainingTime <= 10) {
currentState = SPINNING;
}
updateDisplay();
}
}
}
void updateDisplay() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Washer Status: ");
display.setTextSize(2);
display.setCursor(0, 16);
display.println(stateNames[currentState]);
display.setTextSize(1);
display.setCursor(0, 40);
display.print("IP: ");
display.println(WiFi.localIP());
if (currentState != IDLE && currentState != DONE) {
display.setCursor(0, 52);
display.print("Time left: ");
display.print(remainingTime);
display.println("s");
}
display.display();
}
void handleStart() {
if (currentState == IDLE || currentState == DONE) {
currentState = WASHING;
remainingTime = 30; // 30 seconds (simulating 30 minutes)
lastUpdateTime = millis();
Serial.println("Washer started");
} else if (currentState == PAUSED) {
currentState = WASHING; // Simplified - should resume previous state
lastUpdateTime = millis();
Serial.println("Washer resumed");
}
updateDisplay();
}
void handlePause() {
if (currentState == WASHING || currentState == RINSING || currentState == SPINNING) {
currentState = PAUSED;
Serial.println("Washer paused");
}
updateDisplay();
}
void handleStop() {
if (currentState != IDLE) {
currentState = IDLE;
remainingTime = 0;
Serial.println("Washer stopped");
}
updateDisplay();
}
Once the project is saved:
1. Select the AMB82-MINI development board in the IDE.
2. Select the corresponding serial port.
3. Press and hold the BOOT button, then briefly press the EN button to enter download mode.
4. Click the Upload button.
After uploading is complete, briefly press the EN button to reset and run the program.
Serial OutputThe IP address of the web server will be printed in the serial monitor, e.g., 192.168.31.111.
Open a browser and enter the IP address of the development board to access the washing machine control panel.
- Stop Status
- Paused Status
- Washing Status
Click the buttons on the web interface to switch to the corresponding working state. The OLED will display the current status in real time.
Conclusion
This article demonstrates how to use the Ai-Thinker BW21-CBV-Kit development board and expansion board to create a web-based washing machine control panel via the HTTP protocol.
The system can simulate remote control of the machine's start, pause, and stop functions, while displaying the status on an OLED screen.
The project includes an introduction, hardware setup, flowchart, source code, and demo results, providing a reference for fast development, design, and application of the BW21 module and related products.
Comments