Espruino and Amazon Alexa integration (implementation completed)

Posted on
Page
of 3
Prev
/ 3
Next
  • you found me online, wanted to try....

    the lines with require('dgram') and other highligted red - err.

    but compiled and uploaded anyway.... than an execution error came:

    require('url') get nuts - there is no such module :)
    

    I would say I better wait you get your Alexa :-)

  • Oh well.. url.parse is directly supported by espruino. I hacked away in and IDE with nodejs settings
    and must have required the url module because something wasn't resolving. I am of to work now
    but after i will test the changes as far as i can get.

    Do you have an github account? Would make things a lot easier to share code and collaborate in this project :)

  • yep, github, user (vaxus1).

  • Hi there,

    I am also interested to integrate Alexa with Esp8266. I have an Echo Dot and esp8266 12E and I tried all the attached code with latest Espruino firmware downloaded from github master tree on tip.

    I am getting lots of error when I am running discovery. doPost_handle* method is giving Javascript error.

    I can also post the screen shot of error.

    Another thing that I want to try the Alexa.js by PaddeK as a module but I don’t know how to call/use that module.
    I am a Dot net programmer and new to nodeJs
    Thanks

  • paddek's version is still not working lets wait, his code is very sexy looking :) .

    for esp u have to change some pin definitions. send the err log, interesting.

  • @Vasily its practically your code just refactored a bit.
    According to Amazon my Echo Dot will ship on thursday.
    I try to test and fix the things i messed up while blindly recfactoring
    but without a actual alexa device on hand it will be hard.
    I will make a repository for this on my github account and add you as a collaborator.
    If this is not okay for you let me know and we will find a solution. I simply like to develop with
    a VCS because i work on multiple machines.

  • perfect plan... i was waiting a month to get espruino Udp working, so i wll be happy to see better code and working

  • I am in the midst of testing and was about to move the wifi part as external dependency (because there are at least two version EspruinoWifi/ESP8266WiFi) when i realized something odd.
    You require a "Wifi" module which isn't known and you are not using a callback for the "getIP" method which is required in EspruinoWifi/ESP8266WiFi. Do you use a custom implementation for wifi?

    Edit

    I am using latest travis build from here

    Edit 2

    Okay.. i should go to bed.. the espruino docs clearly say there is a Wifi module and its currently for ESP8266 only :)

    Edit 3

    Ok.. now i am hitting a real snag.. UDP support. I only have a EspruinoWifi and dgram is only
    for ESP8266/ESP32 for now as far as i know. So i will have to wait for 1v95.
    I pushed everything to https://github.com/PaddeK/espruino-alexa­ maybe you can test it beyond this point.

  • @PaddeK! you can use echoshim.io for testing purpose. Just install Alexa App in your smart phone and connect it with Amazon account.

    Echoshim is a browser based Echo emulator that is also connected with the same Amazon account that your actual echo dot.

  • @Vasily,
    Till the time @PaddeK receives his Echo Dot, I will test the code on my dot.
    So here are the details of error that I was talking about.

    Firmware flashed : From here "espruino_1v94_esp8266_4mb.tgz"

    Test 1: I first tried file with name "alexa_ok4done.js" from your attachment.

                         Got a warning popup - Module dgram not found. But compiles without any issue.
                         Result: Error in device discovery. So I fixed the code 
    
     msg += "USN: uuid:Socket-1_0-"+getSerialNumber()+"::u­rn:Belkin:device:**\r\n";
    

    Test 2: I have modified the code and now device is discovered. But when I tried to turn it on/off using Alexa app.

    onPageRequest
    ****************************************­**************
    Header
    Req ( POST  req.url=  /upnp/control/basicevent1
    Req=
    httpSRs {  }
    Post length=
    Post url=
    {
      "method": "GET",
      "host": "",
      "path": "/upnp/control/basicevent1",
      "pathname": "/upnp/control/basicevent1",
      "search": null, "port": null, "query": null }
    ****************************************­**************
    Post (/upnp/control/basicevent1) received, start special handling doPost_handleUpnpControl
    pdata.length == length67 / 299
    pdata.length == length299 / 299
    ERROR: Error processing Serial data handler - removing it.
    Execution Interrupted during event processing.
    at line 340 col 31
          pinf("pdata == " + pdata);
                                  ^
    in function called from system
    New interpreter error: CALLBACK,MEMORY
    onPageRequest
    ****************************************­**************
    Header
    Req ( POST  req.url=  /upnp/control/basicevent1
    Req=
    httpSRs {  }
    Post length=
    Post url=
    {
      "method": "GET",
      "host": "",
      "path": "/upnp/control/basicevent1",
      "pathname": "/upnp/control/basicevent1",
      "search": null, "port": null, "query": null }
    ****************************************­**************
    Post (/upnp/control/basicevent1) received, start special handling doPost_handleUpnpControl
    Uncaught SyntaxError: Got return expected '}'
    at line 415 col 4
      });
       ^
    in function "doPost_handleUpnpControl" called from line 170 col 41
            doPost_handleUpnpControl(req,res);
                                            ^
     at line 171 col 9
            break;
            ^
    in function called from system
    server.on UDP message received
    ---
    

    And via Alexa Ask

    onPageRequest
    ****************************************­**************
    Header
    Req ( POST  req.url=  /upnp/control/basicevent1
    Req=
    httpSRs {  }
    Post length=
    Post url=
    {
      "method": "GET",
      "host": "",
      "path": "/upnp/control/basicevent1",
      "pathname": "/upnp/control/basicevent1",
      "search": null, "port": null, "query": null }
    ****************************************­**************
    Post (/upnp/control/basicevent1) received, start special handling doPost_handleUpnpControl
    pdata.length == length366 / 299
    ERROR: Error processing Serial data handler - removing it.
    Execution Interrupted during event processing.
    at line 336 col 66
    ...ata.length + " / " + length);
    

    I will try @PaddeK module share the results.
    EDIT: Just tried the Alexa module. It is not working. I gave saved the alexa.js in my local disk in SandBox folder. My other modules are loaded from the same location, so seems there is no issue with loading of the module from disk.

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v94 Copyright 2017 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
    >Uncaught SyntaxError: Got UNFINISHED STRING expected EOF
     at line 1 col 27
    Modules.addCached("alexa","'use strict';\n\nconst\n    Defau...
                              ^
    New interpreter error: LOW_MEMORY,MEMORY
    Uncaught Error: Module "alexa" not found
     at line 1 col 28
    var alexa = require('alexa');
                               ^
    =undefined
    

    1 Attachment

  • Thx @Abhinav for the hint to echoshim.io this will be perfect for testing.
    But i have to get UDP working first on my EspruinoWifi.
    For testing the current version of the alexa module try this:

    let Wifi = require('Wifi'),
        Alexa = require('https://raw.githubusercontent.c­om/PaddeK/espruino-alexa/master/alexa.js­');
    
    let alexa = new Alexa(Wifi, {
      wifi: {
        ssid: 'your-wifi-ssid',
        password: 'your-wifi-password'
      }
    });
    

    The used Wifi module is a copy of the EspruinoWifi with some fixes i added. If you have a ESP board you should be fine.. if you use EspruinoWifi you will get an error in getIP without the fixed
    version.

  • You can compile the Linux version of espruino and use that for testing.

  • @PaddeK yes, sorry about this. UDP support for Espruino WiFi is something I am working on - but it still needs a bit more work before it's usable.

    The getIP on ESP8266 is a source of continual frustration for me (much like the baud rate). I really wanted an API that worked the same across all platforms, getIP returns the value on ESP8266 because it can, so most people use that and then the code isn't portable :(

  • getip works fine on esp only after you request wifi connection. regardless it got connected or not.

  • I changed getIP occurrences already to the callback variation.. but in the EspruinoWifi module is a bug that prevented me to use getIP without beeing connected to just get the mac address. That is the fix i was talking about and i will make a pull request when i have looked more into possible side effects of my fix.

    @Gordon Do you have a rough timeline when UDP support is done? Or would it be worth it to hack the few things i need for this together myself.. if at all feasible.

  • Actually GetIP() is used to get just a device-id. It you can thing about any other "id", lets use it instead.

    V.

  • @PaddeK ahh, ok. Thanks - I look forward to a PR when it's ready. It could potentially be difficult since the ESP8266 should be powered off when not connected.

    As a hack for sending UDP, you can do something like this for now:

    function sendUDP(addr,port,data,callback) {
      if (!callback) callback=function(){};
      data = E.toString(data);
      var at = wifi.at;
      at.cmd('AT+CIPSTART=4,"UDP","'+addr+'",'­+port+','+port+',2\r\n',2000,function cb(d) {
        if (d=="4,CONNECT") return cb;
        if (d!="OK") return callback("ERROR: "+d);
        at.cmd('AT+CIPSEND=4,'+data.length+'\r\n­', 1000, function cb(d) {
          if (d=="OK") {
            at.register('> ', function() {
               at.unregister('> ');
               at.write(data);
               return "";
            });
            return cb;
          } else if (d=="Recv "+data.length+" bytes") {
            // all good, we expect this 
            return cb;
          } else if (d=="SEND OK") {
            at.cmd("AT+CIPCLOSE=4\r\n",1000,function­(d) {
              callback(d=="OK"?null:"ERROR: "+d);
            });        
          } else {
            callback("ERROR: "+d);
          }
        });
      });
    }
    

    But I have attached my extremely work in progress UDP implementation.

    It may be that this just isn't possible with the ESP8266 AT firmware. As far as I can tell the AT firmware provides no information on where a UDP packet came from (address/port), so it might not be possible to send a response back to the right address (unless you know for sure where Alexa is).


    1 Attachment

  • @Gordon thanks for the code.. had no time so far to look into it though.
    @Vasily i saw that the getIP call is for getting the mac address and that it is only used to create an identifier.. but i think its a good idea so i left it in.

    Only problem right now.. besides spare time to get into it.. is UDP or more specific multicast UDP.
    In a short research i found inconsistent statements about support of the AT firmware for multicast UDP. I guess next step will be to whip up some dirty test code and see if alexa can find a device.

    By the way there is a chance the rest of the code works as is.. at least for folks with a ESP8266.

  • Why not use the inbuilt getserial() to use as the ide as they will be portable.

  • whatever bug in UDP implementation you found, it works for alexa device discovery at the moment :)
    so... never touch a working system is a prophecy

  • @Wilberforce i think using the mac address is a better fit in this scenario and there are multiple ways to get a mac address then a serial. But to be portable i used the callback variation of getIP so we should be fine in this regard.

    @Vasily nice to hear!! And to be clear... there is no bug in UDP its just not know supported for espruino devices.. so the alexa code right now just works for ESP8266 and maybe ESP32 users.

  • So i am back on this and i was surprised that with v1.95 i had to require Wifi instead of EspruinoWifi and no more errors because of missing Dgram module. But it still does not work for me.. its seems i cant receive alexas UPnP requests.

    @Vasily Does this still work for you after updating to v1.95?

    Edit

    Okay.. after checking v1.95 change nodes, reading the updated references and making some changes i am few steps closer and could fix some bugs in code i never reached before.

    Now i am stuck at an error from espruino "CIPSERVER failed" .. i guess it is the Dgram.createSocket but i have to check that after some sleep and try to reduce it a minimal snippet to reproduce.

    Other odd thing.. Wifi.startAP seems to always want a password.. even with authMode "open". For now i did remove the AP parts.. don't know why they are needed anyways.. maybe you could shed some light on this @Vasily? :)

  • @PaddeK are you using the updated EspruinoWifi.js library I posted? If you just try and use the normal library then it definitely won't work, and you'll likely get the CIPSERVER error you encountered.

  • it's a miracle... I thought 15 mins ago... long time no answers on Espruino, and here you are, both :-)

    I did not make any updates or trials, while my code was working on 1.94 and I was happy with it.
    I am rather waiting your OO version that works.

    p.s. I will try upgrade to 1.95 and check if my script works same. Also I will consider comments from Gordon on right modules.

  • @Gordon I did require "Wifi" because "EspruinoWifi" was not found with 1.95 but i did indeed forget about the new beta version from you here in this thread. As soon as i am home i give it a try and report back.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino and Amazon Alexa integration (implementation completed)

Posted by Avatar for Vasily @Vasily

Actions