Are there limitations on the length of strings passed to php by python in a service? I had a php service running that retrieved data from a python script. Worked well. I decided to add data from another python script.
I tried several ways and all failed. I retrieved each data and concatenated in the php script, before writing to a text file. I tried calling one python script from another python script, called in turn from the php service script, and concatenating the strings in the calling python script, thus sending a single string back to the php service. No matter what happens, or what strategy or configuration I use, the data from one or both python scripts is lost if I try to get both.
I finally combined the two python scripts into a single script. Now all the data is sent to php but lost before being written to file. I can run the files in almost any configuration from the command line, and all will work. Easily. The scripts also run well with setsid. But once they're placed in a service, the problems arise. The data returned are string representations of numbers, The readings from an analog-to-digital converter, and calculations of the current solar azimuth and elevation. As i said, before this, I could get one of the python sctipts data printed to file.
I get the impression I'm running into a length boundary of some kind.
The possible combinations of variables is so great, I decided to check the stack exchange before going further.
I run the scripts on a Raspberry Pi 3B+ with the Raspbian Stretch operating system from my Windows laptop using Mobaxterm in SSH sessions over the Raspberry Pi wifi access point or household wifi.
This is a sample of the solar controller's data:
10:17:00,10:17:00 AM,2018-06-29,46.64,4.21,196.77,25.39,7.75,196.77,24
The first four here are ADC reads, the last four, calculations:
1.827,1.7,1.697,1.949,285.6145,18.655, 0.679704001401382, 0.031167595280969528
A0, A1, A2, A3, Azimuth, Elevat., Sm, Si
This data is written to file immediately after the controller data. All once every thirty seconds.
Briefly, the php script makes a call to python to collect data, then collects data from a charge controller. The controller data is formatted and written to a text file. At the end of the script, the python data is written after the controller data, to the same file and in csv format with a header, rows and columns. The python data form the last eight columns of the table. This is repeated every 30 seconds throughout the day.
Here are the files:
<?php
/* epoutX.php */
require_once 'PhpEpsolarTracer.php';
$tracer = new PhpEpsolarTracer('/dev/ttyXRUSB0');
error_reporting(0);
$y = 1;
$x = 3;
while($x = 3):
$myfile = fopen('/var/www/html/data_'.date('m-d-Y').'.txt', "a") or die("Unable to open file!"."\n");
$pythonephemsensor1 = `/usr/bin/env python3.5 /var/www/html/resources/ephem-sensor.py`;
$pythonephemsensor = preg_replace("/\r|\n/","",$pythonephemsensor1);
$nano = time_nanosleep(30, 0); // ( sec, nanoseconds[<1x10^9] )
if ($tracer->getRealtimeData()) {
$i=0;
$date=date("Y-m-d H:i:s");
$seconds = strtotime($date);
$seconds /= 30;
$seconds = round($seconds);
$seconds *= 30;
$date2 = date("h:i:s", $seconds);
$date = date("h:i:s A", $seconds);
$today = date("Y-m-d"); # Y-m-d
$now = $date;
#______________________________________
fwrite($myfile, $date2);
fwrite($myfile, ",");
fwrite($myfile, $now);
fwrite($myfile, ",");
fwrite($myfile, $today.",");
#---------------------------------------->
while ($i<=5):
$dat[$i] = $tracer->realtimeData[$i].",";
fwrite($myfile, $dat[$i]);
$i++;
endwhile;
$i=12;
$dat[$i] = $tracer->realtimeData[$i].",";
fwrite($myfile, $dat[$i]);
}
fwrite($myfile, $pythonephemsensor.",\n");
endwhile;
fclose($myfile);
?>
The sensor read code was only eight lines, and was added to the calculations code. The sensor read code is at the top of the Python script:
Python script:
#!/usr/bin/python
# ephem-sun
import math
import ephem
import time
from time import gmtime, strftime, localtime
import Adafruit_ADS1x15
# begin sensor reading
adc01 = Adafruit_ADS1x15.ADS1115(address=0x49)
GAIN = 1
lav = 4.1/32767
values = [0]*4
for i in range(4):
values[i] = round(adc01.read_adc(i, gain=GAIN, data_rate=128)*lav, 3)
#print('{0:},{1:},{2:},{3:}'.format(*values))
# end sensor reading
# begin solar calculations
r=360/2/3.14159;
home = ephem.Observer()
home.lat, home.lon ='36.3401','-82.1950'
radians = r
sun = ephem.Sun()
sun.compute(home)
s_az1 = repr(sun.az*radians)
s_alt1 = repr(sun.alt*radians)
s_az = float(s_az1)
s_alt = float(s_alt1)
s_az = round(s_az,4)
s_alt = round(s_alt,4)
r=360/2/3.14159
a=0.14
h=0.535
ah=a*h
alpha=s_alt #40.5603 # Sel
beta=45
Z=90-alpha #49.4397 # 90-alpha
psi=180
theta=s_az #175.9508 # Saz
AM=1/(math.cos(Z/r)+0.50572*pow(96.07995-Z/r,-1.6364))
Si=1.353*((1-ah)*pow(0.7,pow(AM,0.678))+ah)
Sm=Si*(math.cos(alpha/r)*math.sin(beta/r)*math.cos((psi-theta)/r)+math.sin(alpha/r)*math.cos(beta/r))
s_az2 = str(s_az)
s_alt2 = str(s_alt)
Si2 = str(Si)
Sm2 = str(Sm)
#print(s_az2+','+s_alt2+','+Si2+','+Sm2)
#print('{0:},{1:},{2:},{3:}'.format(*values)
# Print both data sets to PHP
print('{0:},{1:},{2:},{3:}'.format(*values)+","+s_az2+','+s_alt2+','+Si2+','+Sm2),"\n"
# end solar calculations
End of code.