How to keep an ENC28J60 Module from Freezing

The ENC28J60 module is good for attaching an Arduino to a LAN or the Internet. The one real problem in doing so is how to keep an ENC28J60 module from freezing. No, not temperature wise ! ūüôĄ¬† (‘Freezing’ in the sense of locking up, stop working, etc.)

How to keep an ENC28J60 Module from Freezing

ENC28J60 LAN Module

There are many Ethernet  arduino libraries for the ENC28J60 module, however,  I am using the UIPEthernet library from HERE. It is updated from the original Ethernet and follow on UIPEthernet libraries. Documentation on the Ethernet library can be found HERE along with sample code. Note: The UIPEthernet library I am using fully supports all the documented functions from the original library.

There are various ERRATA on the ENC28J60, however, the current versions of the UIPEthernet¬† library address those issues. This library also supports the STM32F103C8T6. Usually one would think that such ‘lockup’ or ‘freezing’ problems are related to power problems. That problem is not an issue here, it is taken care of by testing with different good power sources and lots of capacitance.

I have the most trouble when I attach an STM32F103C8T6  to the ENC28J60 module.

ARM Cortex M3 STM32F103C8T6

STM32F103C8T6

I seem to have the least problems when I attach an Arduino UNO to it.

Arduino UNO

Arduino UNO

I think that perhaps the extra speed of the STM32F103C8T6 enhances the problem. In the main loop() code, the library function checks on the ENC28J60 and perhaps due to the faster speed of the STM32F103C8T6 there is something going on that affects the ENC28J60.

Another, less know issue, is the need to occasionally call the library function maintain(). If one looks at the documentation, it seems maintain() is only used to renew a DHCP lease. Further research will show that this function call is needed, periodically, if connected to an ENC28J60.¬† I found that there are fewer ‘freezing’ problems when calling that function in the main loop() code, however, they still occasionally happen.

The best approach to fully solving the issue is to somehow check the status of the ENC28J60 and reset it if it is ‘frozen’. Fortunately, the code for that is available HERE.

This is my sample WEB server code which shows how to keep an ENC28J60 module from freezing by implementing that checking of the ENC28J60.¬† It also shows the use of the maintain() function and the use of the STM32F103C8T6 watch dog timer. I use the watch dog timer to make sure the mictocontroller running the code to control the ENC28J60 is not somehow locked up (‘frozen’).

Here is the code:

From the ASCII chart in the comments at the top of the code, one can see how the STM32F103C8T6 is wired to the ENC28J60.  One thing to note, is that the RESET of the ENC28J60 is accomplished via its RESET pin rather than by calling the Enc28J60.init() function.  This does a hardware reset and seems to work in all lockup cases.

Switching 12V from a 5V Arduino

I built a circuit to allow switching 12V from a 5V Arduino. Here is the wiring diagram and a picture of the actual circuit:

Switch 12V from 5V ArduinoSwitching 12V from a 5V Arduino

 

 

 

 

Get the Fritzing source HERE.  The side with two connections attaches to the 12V device being switched ON/OFF. The side with three  connectors,  connects to the 12V source and Ground. It also connects to an Arduino PIN to do the ON/OFF switching.

The actual circuit (pictured above) is doubled and can handle switching two 12V devices.¬† A PC power supply (converted to a ‘bench power supply’) can be used to power the Arduino and the 12V devices,

You can hook up a PC power supply and make a bench power supply with an adapter. That adapter easily attaches to the power supply and breaks out the 12V, Ground, 5V and 3.3V sources. Pictured below is an example of such an adapter:
PC Bench Power Supply AdaptorThe circuit switching 12V from a 5V Arduino  comes in handy for a number of things. I am using it to turn on a 12V Emergency Alarm.  Here are the two types (siren and siren with strobe) that I am using. They both work with 12V and Ground.

Switching 12V from a 5V ArduinoSwitching 12V from a 5V Arduino

 

 

 

 

 

This circuit switching 12V from a 5V Arduino is part of a much larger home/office alarm system project I am working on.

Decoding an ESP8266 Firmware Image

I recently coded a Python script for Decoding an ESP8266 Firmware Image. It’s the first part of my quest to create a Linux tool for creating a single flash image.

More specifically, the script will decode a single binary image or a ‘combined’ image. A combined image (single flash image) can be created with the flash download tool from Expressif. (Exressif is the company which makes the ESP8266.)¬† However, that download tool only runs on Windows. Here is what the Expressif Flash Tool looks like:

 

ESP8266 Flash Tool on Windows

Notice, the ‘CombineBin’ button. It is used to create a single binary image from the multiple files (‘segments’). In that image you can see seven files (the current SDK 2.1.0 files as of this post date) listed and the addresses to load them . I used the ‘CombineBin’ button and created a single binary image called sdk-2.1.0.bin. (The default file name it creates is called target.bin.)

I run Linux. and the recommend ESP8266 tool for it is esptool.py. The esptool script, however, does not have a option to create a single combined image. It may have that feature as an enhancement at some future time. Hence, my quest, to develop a Linux tool to create a single flash image.

I like to keep my ESP8266 firmware current. To flash the latest SDK, I use this script:

While that works fine, I think it would be more efficient to create a single combined image to flash. Especially since I have a number of ESP8266 modules to flash. The single image (sdk-2.1.0.bin created from the windows tool) can be flashed as follows:

To create a tool to combine all the ‘single’ images into one, one needs to know the format of the single images and the format of the ‘combined’ image.

Espressif has a brief document (HERE), describing the firmware image format. However, that document does not fully cover what is need to decode a combined image. Also, it does not cover all of the single image ‘formats’. Esptool.py has an image_info command, however, it only works on single modules. It does not work on combined modules.

In my ‘reverse engineering’ attempt at Decoding an ESP8266 Firmware Image, I created this Python script called esp8266_parse_bin.py. Here is the code:

Esp8266_parse_bin.py can be used to decode any of the single modules that comprise the combined sdk-2.1.0.bin image or it can be used to decode the ‘combined’ image.

When it is used to decode the combined sdk-2.1.0.bin image via:

Here is what the output looks like:

From that output, one can see where (address) and how each of the single images are placed in the ‘combined’ image.¬† Now that the combined image format is known, a script can be coded to create it from the individual files.

12V to 5V Sensor Circuit

A number of sensors are powered via 12 Volts. Some, however, need 5 Volts.  To power a remote DHT11 temperature sensor, I created a 12V to 5V sensor circuit.

This is the wiring diagram:

12V to 5V Sensor Circuit

Get the Fritzing code for it HERE.

DHT11

DHT11

 

 

 

The DHT11 needs 5 volts,  ground and data. It plugs into the 12V to 5V sensor circuit via a 4 pin header slot. Notice there is a 10K pull-up resistor connected to data.

Here is what the circuit looks like with and without the DHT11 plugged in:

12V to 5V Sensor Circuit Side View12V to 5V Sensor Circuit with DHT11

This type of circuit comes in handy for a larger project such as an alarm or monitoring system where sensors are some distance away from the main monitoring device such as an Arduino.¬† For longer distance cable runs, Cat 5¬† cable is used which has 8 wires.¬† Three wires can be twisted together for 12 volts. Three wires can be twisted together for ground and the remaining 2 wires can be twisted together for data.¬† The multiple wires together increases the ‘wire size‘ and overcomes signal loss and interference in most cases.

4 Character 7 Segment Display

I built another handy circuit to ‘house’ a 4 character 7 segment display. It looks like this:

4 Character 7 Segment Display

Basically it just ‘breaks out’ the 16 pins of the display with two 8 pin female headers. Pins 1-8 are connected to the header at the bottom and pins 9-16 are connected to the header at the top. Note that 6 220 Ohm resistors are needed to protect the LEDs.

 

 

The Fritzing diagram to the right (Download Link) shows which pins select which character (colon or apostrophe) is selected to turn on and thus needs a resistor. The LEDs actually light a bar shape. Each ‘character’ is composed of 7 LEDs (or segments). The image below shows what each of the pins controls.

4 Character 7 Segment Display Pins

 

 

 

 

The LED pattern is as follows:

 

7 Segment Display4 Character 7 Segment DisplayFour of these characters go together to be able to display things like time, temperature, voltage, etc.

 

 

The technical manual is HERE. The pin functions (1-8 bottom and 9-16 top) are as follows:

Connecting this 4 character 7 segment display to an Arduino is usually done in conjunction with 1 or more¬† shift registers due to having so many wires. Connecting without the shift register, an arduino looks like it needs a ‘hair cut’ with so many ‘wires’ all over the place.

4 Character 7 Segment Display

4 Character 7 Segment Display

 

 

 

 

 

 

 

I connected my 4 character 7 segment display circuit with two 74HC595 (shift register IC)  breakout modules. Each 74HC595 module controls 8 pins (1 shift register for the bottom 8 pins and one for the top 8).  With these modules, only 3 wires (besides power and ground) connect to the Arduino.

4 Character 7 Segment Display

 

Using an Arduino library designed for the 74HC595 shift register makes it easier to control the display. I like this shifter library hosted on GitHub. With it, each pin is easily referenced by number.

 

 

Here is a an Arduino sketch I created to display current voltage on the 4 character 7 segment display :

Notice that I created functions to select the specific character to turn on and a function to turn on the LED segments needed to display each number digit. With the shifter library and these functions, you are all set to make something interesting! How about a multimeter?

I2C 2-wire Circuit

My latest project involves collecting data from a number of sensors connected to multiple Arduinos. I2C (or 2-wire) is a perfect fit for this project because I needed an easy way to connect the Arduinos and aggregate the data. With I2C, I can have one Arduino as the master and several other Arduinos as slaves.  To connect the Arduinos together, I created a simple I2C 2-wire Circuit.

Here is the finished product and the wiring diagram:

I2C 2-wire CircuitI2C 2-wire Circuit

 

 

 

 

 

You can download the Fritzing source for the diagram HERE. As you can see from the diagram, it allows multiple connections to Power/Ground and multiple connections to SDA / SCL (the two wires used in I2C). SDA (serial data line) is used for sending data and SCL (serial clock line) is used for clocking.  I2C allows for multiple master/slaves. I have one Arduino as the Master and several Arduinos as slaves. I will eventually use a Raspberry PI as the master.

If multiple Arduinos are connected on a breadboard, there can and probably will be some voltage loss. This is especially true when connecting breadboards together and ‘jumpering’ power/ground from one breadboard to another. It is best to supply each breadboard with its own source to power and ground. I built this little testing circuit to get around the power loss problem. Notice that I used 4.7K pull up resistors in this I2C 2-wire circuit. These resistors are needed to ensure the I2C bus works properly.

ESP8266 DIY Programmer Cradle

If you have been reading my posts lately, you will notice that I have been working a lot with the ESP8266 wireless module. To make it easier to work with, I made an ESP8266 DIY Programmer Cradle. Here is what it looks like (without and with the ESP8266 plugged in):

ESP8266 DIY Programmer CradleESP8266 DIY Programmer Cradle

 

 

 

 

 

You may notice that I made several of them, some with a 100uF capacitor between Volts and Ground and some without. There are two jumpers, one for GPIO00 and one for GPIO02. When programming the ESP8266, GPIO00 is jumpered to Ground (otherwise it is ‘floating’, i.e., not jumpered). An LED is attached to GPIO02 for testing. Take a look at my MicroPython Web server script mentioned in this post to see how to toggle the LED On/Off. The 6 pin header on the side is connected to a USB to Serial adapter for power and/or programming.

The wiring for the ESP826 and the ‘cradle’ are shown in these graphics:

ESP8266 Wiring ESP8266 DIY Cradle Wiring

 

 

 

 

 

 

Get the Fritzing diagram source HERE. The ESP8266 DIY Programmer Cradle is powered by a USB to Serial Adapter (jumpered to 3.3V) like the one shown below:

USB to Serial ConverterIf you add a power pack (for example, one that is used to charge a phone):

ESP8266 with USB to Serial Converter with Powerbankyou’ll have a portable ESP8266 based wireless module. In any case, with the ESP8266 DIY Programmer Cradle, I have an easy and fast way to plugin, program and test the ESP8266 modules.

MicroPython WebREPL on Android

I created an app (apk) to run MicroPython WebREPL on Android. Imagine you have your ESP8266 (with the MicroPython firmware) in some location where the only access to it is via a wireless connection. If you need to access and change/control it, you will need WebREPL.

Of course, a wireless enabled laptop would get access to WebREPL if you have it installed locally. However, if you are connected to the ESP8266, you might not have internet access to remotely load the WebREPL code.

Alternatively, an Android phone provides the ultimate in portability but has no file system (per se) to store and load the WebREPL code from a browser like the laptop would. You probably could load the WebREPL code in a browser while connected to the internet, then switch over the WiFi to the ESP8266. However, I prefer an app.

So, to make WebREPL available thru an app on Android, I used Apache Cordova to create it. This is a ‘screen shot’ of how it appears on the phone:

MicroPython WebREPL on AndroidIf you are interested in the complete Apache Cordova project code to access the MicroPython WebREPL on Android, download it from HERE. If you are only interested in the apk (app) file, download it from HERE.

The current version of WebREPL uses a no longer supported version of an xterm emulator called term.js  found HERE. While that version works on a PC browser and a browser (like Firefox) in Android, it does not work right inside of an Android app. The keyboard does not function right. So I replaced it with its predecessor called xterm.js found HERE.

If you are interested in the steps I took to create the app, here is what I did:

Those commands will create the basic code for an app. Next, I download and unziped the code from webrepl and xterm.js.

Next, I customized the config.xml to add the MicroPython icon by adding this line:

Then I download the png graphic and  put it in the micropython directory with the name MicroPython.png.

Next, I went to the micropython/www directory and removed everything there. Then I copied over to it these files from where I unzipped the webrepl code:

I renamed webrepl.html to index.html. Next,  I copied over to it (micropython/www) all of the files and directories in the dist directory of the unzipped xterm.js. Then, index.html needed to be changed a bit to use xterm.js vs term.js.

Finally, I built the apk with the command:

and installed it in the phone. After connecting the Android phone wirelessly to the ESP8266 to get an IP address, the MicroPython WebREPL on Android apk is ready to run!

ESP8266 MicroPython Firmware

To enhance the capabilities of the ESP8266 there are various firmware packages available. In my last post, I showed how to install NodeMcu which uses eLua. I am a big fan of the Python programming language, so I was very happy to see a Python version for the ESP8266. The ESP8266 MicroPython Firmware is available from micropython.org. Download it from HERE.

To flash the ESP8266, make sure that GPIOO is  wired to GND. Check the following diagram for the complete wiring:

ESP8266 MicroPython Firmware Flashing(Download the Fritzing source for the wiring diagram HERE.) An interesting post showing how to flash the ESP8266 using an Arduino instead of the USB to serial converter (used in the graphic above) can be found HERE.

To flash the ESP8266 MicroPython Firmware, make sure that the flash tool (esptool.py) is installed (or if installed, is up to date):

Here are the commands to flash the ESP8266 MicroPython Firmware:

Version 1.9.1 was the current version when I wrote this post, so the flash file name will vary. Once the firmware is loaded, open a serial terminal program like picocom via:

You might have to ‘hit enter’ a few times to get to the >>> MicroPython REPL prompt.To verify the flash use these commands:

The result should be ‘True’ if flash is OK. Even so, I still had problems when I flashed some of my ESP8266 modules. The problem was resolved by ensuring a stable/good power source and letting esptool.py use its default baud rate to do the flashing.

A WEB accessible REPL prompt is also possible once installed/configured  via:

from the serial connection. Download the WebBREPL code from HERE. This comes in quite handy to connect and enter commands when the only access to the ESP8266 is wireless. If you don’t have a ‘local’ copy of WebREPL to run, it is hosted here: micropython.org/webrepl. Note: Once that code is loaded in your browser, it will connect via websocket to your ESP8266 module.

To show what can be done with ESP8266 MicroPython Firmware, I wrote a WEB server script to toggle pin 2 (GPIO2). This is basically the same functionality as the Lua script in my previous post but this one is coded in Python. Download it from HERE.

For more information on the specifics of MicroPython on the ESP8266, consult the Documentation HERE.

ESP8266 Custom Firmware

If you would like to go beyond the basic features in the ‘default’ ESP8266¬† firmware, it is possible (and relatively easy) to create your own ESP8266 custom firmware.

ESP8266 Custom FirmwareWith ESP8266 custom firmware, your possibilities for interaction with connected devices are almost endless.

The reason why is that you can now connect pins on the ESP8266 to other devices and control those devices ‘programatically’. This is possible due to the custom firmware using the LUA programming language (specifically embedded LUA or eLUA). The team at nodemcu.com have developed an ESP8266 based IOT module called NodeMcu and open source firmware for it. You can see examples of what can be done with LUA on their main WEB page. Their firmware (available on Github HERE) can be FLASHed on any ESP8266 Wifi SOC.

I am using an ESP8266-01 module (as seen in the above picture). It only has two GPIO pins available, however, that is sufficient for what I need. Other ESP8266 models (like the NodeMcu) have a lot more pins broken out for use.

The documentation for the NodeMCU firmware is HERE. There is a ‘cloud’ (i.e. WEB) based service to build a custom firmware image HERE. However, I prefer to experiment on my own, so I installed the development tools locally on my Fedora Linux system. Here is how I did it:

First, make sure all the Fedora Linux prerequisites are installed:

Next, make sure that the flash tool (esptool.py) is installed (or if installed, is up to date):

Next install the development toolchain for the ESP8266:

That will take a while and let you know the PATH to export so the next step can invoke the toolchain correctly. Next, install the NodeMcu firmware:

Next, read the build documentation to see how you want to customize the options and what ESP8266 function modules you want to include. After you customize:

This will take a while and then produce two bin images in the bin directory:

0x00000.bin and 0x10000.bin

Now you are ready to flash those files into your ESP8266 module. Make sure the ESP8266 is in flash mode via GPIO00 pulled low (normal GPIO00 is high or floating). Here is how it should be wired:

ESP8266 Wired for Flashing(The Fritzing diagram source is HERE.) Then flash the ESP8266 with these commands:

An alternative is to combine (aggregate) the binary images into one file. To make the aggregated file, here is a python script to automate it:

To flash that aggregated file use these commands:

The ESP8266 can then be accessed by a serial connection. I recommend getting the ESPlorer developer IDE found HERE. It makes coding and uploading your LUA code very easy. Here is a screenshot of ESPlorer:

ESPlorer Screen Shot (ESP8266 IDE tool)There are sample LUA scripts in the nodemcu-firmware-master directory for you to experiment with or exercise your LUA skills and develop your own.

For example, here are the commands to setup the ESP8266 as an Access Point and set its SSID and Password:

HERE is an example LUA WEB server script to toggle PIN 4 (GPIO2) from a WEB page. It is called init.lua so that when the ESP8266 is powered on it will run that script automatically. Of course just toggling a PIN is not so useful ūüôĄ so you will want to attach it to something like a relay to turn something on or off.

For the latest information on this whole process, consult the NodeMcu documentation HERE.

Load more