An 8 digit VFD for the ESP32

I got hold of a Vacuum Fluorescent Display module from Aliexpress. It comes in two versions : one with an SPI interface, one without. I went with the SPI interface version. The display reminded me of a clock radio I had growing up so it was natural to put it to work as a clock. I wired it to an ESP32-CAM module as shown below:

Details about how the display is programmed were found over here. I wanted to use the SPI hardware interface instead of bit-banging the data and so developed the following program using Arduino for ESP32:

#include <SPI.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <HTTPClient.h>

/*
  ESP32 based clock.
  Uses Vacuum Fluourescent Display (VFD)
  Gets time from an NTP server
*/

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

const char* ssid = "***********";
const char* password =  "****************";

class VFDisplay
{
  public:
    VFDisplay() {};
  void begin()
  {     
    SPI.begin(SCK,MISO,MOSI,SS);
    pinMode(Reset,OUTPUT);
    pinMode(CS,OUTPUT);
    digitalWrite(Reset, LOW);
    delayMicroseconds(5);
    digitalWrite(Reset, HIGH);
    
    setDigitCount(8);
    setBrightness(127);    
    Serial.printf("VFD_init\n");
  }  
  void putChar(unsigned char x, char chr)
  {
    digitalWrite(CS, LOW); 
    writeDisplay(0x20 + x); 
    writeDisplay(chr);
    digitalWrite(CS, HIGH); 
    show();    
  }
  void printString(char *str)
  {
    int x = 0;
    while(*str)
    {
      putChar(x++,*str++);
    }
  }      
  private:
  void setDigitCount(uint8_t count)
  {
      digitalWrite(CS, LOW);
      writeDisplay(0xe0);
      delayMicroseconds(5);
      writeDisplay(count-1);
      digitalWrite(CS, HIGH);
      delayMicroseconds(5);  
  }
  void setBrightness(uint8_t brightness)
  {
    digitalWrite(CS, LOW);
    writeDisplay(0xe4);
    delayMicroseconds(5);
    writeDisplay(brightness);
    digitalWrite(CS, HIGH);
    delayMicroseconds(5);
  }
  void show()
  {    
    digitalWrite(CS, LOW);
    writeDisplay(0xe8);     
    digitalWrite(CS, HIGH); 
  }
  void writeDisplay(uint8_t b)
  {
    SPI.beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE0));
    SPI.write(b);
    SPI.endTransaction();
  }

  uint8_t Reset=15;
  uint8_t CS = 12;
  uint8_t SCK = 4;
  uint8_t MOSI = 2;
  uint8_t MISO = 1;
  uint8_t SS = 12; // not actually used - see CS above

};

VFDisplay vfdisplay;
void setup() {
    
  Serial.begin(115200);  
  vfdisplay.begin();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    vfdisplay.printString("WifiWait");    
    delay(1000);    
  }
  vfdisplay.printString("WifiDone");
  timeClient.begin();
  timeClient.setTimeOffset(3600);
  timeClient.update();
  timeClient.setUpdateInterval(5*60*1000); // update from NTP only every 5 minutes  
}

void loop() {
  timeClient.update();     // This will only go to the Internat if the update interval has passed (set to 5 minutes above)
  String timeString = timeClient.getFormattedTime();
  char TimeCharArray[20];
  timeString.toCharArray(TimeCharArray,19);
  vfdisplay.printString(TimeCharArray);
  delay(100);
}

The system appears to work well and could be extended to include a Bluetooth interface to set an alarm time or time zone etc.

The Renesas RA2A1 microcontroller

Renesas produce an ARM Cortex M23 based microcontroller called the R7FA2A1AB3CFJ or RA2A1. This device has 256k of flash memory and 32k of RAM. It also has a number of peripherals and memory/security mechanisms. I was interested in learning about the device at a low level and so I did not use the extensive libraries and support files available from Renesas. Instead I decided to write my own device header file and linker and build scripts.

The breadboard circuit

The schematic and photograph of the breadboard kit are shown above. The RA2A1 chosen is housed in a 32 pin LQFP package and was broken out into a DIL arrangement using an adapter.

Option words

The post-reset state of the RA2A1 is configured using some option words that are located within the system flash memory. It is important that you avoid accidentally overwriting these bytes when writing your program to flash memory. The option setting memory words are at:0x400 and 0x404 i.e. 0x400-0x407 inclusive. I modified my usual linker script so that the program flash area starts after the option memory (with a little margin). The interrupt vectors must still be stored at address zero so a memory regision called “vector_area” is defined as shown. The linker will only position the interrupt vector table there.

When I started using this chip I accidentally overwrote the option memory and was left with a non-functioning chip. The support people in Renesas helped and provided a script that you can use to erase the lower parts of flash back to factory defaults. Details are here:

https://renesasrulz.com/ra/f/forum/17951/ram-is-not-writable-on-r7fa2a1ab/59092#59092

There is a zip file linked in this conversation which includes a Windows batch file. I use Linux and so ran this command instead:

JLinkExe -CommanderScript RA2A1_Erase_Block_0.JlinkScript.jlink

Interrupt system

The RA2A has a total of 32 interrupt vectors (beyond the 16 internal ARM Cortex ones). These interrupt vectors are mapped to peripheral events using 32 Event Link Setting Registers in the Interrupt Control Unit (one ELSR per interrupt vector). The ELSR’s allow you associate an event number with an interrupt vector. Each peripheral has one or more event numbers associated with it e.g. The Receive event for Serial Communications Interface 0 is event number 0x71. In the examples below, this is associated with interrupt vector 0 as follows:

ICU_IELSR[0] = 0x71;

When a byte is received by SCI0 the first (non-ARM) interrupt vector is fetched and the handler at that address is exectuted.

Pin Function Selection

Like most microcontrollers, the RA2A1 allows you assign pins to a selection of peripherals. The RA2A1 handles this by using a 16 word array for each port (there are 5 ports each with 16 bits). Each of these array elements allows you set pin direction, peripheral function, interrupt mode, pull-ups etc. The user manual provides a table with the peripherals that can be mapped to each I/O pin. There is a protection mechanism associated with the pin function selection system (as well as other systems). This requires writes to a couple of registers to unlock.

The SDADC24

The RA2A1 has a 24 bit Sigma Delta ADC which includes an instrumentation amplifier with a programmable gain and an offset adjustment system. This is potentially very interesting for instrumentation applications. The SDADC is also capable of averaging a number of input samples which reduces noise at the expense of frequency response. It is also possible to control the oversampling of the sigma delta adc to allow for further noise reduction. Included in the examples below is a program which sends the SDADC conversion result out over the SCI0 serial port.

Sample code

Sample code is available on github over here: https://github.com/fduignan/ra2a1

blinky : blinks and LED on P400

button_in : reads an input button and updates and LED in response

clocks : sets the clock speed to 32MHz

systick : implements an interrupt handler for the SysTick interrupt

serial : interrupt driven serial input /output

sdadc24_serial : printing the output from the SDADC to the serial port

Using the FMAC in the STM32G431

The STM32G431 has a Filter Math ACellerator (FMAC) hardware unit inside of it. This unit can take be used to implement an FIR or IIR filter without burdening the CPU. The FMAC unit has input and output circular buffers as well as a coefficient buffer. It is possible to connect the input buffer to an ADC using DMA and similarly it is possible to connect an output buffer directly to a DAC over DMA. In the case of this project I used an ADC interrupt handler to manage data input and output to the FMAC.

There are lots of tools to help you design a digital filter. I chose to use python and jupyter notebook in this case. The jupyter notebook code is as follows (it is also on the github site linked below)

import numpy as np
import scipy as sp
import scipy.signal as sg
import matplotlib.pyplot as plt
Fs=48000
Fpass=1000
Order=16
Wp=Fpass/(Fs/2)
b=sg.firwin(Order+1,Wp,window = "hamming",pass_zero = True)
w,h=sg.freqz(b)
mag=20*np.log10(abs(h))
plt.figure()
plt.semilogx(w*(Fs/(2*np.pi)), mag)
plt.show()
bmax=np.max(np.abs(b))
# Working out the scale factor can be a bit tricky.  There is a 
# 24 bit accumulator in the FMAC.  The ADC has a 12bit range.
# This leaves 12 bits for coefficients if overflows are to be prevented.
# Furthermore, the multiply and accumulate nature of the FIR will push 
# results beyond 24 bits if we are not careful.  This is more pronounced with
# lower cut-off frequencies where there is a large central lobe to the filter 
# coefficients which may lead to overflows, particularly at low input 
# frequencies.  For now I'm just doing this by trial and error
ScaleFactor=4095/(bmax)
f = open('coffs.h', 'w')
f.write("#include <stdint.h>\n")
f.write("#define SCALE_FACTOR ")
f.write(str(int(np.round(ScaleFactor))))
f.write("\n")
f.write("#define FILTER_LENGTH ")
f.write(str(Order))
f.write("\n")
f.write("const int16_t b[]={")
for coeff in b:
    f.write(str(int(np.round(coeff*ScaleFactor))))
    f.write(",\n")
f.write("};\n")

f.close();
plt.figure();
plt.plot(b);

This code outputs a header file that includes the filter coefficients for a low pass FIR filter with a cutoff frequency of 1000Hz. The output at 2kHz is shown below

And it 4kHz this becomes:

It would appear that the filter is indeed working however there are a number of caveats. The FMAC uses fixed point arithmetic so coefficients and input signals must be shifted and scaled appropriately. The FMAC has a limited numeric range (24 bits of fractional data internally, 15 bits input and output) and overflows will happen. This is a particular problem at low frequencies with filters whose coefficients are mostly/all positive. I had to do some manual tweaking of the coefficients to get the output performance I wanted. When testing for such overflows it is useful to input a DC signal of maximum voltage to ensure that no overflows occur.

As usual, code is available over on github

Multi-threading on the STM32L031 Nucleo with the Keil IDE

An earlier blog post showed multi-threading on the TI MSP432. Recently I have been working on an STM32L031 Nucleo board with the Keil ARM-MDK environment. I wanted to demonstrate multi-threading and so ported the code from the MSP432 to the L031. A section of the main.c file looks like this:





#define STACK_SIZE 128
__attribute__((noreturn))  void threadA(void);
__attribute__((noreturn)) void threadB(void);
__attribute__((noreturn)) void threadC(void);
static uint32_t StackA[STACK_SIZE];
static uint32_t StackB[STACK_SIZE];
static uint32_t StackC[STACK_SIZE];

void threadA()
{
	while (1)
	{
		static int State = 0;
		if (State == 0)
		{
			GPIOA->ODR &=~(1u << 0);
			State = 1;
		}
		else
		{
			GPIOA->ODR |= (1 << 0);
			State = 0;
		}
		delay(100000);
	}	
}
int main()
{
	// Initialize I/O ports
	RCC->IOPENR = 1; // Enable GPIOA
	pinMode(GPIOA,0,1); // Make GPIOA Bit 0 an output
	pinMode(GPIOA,1,1); // Make GPIOA Bit 1 an output
	pinMode(GPIOA,2,1); // Make GPIOA Bit 2 an output
	initClock();		// Set the MCU running at 16MHz
	createThread(threadA,StackA, STACK_SIZE);
	createThread(threadB,StackB, STACK_SIZE);
	createThread(threadC,StackC, STACK_SIZE);
	startSwitcher();
}

Three threads are created in this example and they each change the state of a bit on Port A. I should have used the BSRR register to set and clear the bits but for the purposes of getting the ideas behind multi-threading across this was sufficient. The createThread function takes three arguments: The thread’s start address, a pointer to the thread’s stack and the size of this stack. Stacks for the threads are simply declared as arrays of uint32_t’s.

The startSwitcher function starts the SysTick timer running, does some stack adjustments and enables interrupts. It does not return. The SysTick handler is written in assembler and it performs the context switch between threads.

Source code is available on github. I did not include the project (uvproj) files as they contain lots of path information that would not transfer to other systems. If you want to try this for yourself just create a project for the STM32L031 Nucleo in Keil and add the files from github. You should first remove the other source files in your project.

Breadboard Games 2021

Given the pandemic restrictions this year Breadboard Games will be delivered remotely. Participants will each receive a kit and work with a parent to assemble the gaming console. Remote support will be provided via video.

Instructions

This is a breadboard. It allows us to connect electronic components together by pushing them into the holes. In the centre region all the holes in column 1 are connected together inside the board apart from the gap in the middle. Columns 2 to 30 work similarly. You can see more about breadboards at this location


In the outer, between the blue and red lines, connections run horizontally within the breadboard. We usually use these areas to connect power supplies for our electronic components.

Lets begin by putting the black wires into the board as shown above. The top row of holes next to the blue line will be used to distribute the 0V connections for our circuit (the negative terminal on our battery). The connections are expressed in Row,Column terms as follows:

j6 to 0V (approximately directly above just next to the blue line)
j9 to 0V
a1 to 0V
a24 to 0V
a27 to 0V
a30 to 0V

A diode only allows electricity to flow in one direction. We include one here to protect our game in case somebody connects the battery pack in backwards. The diode has two wires : Cathode and Anode.

We want electrical current to flow into hole j7 so put the cathode in there and the anode just above the red line as shown.

Connect the red wire between j5 and j10.
The other component is a resistor. Resistors control the flow of electrical current. We are using this one to limit the brightness of our display so that the batteries last longer. The resistor connections between f9 and f16. Note the way the resistor sits in the valley between both halves of the breadboard.

The screen in our game is a “touch-screen”, just like a phone. We have to add a couple of wires to support this. The yellow wire goes from e10 to f13. The blue wire goes from d9 to f23

The purple wires are used to connect the buttons to the little computer in our game. They are connected as follows:
a3 to a18
a19 to a28
a20 to a25

The buttons go in next. Each button has two “pins” that are connected together electrically when you push down. The left button we will call simply “Left”, the rightmost button we will call “Right” and the middle buttonw we will call “Fire”. The connections are as follows:
Left: c1 and c3
Fire: c25 and c27
Right : c28 and c30

We are nearly done. The computer board that controls our game needs to go in next. Before you put it in be sure that all of the previous connections are correct. It is difficult to remove the computer boards if you need to correct any errors. Push the board firmly into the breadboard with the pin marked “3.3” (top left) in hole g5.

And now for the screen. Be very careful putting this in. It goes in row i. The rightmost pin of the display goes into hole i24. Push down on the pins only, not the glass. The glass will crack if you push on it.

Our game makes sounds. The buzzer is connected between a17 and 0V (just below the blue line). The longer leg of the buzzer (labelled +) goes in to a17.

The battery pack plugs into the board as shown above. Note the red wire is closest to the red line on the breadboard. The on/off switch should be just behind the display on the right hand side. If you like you can stick the breadboard to the battery pack using the adhesive backing on the breadboard. If you decide to do this be very careful as the adhesive backing is very strong.

All software used in this game is available on my github repository

Waiting for /CS

I have been working on an interface between an STM32G431 and a W25Q128FV SPI flash memory chip (128 Mbit/16MByte). The image above shows the memory chip on a breakout board with a surface mount capacitor next to it. Given the nature of breadboards and the wires used I’ve been running the memory interface at a reduced speed (1.3MHz). A PCB will be used at a later stage and which should allow for higher speeds.

Erasing the chip was presenting some problems. The code for erase was as follows:

void serial_flash::bulk_erase()
{
	write_enable();
	SPI->startTransaction();
	SPI->transfer((uint8_t)0xc7);	
	SPI->stopTransaction();
	while(read_status1() & 1); // wait until erase has completed
}

The chip must be put into “write” mode before the erase command (0xC7) is sent. The function exits when the “busy” bit in status register 1 is clear. While everything seemed ok, the function did not erase the chip. I took a closer look at the SPI bus using a logic analyzer. I have a very cheap logic analyzer which doesn’t have a very good trigger mechanism. My normal workaround for this is to put the area under test into an everlasting loop and then view the pins of interest on the logic analyzer. This is a problem for erase operations like this as the SPI flash chip has only so many erase cycles. As a precaution I change the command code to 0xd7 (not a supported command) which allowed me look at the SPI bus without harming the chip. I also commented out loop that polled the status register.

The write enable command (0x06) is plainly visible as is the “fake” chip erase command 0xd7. The CS line is driven low just before the 0x06 command and goes high some time after the 0xd7 command. This is not the correct way to erase this chip. The data sheet clearly states that the CS line must go high for a period after each command. It does not do this after the write enable command. The write_enable function is as follows:

void serial_flash::write_enable(void)
{
	SPI->startTransaction();
	SPI->transfer((uint8_t)0x06);	
	SPI->stopTransaction();		
}	

The stopTransaction function should drive the CS line high but it didn’t seem to be working. The relevant SPI code is:

void spi::stopTransaction(void)
{	
	volatile unsigned Timeout = 1000;    
	while (SPI1->SR & ((1 << 12) + (1 << 11)) );     // wait for fifo to empty
	while (((SPI1->SR & (1 << 0))!=0)&&(Timeout--)); // Wait for RXNE
	Timeout = 1000;    
	while (((SPI1->SR & (1 << 1))==0)&&(Timeout--)); // Wait for TXE
	Timeout = 1000;    
	while (((SPI1->SR & (1 << 7))!=0)&&(Timeout--)); // Wait for Busy		
	SPI1->CR1 &= ~(1 << 6); // Disable SPI (SPE = 0)
				
}

This should have worked but it clearly didn’t. Thinking about the sequence of events involved in the bulk_erase function it occurred to me that the call to startTransaction just after the write_enable command may actually be happening before the SPI peripheral had a chance to raise the CS line. The SPI peripheral is routed through GPIO port A in this setup. I noticed that I could monitor the status of the CS pin by reading GPIOA’s input data register and hence wait for it to go high. The stopTransaction code was modified as follows:

void spi::stopTransaction(void)
{	
	volatile unsigned Timeout = 1000;    
	while (SPI1->SR & ((1 << 12) + (1 << 11)) );     // wait for fifo to empty
	while (((SPI1->SR & (1 << 0))!=0)&&(Timeout--)); // Wait for RXNE
	Timeout = 1000;    
	while (((SPI1->SR & (1 << 1))==0)&&(Timeout--)); // Wait for TXE
	Timeout = 1000;    
	while (((SPI1->SR & (1 << 7))!=0)&&(Timeout--)); // Wait for Busy		
	SPI1->CR1 &= ~(1 << 6); // Disable SPI (SPE = 0)
		
	while((GPIOA->IDR & (1 << 4))==0); // wait for CS to go high
	
}

This produced the following output from the logic analyzer:

A high pulse can now be seen between the two SPI commands. As a final test, I replaced the fake “0xD7” command with “0xC7” and presto: erases now work.

Microbit V1 display driver

The image above shows a Microbit V1 connected to an ST7789 1.14′ display via a Kitronik breakout board. The SPI interface on the Microbit (NRF51822) is a little slow at 4MHz but it is ok for simple outputs as shown above. Code was developed using VSCode, PlatformIO the mbed framework. The full list of connections can be found in the file display.cpp which is available on the github repo over here

An NRF52832 with an ILI9341 display

The NRF52832 is a Bluetooth microcontroller from Nordic Semiconductor. Boards and modules based on it are available from a number of different suppliers. I got a few on Aliexpress that bear the Ebyte logo. These were pre-programmed (and locked) with a software image from Ebyte. As I planned writing my own code I had to first unlock the device. Normally I work with an STLink debugger (clone) but this is apparently not able to unlock the NRF52832. Fortunately I had previously bought a J-Link Edu probe and this able to unlock the module.

The next hurdle was to figure out which pin is which as the board labels are specific to some other application. With a bit of fishing around datasheets and poking with a multimeter I came up with this:

The pin numbers could now be related to port numbers in the NRF52832.

Next the device had to be connected up to the ILI9341 display module. This too was bought on AliExpress. It is an SPI device that includes a touch screen controller. The wiring to the NRF52832 module is as follows:

ili9341NRF52832Remarks
T_IRQP0_40 if screen is touched
T_DOP0_26MISO
T_DINP0_25MOSI
T_CSP0_5Chip select for touch interface (active low)
T_CLKP0_27SPI clock
SDO (MISO)N/C
LEDVia 10 ohm resistor to ground
SCKP0_27SPI clock
SDI (MOSI)P0_25MOSI
D/CP0_6Switches between data and command mode (0=Command mode)
RESETP0_7Clear to 0 to reset the display
CSP0_28Chip select for display (active low)
GND0V
VCC3.3V

MBed and Visual Studio Code / PlatformIO were used to write code for the device. Initially Mbed was used but it can be slow at certain times of the day so I switched to VSC/PlatformIO for local development. This was a little fiddly to set up but it worked out very well in the end. MBed OS was used as a basis for the code and a working demo is available over here.

Mbed does not have a board support library for this specific board however the DELTA DFBM-NQ620 board uses the same NRF52832 module and works fine.

There were a couple issues along the way:

The NRF52832 has a maximum SPI speed of 8MHz which is a little slow for a display like this.

The Touch screen controller does not work well with the 8MHz SPI clock rate so this is switched down to 2MHz temporarily when reading the touch interface.

The rats-nest final system is shown below.

The STM32F411 “Black Pill”

The STM32F411 “Black Pill” board is a step up in performance from the Blue Pill (STM32F103). It contains a 100MHz ARM Cortex M4F CPU with 512kB Flash and 128kB of RAM. The usual sort of peripherals are also included (ADC, Timers, SPI, I2C etc.).

The Black Pill board comes with a set of pads on the underside that can accommodate an SPI flash chip. I was interested in giving this a go so soldered in an ST Micro M25P16 device. This has 2MB of storage that is page programmable (256 Byte pages) and sector erasable (64kB sector size). It also supports a bulk erase function.

There were no great problems getting a driver going except for one thing: When NSS is released (sent high) by the SPI peripheral I found that I had to introduce a short delay in the SPI driver. Without this delay, the interface to the flash chip did not work when one transaction immediately followed another. This may be because the pulse never left the STM32F411, or because of bad wiring, or because the flash chip just needs a moment (the datasheet seems to suggest at lease 100ns of a pause). Anyway, it now works and the image below shows a data read transaction captured using pulseview (Thanks pulseview authors! 🙂 ).

You can just make out the narrow NSS pulses (approx 400ns) that were necessary for the system to work. This image was captured with the SPI bus running at a reduced speed to allow for the limited bandwidth of my logic analyzer.

What could this be used for? Well, a data logger perhaps, or a store for various image assets for a screen or maybe even some sounds. If you use this be mindful of the write cycle limitation of these devices and especially don’t put a write in a fast loop.

Code for this and other examples can be found over on github : https://github.com/fduignan/stm32f411

Green power

The operators of the national grid in Ireland provide information about the fuel mix used for electrical power generation. This is updated every 15 minutes and can be seen here http://www.eirgridgroup.com/how-the-grid-works/system-information/

Delving into the source for this page a little it is possible to locate the web resource for the fuel mix which, it turns out, returns data in JSON.

So, a little bit of building later….

ESP32 with a PL9823 LED

Followed by a little bit of coding:

#include <ArduinoJson.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <SPI.h>
const char* ssid = "XXXXXXXXX";
const char* password =  "XXXXXXXXXX";
StaticJsonDocument<1024> doc; 
#define COAL 0
#define GAS 1
#define IMPORT 2
#define OTHER 3
#define RENEW 4
float coal, gas, import, other, renew;
float domestic;

void setup() {
  SPI.begin(1, 4, 2, 3); // (int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
  Serial.begin(115200);
  
  delay(1000);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to Wifi");
    writePL9823(0x00);
    delay(1000);
    writePL9823(0xffff00);
    delay(1000);    
  }
  Serial.println("Wifi ready");
  
}

void loop() {
  // put your main code here, to run repeatedly:
  if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status

    HTTPClient http;

    // Interesting discovery : setting the date/time to a future data returns the latest value
    http.begin("http://smartgriddashboard.eirgrid.com/DashboardService.svc/data?area=fuelmix&region=ALL&datefrom=17-Dec-2050+23:59&dateto=17-Dec-2050+23:59"); //Specify the URL
    int httpCode = http.GET();                                        //Make the request

    if (httpCode > 0) { // Http error?

      String payload = http.getString();
      Serial.println(httpCode);
      Serial.println(payload);
      deserializeJson(doc, payload);
      coal = doc["Rows"][COAL]["Value"];
      gas = doc["Rows"][GAS]["Value"];
      import = doc["Rows"][IMPORT]["Value"];
      other = doc["Rows"][OTHER]["Value"];
      renew = doc["Rows"][RENEW]["Value"];
      domestic = coal + gas + other + renew;
      Serial.print("Total (without export)= ");
      Serial.println(domestic);
      Serial.print("Renew = ");
      Serial.println(renew);
      Serial.print("% = ");
      Serial.println(renew / domestic);
      if ( (renew / domestic) > 0.5)
      {
        writePL9823(0x00ff00);
      }
      else
      {
        writePL9823(0xff0000);
      }
      delay(15 * 60 * 1000); // wait 15 minutes as that is the update interval
    }
    else {
      Serial.print("HTTP Error ");
      Serial.println(httpCode);
      writePL9823(0xff);
      delay(10000);
    }
    http.end(); //Free the resources
  }
}

void writePL9823(uint32_t Colour)
{
  // each colour bit should map to 4 SPI bits.
  // Format of Colour (bytes) 00RRGGBB
  uint8_t SPI_Output[12];
  int SrcIndex = 0;
  int DestIndex = 0;
  for (DestIndex = 0; DestIndex < 12; DestIndex++)
  {
    if (Colour & (1 << 23))
    {
      SPI_Output[DestIndex] = 0xe0;
    }
    else
    {
      SPI_Output[DestIndex] = 0x80;
    }
    Colour = Colour << 1;
    if (Colour & (1 << 23))
    {
      SPI_Output[DestIndex] |= 0xe;
    }
    else
    {
      SPI_Output[DestIndex] |= 0x8;
    }
    Colour = Colour << 1;
  }
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
  SPI.transfer(SPI_Output, 12);
  delay(10);
  SPI.endTransaction();
}

And hey presto!

A traffic light which is green when renewable power on the grid is greater than 50%, red otherwise. This could inform you when is a good time to turn on the clothes dryer for example.

I wouldn’t recommend that this be used to control devices directly as there are many ways it could be hacked.