-
I've also had it working with an I2C OLED, the SSD1306, here's that code:
function draw(){ g.clear(); g.drawString("Hello World",0,0); g.flip(); } I2C1.setup({scl:NodeMCU.D4,sda:NodeMCU.D3}); var g = require("SSD1306").connect(I2C1, draw);
Display NodeMCU ESP-12 SCL D4 GPIO2 SDA D3 GPIO0 GND GND GND VCC 3V3 3V3
These should probably be put on some wiki somewhere.
-
I've had it working. This is the code I used for a Nokia 5110 aka the PCD8544
SPI1.setup({ sck:NodeMCU.D5, mosi:NodeMCU.D7 }); var g = require("PCD8544").connect(SPI1, NodeMCU.D2 /* RS / DC */, NodeMCU.D8 /* CS / CE */, NodeMCU.D1 /*RST*/, function() { g.clear(); g.setRotation(2); //Flip display 180 g.drawString("Hello",0,0); g.drawLine(0,10,84,10); g.flip(); });
Display NodeMCU ESP-12 LED 3V3 3V3 SCLCK D5 GPIO14 DN<MOSI> D7 GPIO13 D/C D2 GPIO04 RST D1 GPIO05 SCE D8 GPIO15 GND GND GND VCC 3V3 3V3
-
-
I'm parsing some JSON data from a web-server but in-case the server is down or returns something unexpected I want my code to do something safe. I tried checking if the data was undefined but that produces the error:
Uncaught Error: Field or method "Hostname" does not already exist, and can't create it on undefined
So next I tried data.hasOwnProperty but this isn't implemented in Espruino's implementation of JSON :/
So I resorted to try catch which works just fine. Was this the right thing to do or should Espruino have some sort of has for JSON? -
Oh actually I don't suppose it matters, apparently none of the extra pins on the ESP-12E are actually useful :/
-
-
What sort of current capacity do you need? Depending on that I believe you could just pick any of these, just select the "Continuous Drain Current Id" filter to above what you need .
-
@Gordon Thanks, using a separate request for the script solved that. Now memory usage looks like this before and after the clients connection.
>process.memory(); ={ "free": 769, "usage": 631, "total": 1400, "history": 551 } [WS] "40" >process.memory(); ={ "free": 559, "usage": 841, "total": 1400, "history": 199 }
New Code
var page = '<body style="width:100%;height:100%;overflow:hidden;"><svg style="width:100%;height:100%;" viewbox="0 0 500 500" id="svg"><path style="fill:#80e5ff;fill-opacity:1;fill-rule:nonzero;stroke:none" d="M 250 0 C 111.92882 3.7895613e-14 0 111.92882 0 250 C -1.249508e-14 341.05067 48.689713 420.72528 121.4375 464.4375 L 154.625 409.40625 C 100.50052 376.95218 64.28125 317.69934 64.28125 250 C 64.28125 147.43284 147.43284 64.28125 250 64.28125 C 352.56716 64.28125 435.71875 147.43284 435.71875 250 C 435.71875 317.53896 399.66155 376.65256 345.75 409.15625 L 378.71875 464.34375 C 451.37991 420.61135 500 340.98541 500 250 C 500 111.92882 388.07118 -1.8947806e-14 250 0 z " id="ring"/> <rect style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none" id="needle" width="16" height="80" x="242"/> <text xml:space="preserve" style="font-size:122.59261322px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;font-family:Helvetica;-inkscape-font-specification:Helvetica" x="250.01915" y="845.31812" id="text"><tspan id="label" x="250.01915" y="292.95594">0</tspan></text> <path style="fill:#d5f6ff;fill-opacity:1;fill-rule:nonzero;stroke:none" id="up" d="m 294.75099,133.39225 -90.93056,0 45.46528,-78.748173 z" transform="matrix(0.61903879,0,0,0.61903879,95.682477,91.16682)" /> <path transform="matrix(0.61903879,0,0,-0.61903879,95.682477,408.80767)" d="m 294.75099,133.39225 -90.93056,0 45.46528,-78.748173 z" id="dn" style="fill:#d5f6ff;fill-opacity:1;fill-rule:nonzero;stroke:none" /></svg><script src="/interact.js"></script></body>'; var script = 'var ws=new WebSocket("ws://"+location.host,"protocolOne"),timestamp=Date.now(),pos=50,dragging=!1;function $(a){return document.getElementById(a)}function setPos(a){0>a&&(a=0);100<a&&(a=100);pos=a;100<Date.now()-timestamp&&(timestamp=Date.now(),ws.send(pos));$("label").textContent=pos;a=2.8*(pos-50);$("needle").setAttribute("transform","rotate("+a+" 250 250)")}setPos(pos);function dragStart(){dragging=!0;$("ring").style.fill="#ff0000"}document.addEventListener("mousemove",function(a){if(dragging){a.preventDefault();var b=$("svg");setPos(Math.round(180*Math.atan2(a.clientX-b.clientWidth/2,b.clientHeight/2-a.clientY)/Math.PI/2.8+50))}});document.addEventListener("mouseup",function(a){dragging=!1;$("ring").style.fill="#80e5ff";$("up").style.fill="#d5f6ff";$("dn").style.fill="#d5f6ff"});function touchHandler(a){var b=a.changedTouches[0],c="";switch(a.type){case "touchstart":c="mousedown";break;case "touchmove":c="mousemove";break;case "touchend":c="mouseup";break;default:return}var d=document.createEvent("MouseEvent");d.initMouseEvent(c,!0,!0,window,1,b.screenX,b.screenY,b.clientX,b.clientY,!1,!1,!1,!1,0,null);b.target.dispatchEvent(d);a.preventDefault()}document.addEventListener("touchstart",touchHandler,!0);document.addEventListener("touchmove",touchHandler,!0);document.addEventListener("touchend",touchHandler,!0);document.addEventListener("touchcancel",touchHandler,!0);$("ring").onmousedown=dragStart;$("needle").onmousedown=dragStart;$("up").onmousedown=function(a){a.preventDefault();this.style.fill="#00ff00"};$("dn").onmousedown=function(a){a.preventDefault();this.style.fill="#ff0000"};$("up").onmouseup=function(a){setPos(pos+10)};$("dn").onmouseup=function(a){setPos(pos-10)};'; function onPageRequest(req, res) { var a = url.parse(req.url, true); if(a.pathname == "/interact.js"){ res.writeHead(200, {'Content-Type': 'application/javascript'}); res.end(script); }else{ res.writeHead(200, {'Content-Type': 'text/html'}); res.end(page); } } var server = require('ws').createServer(onPageRequest); server.listen(8000); server.on("websocket", function(ws) { ws.on('message',function(msg) { print("[WS] "+JSON.stringify(msg)); }); ws.send("Hello from Espruino!"); });
-
NOTE: This done with an ESP-12E running Espruino on the latest Travis builds of User1 and User2 as they include crypto support which is needed for the WebSocket Server.
This is an example of using WebSockets for real-time interaction, it is adapted from the Interactive Web UI example. The Espruino acts as both the WebSocket Server and a web server to serve the page to the client.
The code for the webpage for this client is in this Gist on GitHub as it was too large to fit on the forum. This code send the value over a WebSocket every time the value is updated instead of sending a POST request when the mouse is lifted like the Interactive Web UI demo.
The Espruino code is just below (New Code in this post). Just upload it to your ESP after setting Wifi up and saving that. I'd recommend visiting the page from a smartphone, the touch controls feel fluid. The webpage is a (espIP):8000
var page = '<body style="width:100%;height:100%;overflow:hidden;"><svg style="width:100%;height:100%;" viewbox="0 0 500 500" id="svg"><path style="fill:#80e5ff;fill-opacity:1;fill-rule:nonzero;stroke:none" d="M 250 0 C 111.92882 3.7895613e-14 0 111.92882 0 250 C -1.249508e-14 341.05067 48.689713 420.72528 121.4375 464.4375 L 154.625 409.40625 C 100.50052 376.95218 64.28125 317.69934 64.28125 250 C 64.28125 147.43284 147.43284 64.28125 250 64.28125 C 352.56716 64.28125 435.71875 147.43284 435.71875 250 C 435.71875 317.53896 399.66155 376.65256 345.75 409.15625 L 378.71875 464.34375 C 451.37991 420.61135 500 340.98541 500 250 C 500 111.92882 388.07118 -1.8947806e-14 250 0 z " id="ring"/> <rect style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none" id="needle" width="16" height="80" x="242"/> <text xml:space="preserve" style="font-size:122.59261322px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;font-family:Helvetica;-inkscape-font-specification:Helvetica" x="250.01915" y="845.31812" id="text"><tspan id="label" x="250.01915" y="292.95594">0</tspan></text> <path style="fill:#d5f6ff;fill-opacity:1;fill-rule:nonzero;stroke:none" id="up" d="m 294.75099,133.39225 -90.93056,0 45.46528,-78.748173 z" transform="matrix(0.61903879,0,0,0.61903879,95.682477,91.16682)" /> <path transform="matrix(0.61903879,0,0,-0.61903879,95.682477,408.80767)" d="m 294.75099,133.39225 -90.93056,0 45.46528,-78.748173 z" id="dn" style="fill:#d5f6ff;fill-opacity:1;fill-rule:nonzero;stroke:none" /></svg><script>var ws=new WebSocket("ws://"+location.host,"protocolOne"),timestamp=Date.now(),pos=50,dragging=!1;function $(a){return document.getElementById(a)}function setPos(a){0>a&&(a=0);100<a&&(a=100);pos=a;100<Date.now()-timestamp&&(timestamp=Date.now(),ws.send(pos));$("label").textContent=pos;a=2.8*(pos-50);$("needle").setAttribute("transform","rotate("+a+" 250 250)")}setPos(pos);function dragStart(){dragging=!0;$("ring").style.fill="#ff0000"}document.addEventListener("mousemove",function(a){if(dragging){a.preventDefault();var b=$("svg");setPos(Math.round(180*Math.atan2(a.clientX-b.clientWidth/2,b.clientHeight/2-a.clientY)/Math.PI/2.8+50))}});document.addEventListener("mouseup",function(a){dragging=!1;$("ring").style.fill="#80e5ff";$("up").style.fill="#d5f6ff";$("dn").style.fill="#d5f6ff"});function touchHandler(a){var b=a.changedTouches[0],c="";switch(a.type){case "touchstart":c="mousedown";break;case "touchmove":c="mousemove";break;case "touchend":c="mouseup";break;default:return}var d=document.createEvent("MouseEvent");d.initMouseEvent(c,!0,!0,window,1,b.screenX,b.screenY,b.clientX,b.clientY,!1,!1,!1,!1,0,null);b.target.dispatchEvent(d);a.preventDefault()}document.addEventListener("touchstart",touchHandler,!0);document.addEventListener("touchmove",touchHandler,!0);document.addEventListener("touchend",touchHandler,!0);document.addEventListener("touchcancel",touchHandler,!0);$("ring").onmousedown=dragStart;$("needle").onmousedown=dragStart;$("up").onmousedown=function(a){a.preventDefault();this.style.fill="#00ff00"};$("dn").onmousedown=function(a){a.preventDefault();this.style.fill="#ff0000"};$("up").onmouseup=function(a){setPos(pos+10)};$("dn").onmouseup=function(a){setPos(pos-10)};</script></body>'; function onPageRequest(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(page); } var server = require('ws').createServer(onPageRequest); server.listen(8000); server.on("websocket", function(ws) { ws.on('message',function(msg) { print("[WS] "+JSON.strin
Immediately after uploading the code the memory looks like this
>process.memory(); ={ "free": 785, "usage": 615, "total": 1400, "history": 532 }
Then when the first user connects
ERROR: Out of Memory! Execution Interrupted >process.memory(); ={ "free": 588, "usage": 812, "total": 1400, "history": 1 }
I believe the history is cleared and the out of memory error isn't a problem
-
I have a bunch of data that I want to be transferred from my PC to the Espruino then to a display. Would it be better to have a script on the Espruino parse the JSON Data and write it to the display, or would it be better for the PC to send direct graphics drawString's then flip it at the end?
I was thinking the latter as it would mean the Espruino doesn't need to be written then saved, the PC can just write the init code for the display when it connects.
-
DrAzzy means when you set your interval you assign it to a global variable. For example
var state, interval; function toggleLed(){ digitalWrite(LED1,state=!state); } setWatch(function() {interval=setInterval(toggleLed,500);},BTN, {repeat: true, edge: 'rising', debounce:5});
You would call clearInterval(interval) to stop only this interval.
-
Thanks @Gordon I didn't know about that should make it easier for my brain, I'm just used to everything always being RGB.
-
-
I've recently started playing with a WS2812B ring and I expected the color format to be RGB, however when I send a [255,0,0] the first LED lights up green instead of red. This baffled me so I tried [0,255,0] and the first LED turned red and [0,0,255] blue. Are some variants of the WS2812b's GRB instead of RGB or am I doing something wrong? This also happens when using the graphics library the color scheme is GRB.
-
I'm assuming that the Graphics library isn't included in the build for ESP8266 both from this post by @MaBe and my own testing with an ESP-12 and it returning "Uncaught ReferenceError: "Graphics" is not defined". Perhaps this should be documented on the Espruino on ESP8266 docs page.
If anyone does get a build working with graphics I'd love to know. I'm trying to fit a power supply, ESP8266 and SSD1306 (or some other display) into a light switch in a wall for monitoring server temps and usage, I'm not sure if I can fit one of my original Espruinos in there too.
-
-
-
Nothing to add just want to say I'd be interested in the bare-bones Espruino's. Always finding myself making something cool I'd like to keep but that isn't worth wasting my (expensive when compaired to the trinket I'm making) Espruino on. Been thinking about printing my own bare-bones STM PCB's for the job but I just seem to never get round to it.
-
It appears Gordon got lucky, like @ColinP I also got the LCD CC2451 variant. To add insult to injury I can't even pair it with my phone. Espruino as a wearable perhaps too good to be true xD
-
-
-
-
Hmm, I can't get it to work at all, This display has a pin labeled A0, I'm assuming this is RS/DC for setting if it is Data or a Command.
As I don't have the middle pins soldered on I've changed the pins to
GND -> GND
VCC -> 3.3v
LCD_BL -> 3.3v
LCD_CS -> C5
LCD_RST -> C6
LCD_A0 -> C7
SCK - > B13
MOSI -> B15With this code:
var spi = new SPI(); spi.setup({ sck: B13, mosi: B15 }); var g = require("ST7565").connect(spi, C7 /* RS / DC */, C5 /* CS / CE */, C6 /* RST */, function() { g.clear(); g.drawString("Hello",0,0); g.drawLine(0,10,84,10); g.flip(); });
That should work fine right?
-
It didn't occur to me that the value for setFontVector was the height, the reference just says size. Thanks