ESP8266 Access Point mode send HTML response

Hi,

I've been experimenting with an ESP8266 hooked to my Arduino via an ESP01 adapter.

It seems to work fine, I can send commands to it, it responds.

When I set it in AP mode (AT+CWMODE=2) and try to respond to requests with an HTML page it works, BUT the browser seems to load the page continously, even though I can see the page displayed with the expected content.

Also, it only works if my HTML response is fairly low (somewhere around 70 chars is max). If I go beyond that it doesn't work anymore, it doesn't even seem to receive the commands properly.

I basically send the ESP8266 the following:

AT
AT+RST
AT+CWMODE=2
AT+CIPMUX=1
AT+CIPSERVER=1,80 //starts the server on port 80
AT+CIFSR //shows the IP 192.168.4.1
AT+CIPSEND=0,74
....here goes the HTML 74 chars long....

The HTML looks like this:
String html = "";
html.concat("

");
html.concat(random(100));
html.concat("

");
html.concat("\r\n\r\n");

Do I have to split the HTML into multiple shorter chunks? How do I do that, and how do I use it with CIPSEND (eg. multple CIPSEND commands with the shorter lengths, or one CIPSEND with the length of the entire string and then send chunks)?

I've attached the files I'm working with (the library is written by me and I've tried 2 ways of sending the commands).

Thanks for your help in advance

EZ8266.cpp (8.02 KB)

EZ8266.h (1.51 KB)

wifiTest.1ino.ino (767 Bytes)

no one?

codingdude:
no one?

Maybe you would get a better response if you read and followed the forum guidelines in the sticky post. And that includes posting a schematic: your problem could be at least partly power supply related, for example.

Send http header with connection type “close”.

In cipsend after id specify how much data you are sending (http header and html).

Dont forget to count /r and /n as 1 byte each.

Thanks @surepic I did try something like that, but then I guess I'm running into the problem that the message I try to send is too big.

I added something like this:

String html = "HTTP/1.1 200 OKrn";
html.concat("Connection: close\r\n");
html.concat("rn");
html.concat("");
html.concat("

");
html.concat(random(100));
html.concat("

");
html.concat("\r\n\r\n");

And then send the string via an AT+CIPSEND command. But if I do this nothing happens when I try to access it via a browser.
The string is probably already too big to send in one go.

Also, I'm using html.length() to get the length of the string. I probably shouldn't be using String but my C is a little rusty

regarding @PaulRB request for the schematic it's nothing complicated, I just hook the ESP01 adapter like this:

VCC to the 5V (the adapter handles the power down to 3V)
GND to arduino ground
and RX, TX to the pins as indicated in the .ino

The thing works because I get all commands to respond OK, only run into trouble when I try to send a longer HTML page.

What adaptor? Does it also include level converters 3.3V<--->5V for the TX & Rx lines?

Not a direct answer to your question, but consider programming the ESP8266 directly. It's much easier to debug than AT-commands, and there are many good resources on how to host HTML pages with it.

If you really, really want to use AT-commands (even though there are many reasons why you shouldn't), use a high-level AT library rather than trying to write the AT code yourself.

Pieter

I dont know what html.length does im not using esp library.
If html.length is calculating length of html portion of data to be send then you have to add your http header size if its const size then add to the length and send via cipsend but then you still have to mention html length i.e. html portion size in “content size” in http header.
Try with chrome first that browser doesnt care about correct headers send html portion only.

Another advice split your data to be sent into chunks but still in cipsend mention not the chunk size but size of all data. Then send with chunks esp will wait till you will send everything mentioned in size.

@PaulRB- esp01 adapter does converting to proper logic level and has 1000uf capacitor to solve voltage drop issues.

@PieterP you wrote about reasons not to use AT commands can you tell us reasons? I am using AT commands in my projects its very stable would like to know what is wrong with that style/method.
I came into that dilema to use at or write code for esp then i thought if at firmware is written by espressif then it must be better then any library written for it. That was the only reason i decide to adapt for esp AT commands.

PieterP:
I really don't recommend AT commands for anything other than really simple stuff (and even then programming it directly is easier).
If you need more advanced functions like MQTT, programming it directly is the way to go.
The AT vs direct programming matter has been discussed many times on this forum.

AT commands and complex networking protocols
AT commands are a convoluted way to communicate with the ESP, and all code executes on the Arduino. AT commands only really support raw TCP and UDP, if you want to use protocols on top of that (e.g. HTTP, MQTT, OSC ...), all that code executes on your Arduino, using a lot of resources. Especially if you need to parse text-based requests requiring large or dynamic buffers.
It's also much, much harder to debug or to check for errors. If you program the ESP directly, you have high-level functions and classes for these protocols, that provide a simple interface and you can easily check if a function completed successfully by checking if it returned true or false. This makes error handling much simpler.
These libraries do not exist for AT commands, and even if they do exist, they are much less mature. Their documentation is often very poor, and the online resources are limited, because pretty much nobody uses them.

If you have to do multiple protocols at once (e.g. a HTTP web server and an OSC client), you can just about forget about it if you try to use AT commands.
Pretty much all code is blocking, because it uses the same Serial connection, and you can really only do one thing at once.

If you program the ESP directly, you can have many services running "in parallel". (e.g. a HTTP web server, an OSC client, a DNS server, mDNS server, OTA service ... all at once.)

Resource management
The Arduino has limited resources (32KiB flash, 2KiB or RAM and an 8-bit 16MHz CPU in case of the UNO) while the ESP is much faster and has many times more memory (4MiB flash, 64 KiB of instruction RAM, 96 KiB of data RAM, 32-bit CPU up to 160MHz).
So if you've got the choice, it makes sense to run all heavy networking code and text processing/parsing on the ESP, and save the resources of the Arduino for your actual microcontroller sketch.

Abstraction and code quality
My preferred approach is to have one piece of code on the ESP that connects to WiFi, sets up mDNS services, OTA update services, web servers, UDP listeners, MQTT clients ... whatever you need.
All code that receives and parses network requests runs on the ESP as well. If the ESP receives a command (e.g. "Turn on the lights"), it sends a command over serial to the Arduino (e.g. "Pin 16, HIGH").

On the other hand, if the Arduino wants to send something over the internet (e.g. to post the temperature to a database), it just sends it over serial to the ESP (e.g. "Temperature=21.3°C"), and the ESP takes this data, establishes a connection to the server, constructs a request, adds the right data, sends the request, checks the response, etc.

This greatly improves the readability and abstraction of both pieces of code. Having to mix networking stuff and microcontroller logic in one sketch is just really cumbersome.

For communication between the two MCUs, take a look at Robin2's Serial Input Basics.

Debugging
Another advantage is that you can test the microcontroller code without the ESP, by just using the Serial Monitor. The Arduino doesn't care if it's talking to an ESP8266 or to a human with a serial console.

You can also use UART1 on the ESP for printing debug information from the ESP, and UART0 on the Arduino for printing debug information from the Arduino.
If you use AT commands, debugging becomes a whole lot harder, especially if you use an UNO, where the only UART that is used for debugging over USB is also used for AT communication. This means that you can't read the responses from the AT firmware, only the commands sent by the Arduino.

Please don't take my word for it, try it yourself, and pick the approach you like the most.
I used AT commands when I got my first ESP8266, but ever since I discovered the ESP8266 Arduino Core for programming it directly, I've never touched AT commands again.

Whichever approach you pick, I'd recommend to get a dev board that supports both approaches.
At $3.50, there's really no valid argument against a WeMos D1 mini (clone).
Even if you decide to use AT commands, it will still be easier because you have a decent power supply on-board, and you can use the USB interface to update the AT firmware really easily.

Thanks for interesting “article” now it makes sense. But by doing it on arduino has one big advantage for newbies like me. In small memory of uno you have to rewrite your code few times making it smaller and smaller and come up with new methods to overcome that problems.

surepic:
But by doing it on arduino has one big advantage for newbies like me. In small memory of uno you have to rewrite your code few times making it smaller and smaller and come up with new methods to overcome that problems.

I would hardly call that an advantage ...

Libraries that were available at the time i started esp projects with arduino were using Strings. Too many requests to Serial class and String class functions.

Because of small memory on uno i was forced to write code from scratch without using them just cstrings, intensive use of progmem and some of data reading from eeprom.