完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
有人可以帮助我处理我的代码吗?它是一个使用 ESP 的 PIR 传感器,现在代码可以工作,但不会发送 pin 活动,请提供一些帮助......
代码:全选/********* andy Complete project details at *********/ #include #include #define MESH_ID 6734922 #define GROUP_SWITCH 1 #define GROUP_HT 2 #define GROUP_MOtiON 3 #define timeSeconds 10 uint8_t receiverAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; bool ack_received; typedef struct struct_message { int mesh_id; // int motionSensor; uint8_t sensor_id[6]; byte category; int status; float temperature; float humidity; float battery; } struct_message; struct_message msg; // Set GPIOs for LED and PIR Motion Sensor const int led = 2; const int motionSensor = 5; // Timer: Auxiliary variables unsigned long now = millis(); unsigned long lastTrigger = 0; boolean startTimer = false; // Checks if motion was detected, sets LED HIGH and starts a timer void IRAM_ATTR detectsMovement() { Serial.println("MOTION DETECTED!!!"); digitalWrite(led, HIGH); startTimer = true; lastTrigger = millis(); } void setup() { // Serial port for debugging purposes // Serial.begin(115200); // PIR Motion Sensor mode INPUT_PULLUP pinMode(motionSensor, INPUT_PULLUP); digitalRead(motionSensor); // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING); // Set LED to LOW pinMode(led, OUTPUT); digitalWrite(led, LOW); ack_received = false; WiFi.mode(WIFI_STA); if (esp_now_init() != 0) { delay(100); ESP.restart(); } esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); esp_now_register_send_cb(OnDataSent); esp_now_add_peer(receiverAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); sendReading(); } void loop() { // Current time now = millis(); // Turn off the LED after the number of seconds defined in the timeSeconds variable if(startTimer && (now - lastTrigger > (timeSeconds*1000))) { Serial.println("Motion stopped..."); digitalWrite(led, LOW); startTimer = false; } // #ifdef TEST_NODE // sendReading(); // delay(5000); //#else // if ( ack_received == true) gotoSleep(); //#endif //if ( millis() >= longest_up_time) gotoSleep(); } void sendReading() { msg.mesh_id = MESH_ID; msg.category = GROUP_MOTION; // GROUP_SWITCH or GROUP_MOTION WiFi.macAddress(msg.sensor_id); msg.status = digitalRead(motionSensor); msg.battery = analogRead(A0) * 4.2 / 1023; // voltage divider //#ifdef TEST_NODE // msg.status = random(0, 2); // msg.battery = 3.5; //#endif esp_now_send(receiverAddress, (uint8_t *) &msg, sizeof(msg)); } void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { ack_received = true; } //} |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
988 浏览 1 评论
553浏览 6评论
463浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
447浏览 5评论
448浏览 4评论
421浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-29 04:13 , Processed in 0.835022 second(s), Total 74, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号