From 6cfc26f1a1bb782ce3e9af11d788cf60ae3796bd Mon Sep 17 00:00:00 2001 From: nixbi93 Date: Thu, 9 Oct 2025 08:33:33 +0200 Subject: [PATCH] not working --- platformio.ini | 7 ++-- src/main.cpp | 91 +++++++++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/platformio.ini b/platformio.ini index d740d05..1bcdba3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/main.cpp b/src/main.cpp index fc62860..a5a31d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,47 +2,89 @@ #include #include #include +#include +#include #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 link; +//StrategyLink linkstrategy; +StrategyLink 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); - } -}; \ No newline at end of file