la mayoria del codigo para suvir al arduino es innecesario.. :P
el codigo de sobra tampoco molesta osea que no veo por que quitarlo...
antes de iniciar el programa de arduino es necesario haber conectado la placa( se podria mejorar.. pero este programa es una pruebilla pequeña).
bueno pues pal que le interese hay lo tiene!
codigo para el arduino:
Código: Seleccionar todo
#define dht_dpin 10
#define potpin 0
#define moist 1
#define bomba 11
#define lm 2
byte bGlobalErr;//for passing error code back from complex functions.
byte dht_dat[4];//Array to hold the bytes sent from sensor.
int timer1;
int timer2;
char inByte;
//
float temp;
//
float brigth;
//
float thum;
//
float ttemp;
void setup(){
pinMode(bomba,OUTPUT);
InitDHT();//iniciamos el senosr DHT
Serial.begin(9600);
delay(1000);//esperamos 1000ms (recomendado x el DHT)
establishContact();
}//end func
void loop(){
if (Serial.available() > 0) {
inByte = Serial.read();
switch(inByte){
case 'A':
ReadDHT();
//enviamos...
Serial.print(dht_dat[0],DEC);
Serial.print('.');
Serial.print(dht_dat[1],DEC);
Serial.print(",");
Serial.print(dht_dat[2],DEC);
Serial.print('.');
Serial.print(dht_dat[3],DEC);
Serial.print(":");
break;
case 'B':
brigth = analogRead(potpin);
//enviamos...
Serial.print(brigth,DEC);
Serial.print(":");
break;
case 'C':
thum = analogRead(moist);
//enviamos...
Serial.print(thum,DEC);
Serial.print(":");
break;
case 'D':
ttemp = (5.0 * analogRead(lm)*100.0)/1023.0;
Serial.print(ttemp,DEC);
Serial.print(":");
break;
case 'E':
riego();
break;
}//endswitch
}//endif serialaviable
stop_riego();
}// end loop()
/*Below here: Only "black box" elements which can just be plugged
unchanged into programs. Provide InitDHT() and ReadDHT(), and a function
one of them uses.*/
void riego(){
timer1 = millis();
digitalWrite(bomba,HIGH);
}
void stop_riego(){
timer2 = millis();
if((timer2 - timer1) >= 5000){
digitalWrite(bomba,LOW);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("0,0,0"); // mandamos esta string asta establecer conexion con algun programa
delay(300);
}
}
void InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}//end func
void ReadDHT(){
/*Uses global variables dht_dat[0-4], and bGlobalErr to pass
"answer" back. bGlobalErr=0 if read went okay.
Depends on global dht_dpin for where to look for sensor.*/
bGlobalErr=0;
byte dht_in;
byte i;
// Send "start read and report" command to sensor....
// First: pull-down I/O pin for 23000us
digitalWrite(dht_dpin,LOW);
delay(23);
/*aosong.com datasheet for DHT22 says pin should be low at least
500us. I infer it can be low longer without any]
penalty apart from making "read sensor" process take
longer. */
//Next line: Brings line high again,
// second step in giving "start read..." command
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);//DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking line
//low. That low should last 80us. Acknowledges "start read
//and report" command.
//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);
delayMicroseconds(40);
dht_in=digitalRead(dht_dpin);
if(dht_in){
bGlobalErr=1;//dht start condition 1 not met
return;
}//end "if..."
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);
if(!dht_in){
bGlobalErr=2;//dht start condition 2 not met
return;
}//end "if..."
/*After 80us low, the line should be taken high for 80us by the
sensor. The low following that high is the start of the first
bit of the forty to come. The routine "read_dht_dat()"
expects to be called with the system already into this low.*/
delayMicroseconds(80);
//now ready for data reception... pick up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
//Next: restore pin to output duties
pinMode(dht_dpin,OUTPUT);
//Next: Make data line high again, as output from Arduino
digitalWrite(dht_dpin,HIGH);
//Next see if data received consistent with checksum received
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
/*Condition in following "if" says "if fifth byte from sensor
not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3;}//DHT checksum error
};//end ReadDHT()
byte read_dht_dat(){
//Collect 8 bits from datastream, return them interpreted
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code expects the system to have recently entered the
//dataline low condition at the start of every data bit's
//transmission BEFORE this function is called.
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(digitalRead(dht_dpin)==LOW);
//signalling end of start of bit's transmission.
//Dataline will now stay high for 27 or 70 uS, depending on
//whether a 0 or a 1 is being sent, respectively.
delayMicroseconds(30);//AFTER pin is high, wait further period, to be
//into the part of the timing diagram where a 0 or a 1 denotes
//the datum being send. The "further period" was 30uS in the software
//that this has been created from. I believe that a higher number
//(45?) might be more appropriate.
//Next: Wait while pin still high
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));// "add" (not just addition) the 1
//to the growing byte
//Next wait until pin goes low again, which signals the START
//of the NEXT bit's transmission.
while (digitalRead(dht_dpin)==HIGH);
}//end of "for.."
return result;
}//end of "read_dht_dat()"
Código: Seleccionar todo
#include <CommMG.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
_CommSetDllPath("C:\Program Files (x86)\AutoIt3\Include\commg.dll")
Global $x,$y, $lstx,$lsty
Func conecta($port,$baud)
If $baud = "" Then $baud = 9600
Local $sportSetError
ConsoleWrite(_CommListPorts(0))
_CommSetPort($port, $sportSetError, 9600, 8, "none",2,1)
if $sportSetError = '' Then
Return 1
ConsoleWrite("using " & _CommGetVersion(1))
Else
ConsoleWrite("error")
return $sportSetError
EndIf
EndFunc
Global $whe=1247, $whi=443
Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("Form1", $whe, $whi, -1, -1)
_GDIPlus_Startup ()
Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
$Label1 = GUICtrlCreateLabel("Tº", 8, 16, 15, 17)
$Label2 = GUICtrlCreateLabel("(S)", 1168, 400, 17, 17)
GUISetOnEvent($GUI_EVENT_CLOSE, "salir")
GUISetState(@SW_SHOW)
For $i = 0 To $whi Step $whi/10
_GDIPlus_GraphicsDrawLine($hGraphic,0,($whi-$i),$whe,($whi-$i))
GUICtrlCreateLabel($i/$whi*100,0,$whi-$i+3,15,17)
Next
For $i = 0 To $whe Step $whe/100
_GDIPlus_GraphicsDrawLine($hGraphic,($whe-$i),0,($whe-$i),$whi)
Next
conecta("3",9600)
$timer = TimerInit()
While 1
$y = get_DHT11()
$x = TimerDiff($timer)/1000*($whe/100)
GUICtrlSetData($Label2,$x/($whe/100))
If $x > $whe Then
$timer = TimerInit()
$x = 0
$y = 0
$lstx = 0
$lsty = 0
EndIf
If $lstx = 0 Then
$lstx = $x
$lsty = $whi-$y
EndIf
_GDIPlus_GraphicsDrawLine($hGraphic,$lstx,$lsty,$x,$whi-$y)
$lstx = $x
$lsty = $whi-$y
Sleep(500)
WEnd
Func salir()
Exit
EndFunc
Func get_DHT11()
_CommSendstring("D")
Sleep(100)
$rec = _CommGetLine(":", 20, 500)
$y = $rec*$whi/100
ConsoleWrite($rec&@CRLF)
Return $y
EndFunc