zdenekpetrzd

Arduino eth draha

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