Hi everyone,
I'm working on a project using an Arduino Uno to control an ESP8285 module (running AT firmware) for MQTT.
I am completely stuck on the very first step: The ESP8285 will not connect to my Wi-Fi network.
The module is silent and times out after sending the connection command.
My Setup & Problem
• Module: ESP8285 with AT Firmware (controlled by Arduino Uno). Fails to connect.
• Command: AT+CWJAP="SIDD","Pass"
• Result: Returns Timeout (no OK or FAIL response).
• Communication: Hardware Serial (Pins 0/1) set to 9600 baud. Confirmed stable and reliable.
• Power: Dedicated external 3.3V supply used. Confirmed stable (ruled out the main cause).
// --- Global Configuration ---
// Using Hardware Serial (Pins 0 and 1) for ESP8285 communication (Most reliable)
#define ESP_SERIAL Serial
#define DEBUG_SERIAL Serial
const int LED_PIN = LED_BUILTIN; // Pin 13 on Arduino Uno for visual status
// Global flag to track connection status
bool isConnected = false;
// Network Credentials (Check these CAREFULLY for typos and case-sensitivity)
const char* WIFI_SSID = "Mena";
const char* WIFI_PASSWORD = "12341234xwz";
// MQTT Broker Details (Your Mac/Mosquitto)
const char* MQTT_SERVER_IP = "192.168.1.5";
const int MQTT_PORT = 1883;
const char* MQTT_TOPIC = "esp/sensor/data";
const char* MQTT_CLIENT_ID = "ESP_Client_1";
const char* MQTT_USER_CFG_CMD = "AT+MQTTUSERCFG=0,1,\"ESP_Client_1\",\"\",\"\",0,0,\"\"";
// Sensor Data (Example values - replace with actual sensor reading logic)
float temperature = 25.10;
float humidity = 43.20;
// --- Function Prototypes ---
void sendATCommand(const char* command, int waitTime, const char* expectedResponse);
bool connectWiFi();
void configureAndConnectMQTT();
void publishSensorData();
// --- Setup ---
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // LED off initially
// CRITICAL CHANGE: Using 9600 baud for stability with Arduino Uno and ESP8285
DEBUG_SERIAL.begin(9600);
ESP_SERIAL.begin(9600);
DEBUG_SERIAL.println("--- Starting ESP8285 Setup for MQTT (Baud: 9600) ---");
// Flash LED twice to confirm setup started
digitalWrite(LED_PIN, HIGH); delay(100); digitalWrite(LED_PIN, LOW); delay(100);
digitalWrite(LED_PIN, HIGH); delay(100); digitalWrite(LED_PIN, LOW); delay(500);
// 1. Permanently set ESP8285 firmware to 9600 baud for reliable communication
DEBUG_SERIAL.println("Setting ESP Baud Rate to 9600...");
sendATCommand("AT+UART_CUR=9600,8,1,0,0", 500, "OK");
// 2. Initial AT commands
sendATCommand("AT", 200, "OK");
sendATCommand("AT+RST", 2000, "ready");
sendATCommand("AT+CWMODE=1", 200, "OK"); // Set to station mode
// Attempt the Wi-Fi connection and set the global flag
isConnected = connectWiFi();
if (isConnected) {
DEBUG_SERIAL.println("--- Wi-Fi Connected. Proceeding to MQTT ---");
digitalWrite(LED_PIN, HIGH); // SUCCESS: LED ON SOLID
configureAndConnectMQTT();
} else {
DEBUG_SERIAL.println("!!! FAILED TO CONNECT WI-FI. LED blinking error. !!!");
// FAILURE: LED blinks fast, handled in loop()
}
}
// --- Main Loop ---
void loop() {
if (isConnected) {
// If connected, LED is solid. Send data every 10 seconds.
publishSensorData();
digitalWrite(LED_PIN, HIGH); // Keep LED on
delay(10000);
} else {
// If not connected, blink the LED rapidly to show error state.
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
// After blinking for a while, retry connection every 10 seconds
static unsigned long lastRetry = 0;
if (millis() - lastRetry > 10000) {
DEBUG_SERIAL.println("!!! Connection failed in setup. Retrying Wi-Fi... !!!");
isConnected = connectWiFi();
if (isConnected) {
digitalWrite(LED_PIN, HIGH); // SUCCESS: LED ON SOLID
configureAndConnectMQTT();
}
lastRetry = millis();
}
}
}
// --- Core Communication Functions ---
bool connectWiFi() {
char command[64];
sprintf(command, "AT+CWJAP=\"%s\",\"%s\"", WIFI_SSID, WIFI_PASSWORD);
DEBUG_SERIAL.print(">>> Command: ");
DEBUG_SERIAL.println(command);
ESP_SERIAL.println(command);
// Set a 30-second timeout for robust connection
long startTime = millis();
String response = "";
while (millis() - startTime < 30000) {
if (ESP_SERIAL.available()) {
char c = ESP_SERIAL.read();
DEBUG_SERIAL.write(c);
response += c;
// Look for the success messages
if (response.indexOf("WIFI GOT IP") >= 0) { // More specific success check
return true;
}
// Look for the specific failure message
if (response.indexOf("FAIL") >= 0 || response.indexOf("ERROR") >= 0) {
return false;
}
}
}
DEBUG_SERIAL.println("\n[Wi-Fi Connection Timeout]");
return false;
}
void configureAndConnectMQTT() {
DEBUG_SERIAL.println("--- Configuring MQTT Client ---");
// 1. Configure the Client (ID, No Auth)
sendATCommand(MQTT_USER_CFG_CMD, 500, "OK");
// 2. Connect to the Broker
char connect_command[64];
sprintf(connect_command, "AT+MQTTCONN=0,\"%s\",%d,0", MQTT_SERVER_IP, MQTT_PORT);
DEBUG_SERIAL.print(">>> Command: ");
DEBUG_SERIAL.println(connect_command);
// Wait for +MQTTCONNECTED and OK
sendATCommand(connect_command, 5000, "+MQTTCONNECTED");
sendATCommand("", 500, "OK"); // Check for final OK
}
void publishSensorData() {
char payload[32];
sprintf(payload, "T:%.2f,H:%.2f", temperature, humidity);
char publish_command[128];
sprintf(publish_command, "AT+MQTTPUB=0,\"%s\",\"%s\",0,0", MQTT_TOPIC, payload);
DEBUG_SERIAL.print("Attempting to send: ");
DEBUG_SERIAL.println(payload);
DEBUG_SERIAL.print(">>> Command: ");
DEBUG_SERIAL.println(publish_command);
sendATCommand(publish_command, 1000, "+MQTTPUB:OK");
DEBUG_SERIAL.println("--- Data Send Attempt Complete ---");
}
void sendATCommand(const char* command, int waitTime, const char* expectedResponse) {
if (strlen(command) > 0) {
ESP_SERIAL.println(command);
}
long startTime = millis();
String response = "";
while (millis() - startTime < waitTime) {
if (ESP_SERIAL.available()) {
char c = ESP_SERIAL.read();
DEBUG_SERIAL.write(c);
response += c;
}
if (expectedResponse != NULL && response.indexOf(expectedResponse) >= 0) {
break;
}
if (response.indexOf("ERROR") >= 0 || response.indexOf("FAIL") >= 0) {
DEBUG_SERIAL.println("\n[AT ERROR Detected]");
break;
}
}
}
`


