Page 1 of 5

WiFi Communication with ESP8266

Posted: Wed Sep 24, 2014 5:20 pm
by ahmedkhalid
Hello Everyone!!
Today I an in need of great favor from you guys. I want to interface AVR ATMEGA-32 mico-controller with the latest and the cheapest WiFi module (ESP8266) available. But the thing is I don't have any previous knowledge about WiFi Communication. I don't know how to configure this module? Which software/app to be use. If anyone have any idea about this module and how to interface it with micro-controller then plz help me. It will indeed be a great favor.
Thanks in advance!!

Re: WiFi Communication with ESP8266

Posted: Wed Sep 24, 2014 6:37 pm
by Benj
Hello

Yes this is coming soon. I've got a module working and replying correctly and made progress on the Flowcode component this afternoon. So far everything is working well and I hope to finish the component tomorrow.

Re: WiFi Communication with ESP8266

Posted: Wed Sep 24, 2014 7:55 pm
by waleedabbasi12
Hi Benj
I also want to interface this cheapest module with Atmega32A for my project.

Can you please guide me to do this job.

Thanx in advance

Waleed

Re: WiFi Communication with ESP8266

Posted: Wed Sep 24, 2014 8:25 pm
by ahmedkhalid
Hello Benj!

I am desperately waiting for this flowcode component.

Re: WiFi Communication with ESP8266

Posted: Thu Sep 25, 2014 5:55 pm
by Benj
Hello,

Here is the progress so far...

Here is the component, need to be copied to the "C:\Program Files (x86)\Flowcode 6\components" directory before restarting Flowcode.
WLAN_ESP8266.fcpx
ESP8266 Component
(17.21 KiB) Downloaded 800 times
It's not 100% complete but is fairly reliable.

Here are some examples so far.
ESP8266_FirmwareTest.fcfx
Prints out the module firmware version onto a LCD.
(6.5 KiB) Downloaded 688 times
ESP8266_WifiScan.fcfx
Scans for nearby WIFI APs and displays them on the LCD complete with sig strength and encryption type
(11.42 KiB) Downloaded 610 times
ESP8266_CreateAP.fcfx
Creates an access point you can connect your phone or laptop to
(7.25 KiB) Downloaded 581 times
ESP8266_JoinAP.fcfx
Joins an existing AP such as a WIFI enabled internet router
(7.25 KiB) Downloaded 538 times
ESP8266_TCPClient.fcfx
Joins an existing AP such as a WIFI enabled internet router then connects to the MatrixTSL website, requests a text file and displays the contents of the file on the LCD.
(9.83 KiB) Downloaded 647 times
Only thing left to get working is the TCP Server which should be simple enough.

I have made to following connections to the module.

GND -> GND
VCC -> 3V3 with at least 300mA spare current
TXD -> RX on Microcontroller
RXD -> TX on Microcontroller
RST -> VCC
CH_PD -> VCC

I used a 3V3 microcontroller and this is working very well. A 5V microcontroller would need to convert the TX -> RXD connection to provide 3V3. A simple resistor based potential divider should work well.

I also connected the TXD pin of the module to the RX pin of my 4D Systems Serial USB cable and this allows me to monitor all the data travelling in and out of the module using RealTerm on my PC.
Monitor.jpg
Monitor.jpg (158.22 KiB) Viewed 22019 times
Note that I have had some problems with the TCpClient example. It works well to collect a text file from the Matrix server but you cannot communicate with Google and a lot of other sites as you are using the IP address rather then the DNS name and new security measures means this is frowned upon. If I try to connect to Google as a client then it currently locks the WIFI module and you have to power cycle it.

I would recommend using a hardware UART channel as the baud is pretty high and the component requires the hardware receive interrupt to function, some of the lower speed devices may struggle with this baud I need to test on PIC, AVR and Arduino to confirm if these will all work, I think they should but not confirmed yet.

Re: WiFi Communication with ESP8266

Posted: Thu Sep 25, 2014 6:43 pm
by waleedabbasi12
Hi Benj

Is there any problem if I connect the TXD and RXD pins of the module to 5V microcontroller?

Re: WiFi Communication with ESP8266

Posted: Thu Sep 25, 2014 6:55 pm
by Benj
Hello,

Module TXD to 5V Micro RX pin is ok as 5V side is an input.

Module RXD to 5V Micro TX pin is not ok. You will need two resistors, I have heard that 1K and 2K works well.

This should work.
Micro TX Pin -> 1K -> Module RXD -> 2K -> GND

If you don't have 2K resistors then use 2 x 1K in series.

(5V / (1000 + 2000)) * 2000 = 3.3V

If anyone is having issues then I seem to remember coming across an issue before where the RX interrupt doesn't fire correctly with 3V3 incoming signals on specific devices. Therefore the E-block I'm developing will have full level shifting capabilities on the I/O as well as it's own switch mode regulator to supply the current. If anyone does have an issue then let us know what target device your using and what clock speed.

Re: WiFi Communication with ESP8266

Posted: Thu Sep 25, 2014 8:38 pm
by ahmedkhalid
Thanks a ton Benj!
I just want to know is there any way to simulate this module in flowcode? Like any injector or any thing else for simulation?

Re: WiFi Communication with ESP8266

Posted: Fri Sep 26, 2014 9:42 am
by Benj
Hello,

Currently no simulation but the code is there to do the simulated TCP/IP comms so I should be able to get this working. No injector planned at the moment to simulate the module responses but it should be possible. I wrote a blog a while back about creating injector components so this should help out anyone who wants to try and create a component.

Re: WiFi Communication with ESP8266

Posted: Fri Sep 26, 2014 12:37 pm
by Benj
I have just tried a 8-bit PIC device running at 5V and can confirm that the potential divider works well on the microcontroller TX pin.

To get the baud rate working I had to change the 19.6608MHz crystal on the EB006 for a 18.432MHz crystal to hit the 115200 baud rate on the nose. The 19.6608MHz crystal seems slightly too much out of tolerance to work.

Here is my test program.
ESP8266_FirmwareTest_16F1937.fcfx
(6.46 KiB) Downloaded 471 times
Here are the calculations.

((19660800 / 115200) - 16) / 16 = 9.6666

((18432000 / 115200) - 16) / 16 = 9

A crystal speed of 20.2752MHz should also work but may be too fast for a lot of 8-bit PICs to run correctly.

Re: WiFi Communication with ESP8266

Posted: Fri Sep 26, 2014 4:45 pm
by ahmedkhalid
Hello Benj!
The sample code you provided, I use them on my hardware. When WiFi module is not connected to micro-controller, even then it initializes the module. So I used a while loop and now it does not initializes the module. I am using:
> ATMEGA 16
>internal clock frequency 8 Mhz
>using potentiometer for RX pin of module
>baud rate= 115200

Is there any problem with the above mentioned configurations? If so, what should I do to make my module work?

Re: WiFi Communication with ESP8266

Posted: Fri Sep 26, 2014 5:11 pm
by Benj
Hello,

Do you have a scope to test out your baud rate at all? Could be the internal oscillator is not giving you an accurate enough clock or 8MHz is too slow for 115200 baud to work correctly.

((8000000 / 115200) - 16) / 16 = 3.3

The AVR baud calculation currently gives a baud register value of 3.3 so there is quite a large error at 8MHz. Ideally the baud number should be an integer or slightly over e.g. 3.01 would likely be in tolerance.

8000000 / ((3 x 16) + 16) = 125000 bits per second

9.216MHz should give you bang on 115200 bits per second but there will be other speeds that also work. 115200 is a bit of a tricky baud to achieve.

I've now got the webserver example working but am having problems with the string substitutions. Basically we send out a string and replace %0 in the outbound HTML with OutString0, however before we start sending we need to tell the module how many bytes to transmit so this is worked out before the substitution. I'll think on it and see if I can find a solution, inbound substitutions should be working ok as is.
WLAN_ESP8266.fcpx
(21.54 KiB) Downloaded 522 times
ESP8266_TCPServer.fcfx
(11.48 KiB) Downloaded 503 times
Also here is the component source if anyone wants to see how it's been done, have a play with or edit the component code.
WLAN_ESP8266.fcfx
(262.1 KiB) Downloaded 569 times
Most of the macros are not used and taken from the Wiznet component, look at the interface manager to find the exposed macros.

Re: WiFi Communication with ESP8266

Posted: Sat Sep 27, 2014 2:24 pm
by ahmedkhalid
Thanks for the guidance and support! It really means alot!!

Now the thing is I was able to create an AP and also joined the module with the existing WiFi AP.

But now when I am trying to create an AP again, Lcd displays "Fail".

Do I have to connect the RST pin of the module with any of the micro-controller pin?? For now I have connected it to VCC.

Re: WiFi Communication with ESP8266

Posted: Mon Sep 29, 2014 8:23 pm
by hyperion007
I to get AP Fail. Tested with Reset connected to VCC and to the MCU but same result.

However I do see an AP named ESP_992153 using the app Wifi Analyzer on my phone. It is broadcasting on channel 1.

I will try to get my hands on a 18.432MHz crystal and try again.

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 5:07 am
by Rudi
ahmedkhalid wrote: Do I have to connect the RST pin of the module with any of the micro-controller pin?? For now I have connected it to VCC.
Base:
------
VCC -> VCC, CH-PD(chip enable)
GND -> GND

choices:
--------
RST
if you want to Reset by Mikrocontroller, you can connect it to a pin and switch ~20ms to gnd

GPIO0
if you want to Firmware update over Mikrocontroller then you must connect to a pin and make this procedure

RST + GPIO0 goes GND ( LOW ) ~20ms
this will reset the ESP8266
then free RST
wait ~20ms
then free GPIO0
then the ESP8266 is in Firmware upload Mode

in upload time, the GPIO2 is flashing
if Firmware Upload finnished, the GPIO2 goes low.
this you can int or watch then make a RST by pin and the new Firmware is booting.

btw
If you connect the RST to Vcc ( 3,3 V+ ) or switch the pin connected pin to high -
the AT Command AT+RST never would be fire.
if wdt fire and RST is high connected, the Firmware never boot in main .

best wishes
rudi
;-)

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 6:32 am
by hyperion007
Ben, you said you had the RST pin connected to VCC, so how should we configure the component? Set it as hardware or software reset? Shouldn't the reset pin option under properties be grayed out if we choose software reset?

I have ordered a couple of 18.432 Mhz crystals now so I hope to get it up and running soon. I use the PIC18F26K80 and I hope it will work :)

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 10:45 am
by Benj
Hello,

Yes there are two reset modes, hardware toggles the reset pin and software calls the AT+RST command. I went for the software option in my programs as my hardware currently ties the reset pin to 3V3.

Yes the reset pin property should probably be greyed out or hidden when using software mode. I'll see if I can squeeze this in.
I have ordered a couple of 18.432 Mhz crystals now so I hope to get it up and running soon. I use the PIC18F26K80 and I hope it will work :)
Think this should work just fine but let us know if not.

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 11:03 am
by hyperion007
Thanks Ben. How come the baud rate is this "finicky" and requires this "special" crystal frequency?

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 11:12 am
by Benj
Hello,

It's mainly down to the high speed nature of the bus and the integer divider nature of the baud generator.

for example at 19.6608MHz

((19660800 / 115200) - 16) / 16 = 9.666

Which works out as 9 in the baud register which gives an actual baud of 19660800 / (9 x 16 + 16) = 122880 bps

We have already set the UART to high speed mode to give as much flexibility as possible.

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 11:16 am
by hyperion007
Ok, but will there be similar issues with other serial comms at the 115200 baud rate? or is this ESP8266 chip less tolerant against deviations from set baud rate?

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 2:49 pm
by Benj
Hello,

Yes the problem is getting the hardware UART running at that specific frequency. If other devices are more tolerant then it's probably just a case of getting in the ball park. I imagine that the ESP module itself is fairly tolerant but I could not get the default calc working at 19.6608 which is why I opted to do the maths and hit the baud on the nose.

The current calc is integer based so the answer 9.666 is rounded down to 9. I could try changing the calc to try and round up instead of always rounding down if the real portion of the number is 0.5 or above. At 19.6608MHz and this would give a closer match at 111709 bps and may well work as is. Certainly at 20MHz 10 gives a value that's almost on the nose but the current calculation would get it very wrong and still use 9 instead of 10.

However seeing as the calculation has been in place for years I am a bit nervous of changing it.

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 2:53 pm
by hyperion007
Sure I get that :) I was just curious, not suggesting any changes ;)

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 4:03 pm
by Benj
I'll see how well the preprocessor can deal with floating point numbers and see if I can make it slightly more tolerant by rounding up where applicable.

Not promising anything but I will have a go.

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 5:22 pm
by Benj
Chatting with Leigh we think we have a simple yet viable solution to fix the issue, well at least make it a bit more likely to get the baud literally as close as is possible for all cases.

Here are the updated files which can be used. If anyone gives them a go then please let us know how you get on.
PIC16BIT_CAL_UART.c
(22.68 KiB) Downloaded 397 times
PIC_CAL_UART.c
(22.58 KiB) Downloaded 438 times
AVR_CAL_UART.c
(58.89 KiB) Downloaded 464 times
The files should live here "C:\Program Files (x86)\Flowcode 6\CAL\TARGET" where TARGET is the family of Micro you are targeting.

For reference the calculation has changed from this (the calc provided from the datasheet).

((ClockSpeed / Baud Rate) - 16) / 16

to this.

((ClockSpeed / Baud Rate) - 8 ) / 16

Which in theory does the 0.5 round up without any nasty floating point manipulations in the pre-processor.

ARM has a different approach to calculating baud so this mod should not be necessary.

I wonder if we can put the % error into the RS232 component or CAL UART component so it's easy to see how close you are on your given platform and clock speed.

Re: WiFi Communication with ESP8266

Posted: Tue Sep 30, 2014 5:35 pm
by Benj
I can confirm that the mods allow the 16F1937 chip to work with the ESP8226 module at a clock speed of 19.6608MHz so this is good news :D

If only everything was that easy!

If I don't hear of any issues with this mod then it will be packaged into the upcoming 6.1 release.