zdenekpetrzd

Arduino ethernet rozděluje data

Mar 3rd, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.33 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.  
  21. #include <SPI.h>
  22. #include <Ethernet.h>
  23. #include "avdweb_VirtualDelay.h"
  24.  
  25. VirtualDelay delay1;
  26. VirtualDelay delay2;
  27. // Reset arduina
  28. #define SYSRESETREQ (1<<2)
  29. #define VECTKEY (0x05fa0000UL)
  30. #define VECTKEY_MASK (0x0000ffffUL)
  31. #define AIRCR (*(uint32_t*)0xe000ed0cUL) // fixed arch-defined address
  32. #define REQUEST_EXTERNAL_RESET (AIRCR=(AIRCR&VECTKEY_MASK)|VECTKEY|SYSRESETREQ)
  33.  
  34. // Enter a MAC address and IP address for your controller below.
  35. // The IP address will be dependent on your local network:
  36. byte mac[] = {
  37. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  38. };
  39. IPAddress ip(192, 168, 0, 177);
  40.  
  41. // Enter the IP address of the server you're connecting to:
  42. IPAddress server(192, 168, 0, 110);
  43.  
  44. // Initialize the Ethernet client library
  45. EthernetClient client;
  46. /*
  47. 1_Reverser 11_RychlomerVyp 21_JisticF140 31_JisticF121 41_JisticF122 51_JisticF150 61_JisticF203
  48. 2_Regulator 12_Sander 22_JisticF182 32_JisticF180 42_JisticF123 52_JisticF151 62_JisticF202
  49. 3_EngineBrakeControl 13_RoletaPP 23_JisticF109 33_JisticF127 43_JisticF124 53_JisticF152 63_KohoutSberaceP
  50. 4_DynamicBrake 14_RoletaPL 24_JisticF116 34_JisticF128 44_JisticF125 54_JisticF130 64_KohoutSberaceZ
  51. 5_TrainBrakeControl 15_RoletaBocniPP 25_Jistic07neobsazeny 35_JisticF153 45_JisticF126 55_JisticF131 65_RucniBrzda
  52. 6_HandBrake 16_RoletaBocniPL 26_JisticF111 36_JisticF154 46_JisticF106 56_JisticF132 66_PrepinacBaterie
  53. 7_PantographControl 17_OknoP 27_Jistic09neobsazeny 37_JisticF134 47_JisticF191 57_JisticF129 67_PrepinacCZ
  54. 8_Headlights 18_OknoL 28_Jistic10neobsazeny 38_JisticF135 48_JisticF192 58_JisticF144 68_PrepinacOsvetleniPodvozku
  55. 9_SpeedometerKPH 19_JisticF104 29_JisticF107 39_JisticF136 49_Jistic31neobsazeny 59_Jistic41neobsazeny 69_PrepinacRizeni
  56. 10_Rychlomer 20_JisticF101 30_JisticF102 40_JisticF120 50_Jistic32neobsazeny 60_JisticF114 70_KlickaTopeni
  57.  
  58. 71_TopeniVlaku 81_PrepinacPozickaZP 91_EDB 101_TlacitkoARR40 111_TlacitkoARRMinus 121_TlacitkoRuseniOchran
  59. 72_PrepinacNJ 82_PrepinacPozickaPL 92_TlacitkoBdelostiL 102_TlacitkoARR50 112_TlacitkoPistala 122_PrepinacSterace
  60. 73_VirtualDJNJ 83_PrepinacPozickaPP 93_TlacitkoBdelostiP 103_TlacitkoARR60 113_TlacitkoPJ 123_SteracP
  61. 74_PrepinacSystemu 84_PrepinacOsvetleniPristroju 94_PrepinacRezimRizeni 104_TlacitkoARR70 114_KontrolkaPJ 124_SteracL
  62. 75_VypinacHV 85_PrepinacOsvetleniKabiny 95_PrepinacRezimJizdy 105_TlacitkoARR80 115_PrepinacVolbaVlaku 125_PrepinacPoruchaMS
  63. 76_PrepinacVentilatory 86_OsvStTlumene 96_PrepinacPT 106_TlacitkoARR90 116_TlacitkoPiskovani 126_PrepinacDJ
  64. 77_PrepinacKompresor2 87_OsvStPlne 97_TlacitkoARR0 107_TlacitkoARR100 117_OvladacBP 127_PrepinacOdvodneni
  65. 78_PrepinacKompresor1 88_PrepinacReflektoru 98_TlacitkoARR10 108_TlacitkoARR120 118_OvladacBSE 128_PrepinacVentilatorKabiny
  66. 79_PrepinacSberace 89_Smer 99_TlacitkoARR20 109_TlacitkoARR140 119_TlacitkoPlus 129_VentilatorKabiny
  67. 80_PrepinacPozickaZL 90_Kontroler 100_TlacitkoARR30 110_TlacitkoARRPlus 120_TlacitkoMinus 130_VentilatorKabiny_moturek
  68.  
  69. 131_VentilatorKabiny_drzak 141_DiagK141 151_DiagQ14A 161_DiagQ12L_Q14L_B 171_DiagK106 181_IndNapeti 191_KontrolkaZaver
  70. 132_HoukackaPomocnik 142_DiagK140 152_DiagQ13B 162_Diag1-2_XA 172_DiagNR 182_IndHVst 192_KontrolkaPrutok
  71. 133_DvirkaDiagnostiky 143_DiagK116 153_DiagQ14B 163_Diag3-4_XB 173_DiagDR 183_VmetrSS 193_PocitadloNaprav0XX
  72. 134_DiagK117_K138 144_DiagW 154_DiagQ13L_D1 164_DiagK101_J0 174_PrepinacLampaStolek 184_VmetrST 194_PocitadloNapravX0X
  73. 135_DiagA102_N 145_DiagG 155_DiagQ14L_D1 165_DiagQ11A_J 175_PrepinacLampaSJR 185_AmetrBuzeni 195_PocitadloNapravXX0
  74. 136_DiagS125_S126 146_DiagK36A_S0 156_DiagQ13L_R1 166_DiagQ12A_J 176_Vysilacka 186_Ammeter 196_TlakPotrubi
  75. 137_DiagS502_H280 147_DiagK36L_S1 157_DiagQ14L_R1 167_DiagQ11L_Q13L_J1 177_AmetrBaterie 187_AmetrMS1 197_BrakePipePressureBAR
  76. 138_DiagQ06_H280 148_DiagP 158_DiagQ11B_Q11C 168_DiagQ12L_Q14L_J1 178_VmetrBaterie 188_AmetrMS2 198_TlakVzduchojem
  77. 139_DiagK117 149_DiagZ 159_DiagQ12B_Q12C 169_DiagK40A_E0 179_IndTopeni 189_PomernyTah 199_TlakValce
  78. 140_DiagS407 150_DiagQ13A 160_DiagQ11L_Q13L_B 170_DiagK40L_E1 180_IndHVss 190_KontrolkaPorucha 200_LocoBrakeCylinderPressureBAR
  79.  
  80. 201_TrainBrakeCylinderPressureBAR 211_PrepinacDiagCerpadlo1 221_PrepinacDiagNabijeni 231_DiagSkrinOchran 241_DiagVentilator1 251_ZvukVentilatoryTM
  81. 202_TlakPrevodnik 212_PrepinacDiagPolovodicovyPrvek 222_DiagMazaniKompresor1 232_DiagMazaniKompresor2 242_ZvukVentilatorKabiny 252_ZvukVZSignalizace
  82. 203_Opakovac 213_PrepinacDiagBuchholzovoRele 223_DiagNadproudUnipulsu 233_DiagVypnutiEDB 243_ZvukPomocnyKompresor 253_ZvukVZSignalizaceTlacitko
  83. 204_OpakovacModra 214_PrepinacDiagOlejTR 224_DiagPomocnyKompresor 234_DiagTopeniJimek 244_ZvukTrafo 254_ZvukVZStart
  84. 205_OpakovacNatoceni 215_PrepinacDiagSkrinOchran 225_DiagRegulace1 235_DiagRegulace2 245_ZvukUnipuls 255_ZvukZK
  85. 206_PrepinacLS90 216_PrepinacDiagVentilace1 226_DiagCerpadlo2 236_DiagNabijeni 246_ZvukKompresory 256_ZvukNakolku
  86. 207_KontrolkaVZzelena 217_PrepinacDiagVentilace2 227_DiagCerpadlo1 237_DiagKompresory 247_ZvukPistaly 257_ZvukOkno
  87. 208_KontrolkaVZcervena 218_PrepinacDiagSkluz 228_DiagPrvkyMenicu 238_DiagCentralniZdroj 248_ZvukHoukacky 258_ZvukMSvyp
  88. 209_UkazatelARR 219_PrepinacDiagCZ 229_DiagBuchholzovoRele 239_DiagSkluz 249_ZvukPiskovani 259_ZvukStanoviste
  89. 210_PrepinacDiagCerpadlo2 220_PrepinacDiagKompresory 230_DiagOlejTransformatoru 240_DiagVentilator2 250_ZvukOdkaleni 260_ZvukBlokaceStrojovny
  90.  
  91. 261_NahozeniMasiny
  92. 262_Porucha
  93. 263_PoruchaTM
  94. 264_NapetiTV
  95. */
  96.  
  97. // Provozní dignostika 1. a 2. panel
  98. 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)";
  99. 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)";
  100. // Diagnostika u stropu
  101. String group3 = "SetGroup(3, DiagMazaniKompresor1,DiagNadproudUnipulsu,DiagPomocnyKompresor,DiagRegulace1,DiagCerpadlo2,DiagCerpadlo1,DiagPrvkyMenicu,DiagBuchholzovoRele,DiagOlejTransformatoru,DiagSkrinOchran)";
  102. String group4 = "SetGroup(4, DiagMazaniKompresor2,DiagVypnutiEDB,DiagTopeniJimek,DiagRegulace2,DiagNabijeni,DiagKompresory,DiagCentralniZdroj,DiagSkluz,DiagVentilator2,DiagVentilator1)";
  103.  
  104. 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};
  105. int velikost;
  106. int hodnoty[410];
  107. int skupina1[20];
  108. int skupina2[20];
  109. int skupina3[10];
  110. int skupina4[10];
  111. int testovaci;
  112.  
  113. int locoName;
  114. int rychlostMax;
  115. byte actualHourHEX;
  116. byte actualMinuteHEX;
  117. byte actualsecondHEX;
  118. int actualyearHEX;
  119. byte actualMonthHEX;
  120. byte actualdayHEX;
  121. byte DECtoHEX[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
  122. 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
  123. 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x60};
  124.  
  125. void setup() {
  126. // Open serial communications and wait for port to open:
  127. Serial.begin(115200);
  128. Serial2.begin(9600);
  129. // Inicializace ethernet klienta
  130. ClientInitialize();
  131.  
  132. // Zjištění počtu aktuálně zadaných kontrolek
  133. velikost = sizeof(potrebneKontrolky) / sizeof(int);
  134.  
  135. client.print(group1);
  136. String value1 = client.readStringUntil('\r\n');
  137. client.print(group2);
  138. String value2 = client.readStringUntil('\r\n');
  139. client.print(group3);
  140. String value3 = client.readStringUntil('\r\n');
  141. client.print(group4);
  142. String value4 = client.readStringUntil('\r\n');
  143. delay(1000);
  144. // První spuštění časovačů
  145. delay1.start(100);
  146. delay2.start(50);
  147. pinMode(13,OUTPUT);
  148. }
  149.  
  150.  
  151. void GetAll(int *seznam) {
  152. for(int i=0; i<velikost; i++) {
  153. String vypis = "GetCV(" + String(seznam[i]) + ",0)::";
  154. client.print(vypis);
  155. }
  156. }
  157.  
  158.  
  159. void Prijem() {
  160. while(client.available() > 0) {
  161. const char s[2] = "=";
  162. char str[200];
  163. char str2[80];
  164. char *a;
  165. char *b;
  166. String stringi = client.readStringUntil('\r\n');
  167. stringi.toCharArray(str, 200);
  168. a = strtok(str, s);
  169. int id = String(a).toInt();
  170. if(id < 500) {
  171. b = strtok(NULL, s);
  172. int valueFloat = 0 ;
  173. valueFloat = (String(b)).toInt();
  174. testovaci = valueFloat;
  175. hodnoty[(int)id] = testovaci;
  176. //Serial.println(String(id) + " = " + String((int)hodnoty[id]) + " = " + String(valueFloat));
  177. }
  178. else if(id == 501) {
  179. b = strtok(NULL, s);
  180. String(b).toCharArray(str2, 80);
  181. int j=0;
  182. for(int i=0; i<80; i++) {
  183. if(isDigit(str2[i]) == true) {
  184. if(str2[i] == '1') {
  185. skupina1[j] = 1;
  186. }
  187. else {
  188. skupina1[j] = 0;
  189. }
  190. j++;
  191. }
  192. }
  193. }
  194. else if(id == 502) {
  195. b = strtok(NULL, s);
  196. String(b).toCharArray(str2, 80);
  197. int j=0;
  198. for(int i=0; i<80; i++) {
  199. if(isDigit(str2[i]) == true) {
  200. if(str2[i] == '1') {
  201. skupina2[j] = 1;
  202. }
  203. else {
  204. skupina2[j] = 0;
  205. }
  206. j++;
  207. }
  208. }
  209. }
  210. else if(id == 503) {
  211. b = strtok(NULL, s);
  212. String(b).toCharArray(str2, 80);
  213. int j=0;
  214. for(int i=0; i<80; i++) {
  215. if(isDigit(str2[i]) == true) {
  216. if(str2[i] == '1') {
  217. skupina3[j] = 1;
  218. }
  219. else {
  220. skupina3[j] = 0;
  221. }
  222. j++;
  223. }
  224. }
  225. }
  226. else if(id == 504) {
  227. b = strtok(NULL, s);
  228. String(b).toCharArray(str2, 80);
  229. int j=0;
  230. for(int i=0; i<80; i++) {
  231. if(isDigit(str2[i]) == true) {
  232. if(str2[i] == '1') {
  233. skupina4[j] = 1;
  234. }
  235. else {
  236. skupina4[j] = 0;
  237. }
  238. j++;
  239. }
  240. }
  241. }
  242. else if(id == 600) {
  243. b = strtok(NULL, s);
  244. String cisloLoko = "";
  245. for(int i=0; i<12; i++) {
  246. if(isDigit(b[i]) == true) {
  247. cisloLoko += b[i];
  248. }
  249. }
  250. locoName = cisloLoko.toInt();
  251. }
  252. }
  253. }
  254.  
  255.  
  256. void ClientInitialize() {
  257. // You can use Ethernet.init(pin) to configure the CS pin
  258. Ethernet.init(10); // Most Arduino shields
  259.  
  260. // start the Ethernet connection:
  261. Ethernet.begin(mac, ip);
  262.  
  263. // Check for Ethernet hardware present
  264. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  265. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  266. while (true) {
  267. delay(1); // do nothing, no point running without Ethernet hardware
  268. }
  269. }
  270. while (Ethernet.linkStatus() == LinkOFF) {
  271. Serial.println("Ethernet cable is not connected.");
  272. delay(1000);
  273. }
  274.  
  275. // give the Ethernet shield a second to initialize:
  276. delay(1000);
  277. Serial.println("connecting...");
  278.  
  279. // if you get a connection, report back via serial:
  280. if (client.connect(server, 8888)) {
  281. Serial.println("connected");
  282. } else {
  283. // if you didn't get a connection to the server:
  284. Serial.println("connection failed");
  285. }
  286. client.setTimeout(100);
  287. }
  288.  
  289.  
  290. void ConnectClient() {
  291. // if the server's disconnected, restart the client:
  292. if (!client.connected()) {
  293. Serial.println();
  294. Serial.println("disconnecting.");
  295. client.stop();
  296. delay(1000);
  297. // do nothing:
  298. while (true) {
  299. if (client.connect(server, 8888)) {
  300. Serial.println("Server available again, resseting...");
  301. delay(100);
  302. REQUEST_EXTERNAL_RESET;
  303. break;
  304. }
  305. }
  306. }
  307. }
  308.  
  309.  
  310. int * calcCheckSum (int* pack, int packSize)
  311. {
  312. int sum = 0;
  313. for (int i=0; i < (packSize-1); i++) sum += pack[i];
  314. sum = sum & 0xFF;
  315. pack[packSize-1] = 0x100 - sum;
  316. return pack;
  317. }
  318.  
  319. unsigned int pw [] = { 1, 10, 100, 1000, 10000 } ;
  320. int ExtractDigit(int V, int P)
  321. {
  322. return V / pw[P] % 10 ;
  323. }
  324.  
  325.  
  326. void loop() {
  327. int cas1 = millis();
  328. if(delay2.elapsed()) {
  329. //ConnectClient();
  330. GetAll(&potrebneKontrolky[0]);
  331. client.print("GetGroupValues(1)::");
  332. client.print("GetGroupValues(2)::");
  333. client.print("GetGroupValues(3)::");
  334. client.print("GetGroupValues(4)::");
  335. client.print("GetLocoName::");
  336. digitalWrite(13,hodnoty[190]);
  337. delay2.start(100);
  338. }
  339. Prijem();
  340.  
  341. if(delay1.elapsed()) {
  342. int hodnota = ExtractDigit(locoName, 3);
  343. if(hodnota == 2) {
  344. rychlostMax = 140;
  345. }
  346. else if(hodnota == 3) {
  347. rychlostMax = 120;
  348. }
  349. else {
  350. rychlostMax = 160;
  351. }
  352.  
  353. actualHourHEX = DECtoHEX[hodnoty[406]];
  354. actualMinuteHEX = DECtoHEX[hodnoty[407]];
  355. actualsecondHEX = DECtoHEX[hodnoty[408]];
  356. actualMonthHEX = DECtoHEX[2];
  357. actualdayHEX = DECtoHEX[3];
  358. actualyearHEX = DECtoHEX[19];
  359. byte rychlostHEX = hodnoty[9];
  360. byte ukazatelARRHEX = hodnoty[209];
  361. byte rychlostMaxHEX = rychlostMax;
  362.  
  363. digitalWrite(3, 1);
  364. byte msg[27];
  365. msg[0] = 0x19; //počet bajtů ve zpráve
  366. msg[1] = 0x01; //kód povelu 01
  367. msg[2] = 0x00; // Stavové slovo zprávy
  368. msg[3] = actualsecondHEX; //Vteřiny
  369. msg[4] = actualMinuteHEX; //minuty
  370. msg[5] = actualHourHEX; // Hodiny
  371. msg[6] = actualdayHEX; //den
  372. msg[7] = actualMonthHEX; //Měsíc
  373. msg[8] = actualyearHEX; //Rok
  374. msg[9] = 0x12; //dráha //0x88
  375. msg[10] = 0x65; //0x03
  376. msg[11] = 0x49; //0x03
  377. msg[12] = 0x99; //0x02
  378. msg[13] = rychlostHEX; //Rychlost akt
  379. msg[14] = ukazatelARRHEX; //Rychlost nav
  380. msg[15] = rychlostMaxHEX; //Max V
  381. msg[16] = 0x00; //Tlak
  382. msg[17] = 0x00; //další analog
  383. msg[18] = 0x00; //1. čís
  384. msg[19] = 0x00; //2. čís
  385. msg[20] = 0x00; //3. čís
  386. msg[21] = 0x00; //4. čís
  387. msg[22] = 0x00; //5. čís
  388. msg[23] = 0x00; //Chyb bajt
  389. msg[24] = 0x00; //traťová značka
  390. msg[25] = 0x00; //Traťová značka
  391.  
  392. uint8_t CRC = 0;
  393. for(int8_t i = 0; i< 25; i++){
  394. CRC -= msg[i];
  395. }
  396.  
  397. msg[26] = CRC;
  398.  
  399. Serial2.write(msg,27);
  400. digitalWrite(3, 0);
  401. delay1.start(90);
  402. }
  403. int cas2 = millis();
  404. if(cas2-cas1 > 5) {
  405. Serial.println(cas2-cas1);
  406. }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment