It a revised version of my sensor hub but I keep getting intermittent disconnects. The whole purpose was to move the BMP280 to the main unit rather than the battery powered remote. I’m still trying to trace down what is making it freeze when the wire.h library is called to begin in setup. I’ve tested it on a 32bit sparkfun board, a nano, and a uno…different libraries… all freeze at the same time. very strange. This code below is without the BMP280. The remote code is down below too.
// #include <SoftwareSerial.h>
//
// SoftwareSerial softSerial(2, 3);
// #define CAYENNE_DEBUG SerialUSB
// #define CAYENNE_PRINT SerialUSB
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <CayenneESP8266Shield.h>
#include <DHT.h>
#include <SimpleTimer.h>
#define DHTPIN 7
#define DHTTYPE DHT22
#define ledBluePin 5
#define ledYellowPin 6
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
float dataInfo[6];
float far = 0.0;
RF24 radio(9, 10);
const uint64_t pipe = {0xE8E8F0F0E1LL};
char token[] = "";
char ssid[] = "";
char password[] = "";
#define EspSerial Serial
ESP8266 wifi(EspSerial);
float hum1;
float temp1;
float pressurePa;
float pressureIn;
float temperature;
int altimeter;
int m;
int i;
// int timer1;
unsigned long time;
unsigned long time2;
unsigned long onlineTime;
unsigned long prevTime;
byte disco = 0;
void setup()
{
EspSerial.begin(9600);
delay(10);
// SerialUSB.begin(9600);
pinMode(ledYellowPin, OUTPUT);
pinMode(ledBluePin, OUTPUT);
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, pipe);
radio.startListening();
dht.begin();
Cayenne.begin(token, wifi, ssid, password);
timer.setInterval(30000, getTemp);
// timer1 = timer.setInterval(10000, onlineLED);
// timer.disable(timer1);
}
void loop()
{
//softSerial.print("loop");
Cayenne.run();
// softSerial.println("cayenne - DONE");
timer.run();
// softSerial.println("timer - DONE");
time = millis();
if ( radio.available() )
{
Cayenne.virtualWrite(V0, 1);
for (i = 0; i < 255; i++)
{
analogWrite(ledYellowPin, i);
delay(2);
}
for (i = 255; i > 0; i--)
{
analogWrite(ledYellowPin, i);
delay(2);
}
getData();
}
}
// void onlineLED()
// {
// for (i = 120; i > 30; i--)
// {
// analogWrite(ledBluePin, i);
// delay(3);
// }
//
// Cayenne.run();
// for (i = 30; i < 120; i++)
// {
// analogWrite(ledBluePin, i);
// delay(3);
// }
// }
void getData()
{
// softSerial.println("getData");
radio.read(&dataInfo, sizeof(dataInfo));
time2 = millis();
digitalWrite(ledYellowPin, 0);
far = ((dataInfo[0] * 1.8) + 32);
temperature = dataInfo[4];
pressureIn = dataInfo[5];
Cayenne.virtualWrite(V4, dataInfo[3]);
Cayenne.virtualWrite(V5, dataInfo[2]);
Cayenne.virtualWrite(V11, dataInfo[2]);
Cayenne.virtualWrite(V2, dataInfo[1]);
Cayenne.virtualWrite(V6, far);
Cayenne.virtualWrite(V12, far);
Cayenne.virtualWrite(V3, pressureIn);
Cayenne.virtualWrite(V10, pressureIn);
Cayenne.virtualWrite(V0, 0);
// softSerial.println("getData - DONE");
}
void getTemp()
{
// softSerial.println("getTemp");
temp1 = dht.readTemperature(true);
hum1 = dht.readHumidity();
Cayenne.virtualWrite(V13, temp1);
Cayenne.virtualWrite(V15, temp1);
Cayenne.virtualWrite(V14, hum1);
Cayenne.virtualWrite(V16, hum1);
// softSerial.println("getTemp - DONE");
}
CAYENNE_CONNECTED()
{
// softSerial.println("connected");
prevTime = millis();
//
// for (m = 0; m < 120; m++)
// {
// analogWrite(ledBluePin, m);
// }
// timer.enable(timer1);
// softSerial.println("connected - DONE");
}
CAYENNE_DISCONNECTED()
{
// softSerial.println("disconnected");
prevTime = millis();
disco++;
//
// for (m = 120; m > 0 - 1; m--)
// {
// analogWrite(ledBluePin, m);
// }
// timer.disable(timer1);
// softSerial.println("disconnected - DONE");
}
CAYENNE_OUT(V1)
{
byte transmitTime = ((time - time2) / 1000) / 60;
Cayenne.virtualWrite(V1, transmitTime);
}
CAYENNE_OUT(V23)
{
Cayenne.virtualWrite(V23, disco);
}
CAYENNE_OUT(V24)
{
float time1 = (time / 1000) / 60;
Cayenne.virtualWrite(V24, time1);
}
CAYENNE_OUT(V25)
{
onlineTime = ((time - prevTime) / 1000) / 60;
Cayenne.virtualWrite(V25, onlineTime);
}
And the remote:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include "Seeed_BMP280.h"
#include <Wire.h>
#define DHTPIN 2
#define DHTTYPE DHT22
BMP280 bmp280;
SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
float temperatureDHT;
float temperature;
float volts;
float dataInfo[6];
byte motion;
byte hum;
float pressurePa;
float pressureIn;
void setup()
{
dht.begin();
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipe);
timer.setInterval(300000, getReadings);
bmp280.init();
getReadings();
}
void loop()
{
timer.run();
}
void getReadings()
{
// radio.powerUp();
hum = dht.readHumidity();
delay(50);
temperatureDHT = dht.readTemperature(true);
volts = ((analogRead(A2) * (5.0 / 1023.0)) / 0.08933);
pressurePa = bmp280.getPressure();
temperature = bmp280.getTemperature();
pressureIn = pressurePa * 0.000295;
dataInfo[0] = temperatureDHT;
dataInfo[1] = hum;
dataInfo[2] = volts;
dataInfo[3] = motion;
dataInfo[4] = temperature;
dataInfo[5] = pressureIn;
radio.write(dataInfo, sizeof(dataInfo));
// Serial.println(temperature);
// Serial.println(hum);
// radio.powerDown();
}