Vou deixar aqui os dois então:
Codigo C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SerialPort conexao = new SerialPort("COM3", 9600);
public Form1()
{
InitializeComponent();
}
private void open(object sender, EventArgs e)
{
conexao.Open();
}
private void close(object sender, EventArgs e)
{
conexao.Close();
}
private void bt_ligar_Click(object sender, EventArgs e)
{
for (;;)
{
conexao.Write("1");
string retorno = conexao.ReadLine();
hum_tb.Text = retorno;
Thread.Sleep(3000);
}
}
private void bt_desligar_Click(object sender, EventArgs e)
{
for (;;)
{
conexao.Write("0");
string retorno = conexao.ReadLine();
textBox1.Text = retorno + "\n";
Thread.Sleep(30000);
}
}
private void button3_Click(object sender, EventArgs e)
{
conexao.Write("0");
string retorno2 = conexao.ReadLine();
textBox1.Text = retorno2;
conexao.Write("1");
string retorno = conexao.ReadLine();
hum_tb.Text = retorno;
conexao.Write("2");
string retorno3 = conexao.ReadLine();
humt_tb.Text = retorno3;
}
private void rtb_resposta_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
string retorno = conexao.ReadExisting();
rtb_resposta.Text = retorno;
}
private void bt_humt_Click(object sender, EventArgs e)
{
for (;;)
{
conexao.Write("2");
string retorno3 = conexao.ReadLine();
humt_tb.Text = retorno3 + "\n";
Thread.Sleep(30000);
}
}
}
}
Codigo Arduino:
//SSIR V 1.0 em 19/11/2015
//Porgramação do Sistema Solar Inteligente para Rega V1.0
//Programado por Bruno Pereira
//Carrega as Bibliotecas Necessarias para o Programa
#include<Wire.h>
#include<Adafruit_BMP085.h>
#include<LiquidCrystal_I2C.h>
#include<DHT.h>
#include<RTClib.h>
#include <Adafruit_BMP085.h>
//Define os Pinos do DHT22 e o RTC
#define DHTPIN 2
#define DHTTYPE DHT22
RTC_DS1307 RTC; //Modulo RTC
//Define o Endereço do LCD
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3, POSITIVE);
Adafruit_BMP085 bmp180;
int mostrador = 0;
//Declara o DHT
DHT dht(DHTPIN, DHTTYPE);
//Declara Variaveis
int humidade;
int pino_d = 7; //Pino ligado ao D7 do sensor
int pino_a = A1; //Pino ligado ao A1 do sensor
int val_d = 0; //Armazena o valor lido do pino digital
int val_a = 0; //Armazena o valor lido do pino analogico
const int sensorPin = A2;
int liquid_level;
int SENSOR = A3;
int D = 0;
void setup()
{
//Inicia a Serial
Serial.begin(9600);
//Inicia o Sensor de Pressão
bmp180.begin();
// Inicia o Sensor DHT22 e Define os Pinos
dht.begin();
pinMode(pino_d, INPUT);
pinMode(pino_a, INPUT);
pinMode(sensorPin, INPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
lcd.begin (20,4);
lcd.setCursor(0,0);
lcd.print("---- SSIR V1.0 ----");
lcd.setCursor(3,2);
lcd.print("***BEM VINDO***");
delay(3000);
lcd.clear();
digitalWrite(7,LOW);
digitalWrite(9,LOW);
}
void loop()
{
//Faz a leitura da Interface Serial do C#
char leitura = Serial.read();
//Indica os Valores lidos pelo BMP180
//Variavel Sensor Humidade da Terra
humidade = analogRead(A0);
int Porcento = map(humidade, 1023, 0, 0, 100);
//Variavel do Sensor DHT22
float h = dht.readHumidity();
float t = dht.readTemperature();
// Le os parametros enviados pelo C# para ler Humidade
if (leitura == '1')
{
Serial.println(h);
}
else
{
}
// Le os parametros enviados pelo C# para ler Temperatura
if (leitura == '0')
{
Serial.println(t);
}
else
{
//Le os parametros enviados pelo C# para ler Humidade da Terra
if (leitura == '2')
{
Serial.println(Porcento);
}
else
{
}
//Define os dados do Ecra Principal
lcd.setCursor(0,0);
lcd.print("---- SSIR V1.0 ----");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("---- SSIR V1.0 ----");
lcd.setCursor(0,1);
lcd.print("Humidade: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0,2);
lcd.print("Temperatura: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0,3);
lcd.print("Humidade T. ");
lcd.print(Porcento);
lcd.print("%");
delay(2000);
//Declara função em caso de Humidade da Terra abaixo de 30%
if(Porcento <30)
{
digitalWrite(9,HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("------ ALERTA ------");
lcd.setCursor(5,2);
lcd.print("HUM. BAIXA");
lcd.setCursor(8,3);
lcd.print(Porcento);
lcd.print("%");
delay(2000);
}
else
{
lcd.setCursor(0,0);
lcd.print("---- SSIR V1.0 ----");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("---- SSIR V1.0 ----");
lcd.setCursor(0,1);
lcd.print("Humidade: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0,2);
lcd.print("Temperatura: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0,3);
lcd.print("Humidade T. ");
lcd.print(Porcento);
lcd.print("%");
delay(2000);
}
//Declara Função em Caso de Temperatura abaixo dos 20C
if(t < 20)
{
digitalWrite(7,HIGH);
delay(500);
digitalWrite(7,LOW);
delay(500);
digitalWrite(7,HIGH);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("------ ALERTA ------");
lcd.setCursor(5,2);
lcd.print("TEMP.BAIXA");
lcd.setCursor(8,3);
lcd.print(t);
lcd.print("C");
delay(2000);
}
else
{
lcd.setCursor(0,0);
lcd.print("---- SSIR V1.0 ----");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("---- SSIR V1.0 ----");
lcd.setCursor(0,1);
lcd.print("Humidade: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0,2);
lcd.print("Temperatura: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0,3);
lcd.print("Humidade T. ");
lcd.print(Porcento);
lcd.print("%");
delay(2000);
}
}
}
Obrigado pela ajuda!