-2

In the project, there are three arduinos (stations). Two stations have sensors that capture the temperature and humidity of the environment and send it to the other station, which is the "central", by radio signals. The central receives data from the station with sensors. Currently, I can only make one station with sensors send data to the central, but I would like to make both stations with sensors send data simultaneously to the central. Suppose I put both sensors in different environments, and then they send this data to the central and then the central shows the data from each of these environments. Attached are the codes that I am currently using (only one sensor can be connected for the central to receive the data). please keep in mind that i have very little arduino knowledge

ARDUINO ESP 32 RF24 transceiver module

CENTRAL CODE

//#include <avr/pgmspace.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>



// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL)
// On an arduino ESP32:    21(SDA),  22(SCL)
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define SCREEN_WIDTH 128     // OLED display width, in pixels
#define SCREEN_HEIGHT 64     // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);



RF24 radio(4, 5);  // CE, CSN
const byte address[6] = "00001";


void setup() {

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;  // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000);  // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  //display.setFont(&FreeSerif9pt7b);
  display.setTextColor(WHITE);



  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
}

void loop() {

  //-------------


  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);


    //Imprime a mensagem recebida no display OLED
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("---------------------");
    display.println("- MENSAGEM RECEBIDA -");
    display.println("---------------------");
    display.println();
    display.println(text);
    display.display();

    //Imprime a mensagem recebida no terminal
    Serial.print("Msg. ");
    Serial.println(text);
  }





  //-------------

  //nao pode ter delay, pq senao corre um grande risco de perder mensagens enquanto dormia
}

SENSOR CODE

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DELAY_LOOP 2000

RF24 radio(4, 5);  // CE, CSN
const byte address[6] = "00001";



// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL)
// On an arduino ESP32:    21(SDA),  22(SCL)
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define SCREEN_WIDTH 128     // OLED display width, in pixels
#define SCREEN_HEIGHT 64     // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);



#define SENSOR_TEMPERATURA 32
DHT_Unified dht(SENSOR_TEMPERATURA, DHT11);  // configurando o Sensor DHT - pino e tipo
float umidade = 0;
float temperatura = 0;





void setup() {

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;  // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000);  // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  //display.setFont(&FreeSerif9pt7b);
  display.setTextColor(WHITE);


  dht.begin();  // inicializa a função
  Serial.println("Usando o Sensor DHT");
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);  // imprime os detalhes do Sensor de Temperatura
  Serial.println("------------------------------------");
  Serial.println("Temperatura");
  Serial.print("Sensor:       ");
  Serial.println(sensor.name);
  Serial.print("Valor max:    ");
  Serial.print(sensor.max_value);
  Serial.println(" *C");
  Serial.print("Valor min:    ");
  Serial.print(sensor.min_value);
  Serial.println(" *C");
  Serial.print("Resolucao:   ");
  Serial.print(sensor.resolution);
  Serial.println(" *C");
  dht.humidity().getSensor(&sensor);  // imprime os detalhes do Sensor de Umidade
  Serial.println("------------------------------------");
  Serial.println("Umidade");
  Serial.print("Sensor:       ");
  Serial.println(sensor.name);
  Serial.print("Valor max:    ");
  Serial.print(sensor.max_value);
  Serial.println("%");
  Serial.print("Valor min:    ");
  Serial.print(sensor.min_value);
  Serial.println(" %");
  Serial.print("Resolucao:   ");
  Serial.print(sensor.resolution);
  Serial.println(" %");
  Serial.println("------------------------------------");


  if ((sensor.min_delay / 1000) > DELAY_LOOP) {
    Serial.println(F("ERRO: O tempo de leitura minimo do sensor eh maior que o tempo do LOOP"));
    for (;;)
      ;  // Don't proceed, loop forever
  }


  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
}

int cont = 0;  //esse contador eh apenas para testar, na vida real ele iria estourar rapido
void loop() {

  //-------------

  // Faz a leitura da temperatura e umidade
  sensors_event_t event;

  dht.temperature().getEvent(&event);  // faz a leitura da Temperatura
  if (isnan(event.temperature)) {      // se algum erro na leitura
    Serial.println("Erro na leitura da Temperatura!");
  } else {
    Serial.print("Temperatura: ");  // imprime a Temperatura
    temperatura = event.temperature;
    Serial.print(temperatura);
    Serial.println(" *C");
  }

  dht.humidity().getEvent(&event);       // faz a leitura de umidade
  if (isnan(event.relative_humidity)) {  // se algum erro na leitura
    Serial.println("Erro na leitura da Umidade!");
  } else {
    Serial.print("Umidade: ");  // imprime a Umidade
    umidade = event.relative_humidity;
    Serial.print(umidade);
    Serial.println(" %");
  }

  //-------------

  // Imprime valores lidos no display OLED
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("---------------------");
  display.print("Temperatura. ");
  display.print(temperatura);
  display.println(" C");
  display.setCursor(0, 20);
  display.print("Umidade..... ");
  display.print(umidade);
  display.println(" %");
  display.println("---------------------");
  display.display();

  //-------------

  char buffer[25];
  sprintf(buffer, "%d: %.1fC %.0fU", cont, temperatura, umidade);
  cont++;

  display.setCursor(0, 48);
  display.print("Msg. ");
  display.print(buffer);
  display.display();

  Serial.print("Msg. ");
  Serial.println(buffer);
  Serial.println();


  radio.write(&buffer, sizeof(buffer));

  //-------------

  delay(DELAY_LOOP);
}

I tried to make a copy of the Sensor Code and change the const byte adress, but it didn't worked and I really didn't know what I was doing. I was hoping that this way each sensor station could send a different signal to the central, and at different time intervals to avoid conflict.

adryan
  • 1
  • 2

1 Answers1

0

In order to receive data from multiple nodes, it needs to setup multiple reading pipes on the Central.

const byte centralAddr[6] = "00001";
const byte station1Addr[6] = "00002";
const byte station2Addr[6] = "00003";

uint8_t pipeNum{0};

void setup() {
  // other setup codes

  radio.begin();
  radio.openWritingPipe(centralAddr);
  radio.openReadingPipe(1, station1Addr);
  radio.openReadingPipe(2, station2Addr);
  radio.startListening();
  radio.printDetails();
}

void loop() {
  if (radio.available(&pipeNum)) {
    if(pipeNum == 1) {
      Serial.print("From station1 ");
    }
    else if (pipe_num == 2) {
      Serial.print("From station2 ");
    }    
    char text[32]{0};
    int len = radio.getPayloadSize();
    radio.read(text, len);
  }
}
hcheung
  • 3,377
  • 3
  • 11
  • 23