Linux service + saving logs into a file

Viewed 35

Using RPi, I am sending and receiving data to and from ESP32 at the same time. I have two .py scripts and two linux services. Before I created the services the .py scripts were working correctly (but not at the same time, that's why I am making services) and the rec_data.log contained data from ESP32 (asdfg).

They work in parallel (checked via systemctl status for both services), also ESP32 is getting the message from RPi. But the part with saving data from ESP into a .log file on RPi is not working.

I used StandardOutput=... and the rec_data.log file is getting created, but remains empty. It should get a "asdfg" string per 5 seconds.

My receive.service looks as follow:

[Unit]
Description=Test Service

[Service]
ExecStart=/usr/bin/python3 /home/pi/receive.py
StandardOutput=append:/home/pi/rec_data.log # tried without these two lines also (didnt work)
StandardError=append:/home/pi/rec_err.log # tried without these two lines also (didnt work)

[Install]
WantedBy=multi-user.target

My receive.py script looks as follow:

import sys
from bluetooth import *
#file = open("rec_data.log", "a") // this was used before the service to save into a file
def rx_and_echo():
    while True:
        data1 = sock.recv(buf_size)
        if data1:
            data1 = str(data1)
            data1 = data1[2:-1]
            #file.write(data1 + '\n') // this was used before the service to save into a file
            print(data1) // this two lines should save log into a .log file from the running service
            sys.stdout.flush()

addr = "E0:E2:E6:CF:BB:AA"
service_matches = find_service(address=addr)
buf_size = 1024

if len(service_matches) == 0:
    print("couldn't find the SampleServer service =(")
    sys.exit(0)

print('connected')

first_match = service_matches[0] 
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]

port = 1 
sock = BluetoothSocket(RFCOMM)
#sock.connect((host, port)) // this is done on send.py so cannot be doubled here

rx_and_echo()
sock.close()

My ESP script:

#include "BluetoothSerial.h"
#include <LiquidCrystal.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String commandString = "";

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

static const char buffer[] = "asdfg";
const size_t buffer_size = sizeof(buffer) - 1;

void loop() 
{
  SerialBT.write((const uint8_t*)buffer, buffer_size); // sending to RPi

  String RPiData = SerialBT.readString(); // receiving from RPi
  Serial.println(RPiData); 
  delay(1000);
 }

Imo there is some problem with .ino file. Like it is not sending the string correctly.

0 Answers
Related