20170628_211524s

Itead Studio Sonoff SC Revisited

A few months ago I wrote about the Sonoff SC sensor hub by Itead Studio. It’s a device with a Sharp GP2Y1010AU0F [Aliexpress] dust sensor, a DHT11 humidity and temperature sensor, an LDR as light sensor and a mic. The sensors are driven by an ATMega328P microcontroller but there is also an ESP8266 on board for WiFi communication, a pretty standard set up when you have several sensors and the ESP8266 GPIOs are just not enough.

20170105_125851s

On the first post I already did a small mod to replace the DHT11 humidity and temperature sensor with a more accurate and pin compatible DHT22. Since then, several readers have contributed with code and ideas. My progress implementing and testing them is slow, so I though about writing a first post about some modifications I (and others) have done to the device.

So this is a work in progress post. At the moment, all the code for these modifications is in the dev branch of the repository.

This custom Sonoff SC firmware is released as free open software and can be checked out at my SonoffSC repository on Github.

Flashing the ATMega328p via bootloader

First I’d like to say is that, although I used a AVR programmer for the ATMega328 in my last post this is not required. You can use an FTDI USB to TTL [Ebay, also Aliexpress] board to flash the AVR too since it comes with the Arduino bootloader.

Just remember to connect TX to TX and RX to RX since the pins in the board are labeled from the programmer point of view. Connect RST to the DTR line and the power lines and you are good to go.

20170105_130308s

Adding a microwave radar as presence sensor

Now, this is a suggestion in the issues section of the repository. Since the usual PIR sensors are quite bulky my first idea was to use one of those nice Panasonic EKMB PIR [Ebay]. They are small, very sensitive (with versions with up to 12m detection) and really expensive. They are ultra low power devices meant for low power sensors, but that’s not the case here.

So I moved to a microwave sensor I first spotted at Scargill tech blog. This is a 5.8GHz microwave sensor [Ebay, also on Aliexpress] that costs around 1.5 USD, has a configurable range of up to 8 meters and a detection pulse that’s 30s by default. The device can “sense” movement even through a thin wall so the SonoffSC enclosure is no big deal. But after a few tests I realized it was capturing people on the street outside my house so I decided to replace the resistor in R9_1 with a 100k one. Now the range is about 3-4 meters, safely covering the main walking paths in my livingroom. I also added a noise filter in the power line using a capacitor and a resistor as per Pete’s advice.

20170515_091255s

The microwave sensor is connected to GPIO13 (SCK) in the ISP header. A bit of solder to the VCC, GND and SCK lines and hot glue again to prevent it from going loose.

20170513_232054s

The placement of the sensor is still under testing. Right now I have it on the base of the SonoffSC, hot glued to it with the cables running to the oposite side of the board. Even thou the placement is neat, I have found that sometimes it misses a moving target, still testing if it’s due to the microwave beam or the code.

20170513_230746s 20170513_232224s

Since GPIO13 has no interrupts on the ATMega328P the code checks in the main loop for the pin state. If it changes it immediately reports the change to the ESP8266 via serial.

Adding an RGB LED ring – again

Actually this one was already done thanks to the contributions by Blair Thompson (check his video here). But I decided to go for a slighly different path. The ring is a 24 WS2812 LED ring [Ebay, also on Aliexpress] placed in the middle compartment of the enclosure. The VCC, GND and DI lines are brought out and connected to the ISP header like with the microwave sensor using GPIO 12 (MISO) now. Just like Blair’s approach.

20170513_230549s 20170513_230733s 20170513_232204s

WS2812FX

The difference with Blair’s code is in the library I’m using. On top of  the Adafruit NeoPixel library I’m using the WS2812FX library by Harm Aldick (kitesurfer1404). The library defines an indexed list of effects for LED stripes and so. Very convenient to be called from a remote MQTT based RPC.

Another incompatible change is that by default (although this might change in the final version) it performs a blue fade in when movement is detected and fades out when no movement. This behaviour is hardcoded in the current dev branch but I might:

  1. Allow the user to configure the behaviour via the web interface
  2. Use an external service (Node-RED) to write the business inteligence and just send data and receive instructions via MQTT

I like the second option better, moving the intelligence outside the device looks reasonable and more flexible. But I’m aware most users might want to have the behaviour embedded…

Blair’s code is still in the master branch, version 0.3.0. Once the current dev branch is merged to master it will be tagged as version 1.0.0.

Adding a mini-fan to improve dust sensor readings

One of the issues several people have detected is that the dust sensor has very little range. This is probably due to it being inside an enclosure with little air vent. There was a little debate about it in the repo and I decided to try a small SEPA MF15B-05 5V fan I found on Aliexpress. It’s a 15x15mm 0.06A fan.

20170526_101539s

The fan is driven by the LED signal in the board (GPIO7). I decided to hot glue it on top of the dust sensor so it forces the air to flow from the outside of the enclosure down. Whenever a new dust reading is scheduled or requested, the fan starts up for 5 seconds before the reading is actually performed. I have also changed the code to use ug/m3 instead of mg/m3.

20170526_101413s 20170526_101351s

I have been testing it for the last 2 days. Still not enough data but it looks like the daily average value is twice the value it used to be and the standard deviation is also greater. I still have to test with the fan running for a longer time (or even all the time) before reading the sensor data. The good thing is that the fan is noise-less (as far as I can hear).

Changes in the code

The code has also gone through a major review. Basically importing features from the ESPurna project. Aside from the changes to support the new functionalities in this post, other main changes include:

  • Improvements in the SerialLink library (now it allows long integers as argument, good for RGB values)
  • Debug via UDP (using Serial is not possible since it’s the communication channel between both microcontrollers)
  • Moved debug strings to program memory
  • Embed the webserver pages and resources into the firmware (faster, more free room for code): will write about it soon

There are already some other minor changes, improvements, bug fixes. I want to include more issues from the repo before merging this to master. Some of these improvements include new sensors. At some point you will have to choose from the different options since there are just so many GPIOs.

Flashing

Remember you will have to flash both microcontrollers again. But this time it won’t be necessary to flash the SPIFFS partition on the ESP8266. You can flash both using an FTDI USB to TTL programmer like the one I linked before. While flashing remove the communication jumpers. Also remember that the pins are labeled from the programmer point of view.

Since there are changes in the memory layout, erase the ESP8266 flash memory prior to upload the new code. The new layout is the 1m64 (1 Mbyte with 64Kbytes for SPIFFS). If you are using PlatformIO these settings are defined in the project file. Otherwise you will have to select the right option from the Arduino menu if you want OTA to work.

That’s it. As I have already said this is a work in progress so there might be changes and new additions to the project. Check the repo from time to time. I’m aware there are missing details here and there so don’t hesitate in asking. And thanks for reading me 🙂

CC BY-SA 4.0 Itead Studio Sonoff SC Revisited by Tinkerman is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

47 thoughts on “Itead Studio Sonoff SC Revisited

  1. Justblair

    Looks good. I have one of those microwave sensors (bought to replace a pir in the kitchen). I think I will give it a try on the sonoff.

    Reply
  2. Adam

    Thanks so much for all your work. I notice that apparently the GP2Y1014AU0F has replaced the GP2Y1010AUOF.

    So the Sonoff SC has the older sensor, I wasnt able to find the differences between them.

    Reply
    1. Xose Pérez Post author

      Apparently the only difference (comparing their datasheets) is that the factory sensitivity value is more stable on the new version and the output voltage at no dust has been lowered from 0.9V to 0.6V, maybe alowing a greater range of values. Appart from that they are virtually identical.

      Reply
  3. Mark

    Will you add the WS2812FX library also to the firmware for esp8266 lightbulb and that “magic”-led strip which also has esp inside 🙂 ?

    Reply
    1. Xose Pérez Post author

      Probably, but I can’t tell you when. If you add a enhancement issue to the repo we could add it to the roadmap.

      Reply
    1. Xose Pérez Post author

      I don’t have access to that sensor, but you can contribute the project 🙂

      Reply
  4. ginsi

    I need some help for building the espurna project on Arduino IDE (I get a lot of “xxx was not declared in this scope” when I try to compile, although I have downloaded and installed the indicated mandatory libraries). I can imagine this place is not exactly intended for such questions and apologize for this, but I don’t know where else to apply… (I tried the Arduino forum with little success insofar) … Suggestions? Thank you in advance for your attention.

    Reply
    1. Xose Pérez Post author

      I will need more info to help you.
      You can contact me directly via email (it’s in the espurna.ino code file) or open an issue on bitbucket.

      Reply
  5. Ronald

    I tried compiling the dev version, after I installed the 0.3.0 last week. I want to make full use of the WS2812-ring I installed.

    Here’s where I quit for today:
    * My version of WS2812FX library (https://github.com/kitesurfer1404/WS2812FX) doesn’t know FX_MODE_FADE_OUT. I replaced it by FX_MODE_STATIC and setting the color to 0. This compiles, but I couldn’t try it because…
    * On the ESP-side, the line link.onSet(commsSet) throws an error because the signature of commsSet() isn’t right. It looks like link.onSet gives an int as second parameter, whereas commsSet() expects a long value.

    Right now I don’t have the time to continue playing with this. But I hope that you soon find the time to release version 1.0.0 — I’m looking forward to it. Thanks for your great work!

    Reply
    1. Xose Pérez Post author

      Oops, there are certainly some issues on the code. At leats on the WS2812FX part, it’s not pointing to the fork I created, let me check it. The link.onSet part looks right to me, the signatures on both sides and on the SerialLink library require a long.

      Reply
      1. Ronald

        Oh, I see. I expected that I’d have to install a new version of SerialLink, so I took it from the included SerialLink.zip — which is still the old version. After installing the unzipped version I was able to compile and flash.
        Now, the next problem is that I can’t connect to the access point for some strange reason. Neither from my notebook, nor from my android phone. The devices connect, but they don’t get assigned an IP address.
        I’ll have to investigate this a little more, later.

        Reply
  6. Sergio

    Newbie here.
    I tried with sonoff Tasmota project to flash ESM and everithing went good using platformio and after reading your fantastic project wanted to try with your software, too.
    Unfortunately I’m not sure about using platformio in the right way: it keeps on popping out the error “VCS: ‘git’ client is not installed in your system”
    Anyways I’m quite sure that git is installed (took from https://desktop.github.com/) but still don’t understand how platformio works…
    What could be my error?

    Reply
  7. ken zebra

    Thank you Xose.. working great.. looking into the code now and trying my luck with mods.. found platform io. amazing..

    Reply
  8. ken zebra

    TIP!!! if using windows make sure you copy the SerialLink folder to the lib directory. windows doesnt do simlinks properly.. fixed my issues

    Reply
  9. ArinRoy

    I really liked the way you have modified the Sonoff SC firmware and added sensors. As this is based on Arduino is there a way to also add other different sensors like different gas for detecting fire or CO or Nautral gas ? Just a thought.

    Reply
    1. Xose Pérez Post author

      Sure, the only restriction is the available GPIOs and the space inside the enclosure. The LDR, the dust sensor, the DHT and the LED, they are all connected to the main board using pretty standard connectors. You can source them and connect other stuff instead, or use the SPI connector for the same purpose.

      Reply
        1. Xose Pérez Post author

          Yes, but you might have to sacrifice some other sensors to do so. The only ADC available in a header (AFAI remember) is already used by the dust sensor.

          Reply
          1. Marco Ghiglieri

            Thanks Xose – I could decide to sacrifice it. My home isn’t so dusty, but it’s strange it always reads a totally clean ambient. Anyway, before going to take it off, I’ll give it a try off the SC shroud.

            PS: I just re-read my past post… sorry, it’s quite unreadable…

        2. Josenivaldo Benito Junior

          Hello Marco,

          Same here, rev.2 with the DHT11 direct soldered.

          Another issue I am facing is not able to flash ATMEGA328. I use platfomio and had flashed hundreds of other arduinos with it but SonoffSC is beating me. It seems like DTR is not working but I triple checked wiring, switched FTDI with a spare (tested on other arduino), still no success. Since I did not have a USBASP around I will have to call it a day and try “Arduino as ISP” tomorrow.

          Reply
          1. Marco Ghiglieri

            Hi! I haven’t worked on it, still – just flashed the Tasmota firmware, I was more confident with it at the time. ASAP I’ll desolder the DHT11 (not that easy!) and try Xose’s work… waiting for the LED ring.

          2. Marco Ghiglieri

            Hi there! Finally I sorted out why my sensors were at 0. It looks like I have some communication problems between the two chips – I have to test the level shifter (those transistors near the jumpers). I can remember I had the Light sensor readings before unsoldering the DHT, so… be carefull doing this mod.

            BTW… I have the green LED always blinking. Is it ok?

          3. Xose Pérez Post author

            Great. That LED blinks at 1/5Hz when connected to your home router or at 1Hz if in AP mode.

  10. Alex Sen

    Hi,

    how can i use the RGB ring?
    I flashed the version 1.1.0 but the RGB ring wont work.

    i send this topic:
    mosquitto_pub -h localhost -t “/test/sonoffsc/color/set” -m “#ff0000”

    [MQTT] Received /test/sonoffsc/color/set => #ff0000
    [LIGHTS] Color to 16581375

    Reply
    1. Xose Pérez Post author

      Please update to version 1.1.1, since there is a bug in the color code for 1.1.0. But your mosquitto command is right, double check the topic base (/test/sonoffsc) and the ring wiring…

      Reply
    1. Xose Pérez Post author

      I screwed the MOSI pin while working with it and had to change use MISO instead. The picture is old.

      Reply
  11. Ayziaa

    I’m really interested in a sonoff sc, but I read the dht11 is not a very goog sensor (~2°c accuracy). Is there any way to change the dht11 to an other sensor without loosing the compatibility to the ewelink app? thx

    Reply
    1. Xose Pérez Post author

      Mmmm… not sure. The DHT22 (not a great sensor but better than the DHT11) is pin compatible with the DHT11 but it does require a small modification in the firmware so I doubt it woould work with the original firmware. Itead has support for the DHT22 (AM2302) and the DS18B20 in other products (Sonoff TH10/16) but I don’t think they added support for them in the Sonoff SC.

      Reply
  12. balvan

    I try to upload the firmware using the platformio but I have a problem with the size
    Checking program size
    text data bss dec hex filename
    38588 106 1020 39714 9b22 .pioenvs / sonoffsc / firmware.elf
    Error: The program size (38694 bytes) is greater than the maximum allowed (32256 bytes)

    platformio.ini
    [platformio]
    env_default = sonoffsc
    src_dir = sonoffsc

    [env:sonoffsc]
    platform = atmelavr
    board = uno
    framework = arduino
    build_flags = -DDHT_TYPE=DHT22
    lib_deps =
    DHT sensor library
    Adafruit NeoPixel
    WS2812FX
    Ticker
    https://github.com/xoseperez/seriallink#0.1.0

    Reply
    1. Xose Pérez Post author

      When I build it it has a final size of 29726 bytes (90.7%)…


      text data bss dec hex filename
      29620 106 692 30418 76d2 .pioenvs/sonoffsc/firmware.elf

      Reply
      1. balvan

        Thanks very much for reply . I use Platformio Home 0.3.2· Core 3.5.0b2 a version sonoff sc 1.1.1 and now have
        text data bss dec hex filename
        38588 106 1020 39714 9b22 .pioenvs\sonoffsc\firmware.elf

        You do not have any other versions of libraries which are defined in platformio.ini

        |– v1.3.0
        | |– v1.0.2
        |– v1.1.3
        |– v1.0.0
        | |– v1.1.3
        |– v2.0.2
        |– v0.1.0

        thx

        Reply
  13. petorian79

    I also receive a message that the program size is exceeding the maximum: tEext data bss dec hex filenamerror: The program size (38694 bytes) is greater than maximum allowed (32256 bytes)

    Reply

Leave a Reply (all comments are moderated, be patient)