当前位置: 首页 > news >正文

WeMos D1+PIR+Android 的小场景制作

最近在做一个有趣的小场景功能,其实已经有成熟产品,但是考虑到没法实现场景扩展,所以自己开始动手做。

场景描述:玄关人体感应,有人进门,致欢迎词,有人离开,致欢送词。

硬件设备:WeMOS D1 + PIR 【HC-SR501 】 + Android手机

数据流:

从WeMOS D1开始

关于WeMos D1的参考链接:

Arduino文档阅读笔记-WeMos D1 ESP8266 WIFI开发板入门_wemos d1手册-CSDN博客

ArduiNo(WeMos D1)基础(一)_arduino d1-CSDN博客

ESP8266之WiFiClient库学习-CSDN博客

WeMos D1主要是是作为TCPClient,将接收到的PIR信息推送给android系统,并接收反馈信息

/*
    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/

#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

const char* host = "djxmmx.net";
const uint16_t port = 17;

void setup() {
  Serial.begin(115200);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  static bool wait = false;

  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }

  // This will send a string to the server
  Serial.println("sending data to server");
  if (client.connected()) { client.println("hello from ESP8266"); }

  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  if (wait) {
    delay(300000);  // execute once every 5 minutes, don't flood remote service
  }
  wait = true;
}

PIR功能的开发测试链接:

在 Arduino 上使用 HC-SR501 人体热释电(PIR)传感器 – Arduino 实验室 (nxez.com)

其中的有些代码还是要调整的:

int pirPin = D5;  // PIR传感器连接的引脚
 
void setup() {
  pinMode(pirPin, INPUT);  // 将PIR传感器引脚设置为输入模式
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);      // 初始化串口通信
}
 
void loop() {
  int pirValue = digitalRead(pirPin); // 读取PIR传感器的值
 
  if (pirValue == HIGH) { // 如果检测到运动
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);  
    Serial.println("Motion detected!"); // 在串口打印消息
    delay(1000); // 延迟1秒
  } else {
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);
  }
}

通过Android系统,搭建TCPServer接收 WeMos发送的PIR的测试信息.

Android功能待更新...

相关文章:

  • freertos源码分析DAY12 (软件定时器)
  • 【第14章:神经符号集成与可解释AI—14.1 神经符号AI系统的基本原理与实现方法】
  • 一款简单的弹窗打赏页HTML源码
  • python入门详解
  • EasyRTC智能硬件:小体积,大能量,开启音视频互动新体验
  • ORB-SLAM3的源码学习: Settings.cc:Settings::readCamera1/readCamera2 从配置文件中加载相机参数
  • 【信息学奥赛一本通 C++题解】1282:最大子矩阵
  • Linux 文件与目录命令学习记录
  • 语音识别中的MFCC特征提取:时频分析如何转化为机器可理解的声学参数?(附完整代码实现)
  • Python常见面试题的详解7
  • Python爬虫系列教程之第四篇:数据存储与管理
  • Kubernetes-node(节点) 组件
  • Java 包装类详解
  • 04运维实用篇(D4_日志)
  • Windows Server 中配置 Active Directory:从零到精通
  • Kubernetes 概述
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_cycle_t 类型
  • 数据库基本概念及基本使用
  • AcWing 801. 二进制中1的个数
  • kamailio常见问题解答
  • 广东早熟荔枝“抢滩”上海,向长三角消费者喊话:包甜,管够
  • 上海浦东机场1号、2号航站楼均推出国内出发安检24小时服务
  • 上海护师邢红获第50届南丁格尔奖,她为何能摘得护理界最高荣誉
  • “春申阡陌”漆画展:将传统漆艺融入现代创作
  • 一生要出片的年轻人,买爆相机
  • 印称一名高级官员在巴基斯坦发动的袭击中死亡