1

As the title suggests, I have a code that runs perfectly on my laptop. It connects to my solar inverter, grabs the information, and writes(appends) them to a CSV file.

When I run the same code through ssh on raspberry Pi, it runs without the error most of the time, however, it does not write(append) to the CSV file.

the code is as follow:

import json
import urllib.request
import csv

# api call
path = 'MY IP/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'

# Initialising the values, if there is an error, these would be reported.
PAC = 'Nan'  #Eenrgy generated @ this moment 

with urllib.request.urlopen(path) as url:
    data = json.loads(url.read().decode()) # reading json file

json_keys = [x for x in data.keys()] 

head = data[json_keys[1]]      # getting the content of the head
timestamp = head['Timestamp']  # Reading Time
date = timestamp[:timestamp.index('T')]                        #the date
time = timestamp[timestamp.index('T')+1: timestamp.index('+')] #the hour

body = data[json_keys[0]] # Body has only 'Data' in it
body = body['Data']       # Getting the content of the Data in the Body

PAC = body['PAC']['Value']  #Eenrgy generated @ this moment 
otpt_lst = [time, PAC]

with open(str(date)+'.csv', 'a', newline='') as csvfile:
    slr_wrtr = csv.writer(csvfile, delimiter=',')
    slr_wrtr.writerow(otpt_lst)

Both are running in Python3, the python3 in my machine is more recent, in RBpi it is 3.5.8, and on my machine, it is 3.7.4. The way I set it up is, I am using RBpi to be SMB. I use my laptop to access the folder in External HDD attached to rbpi.

When I run the code on the attached HDD in my machine, I never ran into problems. when I do so on the RBPI, sometimes I get issues, not being able to read timestamp, etc, which are strange but fine for now.

Regardless, the main issue is when rbpi finishes executing the code, if it is successful, it does not write to the CSV file.

I have checked my permissions on the folder, and I have drwx access. I have tried to write instead of append, changed the filename, etc.

A sample of the JSON file I am reading from is http://www.filedropper.com/getinverterrealtimedatacgi

Appreciate it if someone can point me in the right direction on how to troubleshoot this, please.

  • I have -rw-r--r-- access to the file. – Amin Edalat Apr 26 at 2:03
  • write some fixed text to the file ... that will tell you if the problem is with writing to file or a problem with lack of data – jsotola Apr 26 at 3:19
  • Add print statements to see what your code is doing. – CoderMike Apr 26 at 3:45
  • I think your comment: "...when I do so on the RBPI, sometimes I get issues, not being able to read timestamp, etc, which are strange but fine for now" may be the key here. Indeterminate results are, in my experience, always an indicator of a deeper problem. Fix this problem and I suspect you'll fix the one you are asking about. – jwh20 Apr 26 at 15:03
  • By the way, I also have a Fronius Inverter and I tried your code on my system and it fails with the same "timestamp" error (line 19) regardless of whether I run it on my MacBook or a Pi Zero W running Raspbian. – jwh20 Apr 26 at 15:06
1

After looking closely at your code, which is quite hard to follow, by the way, it appears you are simply trying to log the hour and the PAC value to your CSV file.

Let me suggest the following simpler code which is working for me on both the MacBook and the Pi:

import json
import csv
import requests

# api call
INVERTER_URI = 'http://10.0.0.15/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'

CURRENT_DATA = requests.get(INVERTER_URI)

if CURRENT_DATA.status_code != 200:
    raise SystemError

CURRENT_DATA_JSON = json.loads(CURRENT_DATA.text)

TIMESTAMP = CURRENT_DATA_JSON.get('Head').get('Timestamp')
DATE = TIMESTAMP.split('T')[0]
HOUR = TIMESTAMP.split('T')[1].split(':')[0]

PAC = CURRENT_DATA_JSON.get('Body').get('Data').get('PAC').get('Value')

with open(str(DATE)+'.csv', 'a', newline='') as csvfile:
    slr_wrtr = csv.writer(csvfile, delimiter=',')
    slr_wrtr.writerow([HOUR, PAC])
| improve this answer | |
  • Thanks jwh20, it was my first time working with json files, using get and requests made it much easier to work with the file. Yeah, I was looking for PAC, IAC, etc and the date. I tried your code and it runs better. So far the only way I could resolve the issue I had is by copy pasting my code to a new text file, created by osmc on raspberry pi. (hope it makes sense). For now, I am good, thanks again, and if you have any recommendation on how to make my code easier to follow, I would be happy to hear it. – Amin Edalat Apr 28 at 0:12
  • Use requests vs. urllib. The requests library is much simpler. JSON handling can be a bit confusing at first but before long you'll get the hang of it. What are you doing with the PAC data you're collecting? I post mine to PVOutput.org, which is a great resource that tracks how you're doing. If you can read the JSON data out of the Fronius inverter, you can create a JSON document to post to PVOutput too. Have a look at it! – jwh20 Apr 28 at 0:49
  • @AminEdalat By the way, if this answer helped you, please accept it as an answer to your question. Thanks. – jwh20 Apr 28 at 1:42
  • Thanks jwh20, sure, will do. My hope is to create my own dataset. I came across PVOutput.org, I may send data there too, but at the moment, I am trying to store it locally and try to practice my Python skills on it. – Amin Edalat Apr 28 at 5:58
0

To anyone how comes later on to this thread, one issue was what jwh20 helped me with.

But the main issue I was facing was, being new to Linux, I did not understand where the files are going. So When I run my python code on Windows environment, Windows runs the code and outputs the files in the CURRENT WORKING DIRECTORY.

In Linux however the resulting files are going to my home folder which was located under /home/osmc for me, so the code was working fine but it dumps the CSV file in a different location.

Well, embarrassing but good learning for me. Maybe if I were to use OS and force the change of directory it would have worked better, but currently, I am happy with the way things are, specially now that I know what is where.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.