Need some help with an ESP8266 ESP 01

Basically I got an Arduino Uno with a DF Robot Gravity Alcohol Sensor and the ESP01 that I use to send my data to the Matlab service ThingSpeak.

However after running the code it says WIFI CONNECTION FAILED.
HOWEVER since its a 2.4GHz hotspot from my phone, I see that the ESP has indeed successfully connected to the Wifi but the serial monitor says otherwise.

Also I found out that whenever I use the 0 and 1 ports on the UNO for the ESP only to test out AT commands on an empty sketch, it fully responds fine with OK's on all of them and successfuly connects to a network, disconnects, resets, ect.

But with my code and on pins 6,7 or 10,11 nothing appears to work.
It worked before however it sent my HTTP requests but sometimes just stopped working out of nowhere.
Any ideas?

I got them all connected and have this code:

#include "DFRobot_Alcohol.h"

#define COLLECT_NUMBER 5

#define ALCOHOL_I2C_ADDRESS ALCOHOL_ADDRESS_3

DFRobot_Alcohol_I2C Alcohol(&Wire, ALCOHOL_I2C_ADDRESS);

#include <SoftwareSerial.h>

SoftwareSerial EspSerial(6, 7); // Rx, Tx (to ESP8266)

#define HARDWARE_RESET 8

String statusChWriteKey = "223VCIF1PK9Z4HRC";

long writeTimingSeconds = 5;

unsigned long lastWriteTime = 0;

float alcoholConcentration = 0;

boolean error;

int spare = 0;

void connectWiFi() {

  Serial.println("Connecting to WiFi...");

  EspSerial.println("AT+CWMODE=1");

  delay(500);

  EspSerial.print("AT+CWJAP=\"");

  EspSerial.print("boobies");       // <<<----- CHANGE THIS

  EspSerial.print("\",\"");

  EspSerial.print("0885252088");   // <<<----- CHANGE THIS

  EspSerial.println("\"");

  unsigned long startAttempt = millis();

  while (millis() - startAttempt < 10000) {

if (EspSerial.find("WIFI CONNECTED")) {

Serial.println("WiFi Connected!");

return;

}

  }

  Serial.println("WiFi connection FAILED!");

}

// -------------------------------

// SETUP

// -------------------------------

void setup() {

  Serial.begin(9600);

  // Init Alcohol Sensor

  while (!Alcohol.begin()) {

Serial.println("NO Alcohol Sensor Found!");

delay(500);

  }

  Serial.println("Alcohol Sensor Detected!");

  Alcohol.setModes(MEASURE_MODE_AUTOMATIC);

  // Init ESP

  pinMode(HARDWARE_RESET, OUTPUT);

  digitalWrite(HARDWARE_RESET, HIGH);

  EspSerial.begin(9600);

  EspHardwareReset();

  connectWiFi(); // <<<< CONNECT TO WIFI

}

// -------------------------------

void loop() {

  unsigned long currentTime = millis();

  if (currentTime - lastWriteTime >= writeTimingSeconds * 1000) {

readAlcohol();

writeThingSpeak();

lastWriteTime = currentTime;

  }

  if (error) {

Serial.println(" <<<< ERROR >>>>");

error = false;

  }

}

// -------------------------------

void readAlcohol() {

  alcoholConcentration = Alcohol.readAlcoholData(COLLECT_NUMBER);

  if (alcoholConcentration == ERROR) {

Serial.println("Alcohol Sensor Error!");

alcoholConcentration = -1;

  }

  Serial.print("Alcohol concentration: ");

  Serial.print(alcoholConcentration);

  Serial.println(" PPM");

}

// -------------------------------

void startThingSpeakCmd() {

  EspSerial.flush();

  String cmd = "AT+CIPSTART=\"TCP\",\"184.106.153.149\",80";

  EspSerial.println(cmd);

  Serial.println("Start TCP cmd sent");

  EspSerial.find("OK");

}

// -------------------------------

void writeThingSpeak() {

  startThingSpeakCmd();

  String getStr = "GET /update?api_key=";

  getStr += statusChWriteKey;

  getStr += "&field1=";

  getStr += String(alcoholConcentration);

  getStr += "\r\n\r\n";

  sendThingSpeakGetCmd(getStr);

}

// -------------------------------

void EspHardwareReset() {

  Serial.println("Resetting ESP...");

  digitalWrite(HARDWARE_RESET, LOW);

  delay(200);

  digitalWrite(HARDWARE_RESET, HIGH);

  delay(3000);

  Serial.println("ESP Reset Done");

}

// -------------------------------

String sendThingSpeakGetCmd(String getStr) {

  String cmd = "AT+CIPSEND=" + String(getStr.length());

  EspSerial.println(cmd);

  if (EspSerial.find(">")) {

EspSerial.print(getStr);

Serial.print("GET sent: ");

Serial.println(getStr);

unsigned long startTime = millis();

while (millis() - startTime < 500) {

if (EspSerial.available()) {

String line = EspSerial.readStringUntil('\n');

Serial.println(line);

}

}

return "ok";

  } else {

EspSerial.println("AT+CIPCLOSE");

Serial.println("ESP CIPSEND ERROR, retrying...");

spare++;

error = true;

return "error";

  }

}

My advice would be to replace the Uno and the ESP01 with an ESP8266 or ESP32 board.

So you have permanently changed the ESP-01 baud-rate to 9600 ?

You should most definitely use a voltage divider on the ESP-01 rx to reduce the UNO's 5v logic level to a safe 3.3v.
(i use UNO tx -> 1K -> Esp rx -> 1K -> 1K -> GND) The ESP-01 will break at some point otherwise (if it hasn't already)

Keep in mind that when you just use the UNO as a USB to TTL converter (as you do) you connect UNO Tx to ESP Tx and UNO Rx to ESP Rx. When you connect to the UNO on swSerial, you need to cross the Rx & Tx lines.

UNO Tx -> (voltage divider) -> ESP Rx
UNO Rx <- ESP Tx

Also here you need to use a voltage divider (though there is a 10K pullup on RST)

The esp8266 is a 3.3v device and do not let anybody convince you that it is 5v tolerant. It just isn't.
Putting 5v on the RST in particular is risky since this may potentially increase the voltage on Vcc.
There is no real need to reset manually anyway, just connect RST to 3.3v or even leave it free-floating.
If you do want to manually reset, the best approach would be to switch the pin to INPUT.

digitalWrite(HARDWARE_RESET, LOW);
pinMode(HARDWARE_RESET, OUTPUT); // disables the esp
pinMode(HARDWARE_RESET, INPUT); // enables the esp the 10K pullup will pull it high

There is something to be said for that, but given that only 1 I2C sensor is used, just the ESP-01 should suffice and ditching the UNO (or just keeping it as an uploader) will also work.

You can use the same connections to upload your own firmware, just connect GPIO 0 to GND and power-cycle the ESP-01 and upload code using the esp8266 core. This will overwrite the ESP-01 AT-commands firmware

Regardless, stop connecting the ESP to 5v before it breaks if it hasn't already (partly)

IF your I2C thingy is OK for 3.3v, a more sensible approach might be to keep the ESP-01 you already have, as I understand it can be configured to run the I2C bus, and you can ditch the Uno. If it's not good for 3.3v, I guess an ESP32 is inappropriate anyway

Once you've fixed the voltage, can you share also what you actually see in the Serial Monitor?

also typical sequence after the reset of the ESP-01 would be

  1. AT – check that the module responds (OK).
  2. AT+RST – software reset of the module.
  3. AT+CWMODE=1 – set module to Station mode.
  4. AT+CWJAP="SSID","PASSWORD" – connect to WiFi

➜ this is where you wait for WIFI CONNECTED and OK.

then for good measure

  1. AT+CIFSR – get the IP address.
  2. AT+CIPMUX=0 – set single TCP connection mode (if needed).
  3. AT+CIPSTART="TCP","host",port – open a TCP connection.
  4. AT+CIPSEND=<length> – prepare to send data.
  5. AT+CIPCLOSE – close the TCP connection.

Software I2C but yes it can. the hardware I2C pins are not exposed.

you can map the hardware I2C pins, e.g. to GPIO0 SDA and GPIO2 SCL

  Wire.begin(0, 2);   // for ESP-01 I2C  use pins GPIO0 (SDA) and GPIO2 (SCL) 

using a ESP-01 breakout board

That is using swI2C. The ESP8266 unlike the ESP32 does not have a mutex that allows free assignment of the GPIO pins to peripherals. (just a few options for UART0)

Don't know if the CH_PD pin has a pullup on that breakout board, but otherwise it should be connected to Vcc

using a ESP-01 connected to a BMP280 using I2C pins GPIO0 (SDA) and GPIO2 (SCL)

// BMP280 test for ESP-01 - I2C  use pins GPIO0 (SDA) and GPIO2 (SCL) 

/***************************************************************************
  This is a library for the BMP280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BMEP280 Breakout 
  ----> http://www.adafruit.com/products/2651

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required 
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
//#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bme; // I2C
//Adafruit_BMP280 bme(BMP_CS); // hardware SPI
//Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
  
void setup() {

  Serial.begin(115200);
  delay(2000);
    Serial.println(F("BMP280 test"));
  Wire.begin(0, 2);   // for ESP-01 I2C  use pins GPIO0 (SDA) and GPIO2 (SCL) 
  if (!bme.begin(0x76)) {     //  <<<<  CHANGED from 0x77
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }
    Serial.println("BMP280 sensor found!");
}
  
void loop() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");
    
    Serial.print("Pressure = ");
    Serial.print(bme.readPressure());
    Serial.println(" Pa");

    Serial.print("Approx altitude = ");
    Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
    Serial.println(" m");
    
    Serial.println();
    delay(2000);
}

serial monitor output

BMP280 test
BMP280 sensor found!

Temperature = 23.54 *C
Pressure = 99530.03 Pa
Approx altitude = 150.53 m

Temperature = 24.21 *C
Pressure = 99947.56 Pa
Approx altitude = 115.32 m

Temperature = 24.72 *C
Pressure = 100028.59 Pa
Approx altitude = 108.50 m

Temperature = 24.99 *C
Pressure = 100027.59 Pa
Approx altitude = 108.58 m

Temperature = 25.07 *C
Pressure = 99521.88 Pa
Approx altitude = 151.22 m

Temperature = 25.92 *C
Pressure = 99728.09 Pa
Approx altitude = 133.81 m

Temperature = 26.36 *C
Pressure = 99741.06 Pa
Approx altitude = 132.72 m

Temperature = 26.63 *C
Pressure = 99698.52 Pa
Approx altitude = 136.30 m

photo ESP-01 plugged into a ESP-01 Helper V3 connected to a BMP280