Go Down

Topic: Can't connect to Wifi with ESP8266 (Read 32431 times) previous topic - next topic

sed003

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

Code: [Select]

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


}

pert

ESP8266WiFi is the library included with the esp8266 core:
https://github.com/esp8266/Arduino
This library is used when you are uploading sketches directly to the ESP8266

ESP8266wifi is a different library:
https://github.com/ekstrand/ESP8266wifi
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.

sed003

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.

pert

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:
https://github.com/bportaluri/WiFiEsp/pull/46/commits/1f478a3805de3924809d24d6be45b6f5d1723f7d

sed003

#4
Nov 06, 2016, 01:28 am Last Edit: Nov 06, 2016, 02:21 am by sed003
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

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.

pert

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():
Code: [Select]
Serial1.begin(9600);

Change the line in setup() from:
Code: [Select]
WiFi.init(&Serial1);
to:
Code: [Select]
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 https://github.com/bportaluri/WiFiEsp/pull/46/commits/1f478a3805de3924809d24d6be45b6f5d1723f7d

sed003

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.

sed003

#8
Nov 06, 2016, 10:56 pm Last Edit: Nov 07, 2016, 01:49 am by sed003
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. 

pert

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.

sed003

That's my code:

Code: [Select]

#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);
}


pert

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:
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.

sed003

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.

sed003

I got an FTDI tool. Would that help?

Reji_krish

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?

Go Up