Tag Archives: programming

(Blind) Maze puzzle now with generation and play modes.

Info:

  • PlatformIO tested ( install StackArray.h in lib/StackArray/StackArray.h )
    • Reset button and dipswitches for below options
      • Visited pathmode ( dip switch 1)
        • Preview maze generation ( dip switch 3)
        • Hard mode – starts at first position when hitting a wall ( dip 2 )
        • Longest path mode (longest stackarray before stack.pop (dip 4 )
        • Prints serial info, like a drawn maze
        • Pixel remapping for other displays ( Below mapping i had to use, see source sketch )

How is the maze generated?

It is generated using recursive backtracking:

Start at 0,0 mark visited, select a random direction from unvisited neighbours, push current on the stack, goto new location, and mark this visited.
Repeat until no possible directions.
pop from the stack a previous location, another direction possible? Take that path and repeat.
Repeat popping from stack, until stack empty .. now all cells are visited

longest pop : 41
maze
------------------------
|    ||    ||       || |
---            ---      
| ||    ||    ||    || |
   ------------   ---   
|       || ||    ||    |
   ---         ---   ---
|    ||    || ||       |
      ---         ---
| || || || || || ||    |

| || || || || || || || |
---
|    ||    || || || || |
   ---------   ---
|                   || |
------------------------
x y : 00
maze 4
|4|3|6|3|6|5|3|2|
|2|12|9|12|9|6|9|10|
|14|5|3|2|6|9|6|9|
|14|3|12|11|10|6|13|3|
|10|10|2|10|10|10|6|11|
|8|10|10|10|10|10|10|10|
|6|9|12|9|10|8|10|10|
|12|5|5|5|13|5|9|8|

Easiest play mode: Preview Maze, Show visited path, NO reset to square 0,0 and NOT the longest path so :

Show visited DIP1Reset to 0,0 DIP2Preview DIP3Longest path DIP4
1010

Hard mode:

Show visited DIP1Reset to 0,0 DIP2Preview DIP3Longest path DIP4
0101

Todo: Battery / batterymanager / 3D case / better buttons

Some video shorts put together to show modes

CODE

#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include "StackArray.h"

// Pin Assign
int up=25;
int down=33;
int left=32;
int right=26;
int trailsw=17;
int restartsw=5;
int showmazesw=18;
int popendsw=19;

int xend=7;
int yend=7;

int countpop=0;
int currentpop=0;

int directions[4]{};
int notalldone = 1;
int tmpx=0;
int tmpy=0;
int xgen = 1;
int ygen = 1;
int x = 0;
int y = 0;
int showmaze = 0;

// 0 easy = trail // 1 only red walls // 2 = reset to 0.0
int mode=0;

//int trail=32;
int trail=32;

// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN    2

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 64

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

int maze[8][8] = {

  };

int displaymatrix[8][8] = { 
{ 0,1,2,3,4,5,6,7 },
{ 15,14,13,12,11,10,9,8 }, 
{16,17,18,19,20,21,22,23},
{31,30,29,28,27,26,25,24},
{32,33,34,35,36,37,38,39},
{47,46,45,44,43,42,41,40},
{48,49,50,51,52,53,54,55},
{63,62,61,60,59,58,57,56}
};

int visitmatrix[10][10] = { 
  1,1,1,1,1,1,1,1,1,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,1,1,1,1,1,1,1,1,1
  };


void(* resetFunc) (void) = 0;

void drawmaze2(){
  Serial.print("x y : ");
 Serial.print(x);
 Serial.println(y);
 Serial.print("maze ");
 Serial.println(maze[x][y]);
  for(int ledy=0;ledy<8;ledy++)
  { Serial.print("|");
    for(int ledx=0;ledx<8;ledx++){
    Serial.print(maze[ledx][ledy]);  
    Serial.print("|");  
    if ( maze[ledx][ledy] != 0 ) {
       // mled.dot(ledx,ledy); // draw dot
      //  mled.display();
       // delay(50);
    }
      }  
    Serial.println("");
  }
  Serial.println("");
  delay(100);
}

void drawmaze(){
  Serial.println("maze ");
 
  for(int ledy=0;ledy<8;ledy++)
  { 
    // 1st line
    for(int ledx=0;ledx<8;ledx++){
    if(bitRead(maze[ledx][ledy], 3) == 0){ Serial.print("---"); } else { Serial.print("   "); }
    }
    Serial.println("");
    // 2nd line

    for(int ledx=0;ledx<8;ledx++){
    if(bitRead(maze[ledx][ledy], 0) == 1){
        if(bitRead(maze[ledx][ledy], 2) == 1){
          Serial.print("   "); } 
          else 
          { Serial.print("  |"); }
     }
     if(bitRead(maze[ledx][ledy], 0) == 0){
        if(bitRead(maze[ledx][ledy], 2) == 1){
          Serial.print("|  "); } 
          else 
          { Serial.print("| |"); }
     }
    

    
    }
        Serial.println("");
} 
   
    // last line
    int ledy=7;
    for(int ledx=0;ledx<8;ledx++){
    if(bitRead(maze[ledx][ledy], 1) == 0){ Serial.print("---"); } else { Serial.print("   "); }
    }
    Serial.println("");

    
    
  
  //Serial.println("");
  delay(100);
}

void mazegen(){
  visitmatrix[xgen][ygen]=1;       
  StackArray <int> rowStack; 
  StackArray <int> colStack;
        rowStack.push(xgen);
        colStack.push(ygen);
  while(notalldone == 1){    
     visitmatrix[xgen][ygen]=1;
        while(!rowStack.isEmpty()) {
        int count=0;
        //up
        if ( visitmatrix[xgen-1][ygen] == 0 ){
          directions[count]=1;
          count++;    
        }
        //right
        if ( visitmatrix[xgen][ygen+1] == 0 ){
          directions[count]=2;
          count++;    
        }
        //down
        if ( visitmatrix[xgen+1][ygen] == 0 ){
          directions[count]=4;
          count++;    
        }
        //left
        if ( visitmatrix[xgen][ygen-1] == 0 ){
          directions[count]=8;
          count++;    
        }  
        if (showmaze == 1 ){ 
          strip.setPixelColor(displaymatrix[xgen-1][ygen-1], 32, 32, 32);

          strip.show();
          delay(50);
        }
        // no dir found
        if (count == 0 ) {

if (digitalRead(popendsw) == 0 ){
  currentpop = rowStack.count();
  if (currentpop > countpop ) {
    countpop = currentpop;
  xend = xgen-1;
  yend = ygen-1;
  Serial.print("longest pop : ");
  Serial.println(currentpop);
  }
}

         // mled.dot(x-1,y-1);
         // mled.display();
          xgen = rowStack.pop();
          ygen = colStack.pop();

         // Serial.println("popping ");
          } else {
          // count random direction
          int dir = directions[random(count)];
          //Serial.println("push ");
          rowStack.push(xgen); 
          colStack.push(ygen);
         // Serial.print("nr dir : "); 
         // Serial.println(count);
          //delay(100);
         // Serial.println(dir);
          // move 1,1 to 0,0
          //mled.dot(x-1,y-1);
          //mled.display();
          // set direction in maze, dit moet bit set worden
          int mybits = maze[xgen-1][ygen-1];
          int storedir = mybits | dir;
          maze[xgen-1][ygen-1] = storedir;
          if ( dir == 1){
          int getup = maze[xgen-2][ygen-1];
          int storedir = getup | 4;
          maze[xgen-2][ygen-1] = storedir;
          }
          if ( dir == 2){
          int getup = maze[xgen-1][ygen];
          int storedir = getup | 8;
          maze[xgen-1][ygen] = storedir;
          }
          if ( dir == 4){
          int getup = maze[xgen][ygen-1];
          int storedir = getup | 1;
          maze[xgen][ygen-1] = storedir;
          }
          if ( dir == 8){
          int getup = maze[xgen-1][ygen-2];
          int storedir = getup | 2;
          maze[xgen-1][ygen-2] = storedir;
          }


          
        //  maze[x-1][y-1] = dir;
          //set new square
          if (dir == 1){ xgen--; }
          if (dir == 2){ ygen++; }
          if (dir == 4){ xgen++; }
          if (dir == 8){ ygen--; }
          visitmatrix[xgen][ygen]=1;
          //drawmaze();
          }
        }
        notalldone = 0;                                                  //#2
        // if found 0 in 10x10 matrix visited, do
        for(int checkx=0;checkx<10;checkx++){
          for(int checky=0;checky<10;checky++){
            if ( visitmatrix[checkx][checky] == 0 ){
              tmpx=xgen;
              tmpy=ygen;
              notalldone = 1;  
            }
          }
        }
   }
rowStack.push(tmpx); 
colStack.push(tmpy);

}




void setup() {
  pinMode(32, INPUT_PULLUP);
  pinMode(33, INPUT_PULLUP);
  pinMode(25, INPUT_PULLUP);
  pinMode(26, INPUT_PULLUP);
  pinMode(popendsw, INPUT_PULLUP);
  pinMode(trailsw, INPUT_PULLUP);
  pinMode(restartsw, INPUT_PULLUP);
  pinMode(showmazesw, INPUT_PULLUP);
  
  Serial.begin(115200);

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(10);

 unsigned long seed = 0;
for (int i=0; i<32; i++)
{
  seed = seed | ((analogRead(A0) & 0x01) << i);
}
randomSeed(seed);

if (digitalRead(showmazesw) == 0){
  showmaze = 1;
}


mazegen();
drawmaze();
drawmaze2();
 
 strip.fill(0, 0, 64);
  strip.show(); // Initialize all pixels to 'off'
  strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
  strip.setPixelColor(displaymatrix[xend][yend], 0, 255, 0);

  strip.show();

  x=0;
  y=0;
  
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


void reset2start() {
      strip.setPixelColor(displaymatrix[x][y], 0, 0, 0);
    strip.show();
  x = 0;
  y = 0;
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(10);
  strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
  strip.setPixelColor(displaymatrix[xend][yend], 0, 255, 0);
  strip.show();

}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);

  }
}

void loop() {


if (digitalRead(trailsw) == 0 ){
  trail = 32;
} else { trail = 0; }

if (digitalRead(restartsw) == 0 ){
  mode = 2;
} else {
  mode = 0;
}
    int isUp = (bitRead(maze[x][y], 1));
    int isRight = (bitRead(maze[x][y], 2));
    int isDown = (bitRead(maze[x][y], 3));
    int isLeft = (bitRead(maze[x][y], 0));
  
//isUp = 1;
//isDown = 1;
//isLeft = 1;
//isRight = 1;

if (digitalRead(up) == 0) {
   
  if (isUp == 1){

  strip.setPixelColor(displaymatrix[x][y], 0, 0, trail);
  y++;
     drawmaze();
  if ( y > 7) { y=7;}
  strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
  strip.show();
  } else {
    strip.setPixelColor(displaymatrix[x][y], 255, 0, 0);
    strip.show();
    delay(100);
    strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
    strip.show();
    if (mode == 2){ 
      delay(1000);
      reset2start();
    }
  }
}
if (digitalRead(down) == 0) {
  if (isDown == 1){
   
  
  strip.setPixelColor(displaymatrix[x][y], 0, 0, trail);
  y--;
       drawmaze();

  if ( y < 0) { y=0;}
  strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
  strip.show();
  } else {
    strip.setPixelColor(displaymatrix[x][y], 255, 0, 0);
    strip.show();
    delay(100);
    strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
    strip.show();
    if (mode == 2){ 
      delay(1000);
      reset2start();
    }
  }
}
if (digitalRead(left) == 0) {
      drawmaze();
  if (isLeft == 1){
  strip.setPixelColor(displaymatrix[x][y], 0, 0, trail);
  x--;
       drawmaze();

  if ( x < 0) { x=0;}
  strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
  strip.show();
  } else {
    strip.setPixelColor(displaymatrix[x][y], 255, 0, 0);
    strip.show();
        delay(100);
    strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
    strip.show();
    if (mode == 2){ 
      delay(1000);
      reset2start();
    }
  }
}
if (digitalRead(right) == 0) {
      drawmaze();
  if (isRight == 1){
  strip.setPixelColor(displaymatrix[x][y], 0, 0, trail);
  x++;
       drawmaze();

  if ( x > 7) { x=7;}
  strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
  strip.show();
  } else {
    strip.setPixelColor(displaymatrix[x][y], 255, 0, 0);
    strip.show();
    delay(100);
    strip.setPixelColor(displaymatrix[x][y], 0, 0, 255);
    strip.show();
    if (mode == 2){ 
      delay(1000);
      reset2start();
    }
  }
}
if (x ==xend && y == yend){
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  rainbow(20);
}

delay(200);

}

Backtracking maze generation with Arduino

As POC for the maze game.

Maze generation!

Make an initial cell the current cell
   mark it as visited
While there are unvisited cells
   If the current cell has any neighbours
   which have not been visited
         Choose randomly one of the unvisited neighbours
                Push the current cell to the stack
                Mark wall hole
                Make the chosen cell the current cell
                mark it as visited
        Else if stack is not empty
                Pop a cell from the stack
                Make it the current cell

This is my implementation of backtracking

The displaymatrix function is a implementation of different led mappings

Still have to decide where to place endpoint …
At 8,8 or at first stack pop?
Maybe both?

Code

#include <WEMOS_Matrix_LED.h>
#include <StackArray.h>

int directions[4]{};
int notalldone = 1;
int tmpx=0;
int tmpy=0;
int x = 1;
int y = 1;
MLED mled(5); //set intensity=5

int maze[8][8] = { 
  };

int displaymatrix[8][8] = { 
{ 0,1,2,3,4,5,6,7 },
{ 8,9,10,11,12,13,14,15 }, 
{16,17,18,19,20,21,22,23},
{24,25,26,27,28,29,30,31},
{32,33,34,35,36,37,38,39},
{40,41,42,43,44,45,46,47},
{48,49,50,51,52,53,54,55},
{56,57,58,59,60,61,62,63}
};

int visitmatrix[10][10] = { 
  1,1,1,1,1,1,1,1,1,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,0,0,0,0,0,0,0,0,1,
  1,1,1,1,1,1,1,1,1,1
  };

void setup() {
    Serial.begin(115200);
    randomSeed(analogRead(0));
    mazegen();
    drawmaze();
}

void mazegen(){
  visitmatrix[x][y]=1;       
  StackArray <int> rowStack; 
  StackArray <int> colStack;
        rowStack.push(x);
        colStack.push(y);
  while(notalldone == 1){    
     visitmatrix[x][y]=1;
        while(!rowStack.isEmpty()) {
        int count=0;
        //up
        if ( visitmatrix[x-1][y] == 0 ){
          directions[count]=1;
          count++;    
        }
        //right
        if ( visitmatrix[x][y+1] == 0 ){
          directions[count]=2;
          count++;    
        }
        //down
        if ( visitmatrix[x+1][y] == 0 ){
          directions[count]=4;
          count++;    
        }
        //left
        if ( visitmatrix[x][y-1] == 0 ){
          directions[count]=8;
          count++;    
        }  
        // no dir found
        if (count == 0 ) {

          mled.dot(x-1,y-1);
          mled.display();
          x = rowStack.pop();
          y = colStack.pop();

          Serial.println("popping ");
          } else {
          // count random direction
          int dir = directions[random(count)];
          Serial.println("push ");
          rowStack.push(x); 
          colStack.push(y);
          Serial.print("nr dir : "); 
          Serial.println(count);
          //delay(100);
          Serial.println(dir);
          // move 1,1 to 0,0
          mled.dot(x-1,y-1);
          mled.display();
          // set direction in maze, dit moet bit set worden
          int mybits = maze[x-1][y-1];
          int storedir = mybits | dir;
          maze[x-1][y-1] = storedir;
          if ( dir == 1){
          int getup = maze[x-2][y-1];
          int storedir = getup | 4;
          maze[x-2][y-1] = storedir;
          }
          if ( dir == 2){
          int getup = maze[x-1][y];
          int storedir = getup | 8;
          maze[x-1][y] = storedir;
          }
          if ( dir == 4){
          int getup = maze[x][y-1];
          int storedir = getup | 1;
          maze[x][y-1] = storedir;
          }
          if ( dir == 8){
          int getup = maze[x-1][y-2];
          int storedir = getup | 2;
          maze[x-1][y-2] = storedir;
          }


          
        //  maze[x-1][y-1] = dir;
          //set new square
          if (dir == 1){ x--; }
          if (dir == 2){ y++; }
          if (dir == 4){ x++; }
          if (dir == 8){ y--; }
          visitmatrix[x][y]=1;
          drawmaze();
          }
        }
        notalldone = 0;                                                  //#2
        // if found 0 in 10x10 matrix visited, do
        for(int checkx=0;checkx<10;checkx++){
          for(int checky=0;checky<10;checky++){
            if ( visitmatrix[checkx][checky] == 0 ){
              tmpx=x;
              tmpy=y;
              notalldone = 1;  
            }
          }
        }
   }
rowStack.push(tmpx); 
colStack.push(tmpy);
}



void drawmaze(){
  Serial.println("Generating done - Drawing");
  for(int ledx=0;ledx<8;ledx++)
  {
    for(int ledy=0;ledy<8;ledy++){
    Serial.print(maze[ledx][ledy]);  
    if ( maze[ledx][ledy] != 0 ) {
        mled.dot(ledx,ledy); // draw dot
        mled.display();
       // delay(50);
    }
      }  
    Serial.println("");
  }
  Serial.println("");
  delay(100);
}


void loop() {
}

Converting a analog joystick to digital

When you need a large digital joystick, but only got an analog one. You can use below code to make the joystick act as a digital one.

I’ve played with analog joysticks on digital pins also, it can be done. But it can be buggy, and needs extra code.

Note: The joystick pins are marked with 5V, but when you use a Arduino which can only read till 3.3V using its ADC (Analog Digital Convertors), you can get some weird readings.
When moving down and left is reads okay, but up and right react as being connected together!
Just try it with 3.3V or use a resistor.

Above shows a ESP32, but below code has Arduino Nano pin names, change accordingly.

CODE

The code gives you a direction only once, you will need to move the stick to the middle position first and then move again.

Below gave me readings between 0 and 1024 (10 bits)
Hence the between 350 and 650 for the middle position.

Most will give you a reading between 0 and 4096.

Want to set the resolution yourself?

  analogReadResolution(10); // 10 bits
int val1 =0;
int val2 =0;

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    int sensorValue1 = analogRead(A0);
    int sensorValue2 = analogRead(A1);

    if (sensorValue1 > 650){
      if (val1 == 0){ 
      Serial.print("DOWN");
      Serial.println(" ");
      val1=1;
      }
    }
    else if (sensorValue1 < 350){ 
      if (val1 == 0){
       Serial.print("UP");
      Serial.println(" ");
      val1=1;
      }
    }
    else if (sensorValue2 > 350 && sensorValue2 < 650){
      val1=0;
    }
    
    
    if (sensorValue2 > 650){
      if (val2 == 0){ 
      Serial.print("LEFT");
      Serial.println(" ");
      val2=1;
      }
    }
    else if (sensorValue2 < 350){ 
      if (val2 == 0){
       Serial.print("RIGHT");
      Serial.println(" ");
      val2=1;
      }
    }
    else if (sensorValue2 > 350 && sensorValue2 < 650){
      val2=0;
    }

    delay(100);
}

I asked ChatGPT to write code for a Crack the Code game.

The Game

elcome to Crack the Code!
The code has 4 digits. Each digit is between 0 and 9.
You have 10 tries to guess the code.
After each guess, you will be told how many digits you got correct and how many were misplaced.
Good luck!
Enter your guess: 1234
Guess 1: 1234 - 0 correct, 0 misplaced
Enter your guess: 5678
Guess 2: 5678 - 2 correct, 1 misplaced
Enter your guess: 5689
Guess 3: 5689 - 1 correct, 2 misplaced
Enter your guess: 9688 
Guess 4: 9688 - 1 correct, 2 misplaced
....
Sorry, you did not crack the code. The code was 5568.

Generated python code

import random

def generate_code():
    code = ""
    for i in range(4):
        code += str(random.randint(0, 9))
    return code

def check_guess(code, guess):
    correct_digits = 0
    misplaced_digits = 0
    for i in range(4):
        if guess[i] == code[i]:
            correct_digits += 1
        elif guess[i] in code:
            misplaced_digits += 1
    return (correct_digits, misplaced_digits)

def play_game():
    print("Welcome to Crack the Code!")
    print("The code has 4 digits. Each digit is between 0 and 9.")
    print("You have 10 tries to guess the code.")
    print("After each guess, you will be told how many digits you got correct and how many were misplaced.")
    print("Good luck!")
    code = generate_code()
    num_guesses = 0
    while num_guesses < 10:
        guess = input("Enter your guess: ")
        if len(guess) != 4 or not guess.isdigit():
            print("Invalid input. Please enter a 4-digit number.")
            continue
        num_guesses += 1
        correct, misplaced = check_guess(code, guess)
        print(f"Guess {num_guesses}: {guess} - {correct} correct, {misplaced} misplaced")
        if correct == 4:
            print("Congratulations! You cracked the code!")
            return
    print(f"Sorry, you did not crack the code. The code was {code}.")

play_game()

After this i let the AI made some changes, which kinda worked.
Scary nevertheless

Some Arduino hints/tips/workarounds

These are last weeks findings, I will add to this page when I discover other useful things.

Platformio

  • always include “Arduino.h”
  • Order of functions matter! (Not with Arduino IDE)
  • setup serial monitor speed in platformio.ini
    monitor_speed = 115200

Arduino IDE

  • Build error “panic: runtime error: index out of range [3] with length 3” or length 4.
    Code probably correct, build with another board and build+upload with correct board as workaround.

Generic

  • Using a SSD1306 with other pins?
    For example with Adafruit_SSD1306.h
    in setup(){ place
    Wire.begin(5,4);

Infrared Sending Receiving

I’ve build a IR Blaster in the past and tested IR with EspEasy Dev.
I’m not happy with my Harmony Hub. Not so much control as i like to have.

So now OpenMqttGateway with IR.

I’ve installed platformio in the past.
Not really a fan of visual-code i’m showing you the process on CLI

We can’t use the webinstaller or a precompiled binary because we need to change GPIO ports

wget https://github.com/1technophile/OpenMQTTGateway/releases/download/v1.5.0/nodemcuv2-ir-libraries.zip
wget https://github.com/1technophile/OpenMQTTGateway/archive/refs/tags/v1.5.0.tar.gz
tar xzvf v1.5.0.tar.gz
cd v1.5.0/libs
unzip ../../nodemcuv2-ir-libraries.zip
cd ..
edit platformio.ini
remove ; from 
default_envs = nodemcuv2-ir
vi main/config_IR.h
search for LOLIN, edit and change into 0 (GPIO 0 = D3)

build and upload
pio run --target upload --upload-port /dev/ttyUSB0
build and upload with flash erase
pio run --target erase --target upload --upload-port /dev/ttyUSB0

clean the environment when needed
pio run -t clean

Now you will get a Access Point, connect and enter your Wifi network information and mqtt server.

When connected you will see the IR codes and if you installed mqtt support in home assistant it will see the mqtt topics.

I’ve copied a json output as was send by my IR remote.
Below an example using mosquitto_pub to send codes.

mosquitto_pub -t home/OpenMQTTGateway_ESP8266_IR/commands/MQTTtoIR -h mymqttserver -m '{
  "value": 1270235327,
  "protocol": 3,
  "bits": 32,
  "hex": "0x4BB640BF",
  "protocol_name": "NEC",
  "raw": "8908,4498,536,604,510,1700,532,604,512,604,510,1722,510,604,510,1700,534,1700,534,1702,532,606,510,1722,510,1700,534,604,512,1722,510,1722,510,604,512,604,510,1702,532,606,510,606,510,604,510,604,512,604,512,604,510,1722,510,606,510,1722,510,1702,532,1700,534,1722,512,1722,510,1700,534"
}'

platformio commands

Why use platformio?
I know Arduino IDE is easier for starters.
Platformio is far more flexible, you can make projects with their own versions of libraries.
I like CLI, easier to do version control (git)

pip install -U platformio
pio upgrade
pio update

Some screenshots of my visual code platformio

Wemos with Keypad, led and 7 segment display using MQTT

Warning, read the notes!

There are several caveats.

  • I was needing ALL GPIO pins, even RX/TX ( see trick below)
    When doing so, you can’t use serialprint.
    Do NOT enable, your sketch won’t work!
  • Don’t use pullup on D8, you can’t upload to the wemos if you do that
  • Due to library conflicts in keypad.h, DON’T change the order in the source. You will end up with compile errors!
  • The keypad has a weird pinout, but there are similar keypads with alternative layouts. Measure this using a multimeter.
  • The pull-up resistors will help fighting ghost key presses!
  • I2C needs D1/D2

To use RX/TX as GPIO pins you need to do the following:

//Define pins
  int led = 1; //tx
  int col = 3; //rx

// Change to function mode 3
// see https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations
  pinMode(1, FUNCTION_3);
  pinMode(3, FUNCTION_3);
 
// Revert to normal mode
// pinMode(1, FUNCTION0);

// Define mode input/output
// i'm using led to control the led so thats an output
// I'm using col for the keypad column scanner, that's an input
  pinMode(led, OUTPUT);
  pinMode(col, INPUT);

Complete code

The (*) clears input
The (#) sends the pin code using MQTT

Sending a 0 or 1 to escape/keypadin topic will toggle the led

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClient.h>
// Do not change order!
#include "Keypad.h"
#include <TM1637Display.h>
#include <Ethernet.h>

#define wifi_ssid "SSID"
#define wifi_password "SSIDPASS"
#define mqtt_server "MQTTSERVER"
#define mqtt_port 1883

#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns

int led = 1; //tx
int col = 3; //rx

#define CLK D1
#define DIO D2
#define TEST_DELAY   2000
TM1637Display display(CLK, DIO);

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {D3, D5 , D6 , D7 };
byte colPins[COLS] = {D4 , col, D8 };

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int keyNum = 0;
WiFiClient espClient;
EthernetClient ethClient;

PubSubClient mqtt(espClient);

void setup_wifi() {
  delay(10);
  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}


void setup() {
  setup_wifi();
  mqtt.setServer(mqtt_server, mqtt_port);
  mqtt.setCallback(callback);
  
  pinMode(1, FUNCTION_3);
  pinMode(3, FUNCTION_3);
  pinMode(led, OUTPUT);
  pinMode(col, INPUT);
  // using above? .. then disable serial!
  // Serial.begin(9600);
display.showNumberDec(0, true);
delay(TEST_DELAY);
WiFiClient espClient;
PubSubClient mqtt(espClient);

 mqtt.setClient(espClient);
 mqtt.setServer(mqtt_server, 1883);
    
mqtt.setCallback(callback);
mqtt.subscribe("escape/keypadin");

}

void callback(char* topic, byte* payload, unsigned int length) {
    //  digitalWrite(led, HIGH);

    String topicStr = topic;
      byte value = atoi((char*)payload);
       if (value == 1){
    digitalWrite(led, HIGH);

 }else if (value == 0){
    digitalWrite(led, LOW);
 }
    

}

void reconnect() {
  while (!mqtt.connected()) {
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    if (mqtt.connect(clientId.c_str())) {
      mqtt.publish("outTopic", "hello world");
      mqtt.subscribe("escape/keypadin");
    } else {
      delay(5000);
    }
  }
}



void loop() {

  if (!mqtt.connected()) {
    reconnect();
  }
    mqtt.loop();

  // put your main code here, to run repeatedly:
  char key = keypad.getKey();

  if (key) {

    if(key=='*'){
      keyNum = 0;
    } else if (key=='#'){
         ++value;
  snprintf (msg, MSG_BUFFER_SIZE, "#%1d", keyNum);
        mqtt.publish("escape/keypad", msg);
    }
    
       else{
      if(keyNum<=999){
        keyNum = (keyNum*10) + (int(key)-48);
      }
    }
    
    //  Serial.println(key);
    display.setBrightness(0x0f);

    uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };
    display.setSegments(data);

    display.setSegments(data);
    display.showNumberDec(keyNum);

  }

}

Locate and delete file copies

A simple script to locate files and select which to delete.
Uses md5sum to compare
(just be sure locatedb is up to date!)

./locatemd5 VID_20130926_211302.mp4
1 : 125a65e830c1f3654714daa0f8a41699  /tank/Backup/Nae Bother Drive/Movies/VID_20130926_211302.mp4
2 : 125a65e830c1f3654714daa0f8a41699  /tank/Backup/rclonegdrive/Nae Bother Drive/Movies/VID_20130926_211302.mp4
3 : 125a65e830c1f3654714daa0f8a41699  /tank/Private/Henri/_All online drives/Google Drive Henri/Nae Bother Drive/Movies/VID_20130926_211302.mp4
4 : 125a65e830c1f3654714daa0f8a41699  /tank/Private/gfx/Gsm/GSM Henri/nexus/2013/20130926/VID_20130926_211302.mp4
5 : 125a65e830c1f3654714daa0f8a41699  /tank/Private/Work Directory/Sorted/movies/own/VID_20130926_211302.mp4
6 : 82cd340b2b54d3ef65a02c8f31b04970  /tank/Private/www/cutshort/VID_20130926_211302.mp4
---------- which to delete ---------------
1 2 3 5
delete 1 /tank/Backup/Nae Bother Drive/Movies/VID_20130926_211302.mp4
rm: remove regular file '/tank/Backup/Nae Bother Drive/Movies/VID_20130926_211302.mp4'? y
delete 2 /tank/Backup/rclonegdrive/Nae Bother Drive/Movies/VID_20130926_211302.mp4
rm: remove regular file '/tank/Backup/rclonegdrive/Nae Bother Drive/Movies/VID_20130926_211302.mp4'? y
delete 3 /tank/Private/Henri/_All online drives/Google Drive Henri/Nae Bother Drive/Movies/VID_20130926_211302.mp4
rm: remove regular file '/tank/Private/Henri/_All online drives/Google Drive Henri/Nae Bother Drive/Movies/VID_20130926_211302.mp4'? y
delete 5 /tank/Private/Work Directory/Sorted/movies/own/VID_20130926_211302.mp4
rm: remove regular file '/tank/Private/Work Directory/Sorted/movies/own/VID_20130926_211302.mp4'? y

Bash Script

#!/bin/bash
# below gets array from subshell
shopt -s lastpipe

nr=1
locate "$1" | while read file ; do
        md5sum=$(md5sum "$file")
        echo  "$nr : $md5sum"
        myArray[$nr]="$file"
        (( nr++ ))
done
echo  "---------- which to delete ---------------"
read numbers
for f in $numbers ; do
 echo "delete $f ${myArray[$f]}"
 rm -i "${myArray[$f]}"
done

Magic Mirror no more .. lets reuse the display

I never look in the mirror, so why do i need one?
The mirror foil was already ugly in the corners.
There were bumps.
Never finished a proper interface

This one was made using a touch screen, so there are always fingerprints you could see

I’m going to use the display for an in-house small Escape Room idea i have.

Only the time part still worked, but i could not find the right cables for the touch part. The buttons displayed are meaningless anyway 🙂
Just a mockup

Mirror part was done using a Safety/One way mirror foil.
Cut a part as large as you screen, spray a little water and stick it on.

At some point i displayed Dashticz on there.
Apparently i started playing with a magic mirror setup in 2015, according some timestamps of code on my fileserver.