极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4865|回复: 0

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

[复制链接]
发表于 2014-9-10 22:57:17 | 显示全部楼层 |阅读模式
  1. #include <DHT.h>
  2. #include <SPI.h>         // needed for Arduino versions later than 0018
  3. #include <Ethernet.h>
  4. #include <EthernetUdp.h>         // UDP library from: [email][email protected][/email] 12/30/2008
  5. #include <SoftwareSerial.h>

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

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

  14. // buffers for receiving and sending data
  15. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  16. char  ReplyBuffer[UDP_TX_PACKET_MAX_SIZE];       // a string to send back
  17. char tempBuf[UDP_TX_PACKET_MAX_SIZE];

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

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

  29. //use pin 6 as receivepin and use pin  as transmitpin
  30. SoftwareSerial serial(6,7);


  31. void setup() {
  32.   // start the Ethernet and UDP:
  33.   Ethernet.begin(mac,ip);
  34.   Udp.begin(localPort);
  35.   dht.begin();
  36.   Serial.begin(9600);
  37.   serial.begin(9600);
  38.   
  39.    delay(1000);  
  40.   //Initialize the infrared mode start
  41.   serial.write(InfrInitStart,5);
  42.   if(checkRespondeCode())
  43.   {
  44.   //Initialize the infread mode end
  45.     serial.write(InfrInitEnd,5);
  46.   }
  47.   else
  48.   {
  49.     exit(0);
  50.   }
  51.   if(checkRespondeCode())
  52.   {
  53.    //set the air condition mode version
  54.      serial.write(AirConVerion,5);
  55.   }  
  56.   else
  57.   {
  58.     exit(1);
  59.   }
  60.   if(checkRespondeCode()==false)
  61.   {
  62.     exit(2);
  63.   }
  64. }


  65. boolean checkRespondeCode()
  66. {
  67.   byte RXData;
  68.   delay(1000);
  69.   //Serial.println("check response code");
  70.   if(serial.available())
  71.   {
  72.     delay(200);
  73.     RXData=serial.read();
  74.     //Serial.println(RXData,HEX);
  75.     if(RXData==0x89)
  76.     {
  77.       //serial.println();
  78.       Serial.println("xiang ying 89");
  79.       return true;
  80.     }
  81.     else
  82.     {
  83.       Serial.println(RXData,HEX);
  84.     }
  85.   }
  86.   Serial.println("no xiang ying");
  87.   return false;
  88. }

  89. float getnum(char* strcmd)
  90. {
  91.   int i=0;
  92.   char temp[100];
  93.   for(i=3;i<strlen(strcmd);i++)
  94.   {
  95.     temp[i-3]=strcmd[i];
  96.   }
  97.   temp[i-3]='\0';
  98.   return atof(temp);
  99. }


  100. float getHumidity()
  101. {
  102.   float h = dht.readHumidity();
  103.   if(isnan(h))
  104.   {
  105.     return 0;
  106.   }
  107.   return h;
  108. }

  109. float getTemperature()
  110. {
  111.   float t = dht.readTemperature();
  112.   if(isnan(t))
  113.   {
  114.     return 0;
  115.   }
  116.   return t;
  117. }

  118. void loop() {
  119.   float h=0.0,t=0.0;
  120.   //float h = dht.readHumidity();
  121.   //float t = dht.readTemperature();
  122. /* if (isnan(t) || isnan(h))
  123.   {
  124.     Serial.println("Failed to read from DHT");
  125.   }
  126.   else
  127.   {
  128.     Serial.print("Humidity: ");
  129.     Serial.print(h);
  130.     Serial.print(" %\t");
  131.     Serial.print("Temperature: ");
  132.     Serial.print(t);
  133.     Serial.println(" *C");
  134.   }*/
  135.   
  136.   
  137.   // if there's data available, read a packet
  138.   int packetSize = Udp.parsePacket();
  139.   if(packetSize)
  140.   {
  141.     Serial.print("Received packet of size ");
  142.     Serial.println(packetSize);
  143.     Serial.print("From ");
  144.     IPAddress remote = Udp.remoteIP();
  145.     for (int i =0; i < 4; i++)
  146.     {
  147.       Serial.print(remote[i], DEC);
  148.       if (i < 3)
  149.       {
  150.         Serial.print(".");
  151.       }
  152.     }
  153.     Serial.print(", port ");
  154.     Serial.println(Udp.remotePort());

  155.     // read the packet into packetBufffer
  156.     Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
  157.    
  158.     switch(packetBuffer[0])
  159.     {
  160.       case '0':  //control
  161.         switch(packetBuffer[1])
  162.         {
  163.           case '0': //temperature and humility
  164.             break;
  165.           case '2': //air controler
  166.             switch(packetBuffer[2])
  167.             {
  168.               case '0':
  169.                 if(getnum(packetBuffer)==0.0)
  170.                  {
  171.                    Serial.println("turn on the air condition");
  172.                    sprintf(ReplyBuffer,"turn on the air condition");
  173.                    serial.write(AirConTrunOn,5);
  174.                    Serial.println("0x04,0xff,0x08,0x08,0xfb");
  175.                   
  176.                  }
  177.                  else if(getnum(packetBuffer)==1.0)
  178.                  {
  179.                    Serial.println("turn off the air condition");
  180.                    sprintf(ReplyBuffer,"turn off the air condition");
  181.                    serial.write(AirConTurnOff,5);
  182.                    Serial.println("0x04,0x00,0x08,0x08,0x04");
  183.                  }
  184.                 break;
  185.               case '1':
  186.                 break;
  187.               default:
  188.                 break;
  189.             }
  190.             break;
  191.           default:
  192.             break;
  193.            
  194.         }
  195.         break;
  196.       case '1':  //informaiton
  197.         switch(packetBuffer[1])
  198.         {
  199.           case '0': //temperature and humility
  200.             switch(packetBuffer[2])
  201.             {
  202.                case '1':
  203.                  h=getHumidity();
  204.                  dtostrf(h,10,2,tempBuf);
  205.                  sprintf(ReplyBuffer,"100humidity:%s",tempBuf);                 
  206.                  break;
  207.               case '0':  //temperature
  208.                 t=getTemperature();
  209.                 dtostrf(t,10,2,tempBuf);
  210.                 sprintf(ReplyBuffer,"100Temperature:%s",tempBuf);               
  211.                 break;

  212.               /*case :  //humility
  213.                 float h=getHumidity();
  214.                 sprintf(ReplyBuffer,"101Humidity:%f\n",h);
  215.                 break;*/
  216.             }
  217.             break;
  218.            case '2': //air controler
  219.              break;
  220.         }
  221.         break;
  222.       default:
  223.         Serial.println("Error");
  224.     }
  225.    
  226.     Serial.println("Contents:");
  227.     Serial.println(packetBuffer);
  228.     memset(packetBuffer,'\0',UDP_TX_PACKET_MAX_SIZE);

  229.     // send a reply, to the IP address and port that sent us the packet we received
  230.     Serial.println(ReplyBuffer);
  231.     Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
  232.     Udp.write(ReplyBuffer);
  233.     Udp.endPacket();
  234.   }
  235.   delay(10);
  236. }
复制代码


[pre lang="python" line="1"]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()
[/code]

说明:
输入100获取温度信息
输入101获取湿度信息
输入0200 打开空调
输入0201 关闭空调
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 16:34 , Processed in 0.095792 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表