Monthly Archives: August 2015

Nextion Displays and the ESP8266

the Nextion boxSo a parcel arrived for me this morning in the post. A Nextion display. And what is that, you may well ask?

Think of something like a thermostat or really any home control situation, at some point you have to stop sending text commands from an expensive PC and reduce the operation down to neat and tidy button pushing so that everyone else in the house can actually participate in your invention!

This is where things often fall down, the “Heath Robinson” home-made-looking box with pretty awful buttons telling everyone you have “amateur” status.   Now of course, today, you could choose to use a second hand phone or tablet for your wall-mounted family-friendly display and control panel… OR you can use a touch-sensitive display.

These are REASONABLY cheap – for example….  http://www.banggood.com/UNO-R3-ATmega328P-Board-2_4-Inch-TFT-LCD-Screen-Module-For-Arduino-p-945755.html

So this is an Arduino + touch display for under a tenner. You have to sort out libraries and basically write everything from scratch – and that’s fine – but time consuming.

Te Nextion PCBAnd so it was that my friend introduced me to the Nextion displays. This is a display board that isn’t too expensive – and which has everything built in. Essentially, on a PC you run a free editing program, tell it where you want buttons, text and images and what should happen when you press the buttons.  A button lets you download that to the Nextion board and “Bob’s your uncle”.

The board has only 4 pins – power (5v), ground, serial in, serial out. Press a button and stuff comes out of the serial pin at logic levels and 9600 baud.

You really can’t get any easier than this. Perfect? No, far from it. In my initial trials. the editor has some polishing to do, my first attempt at running the font convertor on a Windows SANS font produced a pretty awful output and I’ve clearly got a learning curve ahead of me – but after 15 minutes I have a display running with serial output on button press – that is one HELL of a lot faster than any other way I can think of.

Nextion displayThe board has another advantage over alternatives – the whole thing is very THIN and so will easily fit into a nice slim wall mounted case.

You could choose to run the board serially – OR via something like an ESP-01 and that is almost definitely what I will be doing.

I’ll have the EXP capture button outputs and send MQTT commands back to base – and accept MQTT commands and send them to the display.

Welcome to NextionWhen you first power up the unit, it comes up with a nice display and responds to button presses… so that gives you a feel for how things will work – that’s before you do any programming – just power the board up.

All you have to do is use the PC software to drop  in some screens, some buttons and some dials and off you go.

So that’s the theory – what is the board like in practice. I have to say that my first experience suggests that the translations have some way to go.  The simplest thing you can do is have a button send something out of the serial port when you press it.

GaugesThat is a 5 seconds job to implement – but it took me nearly an hour to realise because my friend who has already done this showed the use of an on-press function called PRINT which on his board works on it’s own. With the new software you build up commands and add a special command at the end to send everything out of the buffer. Until I found that command, mysteriously named “com_star” which I’m going to assume means “comms_start” – I was lost.

So commands tend to end with a binary zero for reasons I’ve not yet figured out… and at the end of any command, incoming or outgoing, you have 0xff 0xff 0xff  (i.e. 3 bytes each one binary 255).  That in itself makes testing interesting as a simple ASCII terminal might have difficulty with the terminating characters. Not sure what’s wrong with a simple carriage return myself – but I’m sure they have their reasons.

Now that I KNOW – it’s easy. Kind of – the manual needs a lot of work – but I guess it is early days yet – as long as they keep improving things, this little display could well be a winner.

The thing that stuns me the most – looking around the web – is people using this with an Arduino – with an ESP8266 stuck on the end for WIFI! Seems like major overkill to me not to mention the cost.  An ESP-01 talking to one of these devices, with 512k programming space on the ESP, it seems to me one could accomplish one hell of a lot!

Right now  I have the Nextion sending out instructions when a button is pressed, via it’s serial – at 115,200 baud. The text is encoded to be the same as my home control. My home control boards are of course ESP8266 and I’ve used a resistive divider on the serial input…  All good fun.

UPDATE: See comments in here – lots of issues with the software right now – this is not going to be as easy a ride as I thought…. comparison operations that just don’t work – we’ve been writing to the designers all day and I hope that our issues will be sorted. Right now you may want to consider this a work in progress – SO many possibilities but not without pain it would seem. More as we get responses to our questions.

Facebooktwittergoogle_pluspinterestlinkedin

Driving me NUTS

This has been driving me mad and I’ve only just found the answer – thanks to some clues people have given me – but as many of the responses were wrong – you might well find this interesting.

I have a an array of 180 bytes – for 60 serial LEDs – 3 bytes each for red, green and blue respectively.

So this routine calls back every second – and updates the 60 LEDs which are in a circle. The hour indicator is formed of 7 LEDs, bright in the centre, dimmer to the edges, the minutes indicator is 3 LEDs – again bright in the middle. The seconds are shown as 1 LED.

And it was working a treat – except at midnight where it bombed with all LEDs on – and for the LIFE of me I could not see why..

Ignore the bits in RED….

LOCAL void ICACHE_FLASH_ATTR clock_cb(void *arg)
{
if (sysCfg.clock!=255) // fancy clock on a port
    {
        wsBitMask = (uint8_t) sysCfg.clock;

        int hour12=(tm.Hour%12)*5;
        for (int a=0;a<180;a+=3)
            {
                rgb.buffer[a]=0;
                rgb.buffer[a+1]=0;
                rgb.buffer[a+2]=0;
            }
        rgb.buffer[(tm.Second*3+2)%180]=255; // seconds hand blue

        rgb.buffer[(tm.Minute*3-3)%180]=8; // minutes green
        rgb.buffer[(tm.Minute*3)%180]=255; // minutes green
        rgb.buffer[(tm.Minute*3+3)%180]=8; // minutes green

        int x=((hour12)+(tm.Minute/12))*3;

        rgb.buffer[(x-8)%180]=5;
        rgb.buffer[(x-5)%180]=16;
        rgb.buffer[(x-2)%180]=80;
        rgb.buffer[(x+1)%180]=255;
        rgb.buffer[(x+4)%180]=80;
        rgb.buffer[(x+7)%180]=16;
        rgb.buffer[(x+10)%180]=5;

        WS2812OutBuffer(rgb.buffer, 180,1); // 60 LEDs
    }
}

So here’s the thing – all these years I’ve been thinking % was a MOD operator – it’s NOT – it’s a simple remainder..

In order words..

(-3 % 180) which I had COMPLETELY expected to be returning 177 – was in fact returning –3  !!!!!

That explains a LOT. The solution – a simple MOD function.

LOCAL int ICACHE_FLASH_ATTR mod(int a, int b)
{
   int ret = a % b;
   if(ret < 0)
     ret+=b;
   return ret;
}

Hence mod(-3,180) returns the right answer. Here’s the final clock once-a-second callback routine for a 60-LED array.

LOCAL void ICACHE_FLASH_ATTR clock_cb(void *arg)
{
if (sysCfg.clock!=255) // fancy clock on a port
    {
        wsBitMask = (uint8_t) sysCfg.clock;
        int hour12=(tm.Hour%12)*5;

        int a;
        for (a=0;a<178;a+=3)
            {
                rgb.buffer[a]=0;
                rgb.buffer[a+1]=0;
                rgb.buffer[a+2]=0;
            }

        rgb.buffer[mod(tm.Second*3+2,180)]=255; // seconds hand blue
        rgb.buffer[mod(tm.Minute*3-3,180)]=8; // minutes green
        rgb.buffer[mod(tm.Minute*3,180)]=255; // minutes green
        rgb.buffer[mod(tm.Minute*3+3,180)]=8; // minutes green

        int x=((hour12)+(tm.Minute/12))*3;

        rgb.buffer[mod(x-8,180)]=5;
        rgb.buffer[mod(x-5,180)]=16;
        rgb.buffer[mod(x-2,180)]=80;
        rgb.buffer[mod(x+1,180)]=255;
        rgb.buffer[mod(x+4,180)]=80;
        rgb.buffer[mod(x+7,180)]=16;
        rgb.buffer[mod(x+10,180)]=5;

        WS2812OutBuffer(rgb.buffer, 180,1); // 60 LEDs
    }
}

Facebooktwittergoogle_pluspinterestlinkedin

More Websockets

In a previous blog item here I covered the use of MQTT, jQuery Mobile and Websockets to make a controller web page for mobile phones/tablets etc. here https://tech.scargill.net/mqttjquerywebsockets-controller/

I showed just a basic and not too pretty page with buttons that would control real items, in my case via Websockets on a Raspberry Pi, firing off MQTT (also on the Pi) to ESP8266 modules (using our own code based around TuanPMs MQTT project).

Well, I’ve been doing a bit more playing around – here’s a better way to make the buttons – but all based on the previous article so you’ll need to read that first.  I’ve also added a second page – merely to show you how that is done easily – and you could expand that ad-infinitum – but I’ve ALSO shown how to get information back in from the same web socket as I was asked about this aspect. It’s very easy (there was a time when I was terrified of the subject).

So first of all this time my Node-Red code is slightly different – I’ve separated off the incoming websocket node from the outgoing one – as long as they refer to the same item they don’t have to be joined together.

Websockets

So in this version – the same code in “process commands” – as instructions from the web page go out to MQTT to control various gadgets. See other blog item.

Here, a Timestamp (standard Node-Red item) pushes out a timestamp every second – and another function converts that into a simple string as payload – which is then sent back, every second, to the web page. Normally you might send JSON data back for further parsing in the web page but in this case it’s a simple string.

Here’s the code inside timeConvert.

var dt = new Date(msg.payload);
var msg = {    'payload':  dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds() }
return msg;

Page 1And so to the web page itself: I’ve improved it some way as you’ll see as I figured out how to get 4 buttons wide (trivial – I just didn’t spot it in jQuery Mobile the last time around).

As you can see it is quite pretty, has a page button near the top to switch to another page (which is empty as yet so I won’t show it here but it will be obvious in the code how it works and how you can keep adding more.  I’m not happy about the look of the paging button but it’ll do for now.

Then way down at the bottom left – you’ll see a timestamp – that is coming from Node-Red and is in this case updating every second. It COULD be status information – or anything – but for the purpose of demonstration (and it’s also nice to know that something is actually working by visual feedback like this) it is just a string as described above.

I wish I could figure out a simple additional class to colour these buttons individually instead of making themes for each button type – perhaps someone else could contribute here.

So now this is a quite reasonable control page with the option for more pages (not going to get any more buttons on this page).   I did think of having Node-Red return a string with the states of the various outputs and changing the buttons accordingly – but that would not have made for easy reading.  Anyone implementing this will soon figure out how to add such functionality.  Instead of returning simple text, it would make sense to similarly send back jSon as there is a very easy to use function to split this up.

Recall too from the previous blog that I’ve implemented (in the .htaccess file) simple password protection not shown here and not of much use locally but pretty essential if you want to control from outside.

Here is the web page code for the above, note I’ve changed the name of the websocket to /myapp3 – could have called it anything, really.  See the “onmessage” function – you don’t get any easier than this. There you go… https://bitbucket.org/snippets/scargill/AGMq5

newmenuUpdate: 23/08/2015 – there is a better way to do navigation in jQuery mobile (I’m still learning) but it seems incomplete. A slide-in-from-the-left menu system is easy to create – but if you try to make one menu system for all pages – there is no formatting – I guess this will be available in a future version – however, replicating a menu system for each page has it’s advantages as you can disable the current page link! 

Here is a version with the awful button missing hence saving some space on the page. So this is a little like the swipe side menu in NETIO except I’ve implemented it by touching the top title bar because swiping doesn’t work too well on a PC and you might be testing this in a normal browser!!!

The title bar is sitting doing nothing anyway, so why not.

Actually I like this – on a typical phone you could easily get half a dozen pages and some explanatory text or imagery in that side menu… hell, you could even get some more buttons in there and a logo!

Here you go..

https://bitbucket.org/snippets/scargill/yREGd

Facebooktwittergoogle_pluspinterestlinkedin

Windows 10–the Missing bits

Slight diversion as I’ve just been upgrading my PCs to Windows 10 and a friend of mine pointed me to changes as against previous versions – i.e. what’s missing from Windows 10.

Here I’ll take a look and give my comments. Firstly I should say as a hardware-oriented guy I have lots of things stuck into my PC like FTDIs and other SUB gadgets and I have noticed absolutely no difference from Windows 8.1 – i.e. everything still works – so that’s a good start.

So – automatic updates – apparently home users will be forced to take updates – that’s not a bad idea IMHO – the amount of people I know who don’t trust Microsoft, refuse all updates then wonder why their PC has a collapsed through nasties… if it is that important, make sure you get the PRO version or better where the option will still remain.

Media Centre (or Center as they call it) is also gone. I don’t know about you but I can’t remember the last time I turned that on – if I want something like that I use Plex or similar.

No more Hearts… shocking – but there are many alternatives out there.

Desktop gadgets – surely that was abandoned in Windows 8.

No more floppy disks – seriously – do people still use floppy disks? DVDs apparently now need separate playback software. I’m assuming VLC will do that job. I don’t know about you but I install VLC by default on any new machine anyway because the Microsoft Media Player is/was pants.

And finally – Edge – the new browser. I’m guessing most people in here hated Internet Explorer anyway. I’ve not done exhaustive tests but Edge looks fine to me. However I’ll continue to use Google for browsing and search engine as always.

Did I miss any major omissions in Windows 10?

I DO like the new menu – I was using POKKI to replace the hated Windows 8 menu but that just is not necessary any more. A good job I reckon.

Facebooktwittergoogle_pluspinterestlinkedin

MQTT/JQUERY/WEBSOCKETS Controller

I wrote a while ago about using web sockets as against something like NETIO for controlling the home – most folk liked the article but I think part of it was a little complicated – and at the time I’d not really thought it out to make it as simple as possible.

So, this morning I started again and now I have a decent and easy way to make control interfaces from the mobile phone – to ESP boards. I won’t go into MQTT in detail here – I’ll assume you have an MQTT interface of some description on your WIFI boards – if you need more info on that, look elsewhere on the blog.

jquery mobile themesI’ll also assume you have node-red running somewhere. In my case I have a Raspberry Pi2 sitting in a corner with battery backup, running node-red, MQTT and Apache web server.

So – in order to make a half-decent looking phone interface, I decided to go for jQuery Mobile – it is cheap, easy to run and there is a theme generator.

I went along to https://themeroller.jquerymobile.com/ and made myself a set of THEMES – that is some CSS. You can’t get any easier – you just drag the colours onto the various items – save the ZIP file – and then refer to the THEMES folder in your web page.

Here’s what I did – you might want less colours, more colours, different colours etc.… you  can change all aspects of the jQuery Mobile interface which is basically a bunch of items that work well on mobile phones and tablets. Click on these images for larger versions.

So – what’s needed then is a web page…. that shows a series of buttons (jQuery Mobile), a means to send off button presses to Node-Red (web sockets) and some means of then controlling things (MQTT).

In my case I put the web page and dependencies in a folder within /var/www on my Raspberry Pi (could be anywhere – a Windows PC, whatever).  I can access these pages externally and hence there is a password on the folder – that is done in my case with an.htaccess file but you might have other ideas.

The page should begin with a header for accessing jQuery, jQuery Mobile and the themes etc.…  I figured as the phone has to have an Internet connection to work anyway, there was no point in filling up the Pi with libraries other than my custom theme..

    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Simple Controller</title>
        <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        <link rel="stylesheet" href="themes/petes.min.css" />
       
    </head>
   

phoneAnd so above, in the HEAD section of the web page (which I simply edit with NodePad++) links to the local theme and remote jquery. That local stuff all comes in the zip file from the themeroller site – well it does if you make the title when saving “petes” – you might want to call it something else.

Here’s my test page which works equally well on a mobile phone as in a web browser  - I’ve checked this out in Chrome and Edge and on my Android 5 phone – identical results.

As you can see I’ve gone for gross, using matching colours (more or less) for the colour controls I’m actually firing off.

The only difference between the buttons is a simple addition of data-theme="g" or whatever letter – if you look at the theme image at the top, as you add more themes, they start off as “a” then “b” etc. We’ll see this code later.

Rather than put a ton of code in here – here’s a snippet with the entire page.

https://bitbucket.org/snippets/scargill/jgBnK

You’ll see the header information followed by some setup for web sockets.

The basis of my controller as you’ll know if you’ve read Home Control 2015 in this blog, is to send MQTT data to my ESP8266 boards, so for example to control the RGB output on GPIO0 of a board called RED1,  I’ll send the following…. the topic would be red1/toesp and the payload would be for example {rgb:0,255,0,0,70,1}

The above means – send a message to board red1 and tell it to fire out a WS2812b signal on GPIO0 to turn 70 LEDs on fully bright red.

I wanted the web page to be as simple a possible – adding to a button something like….

onClick='send("red1","rgb:0,255,0,0,70,1")'

And that is exactly what I’ve done. But firstly the mechanics of the Node-Red web sockets interface.

In Node-Red, I have this:

node-red

Simpler than it looks, I’ve dropped in a WS input module (standard node-red node unit) and out – and connected them together.

the input listens to “/myapp2”  - could be /fred  or anything you like to call it.  Yes folks that is ALL there is to the setup – but…. assuming my web page connects to this – I need to do something with incoming data (in this blog I’m ignoring firing data back to the page but that is also easy – I referred to that in the last blog – the point of web sockets of course is that this is a continuous connection between the web page – and node-red web socket control.

So what I need is an incoming MQTT message and topic – and that needs firing out to MQTT to control things. The only bit of code here is in “process commands” – this is a function module (drag and drop) and this is what I put in it.

var jsondata=JSON.parse(msg.payload);

if (jsondata.topic)
{
    msg.topic=jsondata.topic+"/toesp";
    msg.payload=jsondata.payload;
    return msg;
}

Simple enough – any incoming information is assumed to be in json format and is “parsed” (built in function) so that I can extract a payload and a topic to simply fire off to MQTT and on to my boards.

As I’m always going to need “/toesp” on the end of the topic – I add it in here rather than needlessly replicate it in the web page.

The web page itself needs to fire out something like (in this case)….

{“topic”:”red1”,”payload”:”{rgb:0,255,0,0,70,1}”}

The inner braces are for my boards…

So – my board ends up with:

topic:  red1/toesp

payload: {rgb:0,255,0,0,70,1}

I wanted to keep the actual buttons as simple as possible so there is a function in the web page to glue the topic and payload together.

function send(topic,payload)
{
ws.send('{"topic":"'+topic+'","payload":"{'+payload+'}"}');
}   

and so all we need now is the connection between the web page and node-red web socket interface (simple as they’re both on the same server) hence if you look at the web page you’ll see a definition for wsUriC which is then used in wsConnect() which is called on page load. This makes the connection.

ws.send() sends a string to Node-Red and my function ensures the string is in the right format.

A typical button looks like this..

                        <div class="ui-block-a" ><input data-theme="a" type="button" value="red" onClick='send("red1","rgb:0,255,0,0,70,1")'></div>

Note the very simple onClick function call to send a command off to a particular board…. and if you study the web page example, putting aside the formatting commands for jQuery Mobile (which you can learn all about here https://jquerymobile.com/)  - there is WAY more than just buttons available to you - but I’ll leave that to the reader to explore (or not).

So I press a button on the phone – and the light comes on or changes colour or whatever. There is no feedback – that would be covered by a call-back function in the page and jQuery to alter the shape or colour of elements accordingly – really quite easy once you get started. I thought of putting that in here but I’ve probably given you enough to think about as it is.

Hope you find this useful. When the connection is working incidentally you get a little TICK at the top of the page – or a little cross if not.

tick

Pete.

p.s.  If you’re wondering what all this cave reference is about – head over to http://www.bedrock.es

Coming up soon: I hope to review the inexpensive NEXION touch display (one is on it’s way) to make a nice wall-mounted thermostatic control – which will talk via serial to an ESP-01 which in turn will control a relay and have a DHT-22 chip for temperature sensing – and Node-Red for background timing control – and of course I will have phone-based monitoring and over-ride based on code very similar to what you see here.

Facebooktwittergoogle_pluspinterestlinkedin

Testing ESP8266 WIFI

Amateur night out radiation shieldHaving had mixed results with both the ESP-Arduino environment and the normal SDK (1.3.0 with latest patch) and C, I decided this morning to up the game and do some focussed testing.

So – I made a simple loop with ESP-Arduino, connecting to WIFI, connecting to MQTT and publishing a message to which I’m subscribed, every second. This failed after a while the other day. Someone suggested I needed a “delay()” call in the loop – this despite the client.loop() call. I wasn’t buying it but decided to give it a whirl anyway.

So here is my test ESP-Arduino code with the delay() call added. https://bitbucket.org/snippets/scargill/7RAg4

At 10:26am I set it off running using a nodeMCU board (only because I had it handy – don’t confuse this with Lua code) and every second, sure enough the message went out and came back in… I occasionally used MQTT spy to inject something in there at random.

In true Hackitt & Bodgitt style and sick of getting it in the neck from my wife Maureen for turning the WIFI off, I made a simple shield to test the boards to see what happens when they lose WIFI. See picture above. I used a plastic glass surrounded with foil so as not to short the board while it is inside.

And so it was that by 11:20am, nearly an hour after starting this all off, the board was sitting working perfectly – maybe there IS something to using delay(). My us of 10ms was completely arbitrary incidentally.

Armed this this – the next step was to dump the board in my poor man’s faraday cage. Not one of my more successful ideas. Even with the mesh on the top it worked perfectly – so more silver foil, I covered the top completely with it… and that didn’t work either. Bugger – a lifetime of theory out of the window.  I tried grounding the aluminium foil…  for a second that didn’t seem to work… and then the every-second signal stopped – constant blue light on the nodeMcu board…Nothing – no error messages – just dead. I removed the board from the cup, careful not to short it against any metal…. still dead. Still a constant blue light.

I pressed reset – sure enough – the board came up and started responding again. I repeated the experiment…I’m amazed at how hard it was to stop the signal even with grounding – but eventually,stop it I did. Took the board out – started up again, put it back in – it stopped, took it out… no blue light this time – and no output (again no error messages either)… the only way to bring it back up was a reset. I waited a minute – nothing. Pressed reset – it started up.

Clearly I need to go back to school on shielding so for my next set of experiments, it just so happens I have another wIFI router sitting doing nothing – so I altered the SSID to this one – knowing I could shut it off as needed without upsetting the wife!

Booted up – no bother – messages sending… .turned the WIFI off – the unit stopped – dead. No error messages. Turned the WIFI back on – nothing – not a sausage. I waited – nothing. Pressed reset – worked perfectly.

So there is something here about losing the WIFI signal. I realised at this point that I’d put a forever loop in with no checking for WIFI connectivity – and so made a slight alteration..

while(1)
  {
    client.loop();
    if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }
    delay(10);

    if (WiFi.status() != WL_CONNECTED)  break;        
  }

This in theory would notice the connection break – and reconnect again to the MQTT.

And sure enough – a series of warnings about no WIFI – turn the WIFI back on – and voila.

So it seems that with the delay() function in the loop – and with the right checks for failed WIFI – this all DOES work.

That leaves the question – what if the MQTT server fails!! I disconnected the MQTT server internet connection and sure enough – the program sat doing nothing. I then sensibly added the next line…

while(1)
  {
    client.loop();
    if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }
    delay(10);

    if (WiFi.status() != WL_CONNECTED)  break;        
    if (!client.connected()) break;
  }

 

Sure enough, as soon as I disconnected my MQTT server, the board started to gripe about lack of MQTT. Connected the MQTT and… again everything started up.

So having satisfied myself that this all works – all that remains to be done is a longer term test. I’m off out for a spot of swimming and will leave this running, firing out auto incrementing messages once a second….

mytimer=millis()+1000;
  while(1)
    {
      client.loop();
      if (mytimer<millis()) {
          sprintf(str,"My counter %ld",mycounter++);
          client.publish("inTopic",str); mytimer=millis()+1000;
          }
      delay(10);
      if (WiFi.status() != WL_CONNECTED)  break;        
      if (!client.connected()) break;
    }

 

SADLY – after waiting some time – this was the result..

 

Incoming: My counter 2520
Incoming: My counter 2521
Incoming: My counter 2522
Incoming: My counter 2523
Incoming: My counter 2524
Incoming: My counter 2525
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server

This despite tests showing the MQTT server is working perfectly…

A quick RESET and the count started again. It got as far as 2525 last time…the second attempt it has climbed to 16070 and still going – beats me why it packed in the first time.

 

For instant gratification (or more regular than the blog, if you’re in Facebook – like my ESP8266WIFI page here. https://www.facebook.com/esp8266wifi

Facebooktwittergoogle_pluspinterestlinkedin

EasyTransfer for ESP8266

I’m going to describe here the transmitting side of the EasyTransfer system as used in Arduino – the reason I did this was because I wanted to ship a bunch of binary data from the ESP8266 to the Arduino where I have a nice little wall mounted LCD display – no point in re-inventing the wheel.

EasyTransfer is a simple enough format – it starts with 0x6 and 0x85 followed by the length (all in bytes) of what you want to send, the data itself and a one-byte checksum which is the length XOR’d by every byte in the data (not including the checksum itself of the header 0x6 and 0x85.

So you point a structure to it – and off it goes and the structure is recreated at the other end. Or it is?

struct transmit_lcdX {
  //put variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE DISPLAY BOARD
  uint8_t cmd;
  int16_t passed;
  uint8_t humidity;
  uint32_t thetime;
  int8_t internal;
  int8_t external;
  uint8_t pre;
  uint8_t heat;
  uint8_t hold;
  int8_t offset;
  uint8_t set;
  uint8_t fallback;
  uint8_t falltemp;
  uint8_t thermenable;
  uint8_t dusk_hour;
  uint8_t dusk_min;
  uint8_t dawn_hour;
  uint8_t dawn_min;
} ;

struct transmit_lcdX transmit_lcd;

 

//Sends out struct in binary, with header, length info and checksum
void ICACHE_FLASH_ATTR EasyTransfer_sendData( uint8_t *bfr, uint8_t size){
    uint8_t buf[40];
    uint8_t CS;
    CS = size;
    buf[0]=0x06;
    buf[1]=0x85;
    buf[2]=CS;
    for(int i = 0; i<size; i++){
        CS^=*(bfr+i);
        buf[i+3]=*(bfr+i);
    }
    buf[size+3]=CS;
    uart0_tx_buffer(buf,size+4);
}

There you go – what could be simpler – define the struct – make an instance of it called transmit_lcd – and call a function passing the address of the structure and it’s length.

Well the first bit I got wrong was to send it out in pieces. I did not realise that Espressif’s function uart0_tx_buffer(buf,len) doesn’t operate immediately – clearly it works in the background under interrupts – I guess the word “later” in the instructions should have made me twig but it didn’t.  So first things first, that structure buf[] has to be static – so that it stays there even after the function has done and cleaned up.

That was fine – and so I called my function – nothing… the LCD would not accept the data… I must’ve spent hours on this – I eventually grabbed a terminal with hex output and…. I’d set the time – and sure enough my checksum was working – but the time was in the wrong place.  2 8-bit integers and a 16-bit come to 4 bytes so what I should have seen was 06 86 0xff 00 00 00 00 aa bb cc dd   where ff is the checksum and aa bb cc dd is the 32 bit time.

But no – the time was much further on! How can this be! WELL it turns out that without a special optimisation, DESPITE the fact that I called some of those variables in the struct 8 bits – they were ACTUALLY being stored as 16 bits -   WHAT!!!

So here we have the version that WORKS – of course you can replace the struct data with any old variables – this just happens to be mine….

struct transmit_lcdX {
  //put variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE DISPLAY BOARD
  uint8_t cmd;
  int16_t passed;
  uint8_t humidity;
  uint32_t thetime;
  int8_t internal;
  int8_t external;
  uint8_t pre;
  uint8_t heat;
  uint8_t hold;
  int8_t offset;
  uint8_t set;
  uint8_t fallback;
  uint8_t falltemp;
  uint8_t thermenable;
  uint8_t dusk_hour;
  uint8_t dusk_min;
  uint8_t dawn_hour;
  uint8_t dawn_min;
__attribute__((packed)) ;

struct transmit_lcdX transmit_lcd;

 

//Sends out struct in binary, with header, length info and checksum
void ICACHE_FLASH_ATTR EasyTransfer_sendData( uint8_t *bfr, uint8_t size){
    static uint8_t buf[40];
    uint8_t CS;
    CS = size;
    buf[0]=0x06;
    buf[1]=0x85;
    buf[2]=CS;
    for(int i = 0; i<size; i++){
        CS^=*(bfr+i);
        buf[i+3]=*(bfr+i);
    }
    buf[size+3]=CS;
    uart0_tx_buffer(buf,size+4);
}

Note the two additions in BOLD above – if either is missing – this does NOT work. 

So now, if you have some legacy stuff on an Arduino and you need to pass a struct back to it using EasyTransfer – you can. 

Facebooktwittergoogle_pluspinterestlinkedin

Home Control Reliability–Scheduler

HomeThis is a follow-on article from my blog Home Control 2015 which concerns itself with IOT around the home - you might want to read that first.

Many articles on DIY home control tend to scratch the surface and you don’t find out about reliability issues until you’ve wasted many hours building stuff just to discover that the authors never really got into real world subjects like “what happens if the router power-cyles”.

I’ve been working on this stuff for some time now along with my friend Aidan and having installed boards around the place I’m now getting into the “nitty gritty” of reliability.

schedulerSo for example, take my system here at Bedrock (so called because it is actually a real cave in the mountains of Andalusia). I have a Pergola – which has a 12v power system and an ESP8266-based relay board to turn the lights on and off. I also have flood lights around the place and an internal light which are controlled by another board. Finally I have a watering system which fires off twice a day for a few minutes.

My system as you’ll know from the main home control article is based on a Raspberry Pi2 running MQTT and Node-Red talking to ESP-8266 “Hackitt & Bodgitt” boards and of course my own software.

I have a little uninterruptable on the Pi2 and that has tests for WIFI failing and tests for MQTT failing and almost no matter what happens, the system stays up.

After 12 days of perfect operation while I was over in Boston with Espressif, last night the WIFI went off. I had no idea what it was so I power-cycled the router. Still nothing. We sat around for a while and the WIFI came back up – clearly some issue with the service provider.

I noted that it had just turned to DUSK and yet no lights.

And then it twigged. My Node-Red node I wrote called scheduler, sends MQTT messages out at dusk and other times to control the lighting… but of course if the WIFI is not connecting at the time, the opportunity is missed.

Now you could use a number of tricks, the various modes of MQTT include a mode to always get the message through. However I can see issues with that and so last night I did an update to my scheduler.

Instead of sending out commands to turn lights on and off at specific times – like dusk and dawn – I’ve added a checkbox to send the instructions out every minute as to the state that is expected. This is entirely optional, controlled by a tickbox.  But this way, if a message for whatever reason is missed – it will be sent regularly and the recipient units can then be assured of catching up.

Time will tell if this improves reliability – my gut feeling tells me it will. The scheduler is available on BitBucket and obviously requires Node-Red. It is probably the most comprehensive timing mechanism available for Node-Red – I would welcome being proven wrong here.

Facebooktwittergoogle_pluspinterestlinkedin

The Boston Trip

beersolar binsSome time ago, the CEO of Espressif, makers of the lowest cost WIFI-enabled processor on the planet and a chip I’ve blogged about frequently in here, invited me over to MIT at Boston to the FAB11 conference with them. As it turns out this was a rather small affair featuring things like 3D printers and other fabrication equipment.

However the main point was to run a series of workshops on the MIT campus about the ESP8266 and so along with me were employees of Espressif including Jeroen Domburg (ES_-HTTPD) and also an independent like me, Ivan Grokhotkov (Esp8266/Arduino).

We arrived on Sunday in sunny Boston and of course the first thing that happened was that Iberia Airlines lost my large case complete with samples, laptop+presentation, clothing, pills, flashy LED ring I had such a hard time getting from China thanks mainly to UPS.

Simmons Hall or Stalag as I will no doubt remember itJust before arrival I’d received a note to say that due to flooding, my single room accommodation was to be “shared” – with someone I’d never met before. Ok, I’d emailed Jeroen before but I did stress to the staff at the hostel (or as I now call it, Stalag) that I really would like my own room – after all I hardly class as a student. I arrived and despite protests, no change – the place is run by mainly apathetic students or so it seemed to me. Now as it happens, Jeroen and I get on just fine and there was not a problem – but if this is typical of how they treat students – you can keep Boston hospitality – not even remotely acceptable. I would not put my cat in the room they gave us, essentially a rectangular concrete prison sell with 2 beds, Ikea wardrobes and 2 simple desks.

However, Jeroen turned up later than me on Sunday– near midnight and as technical people often do, the moment he walked through the door we started chatting about technology and I think we finally gave up around 1am.

We got along just fine.

image

 

MITIvan and Swee-AnSo first thing Monday I went off and bought a load of clothes. The Americans invented the term “convenience store” but I think they only meant that to apply if you have a car. Walking and taking the bus is not at all funny in the heat and so it was I arrived at the mall mid-morning to find the old American staples – CVS, Walgreens, Seers, J C Pennys etc..

I have to say for a country with some pretty large people I had difficulty getting clothes my size (at least with any choice) but finally came away at lunchtime armed to the teeth with new gear. As for Pennys – they don’t even sell plugs! Give me B&Q or Brico Depot.

I’m not sure what I was expecting at the venue, certainly organisation was high up there somewhere but inside MIT campus, 30 minutes before our presentation, no laptops – eventually they turned up in 2 black boxes,  half of the laptops with the wrong power supplies rendering them useless.

As it happens it was not a full house and so we managed just nicely – talking about ESP8266, MQTT, Node-Red and finally a little about ESP-Arduino to an enthusiastic audience.

MIT[6]Of course in each of the three versions of the workshops we improved and I have to say that by and large they went down very well. Clearly the organisers had done little to help promote us because I know the level of interest I get when I talk about ESP8266 and I was expecting a full house every time – still – all good fun.

Part way through the trip and in the middle of a heat wave, the heavens opened up and threw out some pretty amazing hailstones! I mean MONSTERS.

Myself, Jeroem Domburg and Ivan Grokhotkov spent much of our time when not investigating the city, in room E25-117 at MIT giving presentations and practical workshops to an enthusiastic audience – in the main with great weather, to boot. We demonstrated a NETIO page which I put together talking directly to a servo running under ESP8266-Arduino which Ivan put together.  I’ve documented this elsewhere here in at tech.scargill.net.  We also discussed MQTT communications and more including my favourite topic – Node-Red.

We went out at night – the first walk was rather too far for my airplane-equipped feet and I’ve been hobbling since. Amazingly despite having a medical dispensary within MIT itself and a near by Co-op (or as the lady kept insisting COOP as in soup – I didn’t like to tell her the name is made from two words) – no foot fixing kit, so being the inventive type I went off and bought a box of cork tiles and a pair of scissors – and DIY fixed the foot which is going to need some tender care and a pin when I get back to Spain.

MIT[8]Just as we were finishing off, I received my case at long last, I should say thanks to Iberia but frankly I think they have shown themselves to be a bunch of complete tossers. Not only did they give me someone else’s reference number which confused the issue but the fact that I had important medicine in the case didn’t seem to phase them even slightly.

Had I fallen ill while away things would be very different now – as it is, lets see how promptly they cough up for the clothing and toiletries! They’ve offered 50 Euros per day – assuming I guess that everyone goes off and buys t-shirts  - in reality I needed decent shirts, trousers, underwear and toiletries so I can see a battle coming on featuring their social media.

PeteThe end result of all of this however is enlightenment and a pretty good time generally. I’ve met some great people and learned some new stuff. I’m now back in Spain with new ideas, some new thoughts on China having spoken at length to the Chinese guys here – and generally feeling that this was another worthwhile adventure.

I can’ wait to get back into the nice cool lake in Spain (I’ll never complain about cold water again) – heat can be a great thing but in a city built for commerce and not comfort in heat, give me the wilderness any time. Sadly right now in Andalusia it’s a bit foggy!

I’ve written a blog on on the work Ivan has done with ESP8266-Arduino  which I have to say is pretty good –  in our talk we drove a servo from a NETIO Android App – it is so easy it’s like falling off a log. For now, I’ll leave you with some photographs. My thanks to Espressif and to Swee-An for looking after me and providing thought-provoking conversation.

 

Boston[1]

Restaurants

FAB11

MIT[10]

Facebooktwittergoogle_pluspinterestlinkedin