Forum: Mikrocontroller und Digitale Elektronik TCP/IP Server


von Markus B. (boerni98)


Lesenswert?

Hallo Gemeinde,

wie kann ich einen TCP/IP Server auf dem RN-XV mit dem Serial Monitor 
vom Arduino programmieren?
Ich benötige den TCP/IP Server um über eine App die Befehle zu dem RN-XV 
sendet der sie über den RX/TX an den Arduino weitergibt. Der Arduino 
steuert dann Lampen.

Die App: 
https://play.google.com/store/apps/details?id=com.apps.emim.btrelaycontrolfree&hl=de

Die App enthält bereits einen Code für den Arduino Mega und Uno.
Ich fragte ihm was ich tun muss da mit alles mit dem RN-XV funzt und er 
antwortete:

Hello!
The only thing you need is a tcp/ip server. This server will receive 
characters from the app, this are the codes sent by the app.
If you take a look at the provided code, standard Arduino libraries are 
used.
Try to program a TCP/IP server in your board and debug it using serial 
port(everything that comes from the server is printed to the serial 
port) to see what's being sent from the app to the Arduino.
Best regards,
Juan Luis

Habe ich das alles richtig verstanden oder habe ich einen Fehler?

von PittyJ (Gast)


Lesenswert?

Umd das zu verstehen:
>>wie kann ich einen TCP/IP Server auf dem RN-XV mit dem Serial Monitor
>>vom Arduino programmieren?

Du hast eine Arduino. Dieser besitzt einen Serial Monitor.
Das möchtest du als Entwicklungsumgebung verwenden.
Mit dieser Entwicklungsumgebung möchtest du einen TCP/IP Server 
entwickeln, der auf einem Gerät Namens RN-XV laufen soll.

Habe ich das jetzt richtig verstanden?

von Bitflüsterer (Gast)


Lesenswert?

Handelt es sich bei dem geheimnisvollen "RN-XV" um das hier: 
https://www.sparkfun.com/products/10822 ?

Falls ja, dann hat das Ding schon einen TCP/IP Stack und reicht die 
Nutzlast an einem seriellen Interface heraus. Steht alles auf der Seite.

von Markus B. (boerni98)


Lesenswert?

Danke für die Hilfe!

Ja es handelt sich um die: RN-XV WiFly Module - Wire Antenne

Was ist mit der Nutzlast an einem seriellen Interface gemeint?

Ich Nutze die serielle Schnittstelle vom Arduino um den RN-XV zu 
programmieren (also ich will den TCP/IP Server erstellen) danach sende 
ich den code aus der app an den arduino und schließe wieder den RN-XV 
an.

Ist ein TCP/IP Stack das gleiche wie ein TCP/IP Server?

Sorry bin erst 15 und kenne mich auf dem Gebiet fast nicht aus :( 
leider.

von Bitflüsterer (Gast)


Lesenswert?

Markus B. schrieb:
> Danke für die Hilfe!
>
> Ja es handelt sich um die: RN-XV WiFly Module - Wire Antenne

Aha.

> Was ist mit der Nutzlast an einem seriellen Interface gemeint?

Schau Dir mal das TCP/IP Protokoll an. Die Nutzlast sind diejenigen 
Daten die von der Anwendung (Client) zum Server oder umgekehrt 
übertragen werden. Also etwa eine email, eine Tabelle etc. Der Gegensatz 
ist der Header, der grob vereinfacht zur Steuerung der Übertraung dient.

> Ich Nutze die serielle Schnittstelle vom Arduino um den RN-XV zu
> programmieren (also ich will den TCP/IP Server erstellen) danach sende
> ich den code aus der app an den arduino und schließe wieder den RN-XV
> an.
Welchen Code? Meinst Du "Programmcode", einen "Steuerungscode" oder 
etwas das mit Verschlüsselung zu tun hat? Soweit ich die Beschreibung 
verstehe, ist im RN-XV schon ein TCP/IP Stack vorhanden. Du brauchst 
also keinen weiteren mehr. Was Du brauchst ist eine Software, die den 
RN-XV über das serielle Interface steuert und Daten darüber annimmt, 
diese interpretiert und danach die Lampen steuert.

> Ist ein TCP/IP Stack das gleiche wie ein TCP/IP Server?
Soweit ich weiß, gibt es den Begriff nicht.
TCP/IP ist ein Protokoll zur Datenübertragung. Ein Server ist ein Gerät, 
das Anfragen (über irgendein Protokoll) annimmt und darauf reagiert. Ein 
TCP/IP-Server ist sowas ähnliches wie ein Bundesstrassen-Auto - reiner 
Blödsinn.

> Sorry bin erst 15 und kenne mich auf dem Gebiet fast nicht aus :(
> leider.

Hm. Schreibe doch mal was zu Deinen bisherigen Erfahrungen. Was hast Du 
schon selbst programmiert (also keinen fertigen Code compiliert)?

von N0RA D0LPHIN (Gast)


Lesenswert?

>> Sorry bin erst 15 und kenne mich auf dem Gebiet fast nicht aus :(
>> leider.

Glaube ich dir nicht. Das ist eine Masche von dir.

von Markus B. (boerni98)


Lesenswert?

Der Code für den Arduino:


/*Total Relay Control for Arduino MEGA
 Enables Total Relay control using serial port 1 and JY-MCU bluetooth 
serial converter
 or Ethernet Shield and digital IO ports (Refer to RelayPins[]) as 
outputs. Feedback
 is provided sendingback tags and texts with the values in them.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Debug serially using Serial Monitor (serial port 0)

 Author: Juan Luis Gonzalez Bello
 Date: 24/12/2013
 Get the app on google play:
 https://play.google.com/store/apps/details?id=com.apps.emim.btrelaycontrolfree&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5hcHBzLmVtaW0uYnRyZWxheWNvbnRyb2wiXQ..

 ** For Arduino UNO, Serial is used instead of Serial1 (serial one)
 ** If using mega, pins 53,52,51 and 50 are used for SPI-ethernet shield 
communication
 ** After copy-paste of this code, use Tools -> Atomatic Format
 */
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>

// This is the maximum relay count for this example
#define MAX_RELAYS 12

// 0 unused technology, 1 used tech (you can use both)
#define BLUETOOTH 1
#define ETHERNET  1

// 0 unused board, 1 Used board (choose just one!!!)
#define ARD_UNO  0
#define ARD_MEGA 1

// Arrays used to "decode" incoming bytes and ports
// Relay 1 is at pin 53, relay 2 is at pin 51 and so on. You can change 
these numbers in order to fit your circuit.
int RelayPins[MAX_RELAYS]  = {
  53, 51, 49, 47, 45, 43, 40, 39, 37, 35, 33, 31};
int KeysOn[MAX_RELAYS]  = {
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'};
int KeysOff[MAX_RELAYS] = {
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};

// Used to keep track of the relay status
int RelayStatus = 0;
int STATUS_EEADR = 20;

#if ETHERNET
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,60);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
EthernetClient client;
#endif

void setup() {
  #if BLUETOOTH
    #if ARD_UNO
      Serial.begin(9600); // initialize BT serial port:
    #elif ARD_MEGA
      Serial1.begin(9600); // initialize BT serial port:
      Serial.begin(9600);  // initialize debug serial port:
    #endif
  #endif

  #if ETHERNET
    // start the Ethernet connection and the server:
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
  #endif

  // Initialize I/O PORTS
  for(int i = 0; i < MAX_RELAYS; i++){
    pinMode(RelayPins[i], OUTPUT);
  }

  // Initialize relay status (last known status from eeprom)
  RelayStatus = EEPROM.read(STATUS_EEADR);
  for(int i = 0; i < MAX_RELAYS; i++){
    if((RelayStatus & (1 << i)) == 0)
      digitalWrite(RelayPins[i], LOW);
    else
      digitalWrite(RelayPins[i], HIGH);
    SendRelayState(i);
  }

  // Greet arduino total control
  #if BLUETOOTH
    #if ARD_MEGA
      Serial1.println("Welcome! Thanks for your support!");
    #elif ARD_UNO
      Serial.println("Welcome! Thanks for your support!");
    #endif
  #endif

  #if ETHERNET
    server.println("Welcome! Thanks for your support!");
  #endif
}

// Used as a kind of delay in this example
int Prescaler = 0;
unsigned int Seconds   = 1;
unsigned int Minutes   = 0;

void loop() {
  String sSample;
  String sSampleNo;
  String sSeconds;
  String sMinutes;

  int iSample;

  delay(1);

  // This is true each  1 second
  if(Prescaler++ > 1000){
    Prescaler = 0; // Reset prescaler

    // Send analog samples to be displayed at text tags
    for(int i = 0; i < 6; i++){
      iSample = analogRead(i);
      sSample = String(iSample);
      sSampleNo = String(i);
      SendTextTag(i, "A" + sSampleNo + ": " + sSample);
    }

    // Send active time to app
    if((Seconds++ % 60) == 0)
      Minutes++;

    sSeconds = String(Seconds);
    sMinutes = String(Minutes);

    SendTextTag(6, "Seconds On: " + sSeconds);
    SendTextTag(7, "Minutes On: " + sMinutes);
  }

  #if BLUETOOTH
    // BT Relay Control
    #if ARD_UNO
    if (Serial.available() > 0) {
      int inByte = Serial.read();
      for(int i = 0; i < MAX_RELAYS; i++){
        Turn_RelayOn_Key(inByte, i);
        Turn_RelayOff_Key(inByte, i);
      }
      // Character '[' is received every 2.5s in order
      // to tell the android we are still connected.
      // Use this command to refresh all relay states
      // Empty messages are not displayed!!
      if(inByte == '['){
        for(int i = 0; i < MAX_RELAYS; i++)
          SendRelayState(i);
      }
    }
    #elif ARD_MEGA
    if (Serial1.available() > 0) {
      int inByte = Serial1.read();
      for(int i = 0; i < MAX_RELAYS; i++){
        Turn_RelayOn_Key(inByte, i);
        Turn_RelayOff_Key(inByte, i);
      }
      // Character '[' is received every 2.5s in order
      // to tell the android we are still connected.
      // Use this command to refresh all relay states
      // Empty messages are not displayed!!
      if(inByte == '['){
        for(int i = 0; i < MAX_RELAYS; i++)
          SendRelayState(i);
      }
    }
    #endif
  #endif

  #if ETHERNET
    // Wifi Relay Control
    // listen for incoming clients
    client = server.available();
    if (client) {
      char inByte = client.read();
      for(int i = 0; i < MAX_RELAYS; i++){
        Turn_RelayOn_Key(inByte, i);
        Turn_RelayOff_Key(inByte,i);
      }
      // Character '[' is received every 2.5s in order
      // to tell the android we are still connected.
      // Use this command to refresh all relay states
      // Empty messages are not displayed!!
      if(inByte == '['){
        for(int i = 0; i < MAX_RELAYS; i++)
          SendRelayState(i);
      }
    }
  #endif
}

// Turn_RelayOn_Key
//
// Checks if the input is the key for turning on the correspondig relay,
// sends feedback to app, and store relay status in EEPROM (non volatile 
memory)
// Input:
//   input,   is the byte received from App
//   relayNo, is the relay number from 0 to (MAX_RELAYS - 1)
// Output:
//   None
void Turn_RelayOn_Key(int input, int relayNo){
  if (input == KeysOn[relayNo]){
    digitalWrite(RelayPins[relayNo], HIGH);

    SendRelayState(relayNo);
    TurnOffFeedBack(input);

    // Modify and save relay status
    RelayStatus |= (1 << relayNo);
    EEPROM.write(STATUS_EEADR, RelayStatus);
  }
}

// Turn_RelayOff_Key
//
// Checks if the input is the key for turning off the correspondig
// relay, sends feedback to app, and store relay status in EEPROM (non 
volatile memory)
// Input:
//   input,   is the byte received from App
//   relayNo, is the relay number from 0 to (MAX_RELAYS - 1)
// Output:
//   None
void Turn_RelayOff_Key(int input, int relayNo){
  if (input == KeysOff[relayNo]){
    digitalWrite(RelayPins[relayNo], LOW);

    SendRelayState(relayNo);
    TurnOffFeedBack(input);

    // Modify and save relay status
    RelayStatus &= ~(1 << relayNo);
    EEPROM.write(STATUS_EEADR, RelayStatus);
  }
}

// TurnOnFeedBack
//
// Send FeedBack to Android App as text wich will appear on top of app.
// Input:
//   key, is the code for turning on a relay
// Output:
//   None
void TurnOnFeedBack(int key){
   #if BLUETOOTH
    #if ARD_MEGA
      // This is sent via bluetooth
      Serial1.print("Relay turned On: ");
      Serial1.println((char)key);

      // This is sent to the computer (USB)
      Serial.print("Relay turned On: ");
      Serial.println((char)key);
    #elif ARD_UNO
      Serial.print("Relay turned On: ");
      Serial.println((char)key);
    #endif
  #endif

  #if ETHERNET
    //Print to all available clients
    server.print("Relay turned On: ");
    server.println((char)key);
  #endif
}


// TurnOffFeedBack
//
// Send FeedBack to Android App as text wich will appear on top of app.
// Input:
//   key, is the code for turning off a relay
// Output:
//   None
void TurnOffFeedBack(int key){
  #if BLUETOOTH
    #if ARD_MEGA
      // This is sent via bluetooth
      Serial1.print("Relay turned Off: ");
      Serial1.println((char)key);

      // This is sent to the computer (USB)
      Serial.print("Relay turned Off: ");
      Serial.println((char)key);
    #elif ARD_UNO
      Serial.print("Relay turned Off: ");
      Serial.println((char)key);
    #endif
  #endif

  #if ETHERNET
    //Print to all available clients
    server.print("Relay turned Off: ");
    server.println((char)key);
  #endif
}


// SendRelayState
//
// Use this function to turn on and off toggle buttons
// This function uses the button tag "<Butn" to tell the
// App the state of the relay.
// Tag format is: "<ButnXX:Y",
// where XX is a number from 00 to 11 (the relay number)
// and Y is a number 1 or 0 (the corresponding relay state)
// Input:
//   relayNo, Relay Number, number from 0 to (MAX_RELAYS - 1)
// Output:
//   None
void SendRelayState(int relayNo){
  #if BLUETOOTH
    #if ARD_MEGA
     Serial1.print("<Butn");

     // Fill with a "0" for one digit numbers
     if(relayNo < 10)
       Serial1.print('0');

     Serial1.print(relayNo);

     // Print relay state, don't forget the new line character!
     if(digitalRead(RelayPins[relayNo]))
       Serial1.println(":1");
     else
       Serial1.println(":0");

    #elif ARD_UNO
     Serial.print("<Butn");

     // Fill with a "0" for one digit numbers
     if(relayNo < 10)
       Serial.print('0');

     Serial.print(relayNo);

     // Print relay state, don't forget the new line character!
     if(digitalRead(RelayPins[relayNo]))
       Serial.println(":1");
     else
       Serial.println(":0");
    #endif
  #endif

  #if ETHERNET
     server.print("<Butn");

     // Fill with a "0" for one digit numbers
     if(relayNo < 10)
       server.print('0');

     server.print(relayNo);

     // Print relay state, don't forget the new line character!
     if(digitalRead(RelayPins[relayNo]))
       server.println(":1");
     else
       server.println(":0");
  #endif
}

// SendTextTag
//
// Use this function to modify the displayed text below
// relay pictures. Tag format is: "<TextXX:YYYYY...",
// where XX is a number from 00 to 11 (the tag number)
// and YYYYY... is the string to be displayed at tag
// Input:
//   tagNo: tag Number, number from 0 to (MAX_RELAYS - 1)
//   text: the string to be displayed (MAX 1000 characters)
// Output:
//   None
void SendTextTag(int tagNo, String text){
  #if BLUETOOTH
    #if ARD_MEGA
     Serial1.print("<Text");

     // Fill with a "0" for one digit numbers
     if(tagNo < 10)
       Serial1.print('0');

     Serial1.print(tagNo);
     Serial1.print(':');
     Serial1.println(text);

    #elif ARD_UNO
     Serial.print("<Text");

     // Fill with a "0" for one digit numbers
     if(tagNo < 10)
       Serial.print('0');

     Serial.print(tagNo);
     Serial.print(':');
     Serial.println(text);
    #endif
  #endif

  #if ETHERNET
     server.print("<Text");

     // Fill with a "0" for one digit numbers
     if(tagNo < 10)
       server.print('0');

     server.print(tagNo);
     server.print(':');
     server.println(text);
  #endif
}


Bis jetzt habe ich nur ein paar Lichtprogramme mit 15 Leds und 
verschiedenen Mustern gemacht.

von Bitflüsterer (Gast)


Lesenswert?

Da Du keine spezifischen Fragen stellst und auf Fragen nicht reagierst, 
betrachte ich das Thema als erledigt.

von Markus B. (boerni98)


Lesenswert?

Meine Hauptfrage ist:

was muss ich in den Serial Monitor (damit ich über die serielle 
Schnittstelle vom Arduino den RN-XV programmieren kann) eingeben damit 
ich diesen TCP/IP Stack erstellen kann?

Auf welche Frage habe ich nicht reagiert?

von Bitflüsterer (Gast)


Lesenswert?

Markus B. schrieb:
> Meine Hauptfrage ist:
>
> was muss ich in den Serial Monitor (damit ich über die serielle
> Schnittstelle vom Arduino den RN-XV programmieren kann) eingeben damit
> ich diesen TCP/IP Stack erstellen kann?

Diese Frage ist Dir schon zweimal beantwortet worden. Wenn Du einen Text 
nicht verstehen kannst, dann hat es keinen Sinn Dir etwas mitzuteilen.

> Auf welche Frage habe ich nicht reagiert?

Auf die Frage nach Deinen Erfahrungen. Ein einzelner Satz ist keine 
Beschreibung. Oder was meinst Du was ich mir Deiner Meinung nach unter: 
"
Bis jetzt habe ich nur ein paar Lichtprogramme mit 15 Leds und
verschiedenen Mustern gemacht." vorstellen soll?

15 LEDs mit Muster wäre z.B. einfach ein
1
int i = 0;
2
while (1) {
3
  PORTB = i++;
4
  PORTC = i++;
5
}

Inhaltlich und der Komplexität nach, hat das wohl kaum ein Zusammenhang 
zu Deinem Vorhaben.
Eine Erklärung zu Erfahrungen besteht muss mir einen Eindruck vermitteln 
ob Du überhaupt Kenntnisse hast, an die sich Dein Problem irgendwie 
anschliessen läst.

Programmieren ist nichts was man sich mal mit drei Sätzen in einem Forum 
aneignet wie man eine Tüte Chips vor der Glotze verdrückt.

Abgesehen davon ist das posten von sehr langem Code, der dazu mit Deinem 
Thema nur am Rande zu tun hat auch nicht gerade sinnvoll. Dazu gibt es 
auch extra einen Hinweis beim posten.

Also sieh mal allein zu wie Du fertig wirst.

von Markus B. (boerni98)


Lesenswert?

Also ich habe jetzt mal weiter ausprobiert und gesucht und bin jetzt so 
weit, dass ich mit Comm Operator erst eine Verbindung über den Serial 
port herstelle und danach einen TCP Server erstelle.

Allerdings kann ich nur bei der Erstellung des TCP Servers den Listen 
Port auswählen wenn ich das dann an den RN XV schicke kommt nach kurzer 
Zeit dies zurück:

Rec: (Uhrzeit) GET /HTTP/1.1
Host: 192.168.2.106
Connection: Keep-Alive

dies Nachricht kommt so alle paar Minuten zurück allerdings kann ich 
kein WLAN-Netzwerk finden wenn ich mit dem Computer oder Handy suche.

Kann mir jemand weiterhelfen?

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.