ESP Wireless Arduino Bridge

The SPI Bridge device is designed to be simple to use, and flexible enough to be applied to a wide range of projects.

The ESP8266 microcontroller handles the network connectivity, while the arduino handles peripheral interfacing.

Communication

The ESP8266 and Arduino communicate over SPI protocol. However, a custom layer has been added that allows multiple Arduino devices to be connected to the same bus. In this way, it functions much like I2C.

To communicate with an Arduino, a user can pass an HTTP request to the ESP8266 device, which parses out the values, and transmits it to the desired Arduino.  Based upon the Arduino’s programming, it will then return some result which is packaged into a JSON response by the ESP8266, and transmitted back to the user.

In order to get a more complicated response, such as a decimal value, or signed integer, the user must do some decoding of the result, as the ESP8266 doesn’t take any care to translate the binary value received into anything other than unsigned long.

A request string would look something like this:

http://{$ip_addr}/spi.php?addr={$address}&comm={$command}&param={$parameter}&payload={$payload}

 

Wireless Configuration

A configuration webpage has been embedded in the ESP8266 program that allows a user to connect to it, and attach it to a network, customize its access point settings, and change its operating mode.

If a device is moved, or needs to be reconfigured, a reset button can be pressed for five seconds which will put it back into access point mode, at which point a usera user can connect directly to the device to update configuration.

Getting Started

In order to integrate this bridge device into a new project, a user only needs to add custom command options, and return values.

For example, if a user was interested in reading the temperature, they could start with a template sketch, include the support library for interfacing with the temperature sensor, read the current temperature during every application loop, and create a command entry that returns the last read temperature value whenever it is polled via the REST/SPI interface.

The template sketch includes a simple switch statement that evaluates the passed command value. There are also a few functions, and data structures that take care of returning values to the user, everything that is needed for operation is illustrated in the included sketch.

The ‘short_float’ data type (which is no longer named accurately) is designed as a container for packing different data types into a format that can be neatly returned to the ESP8266.

union short_float{

float f;

uint8_t i8[4];

uint16_t i16[2];

uint32_t ul;

};

The user just needs to load the return data into the container, and pass it to the ‘as_packet_pack()’ function, which handles the transmission.

Server Integration & Alerting

The ESP8266 device also has support for creating a relationship with a server for the purpose of alerting.

If the Arduino device triggers an alert on the ESP8266, and a server IP address has been provided, it polls the Arduino for an alert address (programmed by the user), finally, it will include the alert address in an HTTP request to the server.

This design allows the user to program custom follow-up to a alert scenario if they desire.

For example, if a user wanted to ensure that the soil for a plan never got too dry, they could monitor the soil moisture level with the Arduino and a sensor, then when its value got out of range, submit an alert request to their server via the ESP8266, which could email, or text message them, letting them know that they needed to water their plant, alternatively, it the server could simply send a command to run a pump, watering the plants..

Flashing the Firmware

https://github.com/nodemcu/nodemcu-flasher

0x00000 boot_v1.6.bin

0x01000 user1.4096.new.4.bin

0x3fc000 esp_init_data_default.bin

0x7b000 blank.bin (Stored Configuration)

ESP Bridge Bin Files

Automation & Scheduling

To improve automation, I created a basic application in PHP that:

  • Maintains a directory of associated bridge devices, and their commands.
  • Supports the creation of schedules for executing commands at desired intervals.
  • Issues follow up commands if the returned value from the first is outside of configured bounds
  • Records the returned values from any command, and display those values in a chart. (Google Charts API)

The application was built on PHP & MySQL, it will run on a basic LAMP stack.

To get the server up and running, the basic steps below should be followed are:

  • Install Linux, Apache, PHP, MySQL (Maybe PHPMyAdmin)
  • Create a MySQL database (use the name esp_bridge for simplicity) and two MySQL users, one with read access and one with full access.
  • Copy the application files to the website root (Typically /var/www)
  • Copy the apache config file (esp_bridge.conf) from /var/www/esb_bridge to /etc/apache2/sites-available
  • Enable the new site in apache.
  • Copy the mysql_template.txt file (/var/www/esp_bridge/application/config), and rename it to match the name of your database. Update the MySQL User Configuration in this new file.
  • At this point, the site should be functioning as designed.

The PHP application leaves a lot to be desired.  It isn’t pretty, and all of its planned features don’t have a GUI, however, it works for my purposes at this time, and will receive improvements as I need them.

ESP Bridge Source

LAMP Server

Eagle Files for PCB