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
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.
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.
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.
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.
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?
// 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("");
}
}
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.