ESP8266 hookup to Arduino

Hi -

I have purchased a ESP8266 wifi module and I have some questions. It says that the operating voltage is 3.3v so I will use an external regulator to supply that. I've read somewhere that the TX and RX pins also require 3.3v.

  • What voltage does a HIGH on a TX or RX port provide on a Uno
  • What voltage does a HIGH on a TX or RX port provide on a 3.3v Arduino?

Adafruit and a bunch of other sources (seller) recommend a logic level shifter. Can anyone tell me what this does to the voltages of the pins?

I don't have logic level shifter, can I use other components such as a resistor to get the voltage down?

Also I notices that it uses UART instead of SPI. Do I need an external programmer to use this? I have a Tiny programmer, can I use that? What about Arduino as ISP?

Thanks

What voltage does a HIGH on a TX or RX port provide on a Uno

5V

What voltage does a HIGH on a TX or RX port provide on a 3.3v Arduino?

3V3

Can anyone tell me what this does to the voltages of the pins?

It changes the logic voltage signal from on level to the other.

I don't have logic level shifter, can I use other components such as a resistor to get the voltage down?

Yes a 510R series and a 1K pull down will do it.

Also I notices that it uses UART instead of SPI. Do I need an external programmer to use this?

You should not need to program this device.

Thanks for the info Mike. I'm not sure I have that type of resistor since I haven't ever heard of it. I only have a resistor kit with a lot of different resistors. I'll do some digging but if I need to purchase something I might as well get a logic level shifter.

Will I need this resistor for both RX and TX, as well as the pulldown?

Also I may not need to program it or I may need to depending on the stock firmware. I do not have a UART programmer. Any suggestions for a good one? Can this still be USB based? I'm not really familiar with UART, SPI and other communication protocols. I know Arduino uses them but the IDE has a nice way of hiding that logic from me :slight_smile:

I have been using these little WiFi boards a lot lately.

So, for the programmer, you can build a simple one. I made one from these instructions and it works GREAT!

Im using level shifters, picked up off e-bay for $1.20 each and ordered 10. They are handy to have available. From what I have read the ESP8233 TX line can go directly to the arduino RX line without a problem. It is the Arduino TX line that needs to be level shifted. It can be done with a resistor based voltage divider in a pinch.

Now for the fun stuff that does not seem to be documented well.

I tried the three libraries I found for it,

#1) used a lot of memory on the device. Digging into it, they were using Strings for everything.
#2) did not work at all, it would not initalize the unit correctly and when digging into it I could not figure out what they were trying to do.
#3) Had issues with memory leaks and kept reseting the unit after every call.

The current AT firmware for it is v0.21 and I am having a lot of issues getting it to work with any of the code above. The same with code I wrote for it. It accepts and responds to the first connection then stops responding.

The older version of the firmware code v0.20 has a bug in it. If it receives a specific auth packet on the wifi network it will stop sending and responding to arp. Seems to freeze for about 5 min.

I have run into issues with the SoftSerial lib that comes with the IDE as well. Ended up using the AltSoftSerial library which fixed a lot of small issues with garbaged data coming in from the unit at 9600.

Dont know if this will help, but here is the frame work that wrote to communicate with the unit. I am always open to suggestions as to how to improve the code. I am still new to Arduino programing. :wink:

#include <AltSoftSerial.h>
/*

bool send_html(char *html, int conid)          Sends the HTML contained in *html to the listed connection ID
bool Close_Connection(int conid)               Closes the Connection at Connection ID
bool reset()                                   Sends a reset to the unit
bool Join_AP(char ssid[50], char passwd[50])   Joins the AP with the given SSID and Password
bool WifiMode(int mode)                        Sets the Wifi Mode 1=STA 2=AP 3=Both
bool set_mux(int mux)                          Sets the MUX 0=single conneciton 1=multi Connection
bool start_server(int mode, int port)          Set up the server on the given port mode:0=close, 1=open
void WhatsMyIP()                               Sets a global variable called MyIP to the IP Address that the unit is listening on.

ESP8266 Firmware must me AT-0942.bin
*/

// Activate Debug mode to the hardware serial.
//#define DEBUG

#ifdef DEBUG
#define DEBUG_PRINTLN(x)  Serial.println (x)
#define DEBUG_PRINT(x)  Serial.print(x)
#define DEBUG_BEGIN(x)  Serial.begin (x)
#else
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINT(x)
#define DEBUG_BEGIN(x)
#endif

AltSoftSerial esp8266; //pin 8 and 9

char MyIP[20];


void setup() {
  DEBUG_BEGIN(19200);
  esp8266.begin(9600);

  DEBUG_PRINTLN(F("Reseting unit"));
  if (!reset()) DEBUG_PRINTLN(F("FAILED"));
  DEBUG_PRINTLN(F("Setting WiFi Mode"));
  if (!WifiMode(1)) DEBUG_PRINTLN(F("FAILED"));
  DEBUG_PRINTLN(F("Joining AP"));
  if (!Join_AP("MY-SSID", "MY-PASSWORD")) DEBUG_PRINTLN(F("Failed"));
  DEBUG_PRINTLN(F("Getting IP Address"));
  WhatsMyIP();
  DEBUG_PRINT(F("My IP Address is "));
  DEBUG_PRINTLN(MyIP);
  DEBUG_PRINTLN(F("Setting MUX"));
  if (!set_mux(1)) DEBUG_PRINTLN(F("FAILED"));
  DEBUG_PRINTLN(F("Starting Server on Port 80"));
  if (!start_server(1, 80)) DEBUG_PRINTLN(F("FAILED"));
}

void loop() {
  // put your main code here, to run repeatedly:
  webpage();
}

void webpage()
{

  char tdr[101];
  char page[5];
  if (esp8266.available()) // check if the esp is sending a message
  {
    if (esp8266.find("+IPD,"))
    {
      DEBUG_PRINTLN(F("Found +IPD"));
      delay(1000);
      int connectionId = esp8266.read() - 48; // subtract 48 because the read() function returns
      // the ASCII decimal value and 0 (the first decimal number) starts at 48
      if (esp8266.find("GET ")) {
        esp8266.readBytesUntil(' ', page, 4);
        if ( strstr(page, "/api") ) {
          while ( esp8266.read() != -1 );
          DEBUG_PRINTLN(F("Sending JSON"));
          //Put JSON output here
        } else {
          while ( esp8266.read() != -1 );
          DEBUG_PRINTLN(F("Sending WebPage"));
          //Put WebPage Output HERE
          DEBUG_PRINTLN(F("Closing Connection"));
          if (!Close_Connection(connectionId)) DEBUG_PRINTLN(F("Failed!"));
        }
      }
    }
  }
}


bool send_html(char *html, int conid)
{
  char cipSend[25];
  snprintf(cipSend, 25, "AT+CIPSEND=%i,%i\r\n", conid, strlen(html));
  esp8266.println(cipSend);
  delay (1000);
  if (esp8266.find(">")) {
    esp8266.println(html);
  }
  delay (1000);
  return esp8266.find("OK");
}

bool Close_Connection(int conid) {
  char cipSend[25];
  snprintf(cipSend, 25, "AT+CIPCLOSE=%i\r\n", conid);
  esp8266.println(cipSend);
  delay(500);
  return esp8266.find("OK");
}


bool reset() {
  esp8266.println(PSTR("AT+RST\r\n"));
  delay(1000);
  return esp8266.find("ready");
}

bool Join_AP(char ssid[50], char passwd[50]) {
  char cwjap[101];
  snprintf(cwjap, 100, "AT+CWJAP=\"%s\",\"%s\"", ssid, passwd);
  esp8266.println(cwjap);
  delay(4000);
  return esp8266.find("OK");
}

bool WifiMode(int mode) {
  char cwmode[14];
  snprintf(cwmode, 14, "AT+CWMODE=%d\r\n", mode);
  esp8266.println(cwmode);
  delay(1000);
  return esp8266.find("OK");
}

bool set_mux(int mux) {
  char cipmux[14];
  snprintf(cipmux, 14, "AT+CIPMUX=%d\r\n", mux);
  esp8266.println(cipmux);
  delay(1000);
  return esp8266.find("OK");
}

bool start_server(int mode, int port) {
  char server[19];
  snprintf(server, 19, "AT+CIPSERVER=%d,%d\r\n", mode, port);
  esp8266.println(server);
  delay(1000);
  return esp8266.find("OK");
}

void WhatsMyIP() {
  char incoming;
  int ndx = 0;
  esp8266.println(PSTR("AT+CIFSR\r\n"));
  delay(1000);
  esp8266.find("+CIFSR:STAIP,\"");
  esp8266.readBytesUntil('\"', MyIP, 18);
}

Here is what I have going so far, the php page does a JSON call to the unit to get the data.

http://xganon.com/temp.php

If you dont get the temp/humidity information on the page, give it 5 min and try again. It hit the problem mentioned earlier.

Dude, thanks for the information and your code. I'll need to gather some things and have a sit to make this all work, so naturally it'll take a while before I can post back.

Thanks again, I'll be sure to keep you guys in the loop.

Will I need this resistor for both RX and TX, as well as the pulldown?

No the two resistors will only cut down a voltage from 5V to 3V3. So that is from the 5V system into the 3V3 system. Feeding a 3V3 output into a 5V input will normally be fine.

I'm not sure I have that type of resistor since I haven't ever heard of it.

???
It is not a resistor type those were resistor values. 510R is 510 ohms, 1K is 1,000 ohms.

I only have a resistor kit with a lot of different resistors.

So look and see if you have those values:-
510R = green brown brown
1K = brown black red