Auf den Brethboard's funktioniert es, was ich an I/O' benötige ist jetzt klar, somit werde ich mich an die Hardware machen. Platinen fertigen, Löten . . .
Auf jeden Fall viel zeit in der Werkstatt verbringen
![]()
Hier der Vorläufige Code - Wird sicherlich noch verfeinert in nächster zeit.
/*
* 101117 _ Start - Timi_Who
*/
#include <SPI.h> // Serial Peripheral Interface
#include <Ethernet.h> // Ethernet Bibliothek
#include <PubSubClient.h> // MQTT Bibliothek
#include <Bounce2.h> // Tasten Entprellen
// Network parameters
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(10, 0, 0, 17);
IPAddress server(10, 0, 0, 98);
// Flag for button2
int buttonFlag = 0;
// Buffer with a maximum of 2 characters
char buffStr[2];
// For funktion String2Int --> String to hold input
String payloadString = "";
// PWM Index table
// 9 brightness steps LED not linear
const byte pwmTable[9]={0, 2, 4, 8, 16, 32, 64, 128, 255};
// Set LED variables to digital/analog pins
int led1 = 6;
int led2 = 3;
// Set variable values & initially to LOW
int led1Value = LOW;
int led2Value = 0;
// Set button variables to digital/analog pins
int button1 = 7;
int button2 = 8;
// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();
Bounce bouncer2 = Bounce();
// Initiate network instances
EthernetClient ethClient;
PubSubClient client(ethClient);
//-----------------------------------------------
// Setup
void setup()
{
// setup led, button, bouncer 1
pinMode(led1, OUTPUT);
pinMode(button1,INPUT);
digitalWrite(button1,HIGH);
bouncer1 .attach(button1);
bouncer1 .interval(5);
// setup led, button, bouncer 2
pinMode(led2, OUTPUT);
pinMode(button2,INPUT);
digitalWrite(button2,HIGH);
bouncer2 .attach(button2);
bouncer2 .interval(5);
// Setup serial connection
Serial.begin(57600);
// Ip-Adresse = server, port = 1883
client.setServer(server, 1883); // Adresse des MQTT-Brokers
client.setCallback(callback); // Handler für eingehende Nachrichten
// Ethernet-Verbindung aufbauen
Ethernet.begin(mac, ip);
// Kleine Pause
delay(1500);
}
//-----------------------------------------------
// Handle incoming MQTT messages
void callback(char* topic, byte* payload, unsigned int length) {
if ((strcmp(topic, "led") == 0) && (payload[0] == '1')) {
led1Value = HIGH;
}
if ((strcmp(topic, "led") == 0) && (payload[0] == '0')) {
led1Value = LOW;
}
if (strcmp(topic, "led2") == 0) {
int inPayload = payload[0];
if (isDigit(inPayload)) {
// convert the incoming byte to a char and add it to the string:
payloadString += (char)inPayload;
led2Value = payloadString.toInt();
payloadString = "";
}
}
// Set digital pin states according to virtual switch settings
digitalWrite(led1,led1Value);
analogWrite(led2,pwmTable[led2Value]);
Serial.println(led2Value);
}
//-----------------------------------------------
void reconnect() {
// Repeat until connection is made
while (!client.connected()) {
Serial.print("MQTT Verbindungsaufbau...");
// Connection attempt
if (client.connect("arduinoClient")) {
Serial.println("Erfolgreich verbunden!");
// Messasge to outTopic
client.publish("out","TimiArduino Online");
// Subscride inTopic
client.subscribe("led"); // subscribe to topic "led"
client.subscribe("led2"); // subscribe to topic "led2"
} else { // Error case => Message and new attempt
Serial.print("Fehler, rc= ");
Serial.print(client.state());
Serial.println(" Neuer Versuch in 5 Sekunden");
// 5 seconds pause
delay(5000);
}
}
}
//-----------------------------------------------
void loop()
{
// Listen for button interactions and take actions
// Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW
if (bouncer1.update()) {
if (bouncer1.read() == HIGH) {
if (led1Value == LOW) {
led1Value = HIGH;
client.publish("led","1");
} else {
led1Value = LOW;
client.publish("led","0");
}
}
}
if (bouncer2.update()) {
if (bouncer2.read() == HIGH) {
if ((led2Value<sizeof(pwmTable)-1) && (buttonFlag == 0)) {
led2Value++;
itoa(led2Value, buffStr, 10);
client.publish("led2", buffStr);
if (led2Value==sizeof(pwmTable)-1) {
buttonFlag = 1;
}
} else {
if ((led2Value > 0) && (buttonFlag = 1)) {
led2Value--;
if (led2Value == 0) {
buttonFlag = 0;
}
itoa(led2Value, buffStr, 10);
client.publish("led2", buffStr);
}
}
}
}
//-----------------------------------------------
// Try until successful
if (!client.connected()) {
reconnect();
}
client.loop();
}
Timi_Who