May 18, 2015

ESP8266 WiFi library

After several nights spent developing and tweaking I finally have a working ESP8266 WiFi library called WiFiEsp.
The main characteristic of the WiFiEsp library is that is fully compatible with the standard library for the Arduino WiFi shield. This gives a cheap alternative to the expensive WiFi shield to connect Arduino to the Internet.
It uses hardware Serial1 if exists or emulate a second serial port on pins 6 and 7.
You can download WiFiEsp library from GitHub.
Please give it a try and tell me what you think.

Here is a small sketch that connects to a WiFi network.


#include "WiFiEsp.h"

// Emulate Serial1 on pins 7/6 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "Twim";            // your network SSID (name)
char pass[] = "12345678";        // your network password

void setup()
{
  Serial.begin(115200);  // initialize serial for debugging
  Serial1.begin(9600);   // initialize serial for ESP
  WiFi.init(&Serial1);   // initialize ESP serial port

  if (WiFi.status() == WL_NO_SHIELD) {   // check for the presence of the shield
    Serial.println("WiFi shield not present");
    while (true);                        // don't continue:
  }
}

void loop()
{
  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");
  
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  delay(10000);
}


May 2, 2015

Beating heart with Arduino and a MAX7219 8x8 LED matrix

This is a very simple project to display a beating heart using and Arduino board and a 8x8 LED matrix driven by a MAX7219 chip.



The wiring is very simple.
  • MAX7219 VCC pin > Arduino 5V pin
  • MAX7219 GND pin > Arduino GND pin
  • MAX7219 DIN pin > Arduino pin 2
  • MAX7219 CS pin > Arduino pin 3
  • MAX7219 CLOCK pin > Arduino pin 4

The Arduino sketch is not using any library so this is also good to understand how to directly drive the MAX7219 chip through registers.


int ANIMDELAY = 100;  // animation delay, deafault value is 100
int INTENSITYMIN = 0; // minimum brightness, valid range [0,15]
int INTENSITYMAX = 8; // maximum brightness, valid range [0,15]

int DIN_PIN = 2;      // data in pin
int CS_PIN = 3;       // load (CS) pin
int CLK_PIN = 4;      // clock pin

// MAX7219 registers
byte MAXREG_DECODEMODE = 0x09;
byte MAXREG_INTENSITY  = 0x0a;
byte MAXREG_SCANLIMIT  = 0x0b;
byte MAXREG_SHUTDOWN   = 0x0c;
byte MAXREG_DISPTEST   = 0x0f;

const unsigned char heart[] =
{
  B01100110,
  B11111111,
  B11111111,
  B11111111,
  B01111110,
  B00111100,
  B00011000,
  B00000000
};



void setup ()
{
  pinMode(DIN_PIN, OUTPUT);
  pinMode(CLK_PIN, OUTPUT);
  pinMode(CS_PIN, OUTPUT);

  // initialization of the MAX7219
  setRegistry(MAXREG_SCANLIMIT, 0x07);
  setRegistry(MAXREG_DECODEMODE, 0x00);  // using an led matrix (not digits)
  setRegistry(MAXREG_SHUTDOWN, 0x01);    // not in shutdown mode
  setRegistry(MAXREG_DISPTEST, 0x00);    // no display test
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN);

  // draw hearth
  setRegistry(1, heart[0]);
  setRegistry(2, heart[1]);
  setRegistry(3, heart[2]);
  setRegistry(4, heart[3]);
  setRegistry(5, heart[4]);
  setRegistry(6, heart[5]);
  setRegistry(7, heart[6]);
  setRegistry(8, heart[7]);
}


void loop ()
{
  // second beat
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMAX);
  delay(ANIMDELAY);
  
  // switch off
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN);
  delay(ANIMDELAY);
  
  // second beat
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMAX);
  delay(ANIMDELAY);
  
  // switch off
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN);
  delay(ANIMDELAY*6);
}


void setRegistry(byte reg, byte value)
{
  digitalWrite(CS_PIN, LOW);

  putByte(reg);   // specify register
  putByte(value); // send data

  digitalWrite(CS_PIN, LOW);
  digitalWrite(CS_PIN, HIGH);
}

void putByte(byte data)
{
  byte i = 8;
  byte mask;
  while (i > 0)
  {
    mask = 0x01 << (i - 1);        // get bitmask
    digitalWrite( CLK_PIN, LOW);   // tick
    if (data & mask)               // choose bit
      digitalWrite(DIN_PIN, HIGH); // send 1
    else
      digitalWrite(DIN_PIN, LOW);  // send 0
    digitalWrite(CLK_PIN, HIGH);   // tock
    --i;                           // move to lesser bit
  }
}

April 1, 2015

ESP8266 wiring schemas

In this page I want to collect some examples (of increasing complexity) to connect the ESP8266 to Arduino boards.
All the wiring schemes and examples are based on an Arduino One and an 8-pins ESP8266 module also know as ESP-01. Arduino pins 6 and 7 will be used as a second serial port using SoftwareSerial library as described here.



Schema Power RX TX
1) Basic N N N
2) Powering with diodes Y N N
3) Powering with Zener diode Y N N
4) Powering with voltage regulator Y N N
5) RX level shifter with resistors N Y N
6) RX level shifter with diode N Y N



1) Basic

Here are the ESP-01 pins.


The most basic connection scheme is this.


Wiring
  • Arduino 3.3v power > breadboard red power rail
  • Arduino GND > breadboard black ground rail
  • ESP8266 GND > ground rail
  • ESP8266 VCC > power rail
  • ESP8266 CH_PD > power rail
  • ESP8266 TXD > Arduino Digital Pin #6 (RX pin using software serial)
  • ESP8266 RXD > Arduino Digital Pin #7 (TX pin using software serial)
  • A 100uF (or larger) capacitor helps to absorb current spikes


This schema has three main problems:
  • Power source: Arduino boards are typically powered at 5V while ESP8266 needs a 3.3V power source. The ESP can draw up to 250mA while the Arduino Uno 3.3V output pin can provide only 50mA. A capacitor can help but it's not enough.
  • RX: The 5V Arduino TX serial output needs to be shifted to the 3.3V ESP RX input. However, simple prototypes can be built without this because the ESP has a built in protection.
  • TX: The 3.3V ESP TX output should be enough for the 5V Arduino digital input. A reliable project may need a level shifter.


The next wiring schemes will show different approaches to provide a reliable 3.3V power source to the ESP.

2) Powering with diodes

Since the Arduino 5V pin can provide 200mA we can use this to power the ESP. This schema is very cheap and simple allowing to use simple and cheap diodes to convert the 5V to 3.6V. Suitable diodes are the 1N4148 (300 mA) or the 1N4007 (1 A).



3) Powering with Zener diode

An alternative cheap approach is to use a 1N5226 3.3V Zener diode to power the ESP as described here.




4) Powering with voltage regulator (LM1117/LD1117)

Several cheap 3.3V regulators are available on the market. A very common and cheap one is the LM1117 (or LD1117). With such regulator you can convert the 5V power used for Arduino to the 3.3V needed by the ESP.


An AMS1117 module is also a great choice.
A generic voltage regulator like the LM317T can be used as described in this post.


5) RX level shifter with resistors

 The 5V output from the Arduino TX pin can be shifted to 3.3V using a resistor voltage divider.
 In this schema I have used three 220 Ohm resistors for an output current of 7.5 mA.



6) RX level shifter with Diode


You can use a 1N4148 diode on the Arduino TX pin as well to lower the voltage of the signal.





Putting all together


Now pick your preferred schemes to build your perfect Arduino/ESP wiring.


References


5V to 3.3V Logic Level Shifting


March 31, 2015

ESP8266 Resources


Here is my current list of links for the ESP8266 and Arduino.



Wiring

I have collected some wiring schemes of increasing complexity in this post.
However I have developed my own wiring scheme and described it in this Instructable. I think is easier for many people to start with a solid and tested scheme.

Libraries

  • WiFiEsp : This is my own library. It is designed to be very similar to standard Arduino Ethernet and WiFi shield libraries. The idea is to be able to reuse existing sketches and projects with this cheap ESP module. You can find more information on this post.
  • ITEADLIB_Arduino_WeeESP8266 : Seems to be the most stable and reliable ESP library at the moment (Apr 2015).
  • arduino-ESP8266 : Seems a good library
  • ThaiEasyElec (TEE_ESP) : Works only with SoftwareSerial, low level functions, poor documentation, no website or GIT repository.
  • ESP8266 EasyIoT
  • Adafruit: Seems a good library, debugging functions, missing high level functions.
  • espduino : Wifi library (Chip ESP8266 Wifi soc) for Arduino using SLIP protocol (custom firmware) via serial port. Release 0.1 was based on AT commands.
  • ESP8266 : Another library. Nothing special. 
  • ESP8266_client :  A lightweight library for client connections. Nothing special.
  • ESP8266_Simple :  Well documented, works only with SoftwareSerial, few functions.

Reference documentation


Flashing the firmware

I have developed my own procedure using Arduino board for flashing the ESP.


Other resources


  • Espressif forum:
  • ESP8266 forum : This is a community forum centered around the ESP8266. Everything is here, but probably best to read this stuff after going through a complete design example.
  • Electrodragon wiki : Wiki page with multiple sets of information and links for using the ESP8266.
  • Espressif is the manufacturer of the ESP8266. Their site is pretty much a bunch of marketing hoo-hah and you'll have to "contact" them for more information. Not too useful.
  • http://blog.electrodragon.com/esp8266-gpio-test-edited-firmware/ : A demo of turning the LEDs on the ESP8266 board on/off over the Wifi link.
  • http://scargill.wordpress.com : Peter Scargill has been pretty thorough in documenting his experiences in getting the ESP8266 up and running and delving into some of the capabilities of the chip.