1

member

Easy Basics: Project 069b Sonoff smart wifi switch - flashing a custom firmware

of Acoptex.com in ESP8266 ESP-01

Basics: Project 069b

Project name: Sonoff smart wifi switch - flashing a custom firmware

Tags: sonoff basic wifi switch, Arduino, Sonoff ESP8266 update firmware with Arduino IDE, reprogram sonoff smart switch with web server, how to upload new firmware using Arduino IDE, sonoff smart switch

Attachments: webserversketch

WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT. WE ARE NOT RESPONSIBLE FOR ANY DAMAGE, INJURY, DEATH AND OTHER THINGS CAUSED BY THIS PROJECT IMPLEMENTATION. 

In this project, you need these parts :

1. Sonoff smart wifi switch 1pc

2.Arduino IDE ( you can download it from here  )

3.Jumper cables F-M

4. USB to TTL serial converter 1 pc

5. USB mini cable 1 pc (optional)

6. Soldering iron

7. 4 header pins

General

We will learn how to flash custom firmware in the Sonoff device, so that you can control it with your own web server. 

Understanding the USB to TTL converter

You can read more about them here.

Understanding the Sonoff smart WIFI switch

You can read more about it here.

Signals and contacts of Sonoff smart wifi switch

TXD (TX, TXO or UTXD) - transmit pin. 

RXD (RX, RXI or URXD) - receive pin. 

VCC (3V3 or 3.3V) - power supply pin (3-3.6V). 

GND - Ground. Connected to Arduino board GND pin

GPIO14 - General Purpose Input Output pin is not used in this project.

Build the circuit

WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT. WE ARE NOT RESPONSIBLE FOR ANY DAMAGE, INJURY, DEATH AND OTHER THINGS CAUSED BY THIS PROJECT IMPLEMENTATION. 

You can use any USB to TTL Converter (with or without DTR Pin). Make sure that you have a jumper on 3.3V or use 3.3V power supply!

USB to TTL converter -> Sonoff smart wifi module

  • GND->GND 
  • TX->RX
  • RX->TX
  • VCC 3.3V->3.3V

GPIO14 (NOT IN USE)

Step by Step instruction

1. Adding ESP8266 platform to Arduino IDE

The Arduino environment has to be set up to make it compatible with the ESP8266 ESP-01 module. It is required to have Arduino IDE version 1.6.4 or higher in order to install the ESP8266’s platform packages.

  1. Open Arduino IDE. 
  2. Open the Preferences window from the Arduino IDE. Go to File -> Preferences.
  3. Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field and click the “OK” button. If you already have a URL in there, and want to keep it, you can separate multiple URLs by placing a comma between them. (Arduino 1.6.5 added an expanded text box, separate links in here by line.)

  4. Open Boards manager. Go to Tools -> Board -> Boards Manager…

  5. There should be a couple new entries in addition to the standard Arduino boards. Look for esp8266. or scroll down to the ESP8266 entry (usually at the bottom). Select the ESP8266 entry. When you click it an install option will appear. Select the latest version and click install.
  6. The board definitions and tools for the ESP8266 include a whole new set of gcc, g++, and other reasonably large, compiled binaries, so it may take a few minutes to download and install (the archived file is ~110MB). Once the installation has completed, an Arduino-blue “INSTALLED” will appear next to the entry.
  7. Choose your ESP8266 board. Go to Tools -> Board -> Generic ESP8266 Module
  8. You will see Generic ESP8266 Module on the bottom of the Serial Monitor window.

2. Preparing to flash custom firmware

VERY IMPORTANT NOTE:

Before doing firmware update:

  • Please note that this operation will overwrite the original software, this guide is for people who want to use their own software.
  • Do not connect AC power before and during the flashing a custom firmware.
  • Some USB to TTL converters have a single Vcc pin with jumper to switch between 5V and 3.3V. Make sure you have it set to 3.3V.
  1. Open Sonoff smart wifi switch. You will see a board with the ESP8266 chip, relay, LED, push button and other components. You need to access the serial interface. The four serial pins (3V3, Rx, Tx, GND) are available in the middle of the PCB, right next to the on-board button. Newer version of the Sonoff module provide five pins below the button, ignore the pin furthest away from the Button (GPIO14) if available. The square pin right next to the button is the 3.3V line. GPIO Locations - 
    GPIO 03 - RX PIN
    GPIO 01 - TX PIN
    GPIO 04 - Second image (must solder wire to pin on ESP chip)
    GPIO 14 - Below GND PIN
    GPIO 03 - RX PIN, GPIO 01 - TX PIN, GPIO 04 - Second image (must solder wire to pin on ESP chip), GPIO 14 - Below GND PIN
  2. Insert 4 pin header and solder it.
  3. Do wiring - connect the USB to TTL converter to the Sonoff board.
  4. To flash the new software to our SONOFF, we have to start Sonoff in flash mode: 
    Insert the FTDI converter USB in your computer (while holding the push button).
    After 2–3 seconds, release the push button.
    Now it’s in flash mode.
    Do not plug USB to TTL converter to the USB port on your PC/laptop.Do not connect the Sonoff to mains power.Press and hold the push button on the Sonoff board.Insert the USB to TTL converter to USB port in your PC/laptop (while holding the push button). After 2–3 seconds, release the push button.

3. Flashing a custom firmware.

  1. Open Arduino IDE.
  2. Choose your ESP8266 board. Go to Tools -> Board -> Generic ESP8266 Module
  3. Select Flash Mode - DOUT.
  4. Select the correct COM port.
  5. Replace the SSID and password in the webserversketch with your own data.
  6. Verify and upload the webserversketch to your Sonoff smart wifi board.
  7. If you try to upload the webserversketch and it prompts the following error message: warning: espcomm_sync failed error: espcomm_open failed . It means that your Sonoff smart wifi switch is not in flashing mode. You’ll need to prepare it for flashing. Another reason can be a dry soldering joint on one of the pins.The fix of the soldering fix this issue.
  8. Other issue can be that Wifi connects for the first time, but fails if ESP8266 restarted. This is a known issue with ESP8266 core for Arduino as of version 2.3.0. You can read more about it here. Use the following code for workaround - if (WiFi.status() != WL_CONNECTED) {  WiFi.begin(ssid, password);}
  9. After uploading the code, re-assemble your Sonoff. Be very careful with the mains voltage connections.
  10. Open the Arduino serial monitor at a baud rate of 115200. Connect GPIO 0 of your ESP8266 to VCC and reset your board.
  11. After a few seconds your IP address should appear in Serial Monitor. 
  12. Open any browser from your smart phone, tablet or PC connected to the same router ast your Sonoff smart wifi switch. 
  13. Type the IP address and press Enter button.
  14. When you press the buttons in your web server you can control the Sonoff smart wifi switch and any device that is connected to it.

Summary

We have learnt how to flash custom firmware in the Sonoff device, so that you can control it with your own web server. 

Library

  • ESP8266WiFi library included in Arduino IDE. 
  • ESP8266WebServer library included in Arduino IDE.
  • ESP8266mDNS library included in Arduino IDE. 

Sketch

  • No sketches needed in this project


Other projects of Acoptex.com
Easy Raspberry basics: Project 26a Raspberry PI Zero W board - Enable SSH of Acoptex.com in Raspberry Pi Zero W 07-01-2019
Easy Raspberry basics: Project 24a Raspberry PI Zero W board - Apache web server of Acoptex.com in Raspberry Pi Zero W 05-01-2019

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 26-04-2018
Viewed: 1156 times