not working

This commit is contained in:
2025-10-09 08:33:33 +02:00
parent 52e0186d7f
commit 6cfc26f1a1
2 changed files with 61 additions and 37 deletions

View File

@@ -9,9 +9,10 @@
; https://docs.platformio.org/page/projectconf.html
[env:esp01_1m]
platform = espressif8266
board = esp01_1m
platform = espressif32
board = esp32dev
framework = arduino
upload_port = COM9
monitor_port = COM9
lib_deps =
gioblu/PJON@^13.1
bblanchon/ArduinoJson@^7.4.2

View File

@@ -2,47 +2,89 @@
#include<PJON.h>
#include <PJONAny.h>
#include <PJONSoftwareBitBang.h>
#include <WiFi.h>
#include <PJONDualUDP.h>
#define RECEIVER 1
#define SBBPIN 12
String topic = "pjon/44";
String topic = "pjon/44/";
// put function declarations here:
void publishMqtt(PJONAny, uint8_t, String, String );
void receiver_function(uint8_t , uint16_t length, const PJON_Packet_Info);
void error_handler(uint8_t, uint16_t);
long getDistance();
// Strategy
StrategyLink<SoftwareBitBang> link;
//StrategyLink<SoftwareBitBang> linkstrategy;
StrategyLink<DualUDP> linkstrategy;
// Bus Address
PJONAny bus(44);
// WLAN Daten
const char* ssid = "binde";
const char* password = "bindi1518!";
// Ultraschallsensor Pins
const int trigPin = 5; // z. B. GPIO5
const int echoPin = 18; // z. B. GPIO18
const int echoPin = 4; // z. B. GPIO18
void setup_wifi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("connected");
while (WiFi.status() != WL_CONNECTED) {
Serial.println(".");
delay(500);
}
}
struct mqttMessage {
uint8_t method; // 0: publish 1: subscribe
char topic[250];
char message[512];
char topic[10];
char message[10];
};
mqttMessage receiveBuffer;
bool receivedFlag = false;
void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
/* Copy received data in buffer */
memcpy(&receiveBuffer, payload, sizeof(receiveBuffer));
receivedFlag = true;
};
void error_handler(uint8_t code, uint16_t data, void *custom_pointer) {
if(code == PJON_CONNECTION_LOST) {
Serial.print("Connection with device ID ");
Serial.print(bus.packets[data].content[0], DEC);
Serial.println(" is lost.");
}
if(code == PJON_PACKETS_BUFFER_FULL) {
Serial.print("Packet buffer is full, has now a length of ");
Serial.println(data, DEC);
Serial.println("Possible wrong bus configuration!");
Serial.println("higher PJON_MAX_PACKETS if necessary.");
}
if(code == PJON_CONTENT_TOO_LONG) {
Serial.print("Content is too long, length: ");
Serial.println(data);
}
};
void setup() {
// put your setup code here, to run once:
link.strategy.set_pin(SBBPIN);
bus.strategy.set_link(&link);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
setup_wifi();
bus.set_error(error_handler);
//linkstrategy.strategy.set_pin(12);
bus.strategy.set_link(&linkstrategy);
bus.set_receiver(receiver_function);
bus.begin();
}
@@ -50,9 +92,11 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:
bus.update();
bus.receive();
publishMqtt(bus,RECEIVER, topic,String(getDistance()));
if (receivedFlag) {
Serial.println(receiveBuffer.topic);
receivedFlag = false;
}
@@ -69,36 +113,15 @@ long getDistance() {
long distance = duration / 58; // Entfernung in cm
return distance;
}
// JSONMqtt funktions
void publishMqtt(PJONAny bus, uint8_t destAddress, String topic, String message ){
Serial.println(topic);
mqttMessage mqMessage;
mqMessage.method = 0;
topic.toCharArray(mqMessage.topic, topic.length());
message.toCharArray(mqMessage.message, message.length());
Serial.println(sizeof(mqMessage));
bus.send(destAddress,&mqMessage,sizeof(mqMessage));
}
void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
/* Copy received data in buffer */
memcpy(&receiveBuffer, payload, sizeof(receiveBuffer));
receivedFlag = true;
};
void error_handler(uint8_t code, uint16_t data, void *custom_pointer) {
if(code == PJON_CONNECTION_LOST) {
Serial.print("Connection lost with device id ");
Serial.println(bus.packets[data].content[0], DEC);
}
};