#include <DHT.h>
DHT dht;
void setup() {
Serial.begin(9600);
dht.setup(2);
}
void loop() {
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.print(humidity, 1);
Serial.print("\t");
Serial.println(temperature, 1);
}
Code Visual C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace Arduino2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
String[] strPortName = SerialPort.GetPortNames();
foreach (string n in strPortName)
{
comboBox1.Items.Add(n);
}
comboBox1.SelectedIndex = 0;
button2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.SelectedItem + "";
serialPort1.Open();
button1.Enabled = false;
button2.Enabled = true;
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
textBox1.AppendText(serialPort1.ReadLine());
textBox1.AppendText("\n");
string createText = serialPort1.ReadLine() + Environment.NewLine;
File.AppendAllText(@"D:\data_humi.txt", createText);
}
}
catch
{
MessageBox.Show(e.ToString());
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
serialPort1.Close();
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Close();
button1.Enabled = true;
button2.Enabled = false;
}
}
}
Code Matlab
clc;
data = importdata('D:/data_humi.txt');
subplot(2,1,1);
plot(1:length(data),data(:,1),'r','LineWidth',3)
legend('Humi')
xlabel('Time(sec)');
ylabel('%Humi');
grid on
hold on
subplot(2,1,2);
plot(1:length(data),data(:,2),'b','LineWidth',3)
legend('Temp(C)')
xlabel('Time(sec)');
ylabel('Celsius(C)');
grid on