ESP8266 Not Working Properly

Hey, not quite sure what I've done wrong here..

But if I upload a blank sketch I can scan, connect ect. ect. to a Wifi network via AT commands... but this code doesn't do anything. I go into the Serial Monitor and no serial data is displayed ever... can't quite figure it out :confused:

I've blanked out my SSID/Password but other than that its the exact code.

Thanks!

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspServer.h>
#include <WiFiEspUdp.h>



const char* ssid     = "xxx";
const char* password = "xxx";


void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  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.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

}

Did you set the correct baud rate in the serial monitor?

I did, I imagine if I didn't I wouldn't be able to see the data via the Serial Monitor when using AT commands right? but either way I have my SM as: Both NL & CR / 115200

Which board are you running the WiFiEsp code on?

RDaugherty:
But if I upload a blank sketch I can scan, connect ect. ect. to a Wifi network via AT commands.

The Arduino/ESP8266 connections for doing that are the reverse of those needed to use WiFiESP (RX-RX, TX-TX for the former vs. RX-TX, TX-RX for the latter).

pert:
Which board are you running the WiFiEsp code on?
The Arduino/ESP8266 connections for doing that are the reverse of those needed to use WiFiESP (RX-RX, TX-TX for the former vs. RX-TX, TX-RX for the latter).

It is an Uno (Tested on both 3.3V and 5V rail)

That makes sense because I realized later on that if I reverse the header for the RX/TX I get this;

"Conne

"Connecting To: xxxxx"

But then nothing else ever happens there. And its disheartening because I should never get that "Conne" its like its resetting? But I never see the power light flicker.

you have esp8266 connected to Serial and USB/Serial Monitor connected to Serial. so your "Connecting to " is sent to esp8266 too.

the double reset on startup is not unusual

As Juraj pointed out, you need to understand that when you have your ESP8266 connected to pins 0 and 1 of your Uno then you are using Serial to control the ESP8266 via AT commands. The trouble is that we usually use Serial also for sending debug output to the Serial Monitor. The ESP8266 may be confused by this debug output. The problem is made worse because there is debug output to Serial coded into the WiFiESP library, which is left on by default. I found that I needed to turn this off before the library would work with my ESP8266 connected to Serial. To do this you need to open WiFiESP/src/utility/debug.h and change line 32 from:

#define _ESPLOGLEVEL_ 3

to:

#define _ESPLOGLEVEL_ 0

It's unfortunate to lose all debug output when you're first trying to get the system working since you're basically working blind until you can get network communication going, after which you can use that for debug output if you like. I didn't have any problem with that but others are not so lucky with their ESP8266 ventures.

An alternative is to connect the ESP8266 to different pins on your Uno and use SoftwareSerial library. The downside of this is that software serial is not reliable at the default 115200 baud of the ESP8266 AT firmware. You can change the baud rate via an AT command, which would probably be easiest done by sending the command directly from the Serial Monitor, as you were doing before you started on your journey with the WiFiESP libirary.

It looks like you forgot the WiFi.init() call in your sketch. You should add the following line after the Serial.begin() call:

WiFi.init(&Serial);

I recommend starting with the example sketches you will find at File > Examples > WiFiESP until you have gotten everything working correctly to avoid problems like this. Note the example sketches are written assuming you will be using SoftwareSerial library on your Uno but they can be easily adapted if you prefer to stick with the connection on Serial.

pert:
As Juraj pointed out, you need to understand that when you have your ESP8266 connected to pins 0 and 1 of your Uno then you are using Serial to control the ESP8266 via AT commands. The trouble is that we usually use Serial also for sending debug output to the Serial Monitor. The ESP8266 may be confused by this debug output. The problem is made worse because there is debug output to Serial coded into the WiFiESP library, which is left on by default. I found that I needed to turn this off before the library would work with my ESP8266 connected to Serial. To do this you need to open WiFiESP/src/utility/debug.h and change line 32 from:

#define _ESPLOGLEVEL_ 3

to:

#define _ESPLOGLEVEL_ 0

It's unfortunate to lose all debug output when you're first trying to get the system working since you're basically working blind until you can get network communication going, after which you can use that for debug output if you like. I didn't have any problem with that but others are not so lucky with their ESP8266 ventures.

An alternative is to connect the ESP8266 to different pins on your Uno and use SoftwareSerial library. The downside of this is that software serial is not reliable at the default 115200 baud of the ESP8266 AT firmware. You can change the baud rate via an AT command, which would probably be easiest done by sending the command directly from the Serial Monitor, as you were doing before you started on your journey with the WiFiESP libirary.

It looks like you forgot the WiFi.init() call in your sketch. You should add the following line after the Serial.begin() call:

WiFi.init(&Serial);

I recommend starting with the example sketches you will find at File > Examples > WiFiESP until you have gotten everything working correctly to avoid problems like this. Note the example sketches are written assuming you will be using SoftwareSerial library on your Uno but they can be easily adapted if you prefer to stick with the connection on Serial.

Bleh, that was the problem, not calling

WiFi.init(&Serial);

I may look into SoftwareSerial (Which is now NewSoftwareSerial, right?)

But thanks so far!

RDaugherty:
I may look into SoftwareSerial (Which is now NewSoftwareSerial, right?)

There is a library named SoftwareSerial included with the Arduino IDE. It used to be a 3rd party library that I believe was named something like NewSoftwareSerial but was renamed to SoftwareSerial when it was adopted by Arduino, to match the name of their previous software serial library. There are some alternative 3rd party software serial libraries that may be superior to SoftwareSerial, I haven't used any of them so I can't say from personal experience. SoftwareSerial is easy to get started with since it's already installed but of course installing a 3rd party Arduino library is quite simple.
https://www.arduino.cc/en/Reference/SoftwareSerial

Comparison of the alternatives:

Hmm, looks like I was mistaken. I'm getting further but still not connecting;

Connecting to MySpectrumWiFi72-2G
[WiFiEsp] Initializing ESP module
AT
AT
AT+RST
ATE0
AT+CWMODE=1
AT+CIPMUX=1
AT+CIPDINFO=1
AT+CWAUTOCONN=0
AT+CWDHCP=1,1
AT+GMR
[WiFiEsp] Initilization successful - 1.5.4
AT+CWJAP_CUR="MySpectrumWiFi72-2G","xxxx"
[WiFiEsp] Failed connecting to MySpectrumWiFi72-2G
AT+CIPSTATUS
Connecting...

Using this code;

#include <WiFiEsp.h>




const char* ssid     = "MySpectrumWiFi72-2G";
const char* password = "xxxx";


void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.init(&Serial);
    WiFi.begin(ssid, password);
  


  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting...");
  }

  Serial.println(WiFi.localIP());

  
}

void loop() {

}

So when you issue this command, AT+CWJAP_CUR="MySpectrumWiFi72-2G","xxxx", separately with the hardwired serial pins, does it connect?

And if you issue AT+CIFSR

does it give an ip? 192.168.xxx.xxx (home network)

try pinging that?

Edit: hmm. looks like I should have read your first post!!! Where you say that works. Honestly, I am not an eejit (idiot).

Yeah, if I reverse the RX/TX pins and issue the AT commands via SM it connects just fine and spits out an IP.

try including

#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

and this may be obvious, but move Tx and Rx from the hardwired serial.

EDIT: I should have said before void setup in your code

Thanks for the help so far.

With the following code...

#include <WiFiEsp.h>
#include "SoftwareSerial.h"



const char* ssid     = "MySpectrumWiFi72-2G";
const char* password = "xxx";

#ifndef HAVE_HWSERIAL1

SoftwareSerial Serial1(6, 7); // RX, TX
#endif


void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.init(&Serial);
    WiFi.begin(ssid, password);
  


  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting...");
  }

  Serial.println(WiFi.localIP());

  
}

void loop() {

}

I get...

onnecting to MySpectrumWiFi72-2G
[WiFiEsp] Initializing ESP module
AT
[WiFiEsp] >>> TIMEOUT >>>
AT
[WiFiEsp] >>> TIMEOUT >>>
AT
[WiFiEsp] >>> TIMEOUT >>>
AT
[WiFiEsp] >>> TIMEOUT >>>
AT
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
AT+CWJAP_CUR="MySpectrumWiFi72-2G","fancygate667"
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Failed connecting to MySpectrumWiFi72-2G
AT+CIPSTATUS
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
Connecting...

With the last 4 lines repeating.

oh, and change wifiinit to

WiFi.init(&Serial1);

instead of WiFi.init(&Serial);

Still no go :frowning:

Maybe its in the wiring?? But it seems if that was the case the AT commands wouldn't work.

But I used this;

I honestly haven't a clue

but try this code I stole mostly from Martyn Currey

const int LED = 13;
String IncomingString;
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(6, 7); // RX | TX
//int rc = 0;
void setup()
{
  pinMode(LED, OUTPUT);
  Serial.begin(9600);     // communication with the host computer
  //while (!Serial)   { ; }

  // Start the software serial for communication with the ESP8266
  ESPserial.begin(115200);

  Serial.println("");
  Serial.println("Remember to to set Both NL & CR in the serial monitor.");
  Serial.println("Ready");
  Serial.println("");
  ESPserial.println("AT+GMR");
  // delay(1000);
  // rc = ESPserial.read();
  // Serial.println(rc);
  //  delay(1000);
  //  ESPserial.println("AT+CIFSR");
  //  delay(1000);
  //  ESPserial.println("AT+CIFSR");

}


void loop()
{

  boolean StringReady = false;

  while (ESPserial.available()) {
    IncomingString = ESPserial.readString();
    StringReady = true;

    if (StringReady) {
      Serial.println("Received String: " + IncomingString);

      if (IncomingString.indexOf("CIFSR") != -1) {
        digitalWrite(LED, HIGH);
        Serial.println(IncomingString);
      }
      if (IncomingString.indexOf("GMR") != -1) {
        digitalWrite(LED, LOW);
      }
    }
  }
  // listen for communication from the ESP8266 and then write it to the serial monitor
  if ( ESPserial.available() )   {
    Serial.write( ESPserial.read() );
  }

  // listen for user input and send it to the ESP8266
  if ( Serial.available() )       {
    ESPserial.write( Serial.read() );
  }

}

include the LED for debugging, then change AT+CIFSR to AT+CWJAP="SSID","password"
and again move Rx and Tx The \ backslashes necessary

EDIT: I used String variables, I know senior members say "NO STRING VARIABLES", but it works

or I am sorry AT+GMR to AT+CWJAP="",""

forgot I commented out AT+CIFSR

I have a breakout board, but some people insist that you need a voltage divider on transmit to the ESP8266 because it uses lower voltage logic

but I don't think the AT commands would work with hard wired serial pins if that were the case.

Hmm, I get this when trying to compile/upload.

GetFileAttributesEx C:\Users\ROBERT~1\AppData\Local\Temp\arduino_build_78405: The system cannot find the file specified.

Error compiling for board Arduino/Genuino Uno.

you can try try Martyn Currey's exact code, this time move Rx to 3 and Tx to 2

http://www.martyncurrey.com/arduino-to-esp8266-serial-commincation/

and you have to reset the baud rate of ESP8266, do so with hardwired pins and serial monitor, baud rate set to 115200 both NL & CR, issue AT+UART_DEF=9600,8,1,0,0 as in here Tutorial: Making the AT Command ESP8266 WiFi Work with Arduino - SwitchDoc Labs Blog then reset baud rate to 9600, issue AT, wait for "OK" response

then upload code

or, better yet, ignore lunatic ravings (it's really hard to troubleshoot on the forum)

// Basic serial communication with ESP8266
// Uses serial monitor for communication with ESP8266
//
//  Pins
//  Arduino pin 2 (RX) to ESP8266 TX
//  Arduino pin 3 to voltage divider then to ESP8266 RX
//  Connect GND from the Arduiono to GND on the ESP8266
//  Pull ESP8266 CH_PD HIGH
//
// When a command is entered in to the serial monitor on the computer 
// the Arduino will relay it to the ESP8266
//
 
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2, 3); // RX | TX
 
void setup() 
{
    Serial.begin(9600);     // communication with the host computer
    //while (!Serial)   { ; }
 
    // Start the software serial for communication with the ESP8266
    ESPserial.begin(9600);  
 
    Serial.println("");
    Serial.println("Remember to to set Both NL & CR in the serial monitor.");
    Serial.println("Ready");
    Serial.println("");    
}
 
void loop() 
{
    // listen for communication from the ESP8266 and then write it to the serial monitor
    if ( ESPserial.available() )   {  Serial.write( ESPserial.read() );  }
 
    // listen for user input and send it to the ESP8266
    if ( Serial.available() )       {  ESPserial.write( Serial.read() );  }