Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Xose Pérez committed May 20, 2016
0 parents commit c7edfa1
Show file tree
Hide file tree
Showing 12 changed files with 1,722 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.pioenvs
65 changes: 65 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < http://docs.platformio.org/en/latest/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < http://docs.platformio.org/en/latest/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < http://docs.platformio.org/en/latest/userguide/cmd_ci.html >
#
#
# Please choice one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#


#
# Template #1: General project. Test it using existing `platformio.ini`.
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
#
# script:
# - platformio run


#
# Template #2: The project is intended to by used as a library with examples
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
#
# script:
# - platformio ci --lib="." --board=TYPE_1 --board=TYPE_2 --board=TYPE_N
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# ITead Sonoff Custom Firmware

This is a custom C firmware for [ITead Sonoff][1] Smart WiFi Switch. This device
has an ESP8266 on board with a 8Mbit flash memory chip, a mains to 3V3 transformer
and a relay (GPIO12). It also features a button (GPIO0), a LED (GPIO13) and
an unpopulated header you can use to reprogram it.

## Features

* WebServer for configuration and simple relay toggle
* You can configure up to 3 WIFI networks
* MQTT support with configurable host and topic
* Manual switch ON/OFF with button
* Visual status of the connection via the LED

## Flashing

The unpopulated header has all the required pins. My board has a 5 pins header
in-line with the button. They are (from the button outwards):

* 3V3
* RX
* TX
* GND
* MTNS

Last one is not necessary. Mind it's a **3V3 device**, if connected to 5V you will
probably fry it. Button is connected to GPIO0 on the ESP8266 chip, so to enter
flash mode you have to hold the button pressed while powering on the board, then
you can realease it again.

## Firmware

The project is ready to be build using [PlatformIO][2].
Please refer to their web page for instructions on how to install the builder.
Once installed:

```bash
> platformio init -b esp01_1m
> platformio run
> platformio run --target upload
> platformio run --target uploadfs
```

Library dependencies are automatically managed via PlatformIO Library Manager.

## Usage

On normal boot (i.e. button not pressed) it will execute the firmware.
It configures the hardware (button, LED, relay), the SPIFFS memory access, the
WIFI, the WebServer and MQTT connection.

Obviously the default values for WIFI network and MQTT will probably not match
your requirements. Either it connects to a WiFi or not, it will set up a Soft AP
named "SONOFF_XXXX", where XXXX are the las 2 bytes of the radio MAC. Connect with
phone, PC, laptop, whatever to that network, password is "fibonacci". Once connected
browse to 192.168.4.1 and you will be presented a configuration page where you will
be able to define up to 3 possible WIFI networks and the MQTT configuration parameters.

It will then try to connect to the first WIFI network. If fail it will try the second
in 30 seconds, and so on. Once connected it will try to connect the MQTT server.

The device will publish the relay state to the given topic and it will subscribe to
the same topic plus "/set" for remote switching. So if your topic is "/home/living/switch"
you will be able to switch it on/off sending "1"/"0" to "/home/living/switch/set".

You can also use "{identifier}" as place holder in the topic. It will be translated to
your device ID (same as the soft AP network it creates).


[1]: https://www.itead.cc/sonoff-wifi-wireless-switch.html
[2]: http://www.platformio.org
1 change: 1 addition & 0 deletions arduino/code/code.ino
1 change: 1 addition & 0 deletions arduino/code/data
127 changes: 127 additions & 0 deletions data/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

body {
padding: 20px;
width: 600px;
}

span.status,
span.network,
span.ip {
font-weight: bold;
}

form header {
margin: 0 0 20px 0;
}

form header div {
font-size: 90%;
color: #999;
}

form header h2 {
margin: 0 0 5px 0;
}

form header img {
margin: 0 0 10px 0;
}

form > div {
clear: both;
overflow: hidden;
padding: 1px;
margin: 0 0 10px 0;
}

form > div > fieldset > div > div {
margin: 0 0 5px 0;
}

form > div > label,
legend {
width: 30%;
float: left;
padding-right: 10px;
}

form > div > div,
form > div > fieldset > div {
width: 70%;
float: right;
}

form > div > fieldset label {
font-size: 90%;
}

fieldset {
border: 0;
padding: 0;
}

input[type=text],
input[type=email],
input[type=url],
input[type=password],
textarea {
width: 100%;
border-top: 1px solid #ccc;
border-left: 1px solid #ccc;
border-right: 1px solid #eee;
border-bottom: 1px solid #eee;
}

input[type=text],
input[type=email],
input[type=url],
input[type=password] {
width: 100%;
}

input[type=text]:focus,
input[type=email]:focus,
input[type=url]:focus,
input[type=password]:focus,
textarea:focus {
outline: 0;
border-color: #4697e4;
}

@media (max-width: 600px) {

body {
width: 100%;
}

form > div {
margin: 0 0 15px 0;
}

form > div > label,
legend {
width: 100%;
float: none;
margin: 0 0 5px 0;
}

form > div > div,
form > div > fieldset > div {
width: 100%;
float: none;
}

input[type=text],
input[type=email],
input[type=url],
input[type=password],
textarea,
select {
width: 100%;
}
}
120 changes: 120 additions & 0 deletions data/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<!DOCTYPE html>
<html>
<head>
<title>SONOFF Configuration</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="index.css" />
<script src="jquery-1.12.3.min.js"></script>
<script>

$(function() {

$("#config").submit(function(event) {
$(":submit").prop("value", "Wait...").attr("disabled", true);
$.ajax({
'method': 'POST',
'url': '/save',
'dataType': 'json',
'data': $(this).serializeArray()
}).done(function(data) {
if (data.ip) $("span.ip").html(data.ip);
if (data.network) $("span.network").html(data.network);
if (data.status) $("span.status").html(data.status);
$(":submit").prop("value", "Connect").removeAttr("disabled");
}).fail(function() {
$(":submit").prop("value", "Connect").removeAttr("disabled");
});
event.preventDefault();
});

});

</script>
</head>
<body>

<form id="config" action="/" method="post">

<header>
<h2>SONOFF Configuration</h2>
<div>This form lets you define the WIFI network this SONOFF should try to join.</div>
</header>

<div>
<p>Connection: <span class="status">{status}</span></p>
<p>Network: <span class="network">{network}</span> (<span class="ip">{ip}</span>)</p>
</div>

<div>
<label class="desc" for="ssid0">Network SSID 0</label>
<div>
<input id="ssid0" name="ssid0" type="text" class="field text fn" value="{ssid0}" size="8" tabindex="1">
</div>
</div>

<div>
<label class="desc" for="pass0">Network Password 0</label>
<div>
<input id="pass0" name="pass0" type="text" spellcheck="false" value="{pass0}" maxlength="255" tabindex="2">
</div>
</div>

<div>
<label class="desc" for="ssid1">Network SSID 1</label>
<div>
<input id="ssid1" name="ssid1" type="text" class="field text fn" value="{ssid1}" size="8" tabindex="3">
</div>
</div>

<div>
<label class="desc" for="pass1">Network Password 1</label>
<div>
<input id="pass1" name="pass1" type="text" spellcheck="false" value="{pass1}" maxlength="255" tabindex="4">
</div>
</div>

<div>
<label class="desc" for="ssid2">Network SSID 2</label>
<div>
<input id="ssid2" name="ssid2" type="text" class="field text fn" value="{ssid2}" size="8" tabindex="5">
</div>
</div>

<div>
<label class="desc" for="pass2">Network Password 2</label>
<div>
<input id="pass2" name="pass2" type="text" spellcheck="false" value="{pass2}" maxlength="255" tabindex="6">
</div>
</div>

<div>
<label class="desc" for="mqtt_server">MQTT Server</label>
<div>
<input id="mqtt_server" name="mqtt_server" type="text" class="field text fn" value="{mqtt_server}" size="8" tabindex="7">
</div>
</div>

<div>
<label class="desc" for="mqtt_port">MQTT Port</label>
<div>
<input id="mqtt_port" name="mqtt_port" type="text" spellcheck="false" value="{mqtt_port}" maxlength="255" tabindex="8">
</div>
</div>

<div>
<label class="desc" for="mqtt_topic">MQTT Topic</label>
<div>
<input id="mqtt_topic" name="mqtt_topic" type="text" class="field text fn" value="{mqtt_topic}" size="8" tabindex="9">
</div>
</div>

<div>
<div>
<input id="submit" name="submit" type="submit" value="Connect">
</div>
</div>

</form>

</body>
</html>
5 changes: 5 additions & 0 deletions data/jquery-1.12.3.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit c7edfa1

Please sign in to comment.