zhaopengxslc 发表于 2014-9-10 22:57:17

python通过upd获取温湿度数据以及通过红外控制普通空调

#include <DHT.h>
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: [email protected] 12/30/2008
#include <SoftwareSerial.h>

#define DHTPIN 2   // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 2, 177);
unsigned int localPort = 8000;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer; //buffer to hold incoming packet,
charReplyBuffer;       // a string to send back
char tempBuf;

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
DHT dht(DHTPIN, DHTTYPE);

//Infrared communation infomation
byte InfrInitStart[]={0xAA,0xAA,0x08,0x08,0x00};
byte InfrInitEnd[]={0xCC,0xCC,0x08,0x08,0x00};
byte AirConVerion[]={0x02,0x00,0x2C,0x08,0x26};
//fit the air condition type code 0x2C is 044 (meidi KFR-72LW)
//if you want to control another air condition , you should change 0x2c
byte AirConTrunOn[]={0x04,0xff,0x08,0x08,0xfb};
byte AirConTurnOff[]={0x04,0x00,0x08,0x08,0x04};

//use pin 6 as receivepin and use pinas transmitpin
SoftwareSerial serial(6,7);


void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
dht.begin();
Serial.begin(9600);
serial.begin(9600);

   delay(1000);
//Initialize the infrared mode start
serial.write(InfrInitStart,5);
if(checkRespondeCode())
{
//Initialize the infread mode end
    serial.write(InfrInitEnd,5);
}
else
{
    exit(0);
}
if(checkRespondeCode())
{
   //set the air condition mode version
   serial.write(AirConVerion,5);
}
else
{
    exit(1);
}
if(checkRespondeCode()==false)
{
    exit(2);
}
}


boolean checkRespondeCode()
{
byte RXData;
delay(1000);
//Serial.println("check response code");
if(serial.available())
{
    delay(200);
    RXData=serial.read();
    //Serial.println(RXData,HEX);
    if(RXData==0x89)
    {
      //serial.println();
      Serial.println("xiang ying 89");
      return true;
    }
    else
    {
      Serial.println(RXData,HEX);
    }
}
Serial.println("no xiang ying");
return false;
}

float getnum(char* strcmd)
{
int i=0;
char temp;
for(i=3;i<strlen(strcmd);i++)
{
    temp=strcmd;
}
temp='\0';
return atof(temp);
}


float getHumidity()
{
float h = dht.readHumidity();
if(isnan(h))
{
    return 0;
}
return h;
}

float getTemperature()
{
float t = dht.readTemperature();
if(isnan(t))
{
    return 0;
}
return t;
}

void loop() {
float h=0.0,t=0.0;
//float h = dht.readHumidity();
//float t = dht.readTemperature();
/* if (isnan(t) || isnan(h))
{
    Serial.println("Failed to read from DHT");
}
else
{
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
}*/


// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote, DEC);
      if (i < 3)
      {
      Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
   
    switch(packetBuffer)
    {
      case '0'://control
      switch(packetBuffer)
      {
          case '0': //temperature and humility
            break;
          case '2': //air controler
            switch(packetBuffer)
            {
            case '0':
                if(getnum(packetBuffer)==0.0)
               {
                   Serial.println("turn on the air condition");
                   sprintf(ReplyBuffer,"turn on the air condition");
                   serial.write(AirConTrunOn,5);
                   Serial.println("0x04,0xff,0x08,0x08,0xfb");
                  
               }
               else if(getnum(packetBuffer)==1.0)
               {
                   Serial.println("turn off the air condition");
                   sprintf(ReplyBuffer,"turn off the air condition");
                   serial.write(AirConTurnOff,5);
                   Serial.println("0x04,0x00,0x08,0x08,0x04");
               }
                break;
            case '1':
                break;
            default:
                break;
            }
            break;
          default:
            break;
         
      }
      break;
      case '1'://informaiton
      switch(packetBuffer)
      {
          case '0': //temperature and humility
            switch(packetBuffer)
            {
               case '1':
               h=getHumidity();
               dtostrf(h,10,2,tempBuf);
               sprintf(ReplyBuffer,"100humidity:%s",tempBuf);               
               break;
            case '0'://temperature
                t=getTemperature();
                dtostrf(t,10,2,tempBuf);
                sprintf(ReplyBuffer,"100Temperature:%s",tempBuf);               
                break;

            /*case ://humility
                float h=getHumidity();
                sprintf(ReplyBuffer,"101Humidity:%f\n",h);
                break;*/
            }
            break;
         case '2': //air controler
             break;
      }
      break;
      default:
      Serial.println("Error");
    }
   
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    memset(packetBuffer,'\0',UDP_TX_PACKET_MAX_SIZE);

    // send a reply, to the IP address and port that sent us the packet we received
    Serial.println(ReplyBuffer);
    Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
}
delay(10);
}


from socket import *

HOST="192.168.2.177"
PORT=8000
BUFSIZE=1024
ADDR=(HOST,PORT)

udpCliSock=socket(AF_INET,SOCK_DGRAM)
while True:
    data=raw_input(">")
    if not data:
      break
    udpCliSock.sendto(data,ADDR)
    data,ADDR=udpCliSock.recvfrom(BUFSIZE)
    if not data:
      break
    print data,ADDR
udpCliSock.close()


说明:
输入100获取温度信息
输入101获取湿度信息
输入0200 打开空调
输入0201 关闭空调
页: [1]
查看完整版本: python通过upd获取温湿度数据以及通过红外控制普通空调