-
I've written a crypto module to use on boards without the crypto module in firmware - it implements the SHA1 used by the web sockets module. The ws module does not need to be modified.
For testing if the crypto.js file is copied down to a web ide project it can be tested.
The following code assumes the ESP8266 has a wifi.save and is connected to a network.
var page = '<!DOCTYPE html><html lang=en><head><meta charset=utf-8>'; page += ' <meta http-equiv=X-UA-Compatible content="IE=edge">'; page += '<meta name=viewport content="width=device-width,initial-scale=1">'; page += '<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">'; page += '</head><body>'; page += '<div class="well well-lgt">'; page += '<div class="panel-heading"><h3>Websockets</h3></div>'; page += '<button class="btn btn-warning" id="send" onClick="ws.send(count);count=count+1;this.innerHTML=\'Send \'+count">Send to Espruino</button>'; page += '<ul id="list" class="list-group"></ul>'; page += '</div>'; page += '<script>'; page += 'var ws,count=1;setTimeout(function(){'; page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");'; page += 'ws.onmessage = function (event) { console.log({msg:event.data});'; page += 'var ul = document.getElementById("list");'; page += 'var li = document.createElement("li");'; page += 'li.className="list-group-item";'; page += 'li.appendChild(document.createTextNode(event.data));'; page += 'ul.appendChild(li);'; page += '};'; page += 'setTimeout(function() { ws.send("Hello to Espruino!");ws.send(JSON.stringify({browser:95})); }, 1000);'; page += '},1000);</script></body></html>'; function onPageRequest(req, res) { res.writeHead(200, { 'Content-Type' : 'text/html' }); res.end(page); } var server = require('ws').createServer(onPageRequest); server.listen(80); server.on("websocket", function (ws) { ws.on('message', function (msg) { console.log({ ws : msg }); if (msg % 2 === 0) ws.send(msg + ' is multiple of two'); }); ws.send("Hello from Espruino!"); ws.send(JSON.stringify({ date : Date.now() })); });
Start a browser at your Eps8266 address and see:
If you click the button, you can see the event appear in the Web IDE console. Every 2nd click causes the server to send back to the browser.... two way socket!
https://github.com/wilberforce/EspruinoDocs/blob/master/modules/crypto.js
For clarity, this the code that gets sent to the browser, using a CDN to get the bootstrap css:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> </head> <body> <div class="well well-lgt"> <div class="panel-heading"><h3>Websockets</h3> </div> <button class="btn btn-warning" id="send" onClick="ws.send(count);count=count+1;this.innerHTML='Send '+count"> Send to Espruino</button> <ul id="list" class="list-group"> </ul> </div> <script> var ws, count = 1; setTimeout(function () { ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne"); ws.onmessage = function (event) { console.log({ msg : event.data }); var ul = document.getElementById("list"); var li = document.createElement("li"); li.className = "list-group-item"; li.appendChild(document.createTextNode(event.data)); ul.appendChild(li); }; setTimeout(function () { ws.send("Hello to Espruino!"); ws.send(JSON.stringify({ browser : 95 })); }, 1000); </script> </body> </html>
-
-
-
When testing and debugging, after a while to http server code fails to start with an out of sockets error.
This happens after a number of uploads from the web ide.
I believe it is due to the old http server sessions not shutting down properly.
I have my board set up so it auto joins my local wifi.
At the moment I just hit the reset button on the board, and re upload, however it would be handy to shutdown the connections cleanly. Can this be down with an E.event on after a .reset?
Wifi.disconnect and wifi.connect() does do the job, however it would be great if an event could trigger.
-
-
Thanks Gordon.
I used the file analogy as an example.
I'm working on an object store that uses the esp8266 flash. I'm storing the mime types of the objects, as well as the data. This means that css/js/html can all be stored in flash and piped out to an http.response object.I'm intending to have a decent looking ui that is self contained, so that you can connect to the esp8266 as an access point, and then select a wifi ssid, and enter a password to join the network. From there the front end (bootstrap etc) can be delivered via cdn.
I like the idea of having a self contained web server, and with the large flash size on the esp8266, local resources can be stored.
I'll post code once I've got it more refined.
-
I wondering the best way to handle this, and the best way to set modules up for child and parent relationships.
Using a filesystem as an example, say we have var myfs=require('FileSystem'); var file=myfs.createFile('test.txt'); file.write( 'data'); file.close();
Do we have a single module that contains both the FileSystem and File objects?
I want file to have a reference to it's parent, and have it's own methods.
Should this be set up as two modules, where Filesystem is a list of File objects?
I've not seen any modules that contain two or related classes.
-
@tve are there commits that you have not applied to the esp8366 build that should be in for 1v85?
-
I also believe these is a restriction of 1mb max, and that this is in the espurino core rather than the esp8266 implementation.
Well, it's not a restriction in Espruino itself - maybe just something about getFlash?I was going by this comment:
https://github.com/espruino/Espruino/blob/master/targets/esp8266/jshardware.c line 48The restriction seems to be here:
// Save-to-flash uses 12KB at 0x78000 // The jshFlash functions use memory-mapped reads to access the first 1MB // of flash and refuse to go beyond that. Writing uses the SDK functions and is also // limited to the first MB.
#define FLASH_MAX (1024*1024)
#define FLASH_MMAP 0x40200000
#define FLASH_PAGE_SHIFT 12 // 4KB
#define FLASH_PAGE (1<<FLASH_PAGE_SHIFT)Looking at the fetch:
void jshFlashRead( void *buf, //!< buffer to read into uint32_t addr, //!< Flash address to read from uint32_t len //!< Length of data to read ) { //os_printf("jshFlashRead: dest=%p, len=%ld flash=0x%lx\n", buf, len, addr); // make sure we stay with the flash address space if (addr >= FLASH_MAX) return; if (addr + len > FLASH_MAX) len = FLASH_MAX - addr; addr += FLASH_MMAP;
So really #define FLASH_MAX (1024*1024) should be determined by the type of Flash chip in the esp8266 board.
https://github.com/espruino/Espruino/blob/master/targets/esp8266/jswrap_esp8266.c
JsVar *jswrap_ESP8266_getFreeFlash() { JsVar *jsFreeFlash = jsvNewArray(NULL, 0); // Area reserved for EEPROM addFlashArea(jsFreeFlash, 0x77000, 0x1000); // need 1MB of flash to have more space... extern uint16_t espFlashKB; // in user_main,c if (espFlashKB > 512) { addFlashArea(jsFreeFlash, 0x80000, 0x1000); if (espFlashKB > 1024) { addFlashArea(jsFreeFlash, 0xf7000, 0x9000); } else { addFlashArea(jsFreeFlash, 0xf7000, 0x5000); } }
So this could be used rather than the #define:
extern uint16_t espFlashKB; // in user_main,c -
-
-
Thanks. Using the offset works ok- I'm wondering if there is a way of hiding this transparently like Flash() lib does?
I was expecting the esp8266.getFlash() to return these larger regions, it doesn't look like this is implemented yet.
I also believe these is a restriction of 1mb max, and that this is in the espurino core rather than the esp8266 implementation. Does anyone know why this is the case?
-
I can confirm the peek stuff above works, however I get a reset with E.memoryArea trying to read the Esp8266 flash:
>esp8266 = require("ESP8266"); =function () { [native code] } >var base=esp8266.getFreeFlash()[2].area; =1011712 >E.memoryArea(base,10); =" ets Jan 8 2013,rst cause:2, boot mode:(3,6) load 0x40100000, len 1396, room 16 tail 4 chksum 0x89 load 0x3ffe8000, len 776, room 4 tail 4 chksum 0xe8 load 0x3ffe8308, len 540, room 4 tail 8 chksum 0xc0 csum 0xc0 2nd boot version : 1.4(b1) SPI Speed : 80MHz SPI Mode : DIO SPI Flash Size & Map: 32Mbit(512KB+512KB) jump to run user1 @ 1000 don't use rtc mem data
>process.env ={ "VERSION": "1v84", "BUILD_DATE": "Feb 12 2016", "BUILD_TIME": "08:57:53", "GIT_COMMIT": "8d4df7829bc7ebc8bc701c0f04e2fc645b99bd02", "BOARD": "ESP8266_BOARD", "CHIP": "ESP8266", "CHIP_FAMILY": "ESP8266", "FLASH": 0, "RAM": 81920, "SERIAL": "5ccf7f00-d975", "CONSOLE": "Serial1", "EXPORTS": { "jsvLock": 1075994728, "jsvLockAgainSafe": 1075994712, "jsvUnLock": 1075981000, "jsvSkipName": 1075970120, "jsvMathsOp": 1075935700, "jsvMathsOpSkipNames": 1075960212, "jsvNewFromFloat": 1076000164, "jsvNewFromInteger": 1076000232, "jsvNewFromString": 1075999068, "jsvNewFromBool": 1076000200, "jsvGetFloat": 1075970588, "jsvGetInteger": 1075971844, "jsvGetBool": 1075971036, "jspeiFindInScopes": 1075910904, "jspReplaceWith": 1075904248, "jspeFunctionCall": 1076010408, "jspGetNamedVariable": 1075912700, "jspGetNamedField": 1075923460, "jspGetVarNamedField": 1075922944, "jsvNewWithFlags": 1075999608 } }
It works with the 0x3ff00054 address:
>peek32(0x3ff00054).toString(16) ="20000d9" >E.memoryArea(0x3ff00054,4); >E.memoryArea(0x3ff00054,4); ="\xD9\x00\x00\x02"
So this seems to be associated with the address....
https://github.com/espruino/Espruino/wiki/ESP8266-Design-Notes
-
I'm trying to build crypto for esp8266.
CC libs/crypto/jswrap_crypto.o libs/crypto/jswrap_crypto.c: In function 'jswrap_crypto_error': libs/crypto/jswrap_crypto.c:91:3: error: invalid initializer if (e) jsError(e); ^ libs/crypto/jswrap_crypto.c: At top level:JsVar
*jswrap_crypto_error_to_jsvar(int err) { const char *e = jswrap_crypto_error_to_str(err); if (e) return jsvNewFromString(e); return jsvVarPrintf("-0x%x", -err); }
void jswrap_crypto_error(int err) { const char *e = jswrap_crypto_error_to_str(err); >>> if (e) jsError(e); else jsError("Unknown error: -0x%x", -err); }
The issue appears to be the flash_str #define:
[#define](https://forum.espruino.com/search/?q=%23define) jsError(fmt, ...) do { \ FLASH_STR(flash_str, fmt); \ jsError_flash(flash_str, ##__VA_ARGS__); \ } while(0)
root@esp8266-VirtualBox:~/Espruino# grep '#define FLASH_STR' src/*.h src/jsutils.h:#define FLASH_STR(name, x) static char name[] __attribute__((section(".irom.literal"))) __attribute__((aligned(4))) = x
static char name[] vs const char *
Is there a simple way to resolve this?
I'm not sure the protocol here - should I raise as a github issue?
-
-
Hi,
I've started a module that stores and recovers JavaScript fragments in esp8266 flash.
The first 4096 byte page is used to store a json string that holds a structure.
The first part of this contains the length, and the rest is an associate array holding keys, and the start page and number of pages for the object stored at this key.The idea is to be able to store css, html, and js fragments in flash.
Using a .pipe method, requests to these resources can be streamed out to the browser using very little memory overhead.
I have also tested storing functions and eval'ing to load and run the function.
I'm also experimenting with a cache type function - where you specify the source uri, this is fetched from the web, and then stored in cache.
This means images, bootstrap.css and other resources can be stored on the device, meaning it can be run in ap mode, and still have a decent looking interface. This means that a page could be set up, which does a wifi scan, provides a select list of access points, and asks a password to join a network.Let me know if you would like to see the code so far.
-
I've managed to get web sockets working using @JumJum 's js implementation from here:
http://forum.espruino.com/comments/12723204/It would be great to get SHA1 function into the core.
I've attempted to build the crypto module for the esp8266, however get build errors (sorry can't recall what they were specifically)
I wondering if the hashlib library would be a good place to add the sha1 function, so then the ws.js could use this rather than rely on crypto?
-
Hi Gordon,
Is there any easy way to tell which builds now have the E.memoryArea update for reading esp8266 flash?
I don't think this is any @tve builds yet?
Sure.
I was hoping that a require("crypto") would look for a native install first, and then look in modules on espruino site.
Does it not work this way?
The other thing I tried and could quite pull off was using a require("path to github raw") and then attempted to add that to the modules cache, so that a later require("crypto") would find the module already loaded.