Octoprint Installation problem on Ender 3

I am trying to install klipper on my ender 3 with 4.2.2 board using octopi running on my raspberry pi 3B+. I have used the following menu settings to make firmware.

# STM32F103 with a "28KiB bootloader" and serial (on USART1 PA10/PA9)
# communication.

I have flashed the firmware and also coppied the sample config but it seems the printer is not connecting to octopi or it quits after connecting with the following error.

10:08:06 Connected to host via /tmp/printer @0bps
10:08:06 Lost communication with MCU 'mcu'
10:08:06 Lost communication with MCU 'mcu'
Once the underlying issue is corrected, use the
"FIRMWARE_RESTART" command to reset the firmware, reload the
config, and restart the host software.
Printer is shutdown
10:08:06 Lost communication with MCU 'mcu'
10:09:36 Lost communication with MCU 'mcu'

I have also checked the sdcard again to see if the firmware file has been renamed to extension .cur but it is still .bin. Why so? the following is the log from gcode viewer addon

Changing monitoring state from "Offline" to "Detecting serial connection"
Performing autodetection with 7 port/baudrate candidates: /tmp/printer@115200, /tmp/printer@250000, /tmp/printer@230400, /tmp/printer@57600, /tmp/printer@38400, /tmp/printer@19200, /tmp/printer@9600
Trying port /tmp/printer, baudrate 115200
Connecting to port /tmp/printer, baudrate 115200
Handshake attempt #1 with timeout 2.0s
Connected to: Serial<id=0x6c10acf0, open=True>(port='/tmp/printer', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=2.0, xonxoff=False, rtscts=False, dsrdtr=False), starting monitor
Send: N0 M110 N0*125
Recv: // Lost communication with MCU 'mcu'
Recv: // Once the underlying issue is corrected, use the
Recv: // "FIRMWARE_RESTART" command to reset the firmware, reload the
Recv: // config, and restart the host software.
Changing monitoring state from "Detecting serial connection" to "Operational"
Send: N0 M110 N0*125
Recv: // Printer is shutdown
Recv: !! Lost communication with MCU 'mcu'
Changing monitoring state from "Operational" to "Error"
Send: M112
Send: N1 M112*32
Send: N2 M104 T0 S0*35
Send: N3 M140 S0*102
Changing monitoring state from "Error" to "Offline after error"
Connection closed, closing down monitor

Can anyone help me figure out why this happens. Also sometimes when i restart klipper service from terminal, the printer connects but as soon as i execute any function it disconnects with same error.

I tried Fluidd but getting the same error

Shutdown due to webhooks request
Once the underlying issue is corrected, use the
"FIRMWARE_RESTART" command to reset the firmware, reload the
config, and restart the host software.
Printer is shutdown

Please post your klipper log.

Actually somehow its working now but not stable with octoprint. Klipper log showed almost the same thing. But i am facing another issue right now. Hope you could help me with that.
I am trying to add the standard cancel_print marcos to my printer but this returning a error and shutting down the firmware.

The marcos that I am trying to add is

[gcode_macro CANCEL_PRINT]
gcode:
   M220 S100 ; Reset Speed factor override percentage to default (100%)
   M221 S100 ; Reset Extrude factor override percentage to default (100%)
   G91 ; Set coordinates to relative
   {% if printer.extruder.temperature >= 170 %}
      G1 F1800 E-1 ; Retract filament 3 mm to prevent oozing
   {% endif %}

   ;if all axis are homed, lift the hotend to leave room for hot filament to ooze and to keep it clear of the bed.
   {% if printer.toolhead.homed_axes == "xyz" %}
      G1 F6000 Z10 ; Move Z Axis up 10 mm to allow filament ooze freely
      G90 ; Set coordinates to absolute
      G1 X0 Y221 F1000 ; Move Heat Bed to the front for easy print removal
      M84 ; Disable stepper motors
   {% endif %}

   ;set part fan speed to zero.
   M106 S0
   ;bed and hotend are left at the print temps in case I want to restart.

The error I get is

gcode command CANCEL_PRINT already registered

The logs are as follows
klippy.log (74.6 KB)
https://pastebin.ubuntu.com/p/jJPdwpzNjQ/plain/

I think I have found a solution even when I dont know what is the problem. The solution is that I have to sent command echo FIRMWARE_RESTART > /tmp/printer from the octoprint terminal not once but TWICE and then connect the printer from the octoprint web UI. This works all the time. So to automate this, I have made a shell script with the code

#!/bin/sh
sleep 5
echo FIRMWARE_RESTART > /tmp/printer
sleep 5
echo FIRMWARE_RESTART > /tmp/printer

I place this in /home/pi directory and then using the events option in octoprint, run this script when the octoprint server comes online and also when there is an error reported from the firmware.