zdenekpetrzd

Ethernet

Feb 3rd, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.82 KB | None | 0 0
  1. /*
  2.   Telnet client
  3.  
  4.  This sketch connects to a a telnet server (http://www.google.com)
  5.  using an Arduino Wiznet Ethernet shield.  You'll need a telnet server
  6.  to test this with.
  7.  Processing's ChatServer example (part of the network library) works well,
  8.  running on port 10002. It can be found as part of the examples
  9.  in the Processing application, available at
  10.  http://processing.org/
  11.  
  12.  Circuit:
  13.  * Ethernet shield attached to pins 10, 11, 12, 13
  14.  
  15.  created 14 Sep 2010
  16.  modified 9 Apr 2012
  17.  by Tom Igoe
  18.  */
  19.  
  20. #include <SPI.h>
  21. #include <Ethernet.h>
  22. #include "avdweb_VirtualDelay.h"
  23.  
  24. VirtualDelay delay1;
  25. VirtualDelay delay2;
  26.  
  27. // Enter a MAC address and IP address for your controller below.
  28. // The IP address will be dependent on your local network:
  29. byte mac[] = {
  30.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  31. };
  32. IPAddress ip(192, 168, 0, 177);
  33.  
  34. // Enter the IP address of the server you're connecting to:
  35. IPAddress server(192, 168, 0, 110);
  36.  
  37. // Initialize the Ethernet client library
  38. EthernetClient client;
  39.  
  40. String group1 = "SetGroup(1, DiagK117_K138,DiagA102_N,DiagS125_S126,DiagS502_H280,DiagQ06_H280,DiagK117,DiagS407,DiagK141,DiagK140,DiagK116,,DiagW,DiagG,DiagK36A_S0,DiagK36L_S1,DiagP,DiagZ,DiagQ13A,DiagQ14A,DiagQ13B,DiagQ14B)";
  41. String group2 = "SetGroup(2, DiagQ13L_D1,DiagQ14L_D1,DiagQ13L_R1,DiagQ14L_R1,DiagQ11B_Q11C,DiagQ12B_Q12C,DiagQ11L_Q13L_B,DiagQ12L_Q14L_B,Diag1-2_XA,Diag3-4_XB,DiagK101_J0,DiagQ11A_J,DiagQ12A_J,DiagQ11L_Q13L_J1,DiagQ12L_Q14L_J1,DiagK40A_E0,DiagK40L_E1,DiagK106,DiagNR,DiagDR)";
  42.  
  43. int potrebneKontrolky[] = {9,114,129,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,196,198,199,202,203,204,207,208,209,252,253,254,406,407,408};
  44. int velikost;
  45. int hodnoty[410];
  46. int skupina1[20];
  47. int skupina2[20];
  48. int testovaci;
  49.  
  50. int locoName;
  51. int rychlostMax;
  52. byte actualHourHEX;
  53. byte actualMinuteHEX;
  54. byte actualsecondHEX;
  55. int actualyearHEX;
  56. byte actualMonthHEX;
  57. byte actualdayHEX;
  58. byte DECtoHEX[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
  59.                    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
  60.                    0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x60};
  61.  
  62. void setup() {
  63.   // You can use Ethernet.init(pin) to configure the CS pin
  64.   Ethernet.init(10);  // Most Arduino shields
  65.   //Ethernet.init(5);   // MKR ETH shield
  66.   //Ethernet.init(0);   // Teensy 2.0
  67.   //Ethernet.init(20);  // Teensy++ 2.0
  68.   //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  69.   //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet
  70.  
  71.   // start the Ethernet connection:
  72.   Ethernet.begin(mac, ip);
  73.  
  74.   // Open serial communications and wait for port to open:
  75.   Serial.begin(115200);
  76.   Serial2.begin(9600);
  77.   //while (!Serial) {
  78.   //  ; // wait for serial port to connect. Needed for native USB port only
  79.   //}
  80.  
  81.   // Check for Ethernet hardware present
  82.   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  83.     Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
  84.     while (true) {
  85.       delay(1); // do nothing, no point running without Ethernet hardware
  86.     }
  87.   }
  88.   while (Ethernet.linkStatus() == LinkOFF) {
  89.     Serial.println("Ethernet cable is not connected.");
  90.     delay(1000);
  91.   }
  92.  
  93.   // give the Ethernet shield a second to initialize:
  94.   delay(1000);
  95.   Serial.println("connecting...");
  96.  
  97.   // if you get a connection, report back via serial:
  98.   if (client.connect(server, 8888)) {
  99.     Serial.println("connected");
  100.   } else {
  101.     // if you didn't get a connection to the server:
  102.     Serial.println("connection failed");
  103.   }
  104.   client.setTimeout(100);
  105.   delay1.start(100);
  106.   delay2.start(50);
  107.  
  108.   velikost = sizeof(potrebneKontrolky) / sizeof(int);
  109.   client.print(group1);
  110.   String value1 = client.readStringUntil('\r\n');
  111.   client.print(group2);
  112.   String value2 = client.readStringUntil('\r\n');
  113.   delay(1000);
  114. }
  115. /*
  116. int GetLocoName() {
  117.   String cisloLok = "";
  118.   if(client.available()){
  119.     client.flush();
  120.   }
  121.   client.print("GetLocoName");/*
  122.   String lokomotiva = client.readStringUntil('\r\n');
  123.   char buf[12];
  124.   lokomotiva.toCharArray(buf, 12);
  125.   for(int i=0; i<12; i++) {
  126.     if(isDigit(buf[i]) == true) {
  127.       cisloLok += buf[i];
  128.     }
  129.   }
  130.   return cisloLok.toInt();*/
  131.  
  132.  
  133. void GetAll(int *seznam) {
  134.   for(int i=0; i<velikost; i++) {
  135.     String vypis = "GetCV(" + String(seznam[i]) + ",0)";
  136.     client.print(vypis);
  137.   }
  138. }
  139.  
  140. void Prijem() {
  141.   while(client.available() > 0) {
  142.     const char s[2] = "=";
  143.     char str[200];
  144.     char str2[80];
  145.     char *a;
  146.     char *b;
  147.     String stringi = client.readStringUntil('\r\n');
  148.     stringi.toCharArray(str, 200);
  149.     a = strtok(str, s);
  150.     int id = String(a).toInt();
  151.     if(id < 500) {
  152.       b = strtok(NULL, s);
  153.       int valueFloat = 0 ;
  154.       valueFloat = (String(b)).toInt();
  155.       testovaci = valueFloat;
  156.       hodnoty[(int)id] = testovaci;
  157.       //Serial.println(String(id) + "  =  " + String((int)hodnoty[id]) + "  =  " + String(valueFloat));
  158.     }
  159.     else if(id == 501) {
  160.       b = strtok(NULL, s);
  161.       String(b).toCharArray(str2, 80);
  162.       int j=0;
  163.       for(int i=0; i<80; i++) {
  164.         if(isDigit(str2[i]) == true) {
  165.           if(str2[i] == '1') {
  166.             skupina1[j] = 1;
  167.           }
  168.           else {
  169.             skupina1[j] = 0;
  170.           }
  171.           j++;
  172.         }
  173.       }
  174.     }
  175.     else if(id == 502) {
  176.       b = strtok(NULL, s);
  177.       String(b).toCharArray(str2, 80);
  178.       int j=0;
  179.       for(int i=0; i<80; i++) {
  180.         if(isDigit(str2[i]) == true) {
  181.           if(str2[i] == '1') {
  182.             skupina2[j] = 1;
  183.           }
  184.           else {
  185.             skupina2[j] = 0;
  186.           }
  187.           j++;
  188.         }
  189.       }
  190.     }
  191.     else if(id == 600) {
  192.       b = strtok(NULL, s);
  193.       String cisloLoko = "";
  194.       for(int i=0; i<12; i++) {
  195.         if(isDigit(b[i]) == true) {
  196.           cisloLoko += b[i];
  197.         }
  198.       }
  199.       locoName = cisloLoko.toInt();
  200.     }
  201.   }
  202. }
  203. /*
  204. int GetCVint(int ovladac, int stav, int minStav) {
  205.   const char s[2] = "=";
  206.   char str[20];
  207.   char *a;
  208.   char *b;
  209.   String vypis = "GetCV(" + String(ovladac) + "," + String(stav) + ")";
  210.   if(client.available() > 0){
  211.     client.flush();
  212.   }
  213.   client.print(vypis);
  214.   String value = client.readStringUntil('\r\n');
  215.   if(value != "") {
  216.     value.toCharArray(str, 20);
  217.     a = strtok(str, s);
  218.     int id = atoi(a);
  219.     b = strtok(NULL, s);
  220.     int valueInt = atoi(b);
  221.    
  222.     if(ovladac = id) {
  223.       return valueInt;
  224.     }
  225.     else {
  226.     Serial.print("Chyba příjem!   ");
  227.     Serial.print(id);
  228.     Serial.print("   ");
  229.     Serial.println(minStav);
  230.     delay(15);
  231.     client.flush();
  232.     return minStav;
  233.     }
  234.   }
  235.   else {
  236.     Serial.print("Chyba!   ");
  237.     Serial.print(ovladac);
  238.     Serial.print("   ");
  239.     Serial.print(minStav);
  240.     Serial.print("   ");
  241.     Serial.println(value);
  242.     delay(15);
  243.     client.flush();
  244.     return minStav;
  245.     }
  246. }
  247. */
  248. void ConnectClient() {
  249.   // if the server's disconnected, restart the client:
  250.   if (!client.connected()) {
  251.     Serial.println();
  252.     Serial.println("disconnecting.");
  253.     client.stop();
  254.     delay(1000);
  255.     // do nothing:
  256.     while (true) {
  257.       if (client.connect(server, 8888)) {
  258.         Serial.println("connected");
  259.         break;
  260.       }
  261.     }
  262.   }
  263. }
  264.  
  265. int * calcCheckSum (int* pack, int packSize)
  266. {
  267.   int sum = 0;
  268.   for (int i=0; i < (packSize-1); i++) sum += pack[i];
  269.   sum = sum & 0xFF;
  270.   pack[packSize-1] = 0x100 - sum;
  271.   return pack;
  272. }
  273.  
  274. unsigned int pw [] = { 1, 10, 100, 1000, 10000 } ;
  275. int ExtractDigit(int V, int P)
  276. {
  277.   return V / pw[P] % 10 ;
  278. }
  279.  
  280.  
  281. void loop() {
  282.   if(delay2.elapsed()) {
  283.     GetAll(&potrebneKontrolky[0]);
  284.     client.print("GetGroupValues(1)");
  285.     client.print("GetGroupValues(2)");
  286.     client.print("GetLocoName");
  287.     delay2.start(15);
  288.   }
  289.   Prijem();
  290.  
  291.  
  292.   if(delay1.elapsed()) {
  293.     int hodnota = ExtractDigit(locoName, 3);
  294.     if(hodnota == 2) {
  295.       rychlostMax = 140;
  296.     }
  297.     else if(hodnota == 3) {
  298.       rychlostMax = 120;
  299.     }
  300.     else {
  301.       rychlostMax = 160;
  302.     }
  303.    
  304.     actualHourHEX = DECtoHEX[hodnoty[406]];
  305.     actualMinuteHEX = DECtoHEX[hodnoty[407]];
  306.     actualsecondHEX = DECtoHEX[hodnoty[408]];
  307.     actualMonthHEX = DECtoHEX[2];
  308.     actualdayHEX = DECtoHEX[3];
  309.     actualyearHEX = DECtoHEX[19];
  310.     byte rychlostHEX = hodnoty[9];
  311.     byte ukazatelARRHEX = hodnoty[209];
  312.     byte rychlostMaxHEX = rychlostMax;
  313.  
  314.     digitalWrite(3, 1);
  315.     byte msg[27];
  316.     msg[0] = 0x19; //počet bajtů ve zpráve
  317.     msg[1] = 0x01; //kód povelu 01
  318.     msg[2] = 0x00; // Stavové slovo zprávy
  319.     msg[3] = actualsecondHEX; //Vteřiny
  320.     msg[4] = actualMinuteHEX; //minuty
  321.     msg[5] = actualHourHEX; // Hodiny
  322.     msg[6] = actualdayHEX; //den
  323.     msg[7] = actualMonthHEX; //Měsíc
  324.     msg[8] = actualyearHEX; //Rok
  325.     msg[9] = 0x12; //dráha   //0x88
  326.     msg[10] = 0x65;          //0x03
  327.     msg[11] = 0x49;          //0x03
  328.     msg[12] = 0x99;          //0x02
  329.     msg[13] = rychlostHEX; //Rychlost akt
  330.     msg[14] = ukazatelARRHEX; //Rychlost nav
  331.     msg[15] = rychlostMaxHEX; //Max V
  332.     msg[16] = 0x00; //Tlak
  333.     msg[17] = 0x00; //další analog
  334.     msg[18] = 0x00; //1. čís
  335.     msg[19] = 0x00; //2. čís
  336.     msg[20] = 0x00; //3. čís
  337.     msg[21] = 0x00; //4. čís
  338.     msg[22] = 0x00; //5. čís
  339.     msg[23] = 0x00; //Chyb bajt
  340.     msg[24] = 0x00; //traťová značka
  341.     msg[25] = 0x00; //Traťová značka
  342.      
  343.     uint8_t CRC = 0;
  344.     for(int8_t i = 0; i< 25; i++){
  345.       CRC -= msg[i];
  346.     }
  347.  
  348.     msg[26] = CRC;
  349.      
  350.     Serial2.write(msg,27);
  351.     digitalWrite(3, 0);
  352.     delay1.start(90);
  353.   }
  354.   while(Serial2.available()) {
  355.     Serial.print("MamData");
  356.   }
  357.   ConnectClient();
  358. }
Advertisement
Add Comment
Please, Sign In to add comment