Hallo zusammen,
ich wollte die Daten meiner Wetterstation mit dem MEGA + EthernetShield via RS485 einlesen und dann auf einer HTML Seite anzeigen. Aber irgendwie veträgt sich das einlesen der RS485 Daten nicht mit dem Ethernet, dh. Ethernet funktioniert aber es werden keine RS485 Daten eingelesen =( (Ohne Ethernet Teil funktioniert der Sketch). Hat jemand eine Idee wo das Problem in meinem Sketch liegt?
// Do not remove the include below
#include "MEGA_Netzw_RS485.h"
/*-----( Import needed libraries )-----*/
#include <Arduino.h>
#include <RS485_protocol.h>
#include <SoftwareSerial.h>
#include <Time.h>
#include <SD.h>
#include <SPI.h>
#include <EthernetUdp.h>
#include <Ethernet.h>
//----------------RS485 Daten definieren -------------------------------------
// Not all pins on the Mega and Mega 2560 support change interrupts,
//so only the following can be used for RX:
//10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
#define SSerialRX 51 //Serial Receive RS485 Chip Pin1 (connect to TX of other device)
#define SSerialTX 52 //Serial Transmit RS485 Chip Pin4 (connect to RX of other device)
#define SSerialTxControl 50 //RS485 Direction control RS485 Chip Pin2+3 Brücke
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
byte RS485_ACK_Reply[3] = {0,0,0};
//------------------------ Ethernet Definition -------------------------------------------------------------
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xAA, 0xFE, 0xBF };
IPAddress ip (192,168,59,252 ); //IP Arduino Board
IPAddress dns_server (192,168,59,3 ); //IP Router
IPAddress gateway (192,168,59,3 ); //IP Router
IPAddress subnet (255,255,255,0 );
IPAddress timeServer(130,149,17,21); // ntps1-0.cs.tu-berlin.de
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
long timeZoneOffset; // set this to the offset in seconds to your local time;
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
unsigned int localPort = 8888; // local port to listen for UDP packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
//----------------Wetter Daten definieren -------------------------------------
byte ZielNodeID = 0x01; //Ziel ID an die Request gesendet wurde
byte AbsenderNodeID = 0x03; //Ziel ID an die Request gesendet wurde
byte Acknowledge = 0x00; //Rückmeldung 1 ACK erwünscht / 2 ACK Antwort
float TempAussen = 0.01;
int sensorValue = 100;
int windGeschw = 1;
int windMax = 111;
float TempInnen = 0.22;
//----------------Payload Daten definieren -------------------------------------
typedef struct{
byte ZielNodeID; //Ziel ID an die Request gesendet wurde
byte AbsenderNodeID; //Ziel ID an die Request gesendet wurde
byte Acknowledge; //Rückmeldung
float TempAussen; //Zur Messung verwendeter Pin
int sensorValue;
int windGeschw;
int windMax;
float TempInnen; //Zweiter DS18B20
}
Payload;
Payload sample; //Instanz vom Typ Payload deklariere
//==============================================================================================================
// Zusatzfunktionen
//==============================================================================================================
void RS485Write (const byte what)
{RS485Serial.write (what);}
int RS485Available ()
{return RS485Serial.available();}
int RS485Read ()
{return RS485Serial.read();}
static void showNibble (byte nibble) {
char c = '0' + (nibble & 0x0F);
if (c > '9')
c += 7;
Serial.print(c);
}
static void showByte (byte value) {
showNibble(value >> 4);
showNibble(value);
}
//==============================================================================================================
// Initialisieren
//==============================================================================================================
void setup() /****** SETUP: RUNS ONCE ******/
{
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
Serial.begin(57600);
Serial.print("Baudrate ");
Serial.println(57600);
Serial.println("RS485 Netzwerk Remote Controller");
Serial.println("File: MEGA_Netzw_RS485.h");
//------------------------ Ethernet Karte initialisieren ------------------------------
// start Ethernet
Serial.print("Initializing Ethernet...");
Ethernet.begin(mac, ip, dns, gateway, subnet);
Serial.println(" done");
// print your local IP address:
Serial.print("IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
//---------------- RS485 Initialisieren -------------------------------------
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(4800); // set the data rate
}
//==============================================================================================================
// Arbeitsschleife
//==============================================================================================================
void loop()
{
byte RS485_data [sizeof (Payload)];
digitalWrite(Pin13LED, LOW); // Show activity
if (RS485Serial.available()){
digitalWrite(Pin13LED, HIGH); // Show activity
recvMsg (RS485Available, RS485Read, RS485_data, sizeof (RS485_data));
delay(2);
if (RS485_data[0] == 0 || RS485_data[0]==AbsenderNodeID){
sample =*(Payload*)RS485_data; //Nutzdaten abrufen und in Payload umwandeln
Serial.print(" Size RS485_data: ");
Serial.println(sizeof (RS485_data));
Serial.print("Data RS485_data:");
for (byte i = 0; i < sizeof (RS485_data); ++i)
{
Serial.print(' ');
showByte(RS485_data[i]);
RS485_data[i] =0;
}
Serial.println(); // Can be ignored
Serial.print("ZielNodeID: ");
Serial.println(sample.ZielNodeID);
Serial.print("AbsenderNodeID: ");
Serial.println(sample.AbsenderNodeID);
Serial.print("Acknowledge: ");
Serial.println(sample.Acknowledge);
// Serial.print("TempAussen: ");
// Serial.println(sample.TempAussen);
// Serial.print("WindGeschw: ");
// Serial.println(sample.windGeschw);
// Serial.print("Wind Richtung: "); //Ausgabe der gemittelten Windrichtung
// Serial.println(sample.windMax);
// Serial.print("sensorValue: ");
// Serial.println(sample.sensorValue);
// Serial.print("TempInnen: ");
// Serial.println(sample.TempInnen);
// Serial.println(' '); // Can be ignored
}
else {
Serial.print("Nicht mein Geraet oder RS485 A+B verdreht! ");
Serial.print(AbsenderNodeID);
Serial.print(" RS485_data[0]: ");
Serial.println(RS485_data[0]);
}
}// End If RS485SerialAvailable
if (sample.Acknowledge==1){ //ACK Versand vorbereiten und senden
RS485_ACK_Reply[0] = sample.ZielNodeID; //Absender wird neuer Empfänger
RS485_ACK_Reply[1] = AbsenderNodeID; //Eigene Node ID
RS485_ACK_Reply[2] = 0x02; //ACK Empfangen
delay (1); // give the master a moment to prepare to receive
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
sendMsg (RS485Write, RS485_ACK_Reply, sizeof RS485_ACK_Reply);
delay (2); // give RS485 chip time to send
digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
Serial.print("Data ACK_data:");
for (byte i = 0; i < sizeof (RS485_ACK_Reply); ++i)
{
Serial.print(' ');
showByte(RS485_ACK_Reply[i]);
}
Serial.println(); // Can be ignored
sample.Acknowledge = 0;
}
}//--(end main loop )---
//*********( THE END )***********
Gruß
Snoops