Sonoff Basic DIY Setup without flashing

Tags: #<Tag:0x00007f7454318b28> #<Tag:0x00007f7454318a60> #<Tag:0x00007f7454318998>

OLD Example dosen’t work on 3.3.0 firmware.

New one here https://github.com/JAMESBOWLER/SONOFF_DIY

Sonoff DIY Example

To setup I created rules to control. Openhabian on RPI 3+

Product Page Link
Documentation/Tools Github

Steps

  • Wire power and Install header pin
  • Update Firmware using eWelink APP as it won’t work with shipped firmware 3.0.0
  • DIY will connect to wifi ssid:sonoffDiy password:20170618sn Youtube Demo video
    I created wifi with spare router I had lying around and mDNS is required
  • Use DIY mode tool to change SSID to same as pi found here Github
  • Setup power on state
  • Set up DHCP reservation using router (192.168.1.50)
  • Create Items Sitemap and Rules


You can flash 3rd party firmware using this tool to void warranty.

I use the openhab config files edited through VScode

Item

Switch sonoff_one "Lounge Light" <switch> (gLounge) [ "Lighting" ]

Sitemap

sitemap sonoff label="sonoff" {
  Frame label="Lounge" {
    Switch item=sonoff_one icon="light"
  }
}

Rules

rule "Sonoff control Rule"

  when
    Item sonoff_one received update 
  then
  
  try {

    var CommandState = sonoff_one.state.toString.toLowerCase   // Change Sonoff_one to the item in your .items file e.g. Switch sonoff_one "Lounge Light" <switch> (gLounge) [ "Switchable" ] 
    val String Ip = '192.168.1.50'      // Ip of Sonoff
    val String Deviceid = '100086f3e5'  // Device ID - Works without it 

    // Shouldn't need to change anything below 
       
    var CommandURL = 'http://' + Ip +':8081/zeroconf/switch'
    var String CommandData = '{ "sequence": "1560835774", "deviceid": "' + Deviceid + '", "encrypt": false, "iv": "1234567890123456", "selfApikey": "123", "data": "{\\"switch\\": \\"' + CommandState + '\\"}"}'
    var String UpdateResult
    var Number Attempts = 0
    var CommandExec = 'curl@@-sSH@@"Accept: application/json"@@-H@@"Content-Type: application/json"@@-X@@POST@@-d@@' + CommandData + '@@' + CommandURL
  
    do {
      Attempts += 1
      UpdateResult = executeCommandLine(CommandExec, 2000)       
      if (UpdateResult.contains('"error":0')) { logInfo("Sonoff ID:"+ Deviceid +" Control", "IP: " + Ip + " Turned: " + CommandState) } // Success
     
  
    } while (!UpdateResult.contains('"error":0') && Attempts < 5) // Attempt to send 5 times
  
    if (Attempts == 5) {
      logError("Sonoff write", "5 failures updating Sonoff")
    }
          
    if (UpdateResult.contains('"error":400')) {
    logError("Sonoff ID:"+ Deviceid +" Error", "The operation failed and the request was formatted incorrectly. The request body is not a valid JSON format")
    }

    if (UpdateResult.contains('"error":401')) {
    logError("Sonoff Error", "The operation failed and the request was unauthorized. Device information encryption is enabled on the device, but the request is not encrypted")
    }

    if (UpdateResult.contains('"error":404')) {
    logError("Sonoff Error", "The operation failed and the device does not exist. The device does not support the requested deviceid")
    }

    if (UpdateResult.contains('"error":422')) {
    logError("Sonoff Error", "The operation failed and the request parameters are invalid. For example, the device does not support setting specific device information")
    }
  }

  catch(Throwable t) {
    logError("Sonoff write", "Error was caught: {}", t)
   }

end

Google intergration


Now in the google routine for bedtime you can add the light to the sequence.

Layout of Data

Cons

  • If you switch it too fast using sitemap it light may be out of sync. I working on making it more reliable.
4 Likes

James, I’m new on this community and just starting to create a system based on openhab for my home. What a great piece of work here, thanks!

Thank you. After three hours of fiddling round I eventually got my Sonoff connected to openhab.

My solution involved:

  • setting up a temporay wireless diy network on our Unifi network
  • updating the factory firmware on the sonoff
  • realizing that setting the diy mode resets the sonoff device!
  • using the diy software from sonoff
  • adding the manual code into openhab

Easy!

Is this any easier than using the tamoto firmware? Not sure. It certainly feels safer as I have not had to try to backup/flash the firmware or use a soldering iron.

1 Like

For those following along this dosen’t work on the new 3.3.0 Firmware

I have updated it here https://github.com/JAMESBOWLER/SONOFF_DIY

I added polling so when something else changes switch state it updates to openhab.

1 Like

Thanks!