Code for Weather Station

Hello everyone,

I’m quite new at arduino programming and I want to create a simple weather station that reads temperature and humidity values using a SHT31D sensor and transmits it wirelessly via NRF24 to another arduino and displays it on an OLED.

I followed a few online video/tutorials but none are using exactly these components. I managed to make the OLED display work, but the temperature is not being displayed.
I’m pretty sure the part of the code relating to the SHT sensor is wrong.

Here is the transmitter and receiver code, based on some code from the net and butchered by me.

Your help is greatly appreciated

Transmitter:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

Adafruit_SHT31 sht31 = Adafruit_SHT31();

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

struct package
  {
    float temperature = 0;
    float humidity = 0;
  };

typedef struct package Package;
Package data;

void setup() {
  Serial.begin(9600);
  sht31.begin(0x44);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_LOW);
  radio.stopListening();
  }

void loop() {
  Serial.println(data.temperature);
  data.temperature = sht31.readTemperature();
  data.humidity = sht31.readHumidity();
  radio.write(&data,sizeof(data));                                 
}

Receiver

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

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);

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

struct package
  {
   float temperature = 0;
   float humidity = 0;
  };

typedef struct package Package;
Package data;

const unsigned char PROGMEM arduino [] = {
<<REMOVED TO SAVE SPACE IN POST>>
};


void setup() {
  Serial.begin(9600
  );
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  display.clearDisplay();
  delay(1000);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening();               
  }

void loop() {

  if (radio.available())                //check when received data available
  {
    radio.read(&data, sizeof(data));

        Serial.print("Temperature : ");
    Serial.print(data.temperature);
    Serial.println("*C");
    delay(1000);
    Serial.print("Humidity : ");
    Serial.print(data.humidity);
    Serial.println("%");
    delay(1000);

  display.setTextSize(2);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(45, 10);
  display.println(data.temperature);


  display.setTextSize(2);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(45,40);
  display.println(data.humidity);

  display.setTextSize(1);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(106,5);
  display.println("o");
  display.setTextSize(2);
  display.setCursor(112,10 );
  display.println("C");
  display.setTextSize(2);
  display.setCursor(112,40 );
  display.println("%");
  display.setTextSize(1);
  display.setCursor(0,15);
  display.println("Temp...");
  display.setCursor(0,45);
  display.println("Hum...");
  display.display();
  display.clearDisplay();
  }

    else
  {
    Serial.println("Waiting for signal");
    display.setTextSize(1);
    display.setCursor(12,55);
    display.setTextColor(WHITE,BLACK);
    display.println("Waiting for signal");
    display.drawBitmap(0,0,arduino,128,64,WHITE);
    display.display();
    display.clearDisplay();
  }

}

Are you receiving any data in the receiver from the transmitter?

This looks suspiciously like something that has been posted here before:

struct package
  {
    float temperature = 0;
    float humidity = 0;
  };

typedef struct package Package;
Package data;

The declaration is wonky, use this declarations instead:

struct RF_PACKAGE { //Type name
  float temperature;
  float humidity;
} data; //Variable name

It looks like immediately after you have displayed your data, you call clearDisplay, which seems odd.

Danois90:
Are you receiving any data in the receiver from the transmitter?

This looks suspiciously like something that has been posted here before:

struct package

{
    float temperature = 0;
    float humidity = 0;
  };

typedef struct package Package;
Package data;




The declaration is wonky, use this declarations instead:



struct RF_PACKAGE { //Type name
  float temperature;
  float humidity;
} data; //Variable name

Thanks Danois90.
I haven't posted this before. I just created the account to seek help because I couldn't figure it out on my own.

I adjusted the code, but no change...data is not received.

I believe there is also something wrong with the transmitting/receiving part of the code.

radio.openWritingPipe(address);
radio.openReadingPipe(1, address);

vranghel:
I adjusted the code, but no change...data is not received.

Not received or not displayed? What is displayed in the serial monitor from the receiver?

wildbill:
It looks like immediately after you have displayed your data, you call clearDisplay, which seems odd.

Yes, that looks weird. But clearDisplay() only clears the buffer without sending it to the display, so that's not likely to be the problem.

I fiddled around with it, added the external antennas to the NRF24, but now it shows Temp: nan and Hum: nan

Serial monitor shows roughly the same:

Humidity : nan%
Temperature : nan*C

I’m wondering if I didnt connect the sensor properly:

SDA to A5
SCL to A4

Is this correct?

vranghel:
SDA to A5
SCL to A4

Is this correct?

Nope, other way round..

Thanks a lot Danois90!! Works now thanks to all your help.