|
|
- #include <DHT.h>
- #include <SPI.h> // needed for Arduino versions later than 0018
- #include <Ethernet.h>
- #include <EthernetUdp.h> // UDP library from: [email][email protected][/email] 12/30/2008
- #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[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
- char ReplyBuffer[UDP_TX_PACKET_MAX_SIZE]; // a string to send back
- char tempBuf[UDP_TX_PACKET_MAX_SIZE];
- // An EthernetUDP instance to let us send and receive packets over UDP
- EthernetUDP Udp;
- DHT dht(DHTPIN, DHTTYPE);
- void setup() {
- // start the Ethernet and UDP:
- Ethernet.begin(mac,ip);
- Udp.begin(localPort);
- dht.begin();
- Serial.begin(9600);
- }
- 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[i], 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[0])
- {
- case '0': //control
- break;
- case '1': //informaiton
- switch(packetBuffer[1])
- {
- case '0': //temperature and humility
- switch(packetBuffer[2])
- {
- 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 '1': //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);
- }
复制代码
[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获取湿度值
|
|