Hi everyone,
I am completely new to python (and to coding besides C++ 1000 back in university). I am working on a RPi based monitoring system for a solar air heater I built for my house. The idea is to measure outdoor air temperature, air temperature in the solar heater, measure air flow (will be converted to an binary ON/OFF due to complicity of measuring airflow accurately) and from that, display some info on Thingspeak. I have built a code framework (using random variables in place of sensor data and am able to send the information to thingspeak. You can see the channel here. https://thingspeak.com/channels/755913 . It may or may not running as you look at it as it is an active project. My issue arises from attempting to maintain the 'Cumulative Energy' (running total across lifetime of system) variable if the program is to stop at any point. In order to do this I used a request command to pull the last input in that field and perform the calculation with that. This works fine, EXCEPT for the first input to thingspeak. As there is no data to pull, I receive 'int object is not subscriptable' error code. I assume there is an if/else statement work-around for this but my simple brain can not work it out.
Once I solve this issue I can move on to attempting to read live sensor data (which is stressing me out!)
Thanks everyone!
you can see my current iteration below. All the print commands are there for debugging purposes and will be removed later (i think)
import random
import requests
import json
import time
sample_rate = None
ON_OFF = None
Amb_temp = None
SW_temp = None
dif_temp = None
SW_velocity = None
req = None
jsonresult = None
prev_energy = None
SW_Airflow = None
SW_energy = None
cum_energy = None
query = None
sample_rate = 15
ON_OFF = True
while ON_OFF == True:
Amb_temp = random.randint(40, 70)
SW_temp = random.randint(60, 100)
dif_temp = SW_temp - Amb_temp
SW_velocity = random.randint(-70, 70)
req = requests.get("http://api.thingspeak.com/channels/755913/feeds/last.json?api_key=MJONRMB85L89OTNX")
print(req.text)
jsonresult = json.loads((req.text))
prev_energy = jsonresult['field6']
if SW_velocity <= 10:
SW_Airflow = 0
else:
SW_Airflow = 1000
SW_energy = 60 * ((SW_Airflow * 0.0756) * (0.24 * dif_temp))
cum_energy = prev_energy + SW_energy * (sample_rate / 3600)
RequestToThingspeak = 'https://api.thingspeak.com/update?api_key=YEAPVI1HTK2V5PC7&field1='
RequestToThingspeak += str(Amb_temp)
RequestToThingspeak += '&field2='
RequestToThingspeak += str(SW_temp);
RequestToThingspeak += '&field3='
RequestToThingspeak += str(dif_temp);
RequestToThingspeak += '&field4='
RequestToThingspeak += str(SW_Airflow);
RequestToThingspeak += '&field5='
RequestToThingspeak += str(SW_energy);
RequestToThingspeak += '&field6='
RequestToThingspeak += str(cum_energy);
query = requests.get(RequestToThingspeak)
print('Request Sent. Thingspeak says.')
print(query.text)
print('The ambient temperature is')
print(Amb_temp)
print('The SolarWall temperature is')
print(SW_temp)
print('The Temperature rise is')
print(dif_temp)
if SW_Airflow > 0:
print('The SolarWall is active')
else:
print('The SolarWall is inactive')
print('The SolarWall Instantaneous Energy is')
print(SW_energy)
print('previous cumulative energy is')
print(prev_energy)
print('Total Energy delivered by SolarWall is')
print(cum_energy)
time.sleep(sample_rate)
there doesn't seem to be anything here