Can't connect to Wifi with ESP8266

Hi,
I have an arduino compatible device with an ESP8266. Both work fine and using the BareMinimum script I can access the wifi, upload a tiny script that displays hello world on the ESP8266 with no problem.

I got excited and wanted to do the DHT11 project with ThingSpeak. Long story short, I narrowed the problem down to a wifi connection problem. Again, as I said with the bareminimum i am able to send all AT commands with no problems.

This is the script that I am presently using:
Most articles mention #include <ESP8266WiFi.h> but for me the script only compiles if the include is
#include <ESP8266wifi.h>

Notice the case in wifi vs WiFi. I think that shouldn't matter, but I am wondering why it is different for me.

Executing that code, I get "Connecting to Tundra....................." in the setup method.
Code is based on sparkfun script. I removed the parts that are irrelevant to the diagnosing the issue.

Thanks

#include <ESP8266wifi.h>
#include <WiFi.h>

const char* ssid     = "Tundra";
const char* password = "MyPassword";


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() {


}

ESP8266WiFi is the library included with the esp8266 core:

This library is used when you are uploading sketches directly to the ESP8266

ESP8266wifi is a different library:

This library is used when you have the AT firmware running on an ESP8266 that is connected to a separate Arduino via serial and you are uploading your sketches to the separate Arduino, not the ESP8266

It sounds like you're using the second configuration. Thus, you may find that examples for ESP8266WiFi do not apply to the ESP8266wifi library. I actually have found that I prefer the WiFiEsp library to the ESP8266wifi library for the AT module usage of ESP8266.

Yes. That second library is terrible or may be the example is not great. Do you think that first one would work with my configuration? I'll give it a try anyway, but it would be nice to know if is expected to work or not. It seems the first library is more powerful.

sed003:
Do you think that first one would work with my configuration?

If by first one you mean ESP8266WiFi and by my configuration you mean ESP8266 as serial module running AT firmware then definitely not.

Give WiFiEsp a try. It's a very nice library. The only problem with it is the author has logging output to Serial turned on by default. This may be useful for troubleshooting but it wastes a lot of memory and slows down program execution. It also will make the library not work if you have your ESP8266 connected via Serial. Apparently the author just assumes that everyone will be using either software serial or a board with multiple UARTs. All the ESP8266 shields connect via Serial so that's really a big problem, especially since the logging feature isn't documented. You can turn it off as shown here:

I have the Ai-Thinker board. I tried the WiFiESP which seems very simple, but it tells me Shield not present. I know the board is connected correctly and is working so not sure what it could be

I noticed the code says:

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

my module is plugged in the 0 and 1 of the arduino board.

sed003:
I noticed the code says:

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

my module is plugged in the 0 and 1 of the arduino board.

You need to delete those two lines

Delete the line in setup():

Serial1.begin(9600);

Change the line in setup() from:

WiFi.init(&Serial1);

to:

WiFi.init(&Serial);

Remove all Serial.print() and Serial.println() from the sketch. Since the ESP8266 is connected to Serial you won't be able to use Serial output and Serial Monitor to get output from the Arduino. This means you are pretty much working in the dark until you can start getting output via WiFi.

Disable logging output as I have shown in Logging disabled by default by per1234 · Pull Request #46 · bportaluri/WiFiEsp · GitHub

Thanks that worked. It sucks though to not be able to see the output. I had to scan the network looking for the MAC address of my device and make sure it is pingable which it was.

Thanks for all your help.

One more question. Do you think this should work?

#include <ThingSpeak.h>

in the setup method:
ThingSpeak.begin(client);

in the loop
ThingSpeak.setField(1,10); //1 is field1, 10 is my test value
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

I am not sure whether it is not working because ThingSpeak.begin(client) is expecting a different type of client than the WiFiEspClient.

sed003:
ThingSpeak.begin(client) is expecting a different type of client than the WiFiEspClient.

That's strange because that's the thing I like most about the WiFiEsp library, that it creates a client object so that you can use it just as you would Ethernet or WiFi libraries. For example, I have been able to use it with Temboo library no problem by passing the client object to that library, even though the Temboo library was not written specifically for use with WiFiEsp library.

That's my code:

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "Tundra";            // your network SSID (name)
char pass[] = "00867924";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

#include <WiFiEsp.h>
//************************
#include <WiFiEspClient.h>
#include <WiFiEspServer.h>

#include <ThingSpeak.h>


#define WEBSITE "api.thingspeak.com"

String apiKey ="9W8YE"; // api from ts (not my real apiKey)
//const char* WEBSITE = "api.thingspeak.com"; 

unsigned long myChannelNumber = 1794; (not my real channel number)
const char * myWriteAPIKey = "9W8YE";
WiFiEspClient client;  
//************************

void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);  
  // initialize ESP module
  WiFi.init(&Serial);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
  ThingSpeak.begin(client);
}

void loop()
{
  { 
    ThingSpeak.setField(1,10);    
    // Write the fields that you've set all at once.
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);  
  }

  if(!client.connected()){
    client.stop();
  }
  client.flush();
  client.stop();
  delay(10000);
}

That code compiles fine for me. You forgot to remove the SoftwareSerial stuff, which wastes a bunch of memory but shouldn't cause it to not compile.

Please explain what you mean by:

sed003:
ThingSpeak.begin(client) is expecting a different type of client than the WiFiEspClient.

Does the sketch not compile for you? If not then please post the error messages from the console.

The code compiles fine and is uploaded to the device. I just don't see any data sent to the Thingspeak portal. Not sure how to pin point what the issue is.

I got an FTDI tool. Would that help?

HI..
iam trying to do an iot project. Im using an arduino mega and esp8266 esp01 to send some sensor datas to the cloud. but i have no idea how to connect it to my wifi. what code sample ? whether to program the arduino or the wifi module for this?

Kindly follow the steps as given in the blog post..!
Hope it helps you..

Use IoT with esp8266

You can test with

WiFi.persistent(false);
  WiFi.mode(WIFI_OFF);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

This working well with esp8266 v7

// Load Wi-Fi library
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>

SoftwareSerial myserial(D6,D5);


// Replace with your network credentials
const char* ssid     = "DigiHome";
const char* password = "";

// Set web server port number to 80
WiFiServer server(80);
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
  Serial.begin(115200);
  myserial.begin(9600);
  WiFi.mode(WIFI_AP);
  //  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // if you want to configure another IP address
  WiFi.softAP(ssid, password);
  server.begin();
void WiFiStart()
  {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.print(".");
  }
  // Print local IP address and start web server
  //Serial.println("");
  //Serial.println("WiFi connected.");
  //Serial.println("IP address: ");
  //Serial.println(WiFi.localIP());
  server.begin();
}
void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    //Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
          //  client.println("HTTP/1.1 200 OK");
           // client.println("Content-type:text/html");
           // client.println("Connection: close");
           // client.println();
 // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    //Serial.println("Client disconnected.");
    Serial.println("");
  }
}

that will help let me know,ok

I don't recommend the esp01. It is a piece of junk. Use the nodemcu. Much better. It is like an arduino with built in wifi. It uses the exact same code, but you will have to select the board type when compiling. A lot less IOs though, so make sure you have enough pins to handle your project.