-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
252 lines (161 loc) · 6.36 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QtCore/qmath.h>
using namespace std;
/*==============================================================================
To start Qt: cd /home/ricardo/Qt/Tools/QtCreator/bin/qtcreator.sh
ADC used: /sys/bus/iio/devices/iio:device0/in_voltage5_raw
Display: 480x272
pkill -f SunCast
================================================================================*/
#define FORECAST_URL "http://api.thingspeak.com/apps/thinghttp/send_request?api_key=U2AZ2JEEYH4LJTXS"
#define BAT_VMIN 23.8
#define BAT_VMAX 26.0 // 25.4
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->statusBar()->showMessage("Initializing");
// -- change LCD color -------------
ui->lcdVb->setAutoFillBackground(true);
ui->lcdSoC->setAutoFillBackground(true);
ui->lcdPower->setAutoFillBackground(true);
QPalette Pal = ui->lcdVb->palette();
Pal.setColor(QPalette::Normal, QPalette::WindowText, Qt::green);
Pal.setColor(QPalette::Normal, QPalette::Window, Qt::black);
ui->lcdVb->setPalette(Pal);
ui->lcdPower->setPalette(Pal);
Pal.setColor(QPalette::Normal, QPalette::WindowText, Qt::yellow);
Pal.setColor(QPalette::Normal, QPalette::Window, Qt::black);
ui->lcdSoC->setPalette(Pal);
ui->lcdSoC->display( "--" );
ui->lcdVb->display( "--" );
ui->lcdPower->display( "--" );
// ui->SunDayLabel->setText( "Quilaco" );
webdata.setUrl( FORECAST_URL );
webdata.startDownload();
// == task timer =============================
// connect( &taskTimer, SIGNAL(tensecInterval()), this, SLOT(getVoltage()) );
connect( &taskTimer, SIGNAL(tensecInterval()), &readMppt, SLOT(readData()) );
// connect( &taskTimer, SIGNAL(minuteInterval()), this, SLOT(logVoltage()) );
connect( &taskTimer, SIGNAL(hourInterval()), &webdata, SLOT(startDownload()) );
// == signals ===================================================================
connect( this, SIGNAL(sunHoursUpdated(QList<float>)), ui->chart, SLOT(onDataChanged(QList<float>)) );
connect( &webdata, SIGNAL(readyDownload()), this, SLOT(getWebForecast()));
connect( &readMppt, SIGNAL(mpptData(QHash<QString, float> )), this, SLOT(displayMppt( QHash<QString, float>)) );
connect( &readMppt, SIGNAL(mpptData(QHash<QString, float> )), this, SLOT(logData( QHash<QString, float>)) );
connect( &readMppt, SIGNAL(statusBar(QString)), this->statusBar(), SLOT(showMessage(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
/*----------------------------------------------------------------------
;
; Downlad and parse Sun days forecast from Web
;
-----------------------------------------------------------------------*/
#define MAXDAYS 7 // it is defined as a 7 days forecast old fashion parser
void MainWindow::getWebForecast()
{
this->statusBar()->showMessage("Loading web Data");
QString sForecast = webdata.downloadedData();
// search to first keyword and position on sun "td class"
int nPos = sForecast.indexOf("<!-- Amount of Sun -->");
if( nPos==-1) {
this->statusBar()->showMessage("Error parsing web Data");
return;
}
nPos = sForecast.indexOf("<td class", nPos );
nPos = sForecast.indexOf( "<td class=", nPos+1 );
int nl;
QString sDay;
SunHours.clear();
for( int nDay=0; nDay<MAXDAYS; nDay++) {
nPos = sForecast.indexOf( "<td class=", nPos );
nl = sForecast.indexOf( "</td>", nPos ) - nPos;
sDay = sForecast.mid( nPos, nl) ;
nPos += nl;
QStringList sList = sDay.split(0x0a);
SunHours.append(sList.at(2).trimmed().toFloat());
}
// qDebug() << "sunhours" << SunHours;
emit sunHoursUpdated(SunHours);
// StatusBar("Ready");
}
/*----------------------------------------------------------------------
;
; log SunCast data
;
; Date Time, SunHours, Vb, Ib, Va, Ia
;
-----------------------------------------------------------------------*/
void MainWindow::logData(const QHash<QString, float> &mpptData )
{
if( mpptData.size()==0 ) return;
QString sLog;
sLog.append( QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm") );
sLog.append( "," );
sLog.append( QString::number(SunHours.at(0),'g',3) );
sLog.append( "," );
sLog.append( QString::number(mpptData.value("Vb"),'g',4));
sLog.append( "," );
sLog.append( QString::number(mpptData.value("Ib"),'g',3));
sLog.append( "," );
sLog.append( QString::number(mpptData.value("Va"),'g',4));
sLog.append( "," );
sLog.append( QString::number(mpptData.value("Ia"),'g',3));
sLog.append( "," );
sLog.append( QString::number(mpptData.value("Tb"),'g',2));
// sLog.append( "," );
// -- write append into log file ---
qDebug() << sLog;
sLog.append( "\n");
QFile file( "suncast.log" );
if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
this->statusBar()->showMessage("Error: writing log file");
return;
}
QTextStream out(&file);
if( file.size()==0 )
out << "Date Time, SunHours, Vb, Ib, Va, Ia, Tb\n";
out << sLog;
file.close();
}
/*----------------------------------------------------------------------
;
; Display Mppt data
;
-----------------------------------------------------------------------*/
void MainWindow::displayMppt(const QHash<QString, float> &mpptData )
{
// qDebug() << "voltages" << mpptData;
if( mpptData.size()==0 ) {
ui->lcdVb->display( "---" );
ui->lcdSoC->display( "---" );
ui->lcdPower->display( "---" );
return;
}
ui->lcdVb->display( mpptData.value("Vb") );
ui->lcdPower->display( mpptData.value("Vb") * mpptData.value("Ib") );
QString s;
s.sprintf( "Va: %.2f", mpptData.value("Va"));
ui->VaLabel->setText( s );
s.sprintf( "Ia: %.1f", mpptData.value("Ia"));
ui->IaLabel->setText( s );
s.sprintf( "Ib: %.1f", mpptData.value("Ib"));
ui->IbLabel->setText( s );
s.sprintf( "Tb: %.0f", mpptData.value("Tb"));
ui->TbLabel->setText( s );
// == calculate SoC ================
float fSoC;
fSoC = 100*(mpptData.value("Vb")-BAT_VMIN)/(BAT_VMAX-BAT_VMIN);
if( fSoC<0.) fSoC = 0.;
if( fSoC>100.) fSoC = 100;
ui->lcdSoC->display( fSoC );
}
void MainWindow::on_pushButton_clicked()
{
}