diff --git a/Algorithim_Rev6.pdf b/Algorithim_Rev6.pdf deleted file mode 100644 index 5088d97..0000000 Binary files a/Algorithim_Rev6.pdf and /dev/null differ diff --git a/__pycache__/controller.cpython-37.pyc b/__pycache__/controller.cpython-37.pyc deleted file mode 100644 index d1faaea..0000000 Binary files a/__pycache__/controller.cpython-37.pyc and /dev/null differ diff --git a/adc.py b/adc.py new file mode 100644 index 0000000..865081f --- /dev/null +++ b/adc.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +# sudo pip3 install adafruit-circuitpython-ads1x15 + +import time +import board +import busio +import adafruit_ads1x15.ads1115 as ADS +from adafruit_ads1x15.analog_in import AnalogIn + +resistor = 0.00075 + +# Create the I2C bus +i2c = busio.I2C(board.SCL, board.SDA) + +# Create the ADC object using the I2C bus +ads = ADS.ADS1115(i2c) + +# Create single-ended input on channel 0 +#chan = AnalogIn(ads, ADS.P0) + +# Create differential input between channel 0 and 1 +chan1 = AnalogIn(ads, ADS.P0, ADS.P1) +#chan2 = AnalogIn(ads, ADS.P2, ADS.P3) + +print("{:>5}\t{:>5}".format('raw', 'v', 'current')) + +while True: + print("{:>5}\t{:>5.3f}".format(chan1.value, chan1.voltage, chan1.voltage/resistor)) + time.sleep(0.5) \ No newline at end of file diff --git a/caiso_api.py b/caiso_api.py deleted file mode 100755 index 462727a..0000000 --- a/caiso_api.py +++ /dev/null @@ -1,104 +0,0 @@ -#!/usr/bin/python3 -import requests -import time -import shutil -import json -import datetime -import io -import zipfile -import os -from xml.etree import ElementTree - -todays_date = datetime.datetime.now() -yesterdays_date = todays_date - datetime.timedelta(days = 1) -tomorrows_date = todays_date + datetime.timedelta(days = 1) - -string_tail = 'T07:00-0000' -first_url_string = todays_date.strftime("%Y") + todays_date.strftime("%m") + todays_date.strftime("%d") + string_tail -second_url_string = tomorrows_date.strftime("%Y") + tomorrows_date.strftime("%m") + tomorrows_date.strftime("%d") + string_tail - -# base_url variable to store url -base_url = "http://oasis.caiso.com/oasisapi/SingleZip?queryname=PRC_LMP" - -# first url argument -first_url_arg = "&startdatetime=" + first_url_string - -# Second url argument -second_url_arg = "&enddatetime=" + second_url_string - -# url_tail variable to store the tail of the url -url_tail = "&market_run_id=DAM&version=1&node=STREAMVW_6_N001" - -# complete_url variable to store -# complete url address -complete_url = base_url + first_url_arg + second_url_arg + url_tail - -response = requests.get(complete_url) - -if response.status_code == 200: - z = zipfile.ZipFile( io.BytesIO(response.content) ) - filename = z.namelist()[0] - z.extractall("./") - dom = ElementTree.parse(filename) - root = dom.getroot() - root = root[1][0][1] - values = [[]] - for child in root: - sub_string = [] - string = child.tag - for child2 in child: - string = child2.tag - if(string[45:] == "INTERVAL_NUM"): - sub_string.append(int(child2.text) - 1) - elif(string[45:] == "INTERVAL_START_GMT"): - sub_string.append(child2.text) - elif(string[45:] == "INTERVAL_END_GMT"): - sub_string.append(child2.text) - elif(string[45:] == "VALUE"): - sub_string.append(float(child2.text)) - values.append(sub_string) - - values = sorted(values) - values.pop(0) - values.pop(0) - - ##################################################################################################################################### - ############################## This portion is to get weather data ################################################################## - ##################################################################################################################################### - file = open('weather_Api_Key.txt', 'r') - api_key = file.read() - file.close - - # base_url variable to store url - base_url = "https://api.openweathermap.org/data/2.5/onecall?lat=32.7157&lon=-117.1611&exclude=current,minutely,alerts,daily&appid=" - - # complete_url variable to store - # complete url address - complete_url = base_url + api_key - - - # get method of requests module - # return response object - response = requests.get(complete_url) - - - # json method of response object - # convert json format data into - # python format data - if response.status_code == 200: - x = response.json() # This is a dictionary - x = x['hourly'] - - for each in range(0, 48): - if(todays_date.day == datetime.datetime.fromtimestamp(x[each]['dt']).day and todays_date.hour == datetime.datetime.fromtimestamp(x[each]['dt']).hour): - values[todays_date.hour].append(x[each]['clouds']) - ##################################################################################################################################### - ############################### End of getting weather data ######################################################################### - ##################################################################################################################################### - with open('data.json', 'w') as outfile: - json.dump(values, outfile) - - -if os.path.exists(filename): # Delete the file just downloaded - os.remove(filename) # Delete the file just downloaded - diff --git a/controller.py b/controller.py deleted file mode 100755 index e89a71c..0000000 --- a/controller.py +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/python3 -import RPi.GPIO as GPIO -import time -import pigpio -from enum import Enum, unique, auto - -pi = pigpio.pi() -pi.hardware_PWM(12, 8000, 10000) # 10% duty cycle = 10000 (off)0 to (fully on)1000000 - -GPIO.setmode(GPIO.BOARD) # Set up the pins on the board using the physical pin layout -GPIO.setwarnings(False) -PIN1 = 29 # These should remain constant -PIN2 = 31 # These should remain constant -PIN3 = 36 # These should remain constant -PIN4 = 37 # These should remain constant -GPIO.setup(PIN1,GPIO.OUT) # Physical pins 29 set as output -GPIO.setup(PIN2,GPIO.OUT) # Physical pins 31 set to output -GPIO.setup(PIN3,GPIO.OUT) # Physical pins 36 set to output -GPIO.setup(PIN4,GPIO.OUT) # Physical pins 37 set to output - - -@unique -class State(Enum): - state_1 = auto() - state_2 = auto() - state_3 = auto() - state_4 = auto() - state_5 = auto() - state_6 = auto() - state_7 = auto() - -class Controller: - def __init__(self): - self.current_state = State.state_1 - self.switch_states() - - def switch_states(self): - if self.current_state == State.state_1: - GPIO.output(PIN1,GPIO.LOW) # OFF - GPIO.output(PIN2,GPIO.HIGH) # ON - GPIO.output(PIN3,GPIO.HIGH) # ON - GPIO.output(PIN4,GPIO.LOW) # OFF - elif self.current_state == State.state_2: - GPIO.output(PIN1,GPIO.HIGH) # ON - GPIO.output(PIN2,GPIO.LOW) # OFF - GPIO.output(PIN3,GPIO.LOW) # OFF - GPIO.output(PIN4,GPIO.HIGH) # ON - elif self.current_state == State.state_3: - GPIO.output(PIN1,GPIO.HIGH) # ON - GPIO.output(PIN2,GPIO.HIGH) # ON - GPIO.output(PIN3,GPIO.LOW) # OFF - GPIO.output(PIN4,GPIO.LOW) # OFF - elif self.current_state == State.state_4: - GPIO.output(PIN1,GPIO.HIGH) # ON - GPIO.output(PIN2,GPIO.HIGH) # ON - GPIO.output(PIN3,GPIO.HIGH) # ON - GPIO.output(PIN4,GPIO.LOW) # OFF - elif self.current_state == State.state_5: - GPIO.output(PIN1,GPIO.HIGH) # ON - GPIO.output(PIN2,GPIO.HIGH) # ON - GPIO.output(PIN3,GPIO.HIGH) # ON - elif self.current_state == State.state_6: - GPIO.output(PIN1,GPIO.HIGH) # ON - GPIO.output(PIN2,GPIO.HIGH) # ON - GPIO.output(PIN3,GPIO.HIGH) # ON - GPIO.output(PIN4,GPIO.HIGH) # ON - elif self.current_state == State.state_7: - GPIO.output(PIN1,GPIO.HIGH) # ON - GPIO.output(PIN2,GPIO.LOW) # OFF - GPIO.output(PIN3,GPIO.HIGH) # ON - GPIO.output(PIN4,GPIO.HIGH) # ON - else: - GPIO.output(PIN1,GPIO.LOW) # OFF - GPIO.output(PIN2,GPIO.HIGH) # ON - GPIO.output(PIN3,GPIO.HIGH) # ON - GPIO.output(PIN4,GPIO.LOW) # OFF - - def update_state(self, next_state): - self.current_state = next_state - self.switch_states() - - - def print_status(self): - print(self.current_state.name) - print("Pin 1: On" if GPIO.input(PIN1) else "Pin 1: Off") - print("Pin 2: On" if GPIO.input(PIN2) else "Pin 2: Off") - print("Pin 3: On" if GPIO.input(PIN3) else "Pin 3: Off") - print("Pin 4: On" if GPIO.input(PIN4) else "Pin 4: Off") - - - - diff --git a/data.json b/data.json deleted file mode 100644 index 588d5af..0000000 --- a/data.json +++ /dev/null @@ -1 +0,0 @@ -[[0, "2021-04-02T07:00:00-00:00", "2021-04-02T08:00:00-00:00", 34.75144], [1, "2021-04-02T08:00:00-00:00", "2021-04-02T09:00:00-00:00", 33.8331], [2, "2021-04-02T09:00:00-00:00", "2021-04-02T10:00:00-00:00", 30.53981], [3, "2021-04-02T10:00:00-00:00", "2021-04-02T11:00:00-00:00", 30.03254], [4, "2021-04-02T11:00:00-00:00", "2021-04-02T12:00:00-00:00", 32.08217], [5, "2021-04-02T12:00:00-00:00", "2021-04-02T13:00:00-00:00", 38.75], [6, "2021-04-02T13:00:00-00:00", "2021-04-02T14:00:00-00:00", 40.74851], [7, "2021-04-02T14:00:00-00:00", "2021-04-02T15:00:00-00:00", 38.86383], [8, "2021-04-02T15:00:00-00:00", "2021-04-02T16:00:00-00:00", 4.96332], [9, "2021-04-02T16:00:00-00:00", "2021-04-02T17:00:00-00:00", -8.455], [10, "2021-04-02T17:00:00-00:00", "2021-04-02T18:00:00-00:00", -13.6994], [11, "2021-04-02T18:00:00-00:00", "2021-04-02T19:00:00-00:00", -12.73172], [12, "2021-04-02T19:00:00-00:00", "2021-04-02T20:00:00-00:00", -12.56389], [13, "2021-04-02T20:00:00-00:00", "2021-04-02T21:00:00-00:00", -8.7892], [14, "2021-04-02T21:00:00-00:00", "2021-04-02T22:00:00-00:00", -7.06435], [15, "2021-04-02T22:00:00-00:00", "2021-04-02T23:00:00-00:00", -0.86321], [16, "2021-04-02T23:00:00-00:00", "2021-04-03T00:00:00-00:00", -0.78654], [17, "2021-04-03T00:00:00-00:00", "2021-04-03T01:00:00-00:00", 18.54465, 20], [18, "2021-04-03T01:00:00-00:00", "2021-04-03T02:00:00-00:00", 43.04167], [19, "2021-04-03T02:00:00-00:00", "2021-04-03T03:00:00-00:00", 62.65491], [20, "2021-04-03T03:00:00-00:00", "2021-04-03T04:00:00-00:00", 49.58015], [21, "2021-04-03T04:00:00-00:00", "2021-04-03T05:00:00-00:00", 46.42511], [22, "2021-04-03T05:00:00-00:00", "2021-04-03T06:00:00-00:00", 40.71954], [23, "2021-04-03T06:00:00-00:00", "2021-04-03T07:00:00-00:00", 38.22743]] \ No newline at end of file diff --git a/gpio_test.py b/gpio_test.py new file mode 100644 index 0000000..7a7f63a --- /dev/null +++ b/gpio_test.py @@ -0,0 +1,40 @@ +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BCM) + +GPIO.setup(16, GPIO.OUT) +GPIO.setup(20, GPIO.OUT) +GPIO.setup(21, GPIO.OUT) +GPIO.setup(26, GPIO.OUT) +time.sleep(2) + +GPIO.output(16, GPIO.HIGH) +print("Pin 16 HIGH") +time.sleep(2) +GPIO.output(16, GPIO.LOW) +print("Pin 16 LOW") +time.sleep(2) + +GPIO.output(20, GPIO.HIGH) +print("Pin 20 HIGH") +time.sleep(2) +GPIO.output(20, GPIO.LOW) +print("Pin 20 LOW") +time.sleep(2) + +GPIO.output(21, GPIO.HIGH) +print("Pin 21 HIGH") +time.sleep(2) +GPIO.output(21, GPIO.LOW) +print("Pin 21 LOW") +time.sleep(2) + +GPIO.output(26, GPIO.HIGH) +print("Pin 26 HIGH") +time.sleep(2) +GPIO.output(26, GPIO.LOW) +print("Pin 26 LOW") +time.sleep(2) + +GPIO.cleanup() \ No newline at end of file diff --git a/helios.py b/helios.py new file mode 100644 index 0000000..c5be71d --- /dev/null +++ b/helios.py @@ -0,0 +1,203 @@ +import xml.etree.ElementTree as ET #importing the parsing tool ElementTree as the variable ET +from array import * +import requests, zipfile, io, os, glob, pprint +from datetime import datetime, timedelta +import RPi.GPIO as GPIO +from time import sleep + + +##################################################################################################################################### +######################################## Setting up Raspberry Pi GPiOS ############################################################## +##################################################################################################################################### + +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) +GPIO.setup(16, GPIO.OUT) +GPIO.setup(20, GPIO.OUT) +GPIO.setup(21, GPIO.OUT) +GPIO.setup(26, GPIO.OUT) + +##################################################################################################################################### +######################################## Constucting the CAISO query URL ############################################################ +##################################################################################################################################### + +next_day = datetime.now() + timedelta(+1) #getting the date for the previous day +tomorrow = next_day.strftime('%Y%m%d') #storing the previous day in the variable 'yesterday' with the format YYYddMM (the format CAISO uses for the query) +path = "/home/pi/Code/" #where the file will be stored, will be different for the Raspbery Pi + +start_url = "http://oasis.caiso.com/oasisapi/SingleZip?queryname=PRC_LMP&" #beginning of the URL, does not change + +start_date = "startdatetime=" #part of the URL that will tell CAISO what the start date is + +start_time = "T00:00-0000&" #start time, does not change + +end_date = "enddatetime=" #part of the URL that will tell CAISO what the end date is + +end_time = "T23:00-0000&" #end time, does not change + +end_url = "&version=1&market_run_id=DAM&node=STREAMVW_6_N001" #end of the URL, does not change unless you want to change the node + +CAISO_query = start_url + start_date + tomorrow + start_time + end_date + tomorrow + end_time + end_url #constucting the whole URL + +##################################################################################################################################### +######################################## Getting the .xml file from CAISO ########################################################### +##################################################################################################################################### + +r = requests.get(CAISO_query) #using python requests to access the URL, content stored in the variable 'r' +z = zipfile.ZipFile(io.BytesIO(r.content)) #get the .zip file from 'r' and store it in 'z' +z.extractall() #extract the contents of that .zip file which is an .xml file, using this method doesn't save the intial .zip file, just the .xml file that was inside + + +list_of_files = glob.glob(path + '*xml') #from our list of files in the path, only pull the files that end with .xml + +latest_file = max(list_of_files, key=os.path.getctime) #pull that latest .xml file we downloaded + +##################################################################################################################################### +######################################## Pulling data from the weather API ########################################################## +##################################################################################################################################### + +s = requests.get('https://api.weather.gov/gridpoints/SGX/59,15/forecast') + +clouds = "Mostly Cloudy" +rain = "Chance Rain Showers" + +weather_day = [] +weather_type = [] + +x = 0 + +while x < 14: + + day = s.json()['properties']['periods'][x]['name'] + expected = s.json()['properties']['periods'][x]['shortForecast'] + weather_day.append(day) + weather_type.append(expected) + #print() + x += 1 + +zipped = list(zip(weather_day, weather_type)) + +##################################################################################################################################### +########################################### Pulling the correct data from the CAISO .xml file ####################################### +##################################################################################################################################### + +tree = ET.parse(latest_file) #pulling the .xml file and storing it in 'tree' +root = tree.getroot() #need this to access everything in the XML file + +temp_time = [] #temp array for the time aka the interval hour +time = [] #new arary for the time with only the time of the LMP + +temp_cost = [] #temp array for the cost aka the value +cost = [] #new array for the cost with only the cost of the LMP +sort_cost = [] #array for the sorted LMP price + +for elm in root.findall(".//{http://www.caiso.com/soa/OASISReport_v1.xsd}REPORT_DATA"): #go into the REPORT_DATA + + if elm.find("{http://www.caiso.com/soa/OASISReport_v1.xsd}DATA_ITEM").text == "LMP_PRC": #find the tag called DATA_ITEM and if the text inside is LMP_PRC continue + + hour = elm.find(".//{http://www.caiso.com/soa/OASISReport_v1.xsd}INTERVAL_NUM").text #go into the INTERVAL_NUM to get the hour of the day + price = elm.find(".//{http://www.caiso.com/soa/OASISReport_v1.xsd}VALUE").text #find the price of energy + + temp_time.append(hour) #put hour into list + temp_cost.append(price) #put price into list + + +os.remove(latest_file) #delete the .xml file because we got the data from it already + +##################################################################################################################################### +############################################ Sorting the pulled data from CAISO ##################################################### +##################################################################################################################################### + +time = [int(x) for x in temp_time] #casting the time into an int whilst putting it into a new list called time +cost = [float(x) for x in temp_cost] #casting the cost into a float whilst putting it into a new list called cost +sort_cost = [float(x) for x in temp_cost] #casting the cost into a float whilst putting it into a new list called sort_list + +sort_cost.sort() #sort the sort_cost list + +high_threshold = sort_cost.pop(19) #set the high threshold to the sorted cost in the 19th position + +low_threshold = sort_cost.pop(4) #set the low threshold to the sorted cost in the 4th position + +combined = list(zip(time, cost)) #combine the time and cost lists into a 2D list + +sorted_list = sorted(combined) #sort the combined list + +##################################################################################################################################### +############################################ Using all the data for the EMS algorithm ############################################### +##################################################################################################################################### + +if clouds or rain in zipped.pop(1): # if there are clouds or rain in the forecast + print("Today is " + next_day.strftime('%m/%d/%Y')) + print("There will not be a lot of solar production today.\n") #wont be a lot of solar production + print("Hour by hour breakdown: ") + + + for t, c in sorted_list: #for the t, c in the sorted list + + + if c >= high_threshold: #if c is greater than or equal to our threshold, then print out the statment + print("- High threshold of", high_threshold, "passed at", f'Hour {t}', "use batteries to power load") + print("- Contacts 1, 3, 4 open and Contact 2 shut\n") + GPIO.output(26, GPIO.LOW) + GPIO.output(20, GPIO.LOW) + GPIO.output(21, GPIO.LOW) + GPIO.output(16, GPIO.HIGH) + sleep(2) + + elif c <= low_threshold: + print("- Low threshold of", low_threshold, "not passed at", f'Hour {t}', "charge the batteries") + print("- Contacts 2, 3 open and Contacts 1, 4 shut\n") + GPIO.output(16, GPIO.LOW) + GPIO.output(20, GPIO.LOW) + GPIO.output(26, GPIO.HIGH) + GPIO.output(21, GPIO.HIGH) + sleep(2) + + else: + print("- Solar panels generating energy if sun is available and grid is powering house at", f'Hour {t}') + print("- Contacts 1, 2, 4 open and Contact 3 shut\n") + GPIO.output(21, GPIO.LOW) + GPIO.output(16, GPIO.LOW) + GPIO.output(21, GPIO.LOW) + GPIO.output(20, GPIO.HIGH) + sleep(2) + + GPIO.cleanup() + + +else: + print("Today is " + next_day.strftime('%m/%d/%Y')) + print("There will be a lot of solar production today") #wont be a lot of solar production + print() + print("Hour by hour breakdown: ") + + for t, c in sorted_list: #for the t, c in the sorted list + + if c >= high_threshold: #if c is greater than or equal to our threshold, then print out the statment + print("High threshold of", high_threshold, "passed at", f'Hour {t}', "use batteries to power load") + print("- Contacts 1, 3, 4 open and Contact 2 shut\n") + GPIO.output(26, GPIO.LOW) + GPIO.output(20, GPIO.LOW) + GPIO.output(21, GPIO.LOW) + GPIO.output(16, GPIO.HIGH) + sleep(2) + + elif c <= low_threshold: + print("Low threshold of", low_threshold, "not passed at", f'Hour {t}', "charge the batteries") + print("- Contacts 2, 3 open and Contacts 1, 4 shut\n") + GPIO.output(16, GPIO.LOW) + GPIO.output(20, GPIO.LOW) + GPIO.output(26, GPIO.HIGH) + GPIO.output(21, GPIO.HIGH) + sleep(2) + + else: + print("Solar panels generating energy if sun is available and grid is powering house at", f'Hour {t}') + print("- Contacts 1, 2, 4 open and Contact 3 shut\n") + GPIO.output(21, GPIO.LOW) + GPIO.output(16, GPIO.LOW) + GPIO.output(21, GPIO.LOW) + GPIO.output(20, GPIO.HIGH) + sleep(2) + + GPIO.cleanup() \ No newline at end of file diff --git a/info.txt b/info.txt deleted file mode 100644 index 0c89eda..0000000 --- a/info.txt +++ /dev/null @@ -1,26 +0,0 @@ -query for the CAISO Demand Forecast - -http://oasis.caiso.com/oasisapi/SingleZip?queryname=SLD_FCST&market_run_id=DAM&startdatetime=20160415T07:00-0000&enddatetime=20160416T07:00-0000&version=1 - - - -query for the Wind and Solar Forecast - -http://oasis.caiso.com/oasisapi/SingleZip?queryname=SLD_REN_FCST&market_run_id=DAM&startdatetime=20201229T07:00-0000&enddatetime=20201230T07:00-0000&version=1 - - - - - -Open Weather API -https://api.openweathermap.org/data/2.5/onecall?lat=32.7760&lon=-177.0713&appid= - - - -http://oasis.caiso.com/oasisapi/SingleZip?queryname=PRC_LMP&startdatetime=20210314T07:00-0000&enddatetime=20210315T07:00-0000&market_run_id=DAM&version=1&node=STREAMVW_6_N001 - - - - - - diff --git a/run_caiso_api.sh b/run_caiso_api.sh deleted file mode 100755 index aec367d..0000000 --- a/run_caiso_api.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/bash -clear; ./caiso_api.py - - - - - diff --git a/run_test.sh b/run_test.sh deleted file mode 100755 index 800ecfc..0000000 --- a/run_test.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/bash -demon=`ps -e -o comm | grep pigpiod` -clear; -if [ "$demon" != "pigpiod" ]; then - sudo pigpiod; -fi -clear; -./test.py & diff --git a/stop_program.sh b/stop_program.sh deleted file mode 100755 index c6e20ea..0000000 --- a/stop_program.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/bash -clear; -pkill test.py diff --git a/test.py b/test.py deleted file mode 100755 index bb8031c..0000000 --- a/test.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/python3 -from controller import * - -controller = Controller() # Create the controller object from Controller class - -while True: - for each_state in State: - controller.update_state(each_state) - # controller.print_status() - time.sleep(1) diff --git a/weather_Api_Key.txt b/weather_Api_Key.txt deleted file mode 100644 index 1d4ded0..0000000 --- a/weather_Api_Key.txt +++ /dev/null @@ -1 +0,0 @@ -c6eca85c335ece7f866c6bb22a48f0ad \ No newline at end of file diff --git a/works2.py b/works2.py new file mode 100644 index 0000000..7537050 --- /dev/null +++ b/works2.py @@ -0,0 +1,144 @@ +import xml.etree.ElementTree as ET #importing the parsing tool ElementTree as the variable ET +from array import * +import requests, zipfile, io, os, glob, pprint +from datetime import datetime, timedelta + +##################################################################################################################################### +######################################## Constucting the CAISO query URL ############################################################ +##################################################################################################################################### + +next_day = datetime.now() + timedelta(+1) #getting the date for the previous day +tomorrow = next_day.strftime('%Y%m%d') #storing the previous day in the variable 'yesterday' with the format YYYddMM (the format CAISO uses for the query) +path = "E:/As of 11.17.2020/School/2021 Spring/EE 496/Energy Management System/Project Code/" #where the file will be stored, will be different for the Raspbery Pi + +start_url = "http://oasis.caiso.com/oasisapi/SingleZip?queryname=PRC_LMP&" #beginning of the URL, does not change + +start_date = "startdatetime=" #part of the URL that will tell CAISO what the start date is + +start_time = "T00:00-0000&" #start time, does not change + +end_date = "enddatetime=" #part of the URL that will tell CAISO what the end date is + +end_time = "T23:00-0000&" #end time, does not change + +end_url = "&version=1&market_run_id=DAM&node=STREAMVW_6_N001" #end of the URL, does not change unless you want to change the node + +CAISO_query = start_url + start_date + tomorrow + start_time + end_date + tomorrow + end_time + end_url #constucting the whole URL + +##################################################################################################################################### +######################################## Getting the .xml file from CAISO ########################################################### +##################################################################################################################################### + +r = requests.get(CAISO_query) #using python requests to access the URL, content stored in the variable 'r' +z = zipfile.ZipFile(io.BytesIO(r.content)) #get the .zip file from 'r' and store it in 'z' +z.extractall() #extract the contents of that .zip file which is an .xml file, using this method doesn't save the intial .zip file, just the .xml file that was inside + + +list_of_files = glob.glob(path + '*xml') #from our list of files in the path, only pull the files that end with .xml + +latest_file = max(list_of_files, key=os.path.getctime) #pull that latest .xml file we downloaded + +##################################################################################################################################### +######################################## Pulling data from the weather API ########################################################## +##################################################################################################################################### + +s = requests.get('https://api.weather.gov/gridpoints/SGX/59,15/forecast') + +clouds = "Mostly Cloudy" +rain = "Chance Rain Showers" + +weather_day = [] +weather_type = [] + +x = 0 + +while x < 14: + + day = s.json()['properties']['periods'][x]['name'] + expected = s.json()['properties']['periods'][x]['shortForecast'] + weather_day.append(day) + weather_type.append(expected) + #print() + x += 1 + +zipped = list(zip(weather_day, weather_type)) + +##################################################################################################################################### +########################################### Pulling the correct data from the CAISO .xml file ####################################### +##################################################################################################################################### + +tree = ET.parse(latest_file) #pulling the .xml file and storing it in 'tree' +root = tree.getroot() #need this to access everything in the XML file + +temp_time = [] #temp array for the time aka the interval hour +time = [] #new arary for the time with only the time of the LMP + +temp_cost = [] #temp array for the cost aka the value +cost = [] #new array for the cost with only the cost of the LMP +sort_cost = [] #array for the sorted LMP price + +for elm in root.findall(".//{http://www.caiso.com/soa/OASISReport_v1.xsd}REPORT_DATA"): #go into the REPORT_DATA + + if elm.find("{http://www.caiso.com/soa/OASISReport_v1.xsd}DATA_ITEM").text == "LMP_PRC": #find the tag called DATA_ITEM and if the text inside is LMP_PRC continue + + hour = elm.find(".//{http://www.caiso.com/soa/OASISReport_v1.xsd}INTERVAL_NUM").text #go into the INTERVAL_NUM to get the hour of the day + price = elm.find(".//{http://www.caiso.com/soa/OASISReport_v1.xsd}VALUE").text #find the price of energy + + temp_time.append(hour) #put hour into list + temp_cost.append(price) #put price into list + + +os.remove(latest_file) #delete the .xml file because we got the data from it already + +##################################################################################################################################### +############################################ Sorting the pulled data from CAISO ##################################################### +##################################################################################################################################### + +time = [int(x) for x in temp_time] #casting the time into an int whilst putting it into a new list called time +cost = [float(x) for x in temp_cost] #casting the cost into a float whilst putting it into a new list called cost +sort_cost = [float(x) for x in temp_cost] #casting the cost into a float whilst putting it into a new list called sort_list + +sort_cost.sort() #sort the sort_cost list + +high_threshold = sort_cost.pop(19) #set the high threshold to the sorted cost in the 19th position + +low_threshold = sort_cost.pop(4) #set the low threshold to the sorted cost in the 4th position + +combined = list(zip(time, cost)) #combine the time and cost lists into a 2D list + +sorted_list = sorted(combined) #sort the combined list + +##################################################################################################################################### +############################################ Using all the data for the EMS algorithm ############################################### +##################################################################################################################################### + +if clouds or rain in zipped.pop(1): # if there are clouds or rain in the forecast + print("Today is " + next_day.strftime('%m/%d/%Y')) + print("There will not be a lot of solar production today") #wont be a lot of solar production + print() + print("Hour by hour breakdown: ") + + for t, c in sorted_list: #for the t, c in the sorted list + + if c >= high_threshold: #if c is greater than or equal to our threshold, then print out the statment + print("- High threshold of", high_threshold, "passed at", f'Hour {t}', "use batteries to power load") + + elif c <= low_threshold: + print("- Low threshold of", low_threshold, "not passed at", f'Hour {t}', "charge the batteries") + + else: + print("- Grid is powering house at", f'Hour {t}') + + +else: + + for t, c in sorted_list: #for the t, c in the sorted list + + if c >= high_threshold: #if c is greater than or equal to our threshold, then print out the statment + print("High threshold of", high_threshold, "passed at", f'Hour {t}', "use batteries to power load") + + elif c <= low_threshold: + print("Low threshold of", low_threshold, "not passed at", f'Hour {t}', "charge the batteries") + + else: + print("Grid is powering house at", f'Hour {t}') \ No newline at end of file