WS 2812B am Arduino über Modbus steuern, brauche hilfe beim Sketch

Hallo Leute,

ich möchte mit meinem Arduino Nano 33 IoT einen bzw mehrere LED Streifen mit WS2812B ansteuern.
Den LED Streifen zum leuchten bekomme ich.
Allerdings will ich das später aber über Modbus ansteuern.
Also der Arduino bekommt einen Wert (0-255) via Modbus gesendet- der wird dann in eine Variable abgelegt- auf die Variable soll dann wieder die Adafruit Bibliothek zugreifen die ja letztendlich meinen LED Streifen steuert.

Allerdings scheitert es an der Umsetzung.
Ich will erstmal klein anfangen. Also eine einzelne LED die am Arduino angeschlossen ist via Modbus an und aus schalten.
Dies funktioniert auch, allerdings nur so ca 3 min und dann reagiert die LED nicht mehr auf Befehle die ich über den Modbus sende.

Falls jemand von euch sowas schon mal umgesetzt hat wäre der Sketch auch sehr hilfreich. Dann könnte ich den ja vielleicht auch verwenden.

Ich hänge euch den Sketch mit an.

Vielen dank schon mal vorweg.
Als Grundlage dient hier ein Beispiel Sketch aus der Arduino Modbus Bibliothek.

[code]
/*
  WiFi Modbus TCP Server LED

  This sketch creates a Modbus TCP Server with a simulated coil.
  The value of the simulated coil is set on the LED

  Circuit:
   - MKR1000 or MKR WiFi 1010 board

  created 16 July 2018
  by Sandeep Mistry
*/

#include <SPI.h>
#include <WiFiNINA.h> // for MKR WiFi 1010
// #include <WiFi101.h> // for MKR1000

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

const int ledPin = 6;

int status = WL_IDLE_STATUS;

WiFiServer wifiServer(502);

ModbusTCPServer modbusTCPServer;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Modbus TCP Server LED");

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the status:
    printWifiStatus();

  // start the server
  wifiServer.begin();

  // start the Modbus TCP server
  if (!modbusTCPServer.begin()) {
    Serial.println("Failed to start Modbus TCP Server!");
    while (1);
  }

  // configure the LED
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // configure a single coil at address 0x00
  modbusTCPServer.configureCoils(0x00, 1);
}

void loop() {
  // listen for incoming clients
  WiFiClient client = wifiServer.available();
  
  if (client) {
    // a new client connected
    Serial.println("new client");

    // let the Modbus TCP accept the connection 
    modbusTCPServer.accept(client);

    while (client.connected()) {
      // poll for Modbus TCP requests, while client connected
      modbusTCPServer.poll();

      // update the LED
      updateLED();
    }

    Serial.println("client disconnected");
  }
}

void updateLED() {
  // read the current value of the coil
  int coilValue = modbusTCPServer.coilRead(0x00);

  if (coilValue) {
    // coil value set, turn LED on
    digitalWrite(ledPin, HIGH);
  } else {
    // coild value clear, turn LED off
    digitalWrite(ledPin, LOW);
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
[/code]

Ich weiß nicht ob WS2812 und Modbus sich zusammen vertragen.

Die Ansteuerung der WS2812 ist sehr zeikritisch und darum werden normalerweise die Interruptas abgeschaltet.
Das kann den MODBus stören.

Grüße Uwe

Ich würde das gerne einfach ausprobieren.

Das Problem ist nur das ich den Modus nicht richtig ans Laufen bekomme.
Jetzt gerade scheitert es ja an einer einfachen LED die an einem digital Ausgang hängt (keine WS2812).

So ca 3 min läuft alles wie es soll und dann kann ich die Led nicht mehr schalten via Modbus.
Ist vielleicht der Speicher nach der Zeit voll und die Befehle werden deshalb nicht mehr abgearbeitet?

Ich habe schon echt lange und viel gegoogelt allerdings nur Lösungen mit der Mudbus Bibliothek gefunden. Die läuft aber nicht auf meinem Nano 33 IoT.
Mit der Mudbus Bibliothek scheint es aber wohl zuverlässig zu funktionieren.

Womit sendest du die Modbus Pakete/Was ist dein Client (Master)?

Wie sehen die Seriellen Ausgaben bevor dein Nano "einfriert" und später wenn er nicht mehr reagiert?

Lass mal eine nicht blockierende Heartbeat LED parallel blinken, damit du siehst, ob loop noch abgearbeitet wird.

Als master dient eine Siemens Logo 8.3.

Der witz an der Sache ist der Nano scheint einfach so auszusteigen. Es ist egal ob ich Schaltbefehle sende oder nicht. Nach ca 3 bis 5 min. reagiert der Nano einfach nicht mehr.

Habe den Sketch jetzt so geändert.
Komischerweise Blinkt meine Led an Pin 4 nicht.

Aber die Led an Pin 3 kann ich über Modbus ansteuern also kann der Sektch ja nicht ganz falsch sein.

[code]
/*
  WiFi Modbus TCP Server LED

  This sketch creates a Modbus TCP Server with a simulated coil.
  The value of the simulated coil is set on the LED

  Circuit:
   - MKR1000 or MKR WiFi 1010 board

  created 16 July 2018
  by Sandeep Mistry
*/

#include <SPI.h>
#include <WiFiNINA.h> // for MKR WiFi 1010
// #include <WiFi101.h> // for MKR1000

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key Index number (needed only for WEP)


int status = WL_IDLE_STATUS;

WiFiServer wifiServer(502);

ModbusTCPServer modbusTCPServer;

void setup() {
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Modbus TCP Server LED");

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the status:
    printWifiStatus();

  // start the server
  wifiServer.begin();

  // start the Modbus TCP server
  if (!modbusTCPServer.begin()) {
    Serial.println("Failed to start Modbus TCP Server!");
    while (1);
  }
  
  //configurate blink
  pinMode(4, OUTPUT);

  // configure the LED
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);

  // configure a single coil at address 0x00
  modbusTCPServer.configureCoils(0x00, 1);
}

void loop() {
  
  //blink
  digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  
  // listen for incoming clients
  WiFiClient client = wifiServer.available();
  
  if (client) {
    // a new client connected
    Serial.println("new client");

    // let the Modbus TCP accept the connection 
    modbusTCPServer.accept(client);

    while (client.connected()) {
      // poll for Modbus TCP requests, while client connected
      modbusTCPServer.poll();

      // update the LED
      updateLED();
    }

    Serial.println("client disconnected");
  }
}

void updateLED() {

  // read the current value of the coil
  int coilValue = modbusTCPServer.coilRead(0x00);

  if (coilValue) {
    // coil value set, turn LED on
    digitalWrite(3, HIGH);
  } else {
    // coild value clear, turn LED off
    digitalWrite(3, LOW);
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
[/code]

mit nicht blockierend meinte ich eine LED nach dem Muster "Blink Without Delay" im loop und nicht zwei blockierende delays.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.