Skip to content

Instantly share code, notes, and snippets.

@ajfisher
Last active January 3, 2017 23:51

Revisions

  1. ajfisher revised this gist Mar 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _Pebble controlled NeoPixels using ESP8266.md
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ Video: https://www.youtube.com/watch?v=BeIQ47WBVXs

    ## Acknowledgements

    Thanks to [Andy Gelme](http://github.com/geekscape) for his excellent workshop on the ESP8266 and getting me up to speed on the Lua side.
    Thanks to [Andy Gelme](http://github.com/geekscape) for his [excellent workshop on the ESP8266](https://github.com/geekscape/nodemcu_esp8266) and getting me up to speed on the Lua side.

    ## Directions

  2. ajfisher revised this gist Mar 18, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions _Pebble controlled NeoPixels using ESP8266.md
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,10 @@ Video: https://www.youtube.com/watch?v=BeIQ47WBVXs
    * ESPlora
    * Pebble SDK and command line tools

    ## Acknowledgements

    Thanks to [Andy Gelme](http://github.com/geekscape) for his excellent workshop on the ESP8266 and getting me up to speed on the Lua side.

    ## Directions

    * Using ESPTool, flash the ESP8266 with NodeMCU development branch (bins are in this gist) so it has WS8212 support.
  3. ajfisher revised this gist Mar 18, 2015. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _Pebble controlled NeoPixels using ESP8266.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    This is the general instructions for how to get a pebble watch to talk to an ESP8266 and turn NeoPixels different colours.

    ![Pebble Controlled NeoPixels](https://gist.github.com/ajfisher/ee6fadcd837a0f46be8d/raw/3e3f2dc8b7681b06322cfc411d08825d4d06e522/neopixel_pebblewatch.gif)
    ![Pebble Controlled NeoPixels](https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-15/11023004_626914707452886_2000560514_n.jpg)

    Video: https://www.youtube.com/watch?v=BeIQ47WBVXs

    Binary file removed neopixel_pebblewatch.gif
    Binary file not shown.
  4. ajfisher revised this gist Mar 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _Pebble controlled NeoPixels using ESP8266.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    This is the general instructions for how to get a pebble watch to talk to an ESP8266 and turn NeoPixels different colours.

    ![Pebble Controlled NeoPixels](neopixel_pebblewatch.gif)
    ![Pebble Controlled NeoPixels](https://gist.github.com/ajfisher/ee6fadcd837a0f46be8d/raw/3e3f2dc8b7681b06322cfc411d08825d4d06e522/neopixel_pebblewatch.gif)

    Video: https://www.youtube.com/watch?v=BeIQ47WBVXs

  5. ajfisher revised this gist Mar 18, 2015. 2 changed files with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions _Pebble controlled NeoPixels using ESP8266.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,8 @@
    This is the general instructions for how to get a pebble watch to talk to an ESP8266 and turn NeoPixels different colours.

    ![Pebble Controlled NeoPixels](https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-15/11023004_626914707452886_2000560514_n.jpg)
    ![Pebble Controlled NeoPixels](neopixel_pebblewatch.gif)

    Video:

    <iframe width="560" height="315" src="https://www.youtube.com/embed/BeIQ47WBVXs" frameborder="0" allowfullscreen></iframe>
    Video: https://www.youtube.com/watch?v=BeIQ47WBVXs

    ## Prereqs

    Binary file added neopixel_pebblewatch.gif
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  6. ajfisher revised this gist Mar 18, 2015. 1 changed file with 35 additions and 2 deletions.
    37 changes: 35 additions & 2 deletions _Pebble controlled NeoPixels using ESP8266.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,41 @@
    _This is a placeholder at the moment. Just watch this Gist for code etc coming over the next day or so_
    This is the general instructions for how to get a pebble watch to talk to an ESP8266 and turn NeoPixels different colours.

    ![Pebble Controlled NeoPixels](https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-15/11023004_626914707452886_2000560514_n.jpg)

    Video:

    <iframe width="560" height="315" src="https://www.youtube.com/embed/BeIQ47WBVXs" frameborder="0" allowfullscreen></iframe>

    ## Prereqs

    * Pebble watch
    * ESP8266 ESP-01
    * NeoPixel strip (or ring or matrix or single pixel)
    *

    ## Software needed

    * ESPTool
    * ESPlora
    * Pebble SDK and command line tools

    ## Directions

    * Using ESPTool, flash the ESP8266 with NodeMCU development branch (bins are in this gist) so it has WS8212 support.
    * Using ESPlora, change config.lua to have SSID / PASSWORD of your network in them - upload all lua files to the ESP8266
    * Wire up the WS2812 strip so data line is connected to GPIO 2 (change this in server.lua if you use a different pin).
    * Reset the ESP8266 and note it's IP address in ESPlora when it comes up. You should now be able to hit the web server using

    ```
    curl --data "red=255&green=0&blue=255" http://<IP address>
    ```

    * Using CloudPebble, copy app.js contents into the app.js file (make a watch app project and use Pebble JS)
    * Change the HOST IP address to that of your ESP8266
    * Compile and then download the PBW file
    * Use the developer configuration (as documented by Pebble SDK setup) to run:

    ```
    pebble install watch_led.pbw
    ```

    That's it - you should be able to hit it as expected.
  7. ajfisher revised this gist Mar 17, 2015. 8 changed files with 188 additions and 0 deletions.
    64 changes: 64 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    /**
    Pebble - ESP8266 - NeoPixel controller
    Author: Andrew Fisher
    Licence: MIT
    **/
    var UI = require('ui');
    var ajax = require('ajax');

    var HOST = '<<IP>>';


    // Connect to the ESP8266 and post the data
    function colour_request(r, g, b) {
    //console.log("Making request");
    //console.log("r:" + r + " g: " + g);

    var req = {
    url: HOST,
    method: 'post',
    data: {red:r, green:g, blue:b}
    };

    //console.log(JSON.stringify(req));

    ajax(req,
    function(data, status, request) {
    console.log(data);
    },
    function(data, status, request) {
    console.log('The ajax request failed: ' + data + status + JSON.stringify(request));
    }
    );

    }

    // Make a list of the colours to select
    var colours = [
    { title: "OFF", r: 0, g: 0, b: 0, },
    { title: "RED", r: 255, g: 0, b: 0, },
    { title: "GREEN", r: 0, g: 255, b: 0, },
    { title: "BLUE", r: 0, g: 0, b: 255, },
    { title: "YELLOW", r: 255, g: 255, b: 0, },
    { title: "MAGENTA", r: 255, g: 0, b: 255, },
    { title: "CYAN", r: 0, g: 255, b: 255, },
    { title: "WHITE", r: 255, g: 255, b: 255, },
    ];

    // Create the Menu
    var menu = new UI.Menu({
    sections: [{
    title: 'Choose LED colour',
    items: colours
    }]
    });

    // Send command when menu item is selected
    menu.on('select', function(e) {
    colour_request(e.item.r, e.item.g, e.item.b);
    });

    menu.show();
    11 changes: 11 additions & 0 deletions application.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    local module = {}

    function module.start()
    print("application start")
    print("IP address: " .. wifi.sta.getip())

    dofile("server.lua")

    end

    return module
    8 changes: 8 additions & 0 deletions config.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    local module = {}

    module.SSID = {}
    module.SSID["SSID"] = "PASSWORD"



    return module
    4 changes: 4 additions & 0 deletions init.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    app = require("application")
    config = require("config")

    require("setup").start()
    Binary file added nodemcu_dev_0x00000.bin
    Binary file not shown.
    Binary file added nodemcu_dev_0x10000.bin
    Binary file not shown.
    73 changes: 73 additions & 0 deletions server.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    local PORT = 80
    local LED_PIN = 4 -- GPIO 2
    local BRIGHTNESS = 0.2 -- NeoPixels are BRIGHT.
    local NO_PIXELS = 17

    if server then
    print("Closing existing server")
    server:close()
    end

    print("Starting server on: " .. wifi.sta.getip() .. ":" .. PORT)

    server = net.createServer(net.TCP)
    server:listen(PORT, function(conn)

    conn:on("receive", function(client, payload)
    -- print(payload)
    local head = ""
    local body = ""
    local colours = {}
    colours["red"] = 0
    colours["green"] = 0
    colours["blue"] = 0

    if (string.sub(payload, 1, 4) == "POST") then
    head = head .. "HTTP/1.1 200 OK\r\n"
    head = head .. "Content-Type: application/json\r\n"

    -- now we find the RGB components. Going to do this simply
    -- by ripping them out with a find statement
    for c, v in pairs(colours) do
    local match = c .. "=(%d*)"
    _, _, colours[c] = string.find(payload, match)
    -- put in a catch here incase it goes nil
    end

    set_led_colour(colours, NO_PIXELS)

    -- send the JSON back to confirm
    body = body .. "{\"r\":" .. colours["red"] .. ", "
    body = body .. "\"g\":" .. colours["green"] .. ", "
    body = body .. "\"b\":" .. colours["blue"] .. "}"
    else
    head = head .. "HTTP/1.1 500 Server Error\r\n"
    body = body .. "Please ensure you use a POST method"
    end

    body = body .. "\r\n" -- just tidy up the end of the file
    head = head .. "Content-Length: " .. string.len(body) .. "\r\n\r\n"

    --print(head .. body)
    client:send(head .. body)

    end)

    conn:on("sent", function(client)
    client:close()
    end)

    end)


    function create_colour(red, green, blue)
    -- takes the colours and makes single value for writing to the LEDs
    local colour = string.char(green * BRIGHTNESS, red * BRIGHTNESS, blue * BRIGHTNESS)
    return colour
    end

    function set_led_colour(colours, pixels)
    -- takes the colour values and writes that to the number of pixels
    -- assmues a table colours{"red":val, "green":val, "blue":val}
    ws2812.write(LED_PIN, create_colour(colours["red"], colours["green"], colours["blue"]):rep(pixels))
    end
    28 changes: 28 additions & 0 deletions setup.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    local module = {}

    local function wifi_wait_ip()
    if wifi.sta.getip() then
    tmr.stop(1)
    -- print("wifi ready: " .. wifi.sta.getip())
    app.start()
    end
    end

    local function wifi_start(aps)
    for key,value in pairs(aps) do
    -- print("wifi AP: " .. key .. ": " .. value)
    if config.SSID and config.SSID[key] then
    wifi.sta.config(key, config.SSID[key])
    wifi.sta.connect()
    config.SSID = nil -- more secure and save memory
    tmr.alarm(1, 2500, 1, wifi_wait_ip)
    end
    end
    end

    function module.start()
    wifi.setmode(wifi.STATION);
    wifi.sta.getap(wifi_start)
    end

    return module
  8. ajfisher created this gist Mar 15, 2015.
    8 changes: 8 additions & 0 deletions _Pebble controlled NeoPixels using ESP8266.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    _This is a placeholder at the moment. Just watch this Gist for code etc coming over the next day or so_

    ## Prereqs

    * Pebble watch
    * ESP8266 ESP-01
    * NeoPixel strip (or ring or matrix or single pixel)
    *