Skip to content

Latest commit

 

History

History
163 lines (118 loc) · 9.48 KB

javascript_programming_reference_manual.md

File metadata and controls

163 lines (118 loc) · 9.48 KB

Duo: JavaScript Programming Reference Manual


Console Interfaces

The default console is mapped to Duo's on-board USB, and UART1, which enables you to interact with Espruino Web IDE using RBLink.

After power on, Duo's BLE is activated and begin advertising as "Duo JS Interpreter". If you connect your smart device to Duo, then the condole is moved to Bluetooth. There is a UART service containing two characteristics, one for sending JS command and one for echoing the JS interpreter status.

If you have stored WiFi credentials before, after power on, the Duo will try connecting to the stored AP for 3 times. If connection created, then Duo will start the "Telnet" serve. In fact, it is simply a TCP server listening on part 23, since the Telnet protocol is not implemented for now. You can connect a TCP client to the "Telnet" server and then the console is moved to "Telnet" client, which also allows you to send JS command and echo the JS interpreter status for you. However, if the Duo hasn't stored any credentials, after power on the WiFi is turn off by default. If the Duo can not connect to AP within the 3 times retries, it will turn off the WiFi as well.

Reference

The following reference is inherited from the Espruino official website:

The following reference is for Duo only:


WiFi Class

The WiFi class for Duo is different from which is printed on the Espruino Reference site.

  • WiFi.on() - static method
    Turn on WiFi but do not connect to AP. It should be called as least once before you calling other WiFi relatived methods. Error will be caught if you are trying to call other methods while WiFi is off.

  • WiFi.off() - static method
    Turn off WiFi. It will close all of the sockets and disconnect from AP if them are presented.

  • WiFi.connect() - static method
    Connect the Duo to AP. It will turn on WiFi first if WiFi is off and then try connecting to the APs which credentials have been stored before. If connection is failed, Duo will turn off WiFi. Error will be caught if no credentials are stored in Duo.

  • WiFi.disconnect() - static method
    Disconnect the Duo from Ap. It will close all of the sockets if they are presented.

  • WiFi.isReady(JsVar *jsCallback) - static method
    Check if the Duo has connected to an Ap, which means that the Duo has obtained IP address from AP. The callback function is optional. If the callback is presented, it will be called with a JS boolean varialbe passing in, which indicates the result of the method.

  • WiFi.details(JsVar *jsCallback) - static method
    Fetch the details when the Duo is connecting to an AP, including SSID, gateway IP, Duo IP etc. Error will be caught if Duo is not in connection with AP. The callback is optional. If the callback is presented, it will be called with a JS object passing in, which including the details.

  • WiFi.setCredential( {...} ) - static method
    Store a WiFi credential which to be used when Duo tries connecting to AP. Duo is capable of storing up to 5 credentials. If you are going to store a new credential while 5 credentials is tored, the oldest one will be replaced. If the SSID of the credential you are going to set is the same with certain stored credential's, only the password and security will be replaced. Error will be caught if WiFi is off. Thus, you need to turn on WiFi first. The parameter is a JS object, which including the credential info. For instance:

      WiFi.setCredential( {"ssid:":"YOUR_AP_SSID", "password":"YOUR_AP_PASSWORD", "sec":"WPA2", "cipher":"AES"} )  
    

The security value of "sec" key is limited to one of the bellowing string:

    "UNSEC" //Open 
    "WEP"   
    "WPA"   
    "WPA2"  

The cipher value of "cipher" key is limited to one of the belowing string:

    "AES"
    "TKIP"
    "AES_TKIP"

If the "password" is not present, then the security is treated as open.
If the "sec" is not present, then the security is treated as open.
If the "cipher" is not present, then the cipher is default to unset.

  • WiFi.getCredentials(JsVar *jsCallback) - static method
    Fetch the all stored credentials details, including SSID and security. Error will be caught if no credentials stored. The callback is optional. If the callback is presented, it will be called with a JS object passing in, which including the credentials' details.

  • WiFi.clearCredentials() - static method
    Clear all of the stored credentials.

  • WiFi.resolve(JsVar *jsHostName) - static method Resolve the host name being passed in. The host name should be JS string.

  • WiFi.ping(JsVar *jsDestIP, JsVar *jsnTries) -static method
    Ping to the specified host. Error will be caught if Duo is not in connection with AP. For instance, try pinging to 192.168.1.1 with 5 times attempts:

      WiFi.ping("192.168.1.1", 5);
    
  • WiFi.scan(JsVar *jsCallback) - static method
    Scan the nearby AP. It will print each AP's SSID, security and RSSI. Error will be caught if WiFi is off. The callback is optional. If the callback is presented, it will be called with a JS object passing in, which including the scanned AP details.

BLE Class

The BLE class for Duo is different from which is printed on the Espruino Reference site.

  • RBLE.sleep() - static method
    Make Duo stop advertising.

  • RBLE.wake() - static method Make Duo start advertising again.

Support

License

Copyright (c) 2016 Red Bear

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.