Pages

Wednesday, January 27, 2021

Programming Sonoff Basic R2 with custom Sketch making Alexa capable Sonoff Smart Switch

Programming Sonoff with USB to Serial Converter (USB-TTL USB-ITC-ISP) (Prolific USB-to-Serial Comm Port).

Problem :

Recently one of my Sonoff Basic R2 switch got bricked and I was not able to link it to my ewelink app, I tried resetting and attempted to pair using all the different methods available but to no avail and I could never get it working.

I then tried looking at different forums and found out that some header pins have been exposed on the board but we need a little bit of DIY to make it capable of receiving custom sketch from the Arduino IDE installed on our laptop.

I did some DIY and tinkering around with it and now I am going to present a Step by Step instruction to reprogram your Sonoff Basic R2 Smart switch using Arduino IDE installed on Windows. The Sketch I have created can make the Sonoff discoverable from Alexa too which makes it interesting because a switch which is not capable of integrating with Alexa/Google home is not that smart I think 😊.

Caution : Please note that any mistake in the steps mentioned below may cause the Sonoff to be completely dead/unusable so please do it at your own risk.

Things I used:

  • Sonoff Basic R2.
  • USB to Serial Converter (USB-TTL USB-ITC-ISP).
  • Some female jumper wires.
  • Arduino IDE installed on Windows 10 Laptop (1.6.13)
  • Amazon Echo Dot 3, Echo Show 5, & Alexa Android app on my Nokia 8.1.

 

 



I could not find this USB to Serial converter a straightforward adapter and had to test it with different version of drivers and eventually found out one which works well on Windows 10 (release 1909). Attaching a link of the working drivers for Window.

Drivers Link:

https://github.com/Prakash82x/SonoffCustomSketch/tree/main/USB-TTL--USB-STC-ISP_PL23XX_Prolific_DriverInstaller

Prepare your Arduino IDE.

Make sure you have following libraries installed

1.      Espalexa

2.      ESP8266WiFi

 

 

 

Install Board Manager

Open Tools > Board > Boards Manager.

 

 Go to File > Preferences.

 

 

Add these two urls (separated by Comma) in additional board manager:

https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json

Setup USB to UART adapter:

Download the driver from here and install it

If the drivers of the USB-UART adapter is installed properly then it will show something like this in Device Manager.

 

Connect the Sonoff with soldered Female Jumper wires to USB-Serial Adapter. (Do not Plug the adapter to Laptop yet)

Caution: Please DO-NOT Connect the Sonoff with AC Mains. We will just be uploading the code to ESP8266 MCU and for that we are delivering 3.3Volt via UST-TTL adapter from our laptop which is enough to power up the Chip on Sonoff.

 

Now that everything has been Connected as show in the connection diagram.


Here comes the most crucial part of uploading the code.

Push and hold the button present on the Sonoff-board and insert the USB to TTL adapter to a usb Port of your laptop and release the button after 2-3 seconds.

This step is important because it brings the Sonoff to flashing/programming mode.

Without this step we won’t be able to upload codes to it.

Here is the Sketch



 

 

It will show similar to following screen if upload is working..



It will start uploading the code after it completes 100% then just unplug the Adapter and replug it in the laptop in normal mode (without pushing and holding the button)

Go to your Alexa App on phone or ask Echo dot to discover devices.

It will discover a device which will be shown as a bulb named "SmartSwitch" from “Royal Philips Electronics”.

All the resources used in this project are kept at the Git Repo:

https://github.com/Prakash82x/SonoffCustomSketch

Enjoy .. !!!