-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog.cpp
More file actions
183 lines (160 loc) · 6.65 KB
/
Copy pathdialog.cpp
File metadata and controls
183 lines (160 loc) · 6.65 KB
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
#include "dialog.h"
#include "ui_dialog.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <string>
#include <QDebug>
#include <QMessageBox>
#include <QProcess>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QSqlDatabase mydb= QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("/home/pi/Desktop/Temperature_Humidity.db");
if(!mydb.open())
QMessageBox::information(this,"Not Connected","Database is not connected");
else
QMessageBox::information(this," Connected","Database is connected");
QSqlQuery qry;
ui->LCD_Temp->display("-------");
arduino = new QSerialPort(this);
serialBuffer = "";
parsed_data = "";
temperature_value = 0.0;
/*
* Testing code, prints the description, vendor id, and product id of all ports.
* Used it to determine the values for the arduino uno.
*
*
qDebug() << "Number of ports: " << QSerialPortInfo::availablePorts().length() << "\n";
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
qDebug() << "Description: " << serialPortInfo.description() << "\n";
qDebug() << "Has vendor id?: " << serialPortInfo.hasVendorIdentifier() << "\n";
qDebug() << "Vendor ID: " << serialPortInfo.vendorIdentifier() << "\n";
qDebug() << "Has product id?: " << serialPortInfo.hasProductIdentifier() << "\n";
qDebug() << "Product ID: " << serialPortInfo.productIdentifier() << "\n";
}
*/
/*
* Identify the port the arduino uno is on.
*/
bool arduino_is_available = false;
QString arduino_uno_port_name;
//
// For each available serial port
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
// check if the serialport has both a product identifier and a vendor identifier
if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()){
// check if the product ID and the vendor ID match those of the arduino uno
if((serialPortInfo.productIdentifier() == arduino_uno_product_id)
&& (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id)){
arduino_is_available = true; // arduino uno is available on this port
arduino_uno_port_name = serialPortInfo.portName();
}
}
}
/*
* Open and configure the arduino port if available
*/
if(arduino_is_available){
qDebug() << "Found the arduino port...\n";
arduino->setPortName(arduino_uno_port_name);
arduino->open(QSerialPort::ReadOnly);
arduino->setBaudRate(QSerialPort::Baud9600);
arduino->setDataBits(QSerialPort::Data8);
arduino->setFlowControl(QSerialPort::NoFlowControl);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
serialData.clear();
}else{
qDebug() << "Couldn't find the correct port for the arduino.\n";
QMessageBox::information(this, "Serial Port Error", "Couldn't open serial port to arduino.");
}
}
Dialog::~Dialog()
{
if(arduino->isOpen()){
arduino->close(); // Close the serial port if it's open.
}
delete ui;
}
void Dialog::readSerial()
{
/*
* readyRead() doesn't guarantee that the entire message will be received all at once.
* The message can arrive split into parts. Need to buffer the serial data and then parse for the temperature value.
*
*/
QStringList buffer_split = serialBuffer.split(","); // split the serialBuffer string, parsing with ',' as the separator
// Check to see if there less than 3 tokens in buffer_split.
// If there are at least 3 then this means there were 2 commas,
// means there is a parsed temperature value as the second token (between 2 commas)
if(buffer_split.length() < 5){
// no parsed value yet so continue accumulating bytes from serial in the buffer.
serialData = arduino->readAll();
serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
serialData.clear();
}else{
// the second element of buffer_split is parsed correctly, update the temperature value on temp_lcdNumber
serialBuffer = "";
qDebug() << buffer_split << "\n";
parsed_data = buffer_split[1];
temperature_value = parsed_data.toDouble(); // convert to fahrenheit
qDebug() << "Temperature: " << temperature_value << "\n";
parsed_data = QString::number(temperature_value, 'g', 4); // format precision of temperature_value to 4 digits or fewer
Dialog::updateTemperature(parsed_data);
parsed_data1 = buffer_split[3];
humidity_value = 1024-parsed_data1.toDouble(); // convert to fahrenheit
qDebug() << "Humidity: " << humidity_value << "\n";
parsed_data1 = QString::number(humidity_value, 'g', 4); // format precision of temperature_value to 4 digits or fewer
Dialog::updateHumidity(parsed_data1);
double Temperature1=temperature_value;
double Humidity1=humidity_value;
QSqlQuery qry;
qry.prepare("INSERT INTO Temperature_Humidity (Temperature, Humidity) VALUES (?,?);");
qry.addBindValue(Temperature1);
qry.addBindValue(Humidity1);
qry.exec();
QFile file("/home/pi/Desktop/temperature.txt");
QFile file1("/home/pi/Desktop/humidity.txt");
if(!file.open(QFile::WriteOnly | QFile::Text) ){
QMessageBox::warning(this,"title","file not open");
}
if(!file1.open(QFile::WriteOnly | QFile::Text)){
QMessageBox::warning(this,"title","file not open");
}
QTextStream out(&file);
QTextStream out1(&file1);
QString temperature = parsed_data;
out <<temperature;
QString humidity = parsed_data1;
out1<<humidity;
file.flush();
file.close();
file1.flush();
file1.close();
}
}
void Dialog::updateTemperature(QString sensor_reading)
{
// update the value displayed on the lcdNumber
ui->LCD_Temp->display(sensor_reading);
}
void Dialog::updateHumidity(QString sensor_reading)
{
// update the value displayed on the lcdNumber
ui->LCD_Humidity->display(sensor_reading);
}
void Dialog::on_pushButton_clicked()
{
double Temperature=temperature_value;
double Humidity=humidity_value;
QSqlQuery qry;
qry.prepare("INSERT INTO Temperature_Humidity_Button (Temperature, Humidity) VALUES (?,?);");
qry.addBindValue(Temperature);
qry.addBindValue(Humidity);
qry.exec();
}