public ArduinoCOM() { size = 7; code = "AQ_ULSG"; firstInfo = false; sv = new SensorValue(); s = ""; }
public AppAirQuality(int width, int height, MySqlDBConnection mySql) { tLabel = "Temperatura"; cLabel = "CO₂"; dLabel = "Ponto de Orvalho"; pLabel = "PM2.5"; humLabel = "Humidade"; vLabel = "VOCs"; hLabel = "Sensação Térmica"; tUnit = "°C"; cUnit = "ppm"; dUnit = "°C"; pUnit = "ug/m3"; humUnit = "%"; vUnit = ""; hUnit = "°C"; goodLabel = "Bom"; moderateLabel = "Moderado"; badLabel = "Mau"; titleLabel = "Monitorização da Qualidade do ar"; big = "Big"; medium = "Medium"; small = "Small"; se = new SendEmail(); fontBrush = new SolidBrush(Color.FromArgb(8, 128, 186)); sf = new StringFormat(); config = new Button(); cf = new Config(); spaceInicialY = 0.2F * height; spaceInicialY1 = 0.25F * height; spaceInicialX = 0.05F * width; queueHeight = 0.35F * height; firstQueueWidth = 0.33F * width; secondQueueWidth = 0.25F * width; headerWidth = width; legendHeight = 0.10F * height; screenHeight = height; screenWidth = width; result = Resolution(width, height); mySqlDB = mySql; //Last value aux = new SensorValue(); }
public void ReceiveData(Object sender, Stack <SensorValue> allValues, MySqlDBConnection db) { Start : Thread.Sleep(5000); //the same on arduino program string[] separatingChars = { " " }; sp = (SerialPort)sender; //send handshake code to visual studio if (firstInfo == false) { sp.Write(code); firstInfo = true; } //read serial data from arduino program s = sp.ReadExisting(); indata = s.Split(separatingChars, StringSplitOptions.RemoveEmptyEntries); parsedValues = new int[size]; //verify the length of the packet sent by arduino if (indata.Length != size) { Array.Clear(indata, 0, indata.Length); goto Start; } //parse from string[] to int[] for (int i = 0; i < indata.Length; i++) { int.TryParse(indata[i], out parsedValues[i]); } Array.Clear(indata, 0, indata.Length); sv.c = parsedValues[0]; sv.v = parsedValues[1]; sv.r = Convert.ToSingle(parsedValues[2]) / 100; sv.temp = Convert.ToSingle(parsedValues[3]) / 100; sv.pm = parsedValues[4]; sv.h = Convert.ToSingle(parsedValues[5]) / 100; sv.d = fToCelsius(Convert.ToSingle(parsedValues[6]) / 100); sv.dateTime = DateTime.Now; Array.Clear(parsedValues, 0, parsedValues.Length); allValues.Push(sv); //save last read data on sensorvalues stack sv = new SensorValue(); Console.WriteLine("\n Sensor Values: "); Console.Write(allValues.Peek().ToString()); //print last read data Console.WriteLine(); }
public SensorValue SelectLastEntry() { string query = "SELECT * FROM sensorvalue ORDER BY ID DESC LIMIT 1"; //open connection try { OpenConnection(); //create command and assign the query and connection from the constructor MySqlCommand cmd = new MySqlCommand(query, connection); //Execute command cmd.ExecuteNonQuery(); adapter = new MySqlDataAdapter(cmd); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); //SensorValues auxiliar SensorValue aux = new SensorValue(); aux.c = Convert.ToInt32(dataTable.Rows[0][1]); aux.v = Convert.ToInt32(dataTable.Rows[0][2]); aux.r = Convert.ToInt32(dataTable.Rows[0][3]); aux.temp = Convert.ToInt32(dataTable.Rows[0][4]); aux.pm = Convert.ToInt32(dataTable.Rows[0][5]); aux.h = Convert.ToInt32(dataTable.Rows[0][6]); aux.d = Convert.ToInt32(dataTable.Rows[0][7]); //return dt.ToString(); return(aux); } catch (MySqlException ex) { throw new ApplicationException(ex.ToString()); } finally { //close connection CloseConnection(); } }
public void paintForm(object sender, PaintEventArgs e, Stack <SensorValue> allValues) { //Bitmap bm = new Bitmap((int)ScreenWidth, (int)ScreenHeight, PixelFormat.Format24bppRgb); //Graphics g = Graphics.FromImage(bm); Graphics g = e.Graphics; g.Clear(Color.White); // Set Bitmap background to white g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //make the graphics better g.TextRenderingHint = TextRenderingHint.AntiAlias; //text format Font titleFont = new Font("Microsoft Sans Serif", 5, FontStyle.Bold); Font labelsFont = new Font("Microsoft Sans Serif", 5, FontStyle.Bold); Font footerFont = new Font("Microsoft Sans Serif", 5, FontStyle.Bold); RectangleF rCO2 = new RectangleF(spaceInicialX, spaceInicialY1 + queueHeight, queueHeight * 0.90F, queueHeight * 0.90F); RectangleF rPM = new RectangleF(spaceInicialX + firstQueueWidth, spaceInicialY1 + queueHeight, queueHeight * 0.90F, queueHeight * 0.90F); RectangleF rVOC = new RectangleF(spaceInicialX + 2 * firstQueueWidth, spaceInicialY1 + queueHeight, queueHeight * 0.90F, queueHeight * 0.90F); RectangleF rT = new RectangleF(spaceInicialX / 2, spaceInicialY, queueHeight * 0.90F, queueHeight * 0.90F); RectangleF rRH = new RectangleF(spaceInicialX / 2 + secondQueueWidth, spaceInicialY, queueHeight * 0.90F, queueHeight * 0.90F); RectangleF rHI = new RectangleF(spaceInicialX / 2 + 2 * secondQueueWidth, spaceInicialY, queueHeight * 0.90F, queueHeight * 0.90F); RectangleF rDP = new RectangleF(spaceInicialX / 2 + 3 * secondQueueWidth, spaceInicialY, queueHeight * 0.90F, queueHeight * 0.90F); // spaceInicialY + queueHeight //legend RectangleF rGood = new RectangleF(secondQueueWidth - legendHeight * 0.2F, screenHeight * 0.9F + legendHeight * 0.50F, legendHeight * 0.2F, legendHeight * 0.2F); RectangleF rModerate = new RectangleF(2 * secondQueueWidth - legendHeight * 0.2F, screenHeight * 0.9F + legendHeight * 0.50F, legendHeight * 0.2F, legendHeight * 0.2F); RectangleF rBad = new RectangleF(3 * secondQueueWidth - legendHeight * 0.2F, screenHeight * 0.9F + legendHeight * 0.50F, legendHeight * 0.2F, legendHeight * 0.2F); //colors Color red = Color.FromArgb(196, 2, 8); Color green = Color.FromArgb(0, 77, 13); Color yellow = Color.FromArgb(255, 228, 56); //Pens, brushes SolidBrush redBrush = new SolidBrush(red); SolidBrush greenBrush = new SolidBrush(green); SolidBrush yellowBrush = new SolidBrush(yellow); Pen greenPen = new Pen(green, 30); Pen yellowPen = new Pen(yellow, 30); Pen redPen = new Pen(red, 30); Pen greenPenLegend = new Pen(green, 5); Pen yellowPenLegend = new Pen(yellow, 5); Pen redPenLegend = new Pen(red, 5); string mailBody = ""; //legend simbols g.DrawEllipse(greenPenLegend, rGood); g.DrawEllipse(yellowPenLegend, rModerate); g.DrawEllipse(redPenLegend, rBad); ////temperature if (allValues.Peek().tStatus() == "good") { g.DrawEllipse(greenPen, rT); } else if (allValues.Peek().tStatus() == "moderate") { g.DrawEllipse(yellowPen, rT); } else if (allValues.Peek().tStatus() == "bad") { g.DrawEllipse(redPen, rT); if (aux.tStatus() == "good" || aux.tStatus() == "moderate") { mailBody += "\nTemperatura: " + allValues.Peek().temp + tUnit; } } ////co2 if (allValues.Peek().cStatus() == "good") { g.DrawEllipse(greenPen, rCO2); } else if (allValues.Peek().cStatus() == "moderate") { g.DrawEllipse(yellowPen, rCO2); } else if (allValues.Peek().cStatus() == "bad") { g.DrawEllipse(redPen, rCO2); if (aux.cStatus() == "good" || aux.cStatus() == "moderate") { mailBody += "\nCO2: " + allValues.Peek().c + cUnit; } } ////Dew Point if (allValues.Peek().dStatus() == "good") { g.DrawEllipse(greenPen, rDP); } else if (allValues.Peek().dStatus() == "moderate") { g.DrawEllipse(yellowPen, rDP); } else if (allValues.Peek().dStatus() == "bad") { g.DrawEllipse(redPen, rDP); if (aux.dStatus() == "good" || aux.dStatus() == "moderate") { mailBody += "\nPonto de orvalho: " + allValues.Peek().d + dUnit; } } ////pm25 if (allValues.Peek().pStatus() == "good") { g.DrawEllipse(greenPen, rPM); } else if (allValues.Peek().pStatus() == "moderate") { g.DrawEllipse(yellowPen, rPM); } else if (allValues.Peek().pStatus() == "bad") { g.DrawEllipse(redPen, rPM); if (aux.pStatus() == "good" || aux.pStatus() == "moderate") { mailBody += "\nPM2.5: " + allValues.Peek().pm + pUnit; } } ////Relative Humidity if (allValues.Peek().rStatus() == "good") { g.DrawEllipse(greenPen, rRH); } else if (allValues.Peek().rStatus() == "moderate") { g.DrawEllipse(yellowPen, rRH); } else if (allValues.Peek().rStatus() == "bad") { g.DrawEllipse(redPen, rRH); if (aux.rStatus() == "good" || aux.rStatus() == "moderate") { mailBody += "\nHumidade: " + allValues.Peek().r + humUnit; } } ////VOC if (allValues.Peek().vStatus() == "good") { g.DrawEllipse(greenPen, rVOC); } else if (allValues.Peek().vStatus() == "moderate") { g.DrawEllipse(yellowPen, rVOC); } else if (allValues.Peek().vStatus() == "bad") { g.DrawEllipse(redPen, rVOC); if (aux.vStatus() == "good" || aux.vStatus() == "moderate") { mailBody += "\nVOC: " + allValues.Peek().v + vUnit; } } ////HI if (allValues.Peek().hStatus() == "good") { g.DrawEllipse(greenPen, rHI); } else if (allValues.Peek().hStatus() == "moderate") { g.DrawEllipse(yellowPen, rHI); } else if (allValues.Peek().hStatus() == "bad") { g.DrawEllipse(redPen, rHI); if (aux.hStatus() == "good" || aux.hStatus() == "moderate") { mailBody += "\nSensação térmica: " + allValues.Peek().h + hUnit; } } //labels if (Result.Equals(small) || Result.Equals(medium)) { titleFont = new Font("Microsoft Sans Serif", 50 * 0.5F, FontStyle.Bold); labelsFont = new Font("Microsoft Sans Serif", 20 * 0.5F, FontStyle.Bold); footerFont = new Font("Microsoft Sans Serif", 20 * 0.5F, FontStyle.Bold); } else if (Result.Equals(big)) { titleFont = new Font("Microsoft Sans Serif", 50, FontStyle.Bold); labelsFont = new Font("Microsoft Sans Serif", 20, FontStyle.Bold); footerFont = new Font("Microsoft Sans Serif", 20, FontStyle.Bold); } //title SizeF size = g.MeasureString(titleLabel, titleFont); g.DrawString(titleLabel, titleFont, fontBrush, (headerWidth - size.Width) / 2, (screenHeight * 0.2F - size.Height) / 2, sf); //CO2 size = g.MeasureString(cLabel, labelsFont); g.DrawString(cLabel, labelsFont, fontBrush, spaceInicialX + (queueHeight * 0.9F) * 0.5F - (size.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.20F, sf); //PM2.5 size = g.MeasureString(pLabel, labelsFont); g.DrawString(pLabel, labelsFont, fontBrush, firstQueueWidth + spaceInicialX + (queueHeight * 0.9F) * 0.5F - (size.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.20F, sf); //VOC size = g.MeasureString(vLabel, labelsFont); g.DrawString(vLabel, labelsFont, fontBrush, 2 * firstQueueWidth + spaceInicialX + (queueHeight * 0.9F) * 0.5F - (size.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.20F, sf); //Temperature size = g.MeasureString(tLabel, labelsFont); g.DrawString(tLabel, labelsFont, fontBrush, spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (size.Width / 2), (spaceInicialY) + (queueHeight * 0.9F) * 0.20F, sf); //Humidity size = g.MeasureString(humLabel, labelsFont); g.DrawString(humLabel, labelsFont, fontBrush, secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (size.Width / 2), (spaceInicialY) + (queueHeight * 0.9F) * 0.20F, sf); //Heat Index size = g.MeasureString(hLabel, labelsFont); g.DrawString(hLabel, labelsFont, fontBrush, 2 * secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (size.Width / 2), (spaceInicialY) + (queueHeight * 0.9F) * 0.20F, sf); //Dew Point size = g.MeasureString(dLabel, labelsFont); g.DrawString(dLabel, labelsFont, fontBrush, 3 * secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (size.Width / 2), (spaceInicialY) + (queueHeight * 0.9F) * 0.20F, sf); //update the sensor values updateValues(g, allValues); //footer size = g.MeasureString(goodLabel, footerFont); g.DrawString(goodLabel, footerFont, fontBrush, secondQueueWidth * 0.02F + secondQueueWidth, screenHeight * 0.9F + legendHeight * 0.50F + (legendHeight * 0.2F - size.Height) / 2, sf); size = g.MeasureString(moderateLabel, footerFont); g.DrawString(moderateLabel, footerFont, fontBrush, 2.02F * secondQueueWidth, screenHeight * 0.9F + legendHeight * 0.50F + (legendHeight * 0.2F - size.Height) / 2, sf); size = g.MeasureString(badLabel, footerFont); g.DrawString(badLabel, footerFont, fontBrush, 3.02F * secondQueueWidth, screenHeight * 0.9F + legendHeight * 0.50F + (legendHeight * 0.2F - size.Height) / 2, sf); //logos if (Result.Equals(small) || Result.Equals(medium)) { Image airQuality = Image.FromFile("logoApp_.png"); g.DrawImage(airQuality, new PointF((headerWidth * 0.2F - airQuality.Width) / 2, (screenHeight * 0.20F - airQuality.Height) / 2)); Image ulsg = Image.FromFile("ulsLogo_.png"); g.DrawImage(ulsg, new PointF(headerWidth * 0.8F + (headerWidth * 0.2F - airQuality.Width) / 2, (screenHeight * 0.20F - ulsg.Height) / 2)); Image temperature = Image.FromFile("co2_.png"); g.DrawImage(temperature, new PointF(spaceInicialX + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.60F)); Image co2 = Image.FromFile("pm_.png"); g.DrawImage(co2, new PointF(firstQueueWidth + spaceInicialX + (queueHeight * 0.9F) * 0.5F - (co2.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.60F)); Image dp = Image.FromFile("voc_.png"); g.DrawImage(dp, new PointF(2 * firstQueueWidth + spaceInicialX + (queueHeight * 0.9F) * 0.5F - (dp.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.60F)); Image pm = Image.FromFile("temp_.png"); g.DrawImage(pm, new PointF(spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); Image humidity = Image.FromFile("hm_.png"); g.DrawImage(humidity, new PointF(secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); Image voc = Image.FromFile("hi_.png"); g.DrawImage(voc, new PointF(2 * secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); Image hi = Image.FromFile("dp_.png"); g.DrawImage(hi, new PointF(3 * secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); } else if (result.Equals(big)) { Image airQuality = Image.FromFile("logoApp.png"); g.DrawImage(airQuality, new PointF((headerWidth * 0.2F - airQuality.Width) / 2, (screenHeight * 0.20F - airQuality.Height) / 2)); Image ulsg = Image.FromFile("ulsLogo.png"); g.DrawImage(ulsg, new PointF(headerWidth * 0.8F + (headerWidth * 0.2F - airQuality.Width) / 2, (screenHeight * 0.20F - ulsg.Height) / 2)); Image temperature = Image.FromFile("co2.png"); g.DrawImage(temperature, new PointF(spaceInicialX + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.60F)); Image co2 = Image.FromFile("pm.png"); g.DrawImage(co2, new PointF(firstQueueWidth + spaceInicialX + (queueHeight * 0.9F) * 0.5F - (co2.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.60F)); Image dp = Image.FromFile("voc.png"); g.DrawImage(dp, new PointF(2 * firstQueueWidth + spaceInicialX + (queueHeight * 0.9F) * 0.5F - (dp.Width / 2), spaceInicialY1 + queueHeight + (queueHeight * 0.9F) * 0.60F)); Image pm = Image.FromFile("temp.png"); g.DrawImage(pm, new PointF(spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); Image humidity = Image.FromFile("hm.png"); g.DrawImage(humidity, new PointF(secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); Image voc = Image.FromFile("hi.png"); g.DrawImage(voc, new PointF(2 * secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); Image hi = Image.FromFile("dp.png"); g.DrawImage(hi, new PointF(3 * secondQueueWidth + spaceInicialX / 2 + (queueHeight * 0.9F) * 0.5F - (temperature.Width / 2), spaceInicialY + (queueHeight * 0.9F) * 0.60F)); } // Send Mail Alarm if (mailBody.Length > 0) { se.Body = "Valores muito altos\n " + mailBody + "\nData e Hora: " + allValues.Peek().dateTime; se.SendAlarm(); } mailBody = ""; //Last Value inserted on database aux = mySqlDB.SelectLastEntry(); Console.WriteLine("Last values inserted on database: \n" + aux.ToString()); }