not working
This commit is contained in:
@@ -9,9 +9,10 @@
|
|||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:esp01_1m]
|
[env:esp01_1m]
|
||||||
platform = espressif8266
|
platform = espressif32
|
||||||
board = esp01_1m
|
board = esp32dev
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
upload_port = COM9
|
||||||
|
monitor_port = COM9
|
||||||
lib_deps =
|
lib_deps =
|
||||||
gioblu/PJON@^13.1
|
gioblu/PJON@^13.1
|
||||||
bblanchon/ArduinoJson@^7.4.2
|
|
||||||
|
|||||||
91
src/main.cpp
91
src/main.cpp
@@ -2,47 +2,89 @@
|
|||||||
#include<PJON.h>
|
#include<PJON.h>
|
||||||
#include <PJONAny.h>
|
#include <PJONAny.h>
|
||||||
#include <PJONSoftwareBitBang.h>
|
#include <PJONSoftwareBitBang.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <PJONDualUDP.h>
|
||||||
|
|
||||||
#define RECEIVER 1
|
#define RECEIVER 1
|
||||||
#define SBBPIN 12
|
#define SBBPIN 12
|
||||||
|
|
||||||
String topic = "pjon/44";
|
String topic = "pjon/44/";
|
||||||
|
|
||||||
// put function declarations here:
|
// put function declarations here:
|
||||||
|
|
||||||
void publishMqtt(PJONAny, uint8_t, String, String );
|
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();
|
long getDistance();
|
||||||
|
|
||||||
|
|
||||||
// Strategy
|
// Strategy
|
||||||
StrategyLink<SoftwareBitBang> link;
|
//StrategyLink<SoftwareBitBang> linkstrategy;
|
||||||
|
StrategyLink<DualUDP> linkstrategy;
|
||||||
|
|
||||||
|
|
||||||
// Bus Address
|
// Bus Address
|
||||||
PJONAny bus(44);
|
PJONAny bus(44);
|
||||||
|
|
||||||
|
// WLAN Daten
|
||||||
|
const char* ssid = "binde";
|
||||||
|
const char* password = "bindi1518!";
|
||||||
|
|
||||||
// Ultraschallsensor Pins
|
// Ultraschallsensor Pins
|
||||||
const int trigPin = 5; // z. B. GPIO5
|
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 {
|
struct mqttMessage {
|
||||||
uint8_t method; // 0: publish 1: subscribe
|
uint8_t method; // 0: publish 1: subscribe
|
||||||
char topic[250];
|
char topic[10];
|
||||||
char message[512];
|
char message[10];
|
||||||
};
|
};
|
||||||
|
|
||||||
mqttMessage receiveBuffer;
|
mqttMessage receiveBuffer;
|
||||||
bool receivedFlag = false;
|
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() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
|
pinMode(trigPin, OUTPUT);
|
||||||
link.strategy.set_pin(SBBPIN);
|
pinMode(echoPin, INPUT);
|
||||||
bus.strategy.set_link(&link);
|
Serial.begin(9600);
|
||||||
|
setup_wifi();
|
||||||
bus.set_error(error_handler);
|
bus.set_error(error_handler);
|
||||||
|
//linkstrategy.strategy.set_pin(12);
|
||||||
|
bus.strategy.set_link(&linkstrategy);
|
||||||
bus.set_receiver(receiver_function);
|
bus.set_receiver(receiver_function);
|
||||||
bus.begin();
|
bus.begin();
|
||||||
}
|
}
|
||||||
@@ -50,9 +92,11 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
// put your main code here, to run repeatedly:
|
||||||
bus.update();
|
bus.update();
|
||||||
|
bus.receive();
|
||||||
publishMqtt(bus,RECEIVER, topic,String(getDistance()));
|
publishMqtt(bus,RECEIVER, topic,String(getDistance()));
|
||||||
|
|
||||||
if (receivedFlag) {
|
if (receivedFlag) {
|
||||||
|
Serial.println(receiveBuffer.topic);
|
||||||
receivedFlag = false;
|
receivedFlag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,36 +113,15 @@ long getDistance() {
|
|||||||
long distance = duration / 58; // Entfernung in cm
|
long distance = duration / 58; // Entfernung in cm
|
||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// JSONMqtt funktions
|
// JSONMqtt funktions
|
||||||
|
|
||||||
void publishMqtt(PJONAny bus, uint8_t destAddress, String topic, String message ){
|
void publishMqtt(PJONAny bus, uint8_t destAddress, String topic, String message ){
|
||||||
|
Serial.println(topic);
|
||||||
mqttMessage mqMessage;
|
mqttMessage mqMessage;
|
||||||
mqMessage.method = 0;
|
mqMessage.method = 0;
|
||||||
topic.toCharArray(mqMessage.topic, topic.length());
|
topic.toCharArray(mqMessage.topic, topic.length());
|
||||||
message.toCharArray(mqMessage.message, message.length());
|
message.toCharArray(mqMessage.message, message.length());
|
||||||
|
Serial.println(sizeof(mqMessage));
|
||||||
bus.send(destAddress,&mqMessage,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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user