Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Piman
#93751 Hello is there somebody who can help me with my code it's a PIR sensor using ESP NOW the code works but does not send pin activity some help please.. :!:
Code: Select all/*********
  andy
  Complete project details at
*********/

#include <ESP8266WiFi.h>
#include <espnow.h>

#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;
}
//}
User avatar
By cult_cages
#93753 I just got into sending data between esp boards, you should check out UDP. The terminology gets confusing so its better to start at the bottom. But, if your sending data, i just learned that UDP will only send char arrays, so i had to convert the float to string, then string to char array. Here's some websites to check out:

https://siytek.com/esp8266-udp-send-receive/
https://randomnerdtutorials.com/esp8266-web-server/