Commit 2
This commit is contained in:
parent
d377fa89a4
commit
9e702af8c6
13 changed files with 639 additions and 133 deletions
183
src/main.ino
183
src/main.ino
|
|
@ -1,14 +1,22 @@
|
|||
#include <ArduinoJson.h>
|
||||
#include <rtl_433_ESP.h>
|
||||
#include <RCSwitch.h>
|
||||
#include <Wire.h>
|
||||
#include <BME280I2C.h>
|
||||
#include <EnvironmentCalculations.h>
|
||||
#include "Dictionary.h"
|
||||
|
||||
#ifndef RF_MODULE_FREQUENCY
|
||||
# define RF_MODULE_FREQUENCY 433.92
|
||||
#endif
|
||||
|
||||
#define _DICT_PACK_STRUCTURES
|
||||
|
||||
#define LED 8
|
||||
#define LED LED_BUILTIN
|
||||
|
||||
CC1101 radiotx = RADIO_LIB_MODULE;
|
||||
#define RCSWITCH_PIN 43
|
||||
|
||||
//CC1101 radiotx = RADIO_LIB_MODULE;
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
#ifndef RF_MODULE_FREQUENCY
|
||||
|
|
@ -17,6 +25,8 @@ RCSwitch mySwitch = RCSwitch();
|
|||
|
||||
#define JSON_MSG_BUFFER 512
|
||||
#define MINDELAY 10L
|
||||
#define ALTITUDE 150.0
|
||||
#define DELAYLOCALSENSORS 30L
|
||||
|
||||
Dictionary<String, unsigned long> d1;
|
||||
|
||||
|
|
@ -24,15 +34,35 @@ char messageBuffer[JSON_MSG_BUFFER];
|
|||
|
||||
rtl_433_ESP rf; // use -1 to disable transmitter
|
||||
|
||||
BME280I2C bme;
|
||||
bool sensors_enabled;
|
||||
|
||||
int count = 0;
|
||||
|
||||
uint32_t lastts_localsensors = 0;
|
||||
|
||||
void rtl_433_Callback(char* message) {
|
||||
//Serial.println("RTL433 Callback");
|
||||
JsonDocument jsonDocument;
|
||||
deserializeJson(jsonDocument,message);
|
||||
logJson(jsonDocument);
|
||||
count++;
|
||||
}
|
||||
|
||||
JsonDocument analyzeTH_type1(JsonArray payloadrh) {
|
||||
JsonDocument doc;
|
||||
doc["moisture"] = payloadrh[5].as<int>();
|
||||
int16_t payloadtemp = payloadrh[4].as<unsigned char>();
|
||||
payloadtemp += payloadrh[3].as<unsigned char>() << 8;
|
||||
doc["temperature_C"] = payloadtemp / 100.0;
|
||||
doc["battery_ok"] = payloadrh[0].as<unsigned char>() & 0b00000001;
|
||||
uint16_t payloadbat = payloadrh[2].as<unsigned char>();
|
||||
payloadbat += payloadrh[1].as<unsigned char>() << 8;
|
||||
doc["battery_V"] = ((payloadbat & 0b1111111110000000) >> 7) / 100.0;
|
||||
doc["battery_percent"] = (payloadbat & 0b0000000001111111);
|
||||
return doc;
|
||||
}
|
||||
|
||||
void logJson(JsonDocument jsondata) {
|
||||
if(jsondata["model"].is<String>())
|
||||
{
|
||||
|
|
@ -42,6 +72,31 @@ void logJson(JsonDocument jsondata) {
|
|||
if(!d1.contains(ref.c_str()) || millis() > (d1.get(ref.c_str()) + (MINDELAY * 1000)) || d1.get(ref.c_str()) > millis()) {
|
||||
//Serial.println(ref.c_str());
|
||||
//if(d1.contains(ref.c_str())) { Serial.println(d1.get(ref.c_str())); }
|
||||
|
||||
if(jsondata["model"].is<String>() && jsondata["model"] == "RadioHead-ASK") {
|
||||
JsonArray payloadrh = jsondata["payload"];
|
||||
bool treatrh = 0;
|
||||
if(jsondata["len"].as<int>() == 6 && ((payloadrh[0].as<unsigned char>() & 0b00010000 ) > 0)) {
|
||||
treatrh = true;
|
||||
//Serial.println("Analyse RH Payload");
|
||||
JsonDocument adoc = analyzeTH_type1(payloadrh);
|
||||
jsondata["moisture"] = adoc["moisture"];
|
||||
jsondata["battery_ok"] = adoc["battery_ok"];
|
||||
jsondata["battery_V"] = adoc["battery_V"];
|
||||
jsondata["battery_percent"] = adoc["battery_percent"];
|
||||
jsondata["temperature_C"] = adoc["temperature_C"];
|
||||
}
|
||||
if(treatrh == true) {
|
||||
jsondata.remove("from");
|
||||
jsondata.remove("to");
|
||||
jsondata.remove("payload");
|
||||
jsondata.remove("len");
|
||||
jsondata.remove("flags");
|
||||
jsondata.remove("mic");
|
||||
jsondata.remove("duration");
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[measureJson(jsondata) + 1];
|
||||
serializeJson(jsondata, JSONmessageBuffer, measureJson(jsondata) + 1);
|
||||
|
|
@ -50,12 +105,97 @@ void logJson(JsonDocument jsondata) {
|
|||
serializeJson(jsondata, JSONmessageBuffer, JSON_MSG_BUFFER);
|
||||
#endif
|
||||
d1.set(ref.c_str(), millis());
|
||||
ledblink();
|
||||
Serial.println(JSONmessageBuffer);
|
||||
ledblink();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void logJsonRH(JsonDocument jsondata) {
|
||||
if(jsondata["model"].is<String>() && jsondata["model"] == "RadioHead-ASK") {
|
||||
JsonArray payloadrh = jsondata["payload"];
|
||||
bool treatrh = 0;
|
||||
if(jsondata["len"].as<int>() == 6 && ((payloadrh[0].as<unsigned char>() & 0b00010000 ) > 0)) {
|
||||
treatrh = true;
|
||||
//Serial.println("Analyse RH Payload");
|
||||
JsonDocument adoc = analyzeTH_type1(payloadrh);
|
||||
jsondata["moisture"] = adoc["moisture"];
|
||||
jsondata["battery_ok"] = adoc["battery_ok"];
|
||||
jsondata["battery_V"] = adoc["battery_V"];
|
||||
jsondata["battery_percent"] = adoc["battery_percent"];
|
||||
jsondata["temperature_C"] = adoc["temperature_C"];
|
||||
}
|
||||
if(treatrh == true) {
|
||||
jsondata.remove("from");
|
||||
jsondata.remove("to");
|
||||
jsondata.remove("payload");
|
||||
jsondata.remove("len");
|
||||
jsondata.remove("flags");
|
||||
jsondata.remove("mic");
|
||||
jsondata.remove("duration");
|
||||
}
|
||||
}
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[measureJson(jsondata) + 1];
|
||||
serializeJson(jsondata, JSONmessageBuffer, measureJson(jsondata) + 1);
|
||||
#else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
serializeJson(jsondata, JSONmessageBuffer, JSON_MSG_BUFFER);
|
||||
#endif
|
||||
Serial.println(JSONmessageBuffer);
|
||||
//Serial.print(JSONmessageBuffer);
|
||||
//Serial.println();
|
||||
//ledblink();
|
||||
}
|
||||
|
||||
void logJsonAll(JsonDocument jsondata) {
|
||||
// Serial.println(count);
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[measureJson(jsondata) + 1];
|
||||
serializeJson(jsondata, JSONmessageBuffer, measureJson(jsondata) + 1);
|
||||
#else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
serializeJson(jsondata, JSONmessageBuffer, JSON_MSG_BUFFER);
|
||||
#endif
|
||||
Serial.println(JSONmessageBuffer);
|
||||
//ledblink();
|
||||
}
|
||||
*/
|
||||
|
||||
void sendBME280() {
|
||||
JsonDocument jsonDocument;
|
||||
jsonDocument["model"]="LocalSensors";
|
||||
jsonDocument["protocol"]="Local Sensors";
|
||||
//jsonDocument["channel"]=1;
|
||||
//jsonDocument["id"]=1;
|
||||
float temp(NAN), hum(NAN), pres(NAN);
|
||||
|
||||
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
|
||||
BME280::PresUnit presUnit(BME280::PresUnit_hPa);
|
||||
EnvironmentCalculations::AltitudeUnit envAltUnit = EnvironmentCalculations::AltitudeUnit_Meters;
|
||||
EnvironmentCalculations::TempUnit envTempUnit = EnvironmentCalculations::TempUnit_Celsius;
|
||||
|
||||
bme.read(pres, temp, hum, tempUnit, presUnit);
|
||||
|
||||
if(!isnan(pres) && !isnan(temp) && !isnan(hum)) {
|
||||
|
||||
//float temp2 = (int)(temp * 100 + .5);
|
||||
//jsonDocument["temperature_C"]=(float)temp2 / 100;
|
||||
jsonDocument["temperature_C"]=temp;
|
||||
jsonDocument["humidity"]=(int8_t) roundf(hum);
|
||||
//jsonDocument["pressure"]=EnvironmentCalculations::EquivalentSeaLevelPressure(ALTITUDE, temp, pres, envAltUnit, envTempUnit);
|
||||
//float pres2 = (int)(pres * 10 + .5);
|
||||
//jsonDocument["pressure"]=(float)pres2 / 10;
|
||||
jsonDocument["pressure"]=pres;
|
||||
|
||||
logJson(jsonDocument);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ledblink() {
|
||||
digitalWrite(LED, LOW);
|
||||
delay(150);
|
||||
|
|
@ -63,19 +203,21 @@ void ledblink() {
|
|||
}
|
||||
|
||||
void setup() {
|
||||
Serial.setTxBufferSize(512);
|
||||
//Serial.setTxTimeoutMs(100);
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
for (int i=0 ; i<10; i++) {
|
||||
Serial.print("Hello");
|
||||
delay(1000);
|
||||
}
|
||||
if(SS != 21 || MOSI != 20 || MISO != 10 || SCK != 7)
|
||||
//for (int i=0 ; i<10; i++) {
|
||||
// Serial.print("Hello");
|
||||
// delay(1000);
|
||||
//}
|
||||
/* if(SS != 21 || MOSI != 20 || MISO != 10 || SCK != 7)
|
||||
{
|
||||
for ( ; ; ) {
|
||||
Serial.println("Please define pin assignment for SPI in pins_arduino.h (~/.platformio/packages/framework-arduinoespressif32/variants/esp32c3) / SS: 21 MOSI : 20 MISO : 10 SCK : 7");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
} */
|
||||
pinMode(LED, OUTPUT);
|
||||
digitalWrite(LED, HIGH);
|
||||
rf.initReceiver(RF_MODULE_RECEIVER_GPIO, RF_MODULE_FREQUENCY);
|
||||
|
|
@ -86,11 +228,17 @@ void setup() {
|
|||
//d = new Dictionary(25);
|
||||
//d("aa", "bb");
|
||||
//if(d1("aa")) { Serial.println("toto"); }
|
||||
Wire.begin();
|
||||
sensors_enabled = bme.begin();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
rf.loop();
|
||||
if(sensors_enabled == true && (millis() > (lastts_localsensors + (DELAYLOCALSENSORS*1000)) || lastts_localsensors > millis())) {
|
||||
lastts_localsensors = millis();
|
||||
sendBME280();
|
||||
}
|
||||
if (Serial.available())
|
||||
{
|
||||
// Read the JSON document from the "link" serial port
|
||||
|
|
@ -106,15 +254,16 @@ void loop() {
|
|||
if(doc["cmd"].as<String>().compareTo(String("rcsend"))==0) {
|
||||
Serial.println("Transmit RCSwitch");
|
||||
ledblink();
|
||||
//rf.getModuleStatus();
|
||||
rf.disableReceiver();
|
||||
radiotx.SPIsendCommand(RADIOLIB_CC1101_CMD_TX);
|
||||
mySwitch.enableTransmit(RF_MODULE_GDO0);
|
||||
mySwitch.setRepeatTransmit(8);
|
||||
//rf.disableReceiver();
|
||||
//radiotx.SPIsendCommand(RADIOLIB_CC1101_CMD_TX);
|
||||
//mySwitch.enableTransmit(RF_MODULE_GDO0);
|
||||
mySwitch.enableTransmit(RCSWITCH_PIN);
|
||||
//mySwitch.setRepeatTransmit(8);
|
||||
mySwitch.setRepeatTransmit(5);
|
||||
mySwitch.send(doc["value"].as<uint32_t>(), 24);
|
||||
mySwitch.disableTransmit();
|
||||
radiotx.SPIsendCommand(RADIOLIB_CC1101_CMD_RX);
|
||||
rf.enableReceiver();
|
||||
//mySwitch.disableTransmit();
|
||||
//radiotx.SPIsendCommand(RADIOLIB_CC1101_CMD_RX);
|
||||
//rf.enableReceiver();
|
||||
//rf.getModuleStatus();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue