dht11 esp8266 wifiudp通信

  #include <ESP8266WiFi.h>
  #include <WiFiUdp.h>
  #include <SPI.h>
  #include <Wire.h>
  #include<stdlib.h>
  #include <dht11.h>                    //DHT11的头文件 
  
  const char *ssid = "force";
  const char *password = "xsjlaobi250.com";
  
  unsigned int UDPPort = 8888;      //本地端口号
  
  char packetBuffer1[255]; //buffer to hold incoming packet
  char packetBuffer2[255];
  char  ReplyBuffer1[] = "";       // a string to send back
  char  ReplyBuffer2[] = "";
  WiFiUDP Udp;
  
  //温度传感器加载
  dht11 DHT11;
  
  #define DHT11_PIN 2    //DHT11引脚,实际接线接入开发板的D4
  
  int temperature = 0; //存放温度的变量
  int humidity = 0; //存放湿度的变量
  
  // 复位或上电后运行一次:
  void setup() {
    //在这里加入初始化相关代码,只运行一次:
    Serial.begin(9600);  //初始化串口通信,并将波特率设置为115200
    //WiFi.mode ( WIFI_AP );//WIFI设为AP模式
    WiFi.begin ( ssid, password );//连接WIFI热点
   
    while ( WiFi.status() != WL_CONNECTED ) { //WIFI的连接状态
      delay ( 500 );
      Serial.print ( "." );//如果没有连通向串口发送.....
    }
  
    Serial.println ( "" );
    Serial.print ( "Connected to " );
    Serial.println ( ssid );
    Serial.print ( "IP address: " );
    Serial.println ( WiFi.localIP() );//返回获得的本机ip地址
    Udp.begin(UDPPort);// 开始UDP端口侦听
    pinMode(DHT11_PIN, OUTPUT);  //就引脚设为输出模式
    Serial.println();
    Serial.println("本地IP地址: " + WiFi.localIP().toString());//串口打印本地IP地址
  }
  
  
  void loop() {
    int packetSize = Udp.parsePacket();
    /**获取温湿度,后在串口打印出数据**/
    /**一定要有chk,不然不显示内容**/
    int chk1 = DHT11.read(DHT11_PIN);       //将读取到的值赋给chk
    temperature = DHT11.temperature;
    humidity = DHT11.humidity;
    Serial.print("Temperature: ");
    Serial.println(temperature);//串口打印温度
    Serial.print("humidity: ");
    Serial.println(humidity); //串口打印湿度
    itoa(temperature, ReplyBuffer1, 10);
    itoa(humidity, ReplyBuffer2, 10);
    delay(1000);
    Serial.println("本地IP地址: " + WiFi.localIP().toString());    //串口打印本地IP地址
    Serial.println("端口号: " + UDPPort);    //串口打印端口号
    /**如果接收到上位机发来的信息,就返回采集到温湿度数据给上位机**/
    // UDP广播数据到达处理
    if (packetSize) {
      Serial.print("Received packet of size ");
      Serial.println(packetSize);
      Serial.print("From ");
      IPAddress remoteIp = Udp.remoteIP();//获取远程广播包发送IP
      Serial.print(remoteIp);
      Serial.print(", port ");
      Serial.println(Udp.remotePort());
  
      // 将到达的数据包读入packetBufffer1,packetBufffer2
      int len1 = Udp.read(packetBuffer1, 255);
      int len2 = Udp.read(packetBuffer2, 255);
      if (len1 > 0) {
        packetBuffer1[len1] = 0;
      }
      if (len2 > 0) {
        packetBuffer2[len2] = 0;
      }
  
      Serial.println("Contents:");
      Serial.println(packetBuffer1);
      Serial.println(packetBuffer2);
  
      // 回复信息给广播发送IP
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write(ReplyBuffer1);
      Udp.write(ReplyBuffer2);
      Udp.endPacket();
  
    }
  
  }

WiFiUdpSendReceiveString.ino


/*
  WiFi UDP Send and Receive String

 This sketch wait an UDP packet on localPort using a WiFi shield.
 When a packet is received an Acknowledge packet is sent to the client on port remotePort

 Circuit:
 * WiFi shield attached

 created 30 December 2012
 by dlf (Metodo2 srl)

 */
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;
char ssid[] = "yourNetwork"; //  your network SSID (name)
char pass[] = "secretPassword";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

unsigned int localPort = 2390;      // local port to listen on

char packetBuffer[255]; //buffer to hold incoming packet
char  ReplyBuffer[] = "acknowledged";       // a string to send back

WiFiUDP Udp;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv != "1.1.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);
}

void loop() {

  // 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 remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
    Serial.println("Contents:");
    Serial.println(packetBuffer);

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


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值