Off-grid Solar PV Panel project: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| (31 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
= | == Arduino IDE Installation and Configuration on a Raspberry Pi == | ||
[[File:Screen Shot 2023-04-10 at 12.59.04 pm.png | 900px |link=https://www.dropbox.com/s/bwohfh2eh7ys14d/Arduino%20IDE%20installation3.mp4?dl=0 ]] | |||
== Arduino LED Blink Program == | |||
* Program lights up Arduino UNO on-board LED connected to pin 13 for 1 second. | |||
[[File:Screen Shot 2023-04-10 at 1.07.34 pm.png | 900px | link=https://www.dropbox.com/s/26b8stvdtpjz26c/Arduino%20LED%20Blink.mp4?dl=0 ]] | |||
<syntaxhighlight lang="c++"> | |||
/* | |||
Blink | |||
Turns an LED on for one second, then off for one second, repeatedly. | |||
*/ | |||
// the setup function runs once when you press reset or power the board | |||
void setup() { | |||
// initialize digital pin LED_BUILTIN as an output. | |||
pinMode(13, OUTPUT); | |||
} | |||
// the loop function runs over and over again forever | |||
void loop() { | |||
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) = 1 = 5V supply | |||
delay(1000); // wait for 1 second | |||
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW = 0 = 0V supply | |||
delay(1000); // wait for 1 second | |||
} | |||
</syntaxhighlight> | |||
== Arduino UNO - Safety == | |||
* Wear eye protection. | |||
* Always get a teacher to inspect your circuit before powering up. | |||
* Wear cotton shirts and pants and closed shoes. | |||
* Even low voltages can cause burns because wires can heat up if shorted. | |||
[[File:Screen Shot 2023-04-10 at 3.57.48 pm.png | 900px | link=https://www.dropbox.com/s/7lq5vcwjz6jwlyy/Safety%20with%20Circuits.mp4?dl=0 ]] | |||
== Arduino UNO - Building a LED Blink Circuit on a Breadboard == | |||
* Build an external LED Blink Circuit for the Arduino UNO. | |||
[[File:Screen Shot 2023-04-10 at 1.39.38 pm.png | 900px | link=https://www.dropbox.com/s/omux7g4zvstistp/Blink%20LED%20Circuit.mp4?dl=0 ]] | |||
== Arduino UNO - Building a Mini-Solar PV Circuit == | |||
* Demonstration on how to build a small PV circuit that charges a battery on an Arduino UNO. | |||
* The circuit incorporates a Voltage Divider circuit. | |||
* Click on Image below to download and watch video. | |||
[[File:IMG 5304.JPG | 900px | https://www.dropbox.com/s/3ugfnajegfk5rmb/Mini%20Solar%20PV%20Circuit.mp4?dl=0 ]] | |||
== Raspberry Pi - Python Introductory Lesson == | |||
* Install software to help program in Python | |||
* Explore some simple python coding and learn about indentation to delineate code blocks | |||
[[File:Screen Shot 2023-04-10 at 3.27.02 pm.png | 900px | link=https://www.dropbox.com/s/l9zzp8i0i359zox/Python%20Introduction.mp4?dl=0 ]] | |||
== Raspberry Pi and Arduino Serial Communication == | |||
* Write code for the Arduino UNO to send data to the Raspberry pi using Serial communication. | |||
* Write code in Python on the Raspberry Pi to receive data from the Arduino UNO using Serial communication. | |||
* Raspberry Pi requires the installation of: | |||
** '''sudo apt install python3-pip''' - pip to install python packages | |||
** '''python3 -m pip install pyserial''' - pySerial Library | |||
[[File:Screen Shot 2023-04-10 at 3.33.38 pm.png | 900px | link=https://www.dropbox.com/s/bfw4oixvsmudwk2/Python%20Serial%20Intro.mp4?dl=0 ]] | |||
=== Python code for Raspberry Pi to receive serial communication from the Arduino UNO === | |||
<syntaxhighlight lang="python"> | |||
import serial | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
while True: | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
print(line) | |||
</syntaxhighlight> | |||
== Arduino UNO - Reading the Voltage of the Small Solar PV Panel == | |||
* Learn how to read voltage readings from a small Solar PV panel connected to an Arduino UNO using a voltage divider circuit and analog pin A2. | |||
[[File:Screen Shot 2023-04-10 at 5.00.47 pm.png | 900px | link=https://www.dropbox.com/s/si91tj3sehc2aeh/Read%20PV%20Values%20on%20Arduino.mp4?dl=0 ]] | |||
== Python - code to read data from solar panel each minute == | |||
=== Python - collecting data from the arduino === | |||
<syntaxhighlight lang="python"> | |||
#!/usr/bin/env python3 | |||
import serial | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
while True: | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
print(line) | |||
</syntaxhighlight> | |||
=== Python - adding a datestamp === | |||
<syntaxhighlight lang="python"> | |||
#!/usr/bin/env python3 | |||
import serial | |||
import datetime | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
while True: | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
print(line) | |||
now = datetime.datetime.now() | |||
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S") | |||
print(f'This python program was run at this time {date_stamp}') | |||
data = str(line) + "," + date_stamp + "\n" | |||
print(f'The data is = {data}') | |||
</syntaxhighlight> | |||
=== Python - saving data to a file === | |||
<syntaxhighlight lang="python"> | |||
#!/usr/bin/env python3 | |||
import serial | |||
import datetime | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
while True: | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
print(line) | |||
now = datetime.datetime.now() | |||
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S") | |||
print(f'This python program was run at this time {date_stamp}') | |||
data = str(line) + "," + date_stamp + "\n" | |||
print(f'The data is = {data}') | |||
f = open('/home/pi/solar_project/solar_data.txt','a') | |||
f.write(data) | |||
f.close() | |||
</syntaxhighlight> | |||
=== Python - with graphing in Plotly === | |||
<syntaxhighlight lang="python"> | |||
#!/usr/bin/env python3 | |||
import serial | |||
import datetime | |||
import plotly | |||
import plotly.graph_objects as go | |||
import pandas | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
while True: | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
print(line) | |||
now = datetime.datetime.now() | |||
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S") | |||
print(f'This python program was run at this time {date_stamp}') | |||
data = str(line) + "," + date_stamp + "\n" | |||
print(f'The data is = {data}') | |||
f = open('/home/pi/solar_project/solar_data.txt','a') | |||
f.write(data) | |||
f.close() | |||
# import and plot data text file | |||
df = pandas.read_csv('/home/pi/solar_project/solar_data.txt') | |||
== Arduino | fig = go.Figure(data = go.Scatter(mode='markers', | ||
x=df.date_time, | |||
y=df.solar_value, | |||
marker=dict( | |||
color="LightSkyBlue", | |||
size=20) | |||
) | |||
) | |||
fig.update_layout(title="Solar Panel Test", | |||
xaxis_title = 'Time', | |||
yaxis_title = 'Solar Panel Voltage', | |||
font=dict( | |||
size=20, | |||
color="RebeccaPurple" | |||
) | |||
) | |||
plotly.offline.plot(fig, | |||
filename="/home/pi/solar_project/solar_panel_graph.html", | |||
auto_open=False) | |||
print("Solar Panel data plotted") | |||
</syntaxhighlight> | |||
== Python - Reading Solar PV Voltage using Python == | |||
* Learn how to: | |||
** Read solar PV voltage analog values in Python | |||
** Convert the solar PV values into voltage | |||
** Format the voltage values to three decimal points. | |||
[[File:Screen Shot 2023-04-10 at 8.23.18 pm.png | 900px | link=https://www.dropbox.com/s/mddlgeu4wkgt6vo/Reading%20Solar%20PV%20values%20in%20Python2.mp4?dl=0 ]] | |||
<syntaxhighlight lang="python"> | |||
import serial | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
while True: | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
try: | |||
voltage = int(line) | |||
except: | |||
voltage = 0 | |||
voltage_corrected = voltage * (5.0 / 1023.0) * 2 | |||
# https://thepythonguru.com/python-string-formatting/ | |||
# {0} - first formatted term. If there were more terms they would be shown as {1}, {2}, etc. | |||
# .3 - three decimal points | |||
# f - floating point numbers = decimal numbers | |||
# .format() - variables to format | |||
voltage_string = '{0:.3f}'.format(voltage_corrected) | |||
</syntaxhighlight> | |||
== Python - Adding a Time Stamp == | |||
== Python - Saving Data in a Text File == | |||
<syntaxhighlight lang="python"> | |||
import serial | |||
# from datefime module import datetime class | |||
from datetime import datetime | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
while True: | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
try: | |||
voltage = int(line) | |||
except: | |||
voltage = 0 | |||
voltage_corrected = voltage * (5.0 / 1023.0) * 2 | |||
voltage_string = '{0:.3f}'.format(voltage_corrected) | |||
now = datetime.now() | |||
# The strftime method is used to create formatted Strings | |||
time_stamp = now.strftime("%Y-%b-%d %H:%M:%S") | |||
print(f'Current time is {time_stamp} and corrected voltage {voltage_string}') | |||
if now.strftime("%S") == "00": | |||
# save the data in a file every minute | |||
data = time_stamp + "," + voltage_string + "\n" | |||
f = open('/home/pi/Arduino/small_solar_arduino_data.txt','a') | |||
f.write(data) | |||
f.close() | |||
print("Data saved") | |||
</syntaxhighlight> | |||
== Python - Graphing Data from Text File using Plotly == | |||
== Python - Final code == | |||
* The final code displayed below accepts | |||
** two data values from the Arduino (voltage and fanOn) | |||
** add a datetime stamp | |||
** Dweets the data to the cloud | |||
** saves the data in a file named'''solar_arduino_data.txt''' | |||
** and plots the data using Plotly and saves the plot in a file names '''arduino_solar.html''' | |||
<syntaxhighlight lang="python"> | |||
#!/usr/bin/env python3 | |||
import serial | |||
import requests | |||
import datetime | |||
import plotly | |||
import plotly.graph_objects as go | |||
import pandas | |||
import time | |||
if __name__ == '__main__': | |||
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) | |||
ser.reset_input_buffer() | |||
#cycle = 0 | |||
#good_data = 0 | |||
for i in range(5): | |||
voltage_string = '' | |||
now = datetime.datetime.now() | |||
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S") | |||
if ser.in_waiting > 0: | |||
line = ser.readline().decode('utf-8').rstrip() | |||
print(f"separated using commas {line.split(',')}") | |||
mylist = line.split(',') | |||
try: | |||
voltage = int(mylist[0]) | |||
print(voltage) | |||
fanOn = int(mylist[1]) | |||
except: | |||
voltage = 0 | |||
fanOn = 0 | |||
voltage_correct = voltage * (5.0 / 1023.0) * 2 | |||
print('The solar panel voltage = {0:.3f}'.format(voltage_correct)) | |||
print(f'Fan status is {fanOn}') | |||
#print(f'The cycle is {cycle}') | |||
print(f'datestamp is {date_stamp}') | |||
voltage_string = '{0:.3f}'.format(voltage_correct) | |||
fanOn_string = str(fanOn) | |||
#cycle = cycle + 1 | |||
time.sleep(1) | |||
print("Preparing dweet") | |||
dweet_dict = {} | |||
dweet_dict.update({"solar_voltage": voltage_string}) | |||
dweet_dict.update({"fanOn": fanOn_string}) | |||
dweet_dict.update({"date_stamp": date_stamp}) | |||
url = "https://dweet.io/dweet/for/bundoora-*****************?" | |||
#x = requests.post(url, json=dweet_dict) | |||
#print(x.text) | |||
data = "" | |||
data = date_stamp + "," + voltage_string + "," + fanOn_string + '\n' | |||
f = open('/home/pi/Arduino/solar_arduino_data.txt','a') | |||
f.write(data) | |||
f.close() | |||
= | df = pandas.read_csv('/home/pi/Arduino/solar_arduino_data.txt') | ||
fig = go.Figure(data = go.Scatter(mode='markers', | |||
x=df.Date, | |||
y=df.Volt, | |||
marker=dict( | |||
color="LightSkyBlue", | |||
size=20) | |||
) | |||
) | |||
fig.update_layout(title="Arduino Solar Voltage", | |||
xaxis_title = 'Time', | |||
yaxis_title = 'Voltage', | |||
font=dict( | |||
size=20, | |||
color="RebeccaPurple" | |||
) | |||
) | |||
plotly.offline.plot(fig, | |||
filename="/home/pi/Arduino/arduino_solar.html", | |||
auto_open=False) | |||
print("Voltage plotted") | |||
</syntaxhighlight> | |||
Latest revision as of 18:47, 13 July 2023
Arduino IDE Installation and Configuration on a Raspberry Pi
Arduino LED Blink Program
- Program lights up Arduino UNO on-board LED connected to pin 13 for 1 second.
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) = 1 = 5V supply
delay(1000); // wait for 1 second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW = 0 = 0V supply
delay(1000); // wait for 1 second
}
Arduino UNO - Safety
- Wear eye protection.
- Always get a teacher to inspect your circuit before powering up.
- Wear cotton shirts and pants and closed shoes.
- Even low voltages can cause burns because wires can heat up if shorted.
Arduino UNO - Building a LED Blink Circuit on a Breadboard
- Build an external LED Blink Circuit for the Arduino UNO.
Arduino UNO - Building a Mini-Solar PV Circuit
- Demonstration on how to build a small PV circuit that charges a battery on an Arduino UNO.
- The circuit incorporates a Voltage Divider circuit.
- Click on Image below to download and watch video.
Raspberry Pi - Python Introductory Lesson
- Install software to help program in Python
- Explore some simple python coding and learn about indentation to delineate code blocks
Raspberry Pi and Arduino Serial Communication
- Write code for the Arduino UNO to send data to the Raspberry pi using Serial communication.
- Write code in Python on the Raspberry Pi to receive data from the Arduino UNO using Serial communication.
- Raspberry Pi requires the installation of:
- sudo apt install python3-pip - pip to install python packages
- python3 -m pip install pyserial - pySerial Library
Python code for Raspberry Pi to receive serial communication from the Arduino UNO
import serial
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
Arduino UNO - Reading the Voltage of the Small Solar PV Panel
- Learn how to read voltage readings from a small Solar PV panel connected to an Arduino UNO using a voltage divider circuit and analog pin A2.
Python - code to read data from solar panel each minute
Python - collecting data from the arduino
#!/usr/bin/env python3
import serial
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
Python - adding a datestamp
#!/usr/bin/env python3
import serial
import datetime
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S")
print(f'This python program was run at this time {date_stamp}')
data = str(line) + "," + date_stamp + "\n"
print(f'The data is = {data}')
Python - saving data to a file
#!/usr/bin/env python3
import serial
import datetime
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S")
print(f'This python program was run at this time {date_stamp}')
data = str(line) + "," + date_stamp + "\n"
print(f'The data is = {data}')
f = open('/home/pi/solar_project/solar_data.txt','a')
f.write(data)
f.close()
Python - with graphing in Plotly
#!/usr/bin/env python3
import serial
import datetime
import plotly
import plotly.graph_objects as go
import pandas
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S")
print(f'This python program was run at this time {date_stamp}')
data = str(line) + "," + date_stamp + "\n"
print(f'The data is = {data}')
f = open('/home/pi/solar_project/solar_data.txt','a')
f.write(data)
f.close()
# import and plot data text file
df = pandas.read_csv('/home/pi/solar_project/solar_data.txt')
fig = go.Figure(data = go.Scatter(mode='markers',
x=df.date_time,
y=df.solar_value,
marker=dict(
color="LightSkyBlue",
size=20)
)
)
fig.update_layout(title="Solar Panel Test",
xaxis_title = 'Time',
yaxis_title = 'Solar Panel Voltage',
font=dict(
size=20,
color="RebeccaPurple"
)
)
plotly.offline.plot(fig,
filename="/home/pi/solar_project/solar_panel_graph.html",
auto_open=False)
print("Solar Panel data plotted")
Python - Reading Solar PV Voltage using Python
- Learn how to:
- Read solar PV voltage analog values in Python
- Convert the solar PV values into voltage
- Format the voltage values to three decimal points.
import serial
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
try:
voltage = int(line)
except:
voltage = 0
voltage_corrected = voltage * (5.0 / 1023.0) * 2
# https://thepythonguru.com/python-string-formatting/
# {0} - first formatted term. If there were more terms they would be shown as {1}, {2}, etc.
# .3 - three decimal points
# f - floating point numbers = decimal numbers
# .format() - variables to format
voltage_string = '{0:.3f}'.format(voltage_corrected)
Python - Adding a Time Stamp
Python - Saving Data in a Text File
import serial
# from datefime module import datetime class
from datetime import datetime
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
try:
voltage = int(line)
except:
voltage = 0
voltage_corrected = voltage * (5.0 / 1023.0) * 2
voltage_string = '{0:.3f}'.format(voltage_corrected)
now = datetime.now()
# The strftime method is used to create formatted Strings
time_stamp = now.strftime("%Y-%b-%d %H:%M:%S")
print(f'Current time is {time_stamp} and corrected voltage {voltage_string}')
if now.strftime("%S") == "00":
# save the data in a file every minute
data = time_stamp + "," + voltage_string + "\n"
f = open('/home/pi/Arduino/small_solar_arduino_data.txt','a')
f.write(data)
f.close()
print("Data saved")
Python - Graphing Data from Text File using Plotly
Python - Final code
- The final code displayed below accepts
- two data values from the Arduino (voltage and fanOn)
- add a datetime stamp
- Dweets the data to the cloud
- saves the data in a file namedsolar_arduino_data.txt
- and plots the data using Plotly and saves the plot in a file names arduino_solar.html
#!/usr/bin/env python3
import serial
import requests
import datetime
import plotly
import plotly.graph_objects as go
import pandas
import time
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
#cycle = 0
#good_data = 0
for i in range(5):
voltage_string = ''
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%b-%d %H:%M:%S")
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(f"separated using commas {line.split(',')}")
mylist = line.split(',')
try:
voltage = int(mylist[0])
print(voltage)
fanOn = int(mylist[1])
except:
voltage = 0
fanOn = 0
voltage_correct = voltage * (5.0 / 1023.0) * 2
print('The solar panel voltage = {0:.3f}'.format(voltage_correct))
print(f'Fan status is {fanOn}')
#print(f'The cycle is {cycle}')
print(f'datestamp is {date_stamp}')
voltage_string = '{0:.3f}'.format(voltage_correct)
fanOn_string = str(fanOn)
#cycle = cycle + 1
time.sleep(1)
print("Preparing dweet")
dweet_dict = {}
dweet_dict.update({"solar_voltage": voltage_string})
dweet_dict.update({"fanOn": fanOn_string})
dweet_dict.update({"date_stamp": date_stamp})
url = "https://dweet.io/dweet/for/bundoora-*****************?"
#x = requests.post(url, json=dweet_dict)
#print(x.text)
data = ""
data = date_stamp + "," + voltage_string + "," + fanOn_string + '\n'
f = open('/home/pi/Arduino/solar_arduino_data.txt','a')
f.write(data)
f.close()
df = pandas.read_csv('/home/pi/Arduino/solar_arduino_data.txt')
fig = go.Figure(data = go.Scatter(mode='markers',
x=df.Date,
y=df.Volt,
marker=dict(
color="LightSkyBlue",
size=20)
)
)
fig.update_layout(title="Arduino Solar Voltage",
xaxis_title = 'Time',
yaxis_title = 'Voltage',
font=dict(
size=20,
color="RebeccaPurple"
)
)
plotly.offline.plot(fig,
filename="/home/pi/Arduino/arduino_solar.html",
auto_open=False)
print("Voltage plotted")