Add new line in script output

Started by Muskoka, November 15, 2014, 09:44:58 AM

Previous topic - Next topic

Muskoka

New'ish linux user here, need assistance. I've searched the net, and not come up with a solution.

I'm using RossW's newmodbus program on my pi to get data from my Classic 150 lite. All I'm having it do is write to a text file. I've set it up as a cron job to run every 15 minutes, and it works just fine.

The issue I have, is the "written" data in the text file is not separated, with a blank line. How do I tell the script to add a new line between data writes.

Here is my script (lite.sh):

#!/bin/sh
./newmodbus -p 192.168.0.233 >> /home/pi/classicdata.txt

Here is my cron job:

*/15 * * * * /home/pi/scripts/lite.sh

Here is the output (I changed the cron job to write every minute, normally 15 minutes):

Lite Display Panel 1.0.19, RossW
State BulkMppt
Firmware 1849
ClassicTime 09:39:24  15/11/2014
   13 Watts out
12.5 Volts (Battery)
35.0 Volts (PV)
  1.1 Amps (Battery)
  0.0 kWh today (3 amphours)
Lite Display Panel 1.0.19, RossW
State BulkMppt
Firmware 1849
ClassicTime 09:40:23  15/11/2014
   11 Watts out
12.5 Volts (Battery)
34.8 Volts (PV)
  0.9 Amps (Battery)
  0.0 kWh today (3 amphours)

Thanks for any input.

Glen

Off grid - 24v System - Eco Worthy LiFePO4 12V 280ah x2 - Classic Lite 150 with MNGP wBjr - 1540w solar -  12v System - Antra Power 12v 210ah LiFePO4 - Morningstar Tristar MPPT 60 - 820w Solar - Honda / Firman generators

TomW

#1
I think you are using newmodbus wrong.

Just let it log to a logfile of its own design

pass it a directory and it will write a log file with the Classics serial #

This writes the registers to a file in the specified directory. Looks like this:

newmodbus -l /USB/ 192.168.1.233 will have it write the log to a file with the SN as a name.

Mine is a bit different as I request specific registers.:

newmodbus -l  /USB/ 192.168.2.104 #4115/10 4117/10 4118/10 4119


tomw@pi ~ $ cat 13234.log
09:58:10 15/11/2014 u=13234&ID=SOLAR2&4115=25.5&4116=75.6&4117=2.5&4118=0.1&4119=64&4120=4&4121=1.0&4122=88.2&4125=4&4132=6505.1&4133=35.2&4134=37.2&4275=5&4130=0x1004&4131=0xb200&4341=0x0000&4342=0x0000&4343=0x0000&4344=0x0000
10:00:11 15/11/2014 u=13234&ID=SOLAR2&4115=25.4&4116=80.2&4117=1.3&4118=0.1&4119=33&4120=4&4121=0.5&4122=87.8&4125=4&4132=6505.1&4133=35.4&4134=37.6&4275=5&4130=0x1004&4131=0xb200&4341=0x0000&4342=0x0000&4343=0x0000&4344=0x0000



I use other scripts to use the jammed together data.

Tom


:





Do NOT mistake me for any kind of "expert".

( ͡° ͜ʖ ͡°)


24 Trina 310 watt modules, SMA SunnyBoy 7.7 KW Grid Tie inverter.

I thought that they were angels, but much to my surprise, We climbed aboard their starship and headed for the skies

Muskoka

#2
Thanks Tom, but it works fine as is, and does what I want. I was just looking for a way to add a blank line between the timed writes so it's not all squished together. This is a old Raspberry "A" I had sitting around doing nothing, so all I want it to do is take this 15 minute snapshot of the system and save it.

Lite Display Panel 1.0.19, RossW
State BulkMppt
Firmware 1849
ClassicTime 10:58:23  15/11/2014
  194 Watts out
14.6 Volts (Battery)
35.7 Volts (PV)
13.2 Amps (Battery)
  0.3 kWh today (22 amphours)
                                                           <------"""I want this space added between writes"""
Lite Display Panel 1.0.19, RossW
State BulkMppt
Firmware 1849
ClassicTime 10:59:23  15/11/2014
  175 Watts out
14.4 Volts (Battery)
35.1 Volts (PV)
12.1 Amps (Battery)
  0.3 kWh today (22 amphours)
                                                      <------"""I want this space added between writes"""
Lite Display Panel 1.0.19, RossW
State BulkMppt
Firmware 1849
ClassicTime 11:00:24  15/11/2014
  132 Watts out
14.1 Volts (Battery)
35.0 Volts (PV)
  9.4 Amps (Battery)
  0.3 kWh today (22 amphours)

Glen
Off grid - 24v System - Eco Worthy LiFePO4 12V 280ah x2 - Classic Lite 150 with MNGP wBjr - 1540w solar -  12v System - Antra Power 12v 210ah LiFePO4 - Morningstar Tristar MPPT 60 - 820w Solar - Honda / Firman generators

TomW

#3
add an "echo "" >>logfile after you gather the data:

Actually before or after would work.


./newmodbus -p 192.168.0.233 >> /home/pi/classicdata.txt
echo "">> /home/pi/classicdata.txt


Should add a newline return to the file where you want it (I think).

Tom
Do NOT mistake me for any kind of "expert".

( ͡° ͜ʖ ͡°)


24 Trina 310 watt modules, SMA SunnyBoy 7.7 KW Grid Tie inverter.

I thought that they were angels, but much to my surprise, We climbed aboard their starship and headed for the skies

Muskoka

Thanks so much Tom, that worked beautiful.

I've been reading about echo for 2 days now, couldn't figure out how to implement it. I still have a lot to learn about linux and writing scripts. Now that's out of the way I'll explore doing more with the data.

Although, with mymidnite up and running great, I don't know if I'll need anything else, still fun to play around though. As long as the data is logged, and I can can view my system remotely, I'm set.

Glen
Off grid - 24v System - Eco Worthy LiFePO4 12V 280ah x2 - Classic Lite 150 with MNGP wBjr - 1540w solar -  12v System - Antra Power 12v 210ah LiFePO4 - Morningstar Tristar MPPT 60 - 820w Solar - Honda / Firman generators

Muskoka

Actually Tom, something strange is happening.

At one minute intervals it works fine, there is one blank line between writes.

I've now changed the cron job to run every fifteen minutes, and there's nineteen blank lines between writes. How would I get around this, and why is it doing that?

Glen
Off grid - 24v System - Eco Worthy LiFePO4 12V 280ah x2 - Classic Lite 150 with MNGP wBjr - 1540w solar -  12v System - Antra Power 12v 210ah LiFePO4 - Morningstar Tristar MPPT 60 - 820w Solar - Honda / Firman generators

TomW

Quote from: Muskoka on November 15, 2014, 03:40:41 PM
Actually Tom, something strange is happening.

At one minute intervals it works fine, there is one blank line between writes.

I've now changed the cron job to run every fifteen minutes, and there's nineteen blank lines between writes. How would I get around this, and why is it doing that?

Glen

Probably a crond script error?

This line will run the script every 15 minutes:

0,15,30,45 * * * * /path/to/command

*/15 * * * * /path/to/command might work, too. It varies between distributions so you might have different results.

Paste in your crontab contents "crontab -l" will show the contents maybe I can sort it out.

Tom
Do NOT mistake me for any kind of "expert".

( ͡° ͜ʖ ͡°)


24 Trina 310 watt modules, SMA SunnyBoy 7.7 KW Grid Tie inverter.

I thought that they were angels, but much to my surprise, We climbed aboard their starship and headed for the skies

TomW

to check when the file is being written you can run "tail -f /path/to/file" in a terminal and see when the lines get written to the file. It could be that it is not getting data from the Classic and the command falls through and writes the echo to the file? You can add a "&&" to the end of the newmodbus line to only let it continue the script if successful.

Tom



Do NOT mistake me for any kind of "expert".

( ͡° ͜ʖ ͡°)


24 Trina 310 watt modules, SMA SunnyBoy 7.7 KW Grid Tie inverter.

I thought that they were angels, but much to my surprise, We climbed aboard their starship and headed for the skies

Muskoka

#8
Seems to be working fine now, still testing.

I had the script in a directory named "scripts", and I moved it to my home directory, /home/pi. The output file, data.txt resides there as well.

I think there may have been permission issues (?), because every time I'd change crontab it was hit or miss if it would even run. Now that I've moved the script it seems fine.

I've tested at one,two,five, and ten minute intervals, and it writes to the output file fine, with only one line between writes. I'm testing fifteen minute writes now, as this was the time frame before that produced the big gap between writes.

Here's the crontab (crontab -e):

*/15 * * * * /home/pi/data.sh

Here's the script (data.sh):

#!/bin/bash
./newmodbus -p 192.168.0.233 >> /home/pi/data.txt
echo "">> /home/pi/data.txt

Here's the output with a fifteen minute interval (data.txt):

Lite Display Panel 1.0.19, RossW
State Resting because Too low of power out
Firmware 1849
ClassicTime 17:14:23  15/11/2014
    0 Watts out
12.4 Volts (Battery)
  4.7 Volts (PV)
  0.0 Amps (Battery)
  0.6 kWh today (45 amphours)

Lite Display Panel 1.0.19, RossW
State Resting because Too low of power out
Firmware 1849
ClassicTime 17:29:23  15/11/2014
    0 Watts out
12.4 Volts (Battery)
  4.0 Volts (PV)
  0.0 Amps (Battery)
  0.6 kWh today (45 amphours)

Seems to be ok. Thanks for the help Tom.

Glen

Edit: Think I'll work now on getting four writes (one hour) side by side in the output file, instead of one below the other. Also like to have it stop writing to the file while the system is "resting".
Off grid - 24v System - Eco Worthy LiFePO4 12V 280ah x2 - Classic Lite 150 with MNGP wBjr - 1540w solar -  12v System - Antra Power 12v 210ah LiFePO4 - Morningstar Tristar MPPT 60 - 820w Solar - Honda / Firman generators