Posts: 6,662
Threads: 844
Joined: Oct 2020
Reputation:
162
Code: #include <LoRa.h>
#include <SPI.h>
#include <Wire.h>
#include <Bonezegei_SSD1306.h>
Bonezegei_SSD1306 oled(128, 64);
#define ss 41
#define rst 2
#define dio0 40
#include "Arduino.h"
#define ANALOG_A1 4
#define ANALOG_A2 6
#define ANALOG_A3 5
#define ANALOG_A4 7
const int Relay1 = 48;
const int K1 = 47;
const int K2 = 21;
const int K3 = 14;
const int K4 = 13;
const int K5 = 12;
const int K6 = 11;
const int K7 = 10;
const int K8 = 9;
const int IO0 = 0; //GPIO0
const int DI1 = 16; //DI
const int DI2 = 15; //1-wire
void setup()
{
SPI.begin(42, 43, 44, 41); //SPI.begin(PIN_SPI_SCK, PIN_SPI_MISO, PIN_SPI_MOSI, -1); last one is SS not used, use -1
Wire.begin(39, 38);
oled.begin();
oled.clear();
// oled.drawText(1, 0, "Arial8", oled.Font_Arial8);
oled.drawText(1, 0, "KinCony", oled.Font_Arial14);
// oled.drawText(1, 24, "Arial12", oled.Font_Arial12);
// oled.drawText(1, 40, "Arial14", oled.Font_Arial14);
oled.draw(); //update the screen
Serial.begin(115200);
Serial.println("begin");
LoRa.setPins(ss, rst, dio0); //setup LoRa transceiver module
while (!LoRa.begin(433E6)) //433E6 - Asia, 866E6 - Europe, 915E6 - North America
{
Serial.println(".");
delay(500);
}
LoRa.setSyncWord(0xA5);
Serial.println("LoRa Initializing OK!");
pinMode(Relay1,OUTPUT);
pinMode(K1,INPUT);
pinMode(K2,INPUT);
pinMode(K3,INPUT);
pinMode(K4,INPUT);
pinMode(K5,INPUT);
pinMode(K6,INPUT);
pinMode(K7,INPUT);
pinMode(K8,INPUT);
pinMode(DI1,INPUT);
pinMode(DI2,INPUT);
pinMode(IO0,INPUT);
pinMode(ANALOG_A1,INPUT);
pinMode(ANALOG_A2,INPUT);
pinMode(ANALOG_A3,INPUT);
pinMode(ANALOG_A4,INPUT);
digitalWrite(Relay1, HIGH);
}
void loop()
{
uint8_t valk1 = digitalRead(K1);
uint8_t valk2 = digitalRead(K2);
uint8_t valk3 = digitalRead(K3);
uint8_t valk4 = digitalRead(K4);
uint8_t valk5 = digitalRead(K5);
uint8_t valk6 = digitalRead(K6);
uint8_t valk7 = digitalRead(K7);
uint8_t valk8 = digitalRead(K8);
uint8_t valdi1 = digitalRead(DI1);
uint8_t valdi2 = digitalRead(DI2);
uint8_t valIO0 = digitalRead(IO0);
if (valdi1==LOW) digitalWrite(Relay1, HIGH); else digitalWrite(Relay1, LOW);
if (digitalRead(DI2)==LOW)Serial.println("input2 down");
if (digitalRead(IO0)==LOW)Serial.println("DL Button down");
if (digitalRead(K1)==LOW)Serial.println("K1 down");
if (digitalRead(K2)==LOW)Serial.println("K2 down");
if (digitalRead(K3)==LOW)Serial.println("K3 down");
if (digitalRead(K4)==LOW)Serial.println("K4 down");
if (digitalRead(K5)==LOW)Serial.println("K5 down");
if (digitalRead(K6)==LOW)Serial.println("K6 down");
if (digitalRead(K7)==LOW)Serial.println("K7 down");
if (digitalRead(K8)==LOW)Serial.println("K8 down");
//-------------------------------------
if (analogRead(ANALOG_A1)>0) Serial.printf("Current Reading A1 on Pin(%d)=%d\n",ANALOG_A1,analogRead(ANALOG_A1));
if (analogRead(ANALOG_A2)>0) Serial.printf("Current Reading A2 on Pin(%d)=%d\n",ANALOG_A2,analogRead(ANALOG_A2));
if (analogRead(ANALOG_A3)>0) Serial.printf("Current Reading A3 on Pin(%d)=%d\n",ANALOG_A3,analogRead(ANALOG_A3));
if (analogRead(ANALOG_A4)>0) Serial.printf("Current Reading A4 on Pin(%d)=%d\n",ANALOG_A4,analogRead(ANALOG_A4));
//---------------------------------------
int packetSize = LoRa.parsePacket(); // try to parse packet
if (packetSize)
{
Serial.print("Received packet '");
while (LoRa.available()) // read packet
{
String LoRaData = LoRa.readString();
Serial.print(LoRaData);
oled.begin();
oled.clear();
oled.drawText(1, 0, LoRaData.c_str(), oled.Font_Arial14);
String temp=String(LoRa.packetRssi());
oled.drawText(1, 40, temp.c_str(), oled.Font_Arial14);
oled.draw(); //update the screen
}
Serial.print("' with RSSI "); // print RSSI of packet
Serial.println(LoRa.packetRssi());
}
}
before use this code, need to install LoRa SX1278 arduino library firstly.
Posts: 3
Threads: 0
Joined: Aug 2024
Reputation:
0
i have done these instruction. it doesn't work.
Error Msg in Arduino:
--------------------------------------------------------------------------------------
LoRa_Receicer_KC868:7:10: fatal error: LoRa.h: No such file or directory
#include <LoRa.h>
^~~~~~~~
compilation terminated.
exit status 1
LoRa.h: No such file or directory
--------------------------------------------------------------------------------------------
the lib is installed!
Posts: 3
Threads: 0
Joined: Aug 2024
Reputation:
0
I found the Solution:
I had to download the ZIP-File and then to use the Option: ADD Zip Library
downloaded from https://github.com/sandeepmistry/arduino-LoRa
Posts: 3
Threads: 0
Joined: Aug 2024
Reputation:
0
I have found the solution:
I had to download the ZIP-File and then to use the Option: ADD Zip Library
download from: https://github.com/sandeepmistry/arduino-LoRa
Posts: 6,662
Threads: 844
Joined: Oct 2020
Reputation:
162
Posts: 6
Threads: 0
Joined: Nov 2024
Reputation:
0
12-03-2024, 08:50 PM
(This post was last modified: 12-03-2024, 08:56 PM by maso.)
I was too optimistic and full-bodied. Unfortunately, HA MQTT doesn't work the way I want it to. Somehow I didn't quite understand the HA topics, as some are not written to mqtt broker. Can you help me please to get my code right? I tried to re-code your topics which I see in the serial and in the mqtt broker via explorer:
Here is my code. and only the status is written, not the sensor parameters, which I do not understand
// Register new device and send discovery topics
void registerDevice(int address, const LoRaData& data) {
if (registeredNodes[address]) return;
Serial.printf("New device %08X to be registered in MQTT Broker:\n", address);
char configTopic[180];
char payload[1024];
char stateTopic[50];
char commandTopic[60];
// State-& Command-Topics for switch & Sensors
//sprintf(stateTopic, "homeassistant/sensor/alrha%08X/params/state", address);
//sprintf(commandTopic, "homeassistant/sensor/alrha%08X/params/set", address);
sprintf(commandTopic, "homeassistant/sensor/alrhaDCDA0C7B7BD4/params/set", address);
sprintf(stateTopic, "homeassistant/sensor/alrhaDCDA0C7B7BD4/params/state", address);
// Relay
sprintf(configTopic, "homeassistant/switch/alr_ALR_switch/config");
snprintf(payload, sizeof(payload),
"{\"name\":\"relay\","
"\"state_on\":1,"
"\"state_off\":0,"
"\"payload_on\":\"1\","
"\"payload_off\":\"0\","
"\"value_template\":\"{{ value_json.relay.on | string }}\","
"\"state_topic\":\"%s\","
"\"command_topic\":\"%s\","
"\"unique_id\":\"alr_switch\","
"\"device\":{"
"\"identifiers\":[\"alr\"],"
"\"name\":\"alr evb\","
"\"model\":\"ALR\","
"\"manufacturer\":\"KinCony\""
"}}",
stateTopic,
commandTopic);
Serial.printf("Config topic: %s\nPayload: %s\n", configTopic, payload);
mqttClient.publish(configTopic, payload);
Serial.println("MQTT Switch topic configured");
// Sensors
const char* sensorNames[] = {"inword", "outword", "analog_A1", "analog_A2", "analog_A3", "analog_A4", "DS18B20"};
const char* valueTemplates[] = {"{{ value_json.inword }}", "{{ value_json.outword }}", "{{ value_json.analog_f[0] }}",
"{{ value_json.analog_f[1] }}", "{{ value_json.analog_f[2] }}", "{{ value_json.analog_f[3] }}",
"{{ value_json.wire1_data0 }}"};
for (int i = 0; i < 7; i++) {
delay(800);
//sprintf(configTopic, "homeassistant/sensor/alrha%08X/alr_ALR_param%d/config", address, i);
sprintf(configTopic, "homeassistant/sensor/alr_ALR_param%d/config", i);
snprintf(payload, sizeof(payload),
"{\"name\":\"%s\","
"\"unit_of_measurement\":\"%s\","
"\"value_template\":\"%s\","
"\"state_topic\":\"%s\","
"\"command_topic\":\"%s\","
"\"unique_id\":\"alr_param%d\","
"\"device\":{"
"\"identifiers\":[\"alr\"],"
"\"name\":\"alr evb\","
"\"model\":\"ALR\","
"\"manufacturer\":\"KinCony\""
"}}",
sensorNames[i], // Name des Sensors
(i > 1 && i < 6) ? "V" : " ", // Einheit (z. B. Volt) für 4 ADs
valueTemplates[i], // Template für den Wert
stateTopic, // State Topic
commandTopic, // Command Topic
i); // Eindeutige ID
Serial.printf("Config topic: %s\nPayload: %s\n", configTopic, payload);
mqttClient.publish(configTopic, payload);
}
registeredNodes[address] = true;
Serial.printf("All Device %08X Sonsor Topics registered.\n", address);
}
// update MQTT-State values
void updateMQTTState(int address, const LoRaData& data) {
StaticJsonDocument<256> jsonDoc;
jsonDoc["self_addr"] = data.self_addr;
jsonDoc["inword"] = data.inword;
jsonDoc["outword"] = data.outword;
JsonArray analogF = jsonDoc.createNestedArray("analog_f");
for (int i = 0; i < 4; i++) analogF.add(data.analog_f[i]);
jsonDoc["wire1_data0"] = data.wire1_data0;
jsonDoc["relay"]["on"] = data.relay_on;
char buffer[512];
serializeJson(jsonDoc, buffer, sizeof(buffer));
char stateTopic[50];
//sprintf(stateTopic, "homeassistant/sensor/alrha%08X/params/state", address);
sprintf(stateTopic, "homeassistant/sensor/alrhaDCDA0C7B7BD4/params/state");
// MQTT-Payload serial ausgeben
Serial.print("MQTT State Updated:");
Serial.println(buffer);
Serial.println(); // Fügt einen CR ein
// MQTT-Payload senden
mqttClient.publish(stateTopic, buffer);
}
Posts: 6
Threads: 0
Joined: Nov 2024
Reputation:
0
I found the problem. the config len of the mqtt buffer is very high.
This Line helped!
mqttClient.setBufferSize(512); increase from 256 to 512
Posts: 6,662
Threads: 844
Joined: Oct 2020
Reputation:
162
|