Close

Espruino, ESP8266, Pain, and PSUs

A project log for Educational Puzzle Box

Intended to get a school-averse 7 year old to practice maths

dan-williamsDan Williams 09/02/2017 at 20:200 Comments

The software didn't start well.

After flashing the Espruino firmware to the first ESP-12 module, I found it rebooted every 10s. I put this down to an errant watchdog timer persisting from a previous use of the module.

I tried with a new module, and found it a bit more stable, but Espruino wouldn't run more than a few instructions without the module crashing.

Having had no problems with ESP8266 projects before, I decided the Espruino developers must be putting out really bad stuff.

After a lot of googling, I decided perhaps the PSU was at fault, so ordered a better PSU (800mA) to replace the 250mA one I was using (which had done fine for all previous ESP8266 projects). This solved it... :)

The Espruino ESP8266 support is ok, but the docs are a bit lacking; in particular, working out how to use SPI and I2C gave me some grief.

SPI

Here's the code I eventually got working:

SPI1.setup({ sck:D14, mosi:D13 });
var g = require("PCD8544").connect(SPI1, D15 /*DC*/, D0 /*CE*/, D2 /*RST*/, function() {
  g.clear();
  g.drawString("Hello",0,0);
  g.drawLine(0,10,84,10);
  g.flip();
});

Remember, SPI pin labelling isn't TX<->RX like UART; it's MISO<->MISO, MOSI<->MOSI.

I2C

i2c = new I2C();
i2c.setup({scl:4,sda:5});
var pressed = false;
function checkKeys()
{
  var k = mpr.touched();
  if (k == 0)
  {
    pressed = false;
    return;
  }
  if (pressed)
    return;
  pressed = true;
  switch(k)
  {
    case 256: key(1); break;
    case 16: key(2); break;
    case 1: key(3); break;
    case 512: key(4); break;
    case 32: key(5); break;
    case 2: key(6); break;
    case 1024: key(7); break;
    case 64: key(8); break;
    case 4: key(9); break;
    case 128: key(0); break;
    case 2048: del(); break;
    case 8: enter(); break;
  }
}
var user_str = '';
function key(num) {
  user_str += num.toString();
  console.log(user_str);
}
function del()
{
  user_str = user_str.substr(0, user_str.length -1 );
  console.log(user_str);
}
function enter()
{
  console.log("ENTER: " + user_str);
  user_str = '';
}
function ready() {
    //mpr.setThresholds(touch, release); // 15, 8  For typical touch application, the value can be in range 0x05~0x30 for example
    setInterval(function() {
        checkKeys();
    }, 50);
}
var mpr = require("MPR121").connect(i2c, ready);

Discussions