Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Telnet client
- This sketch connects to a a telnet server (http://www.google.com)
- using an Arduino Wiznet Ethernet shield. You'll need a telnet server
- to test this with.
- Processing's ChatServer example (part of the network library) works well,
- running on port 10002. It can be found as part of the examples
- in the Processing application, available at
- http://processing.org/
- Circuit:
- * Ethernet shield attached to pins 10, 11, 12, 13
- created 14 Sep 2010
- modified 9 Apr 2012
- by Tom Igoe
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include "avdweb_VirtualDelay.h"
- VirtualDelay delay1;
- VirtualDelay delay2;
- // 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, 0, 177);
- // Enter the IP address of the server you're connecting to:
- IPAddress server(192, 168, 0, 110);
- // Initialize the Ethernet client library
- EthernetClient client;
- 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)";
- 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)";
- 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};
- int velikost;
- int hodnoty[410];
- int skupina1[20];
- int skupina2[20];
- int testovaci;
- int locoName;
- int rychlostMax;
- byte actualHourHEX;
- byte actualMinuteHEX;
- byte actualsecondHEX;
- int actualyearHEX;
- byte actualMonthHEX;
- byte actualdayHEX;
- byte DECtoHEX[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
- 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
- 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x60};
- void setup() {
- // You can use Ethernet.init(pin) to configure the CS pin
- Ethernet.init(10); // Most Arduino shields
- //Ethernet.init(5); // MKR ETH shield
- //Ethernet.init(0); // Teensy 2.0
- //Ethernet.init(20); // Teensy++ 2.0
- //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
- //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
- // start the Ethernet connection:
- Ethernet.begin(mac, ip);
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- Serial2.begin(9600);
- //while (!Serial) {
- // ; // wait for serial port to connect. Needed for native USB port only
- //}
- // Check for Ethernet hardware present
- if (Ethernet.hardwareStatus() == EthernetNoHardware) {
- Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
- while (true) {
- delay(1); // do nothing, no point running without Ethernet hardware
- }
- }
- while (Ethernet.linkStatus() == LinkOFF) {
- Serial.println("Ethernet cable is not connected.");
- delay(1000);
- }
- // give the Ethernet shield a second to initialize:
- delay(1000);
- Serial.println("connecting...");
- // if you get a connection, report back via serial:
- if (client.connect(server, 8888)) {
- Serial.println("connected");
- } else {
- // if you didn't get a connection to the server:
- Serial.println("connection failed");
- }
- client.setTimeout(100);
- delay1.start(100);
- delay2.start(50);
- velikost = sizeof(potrebneKontrolky) / sizeof(int);
- client.print(group1);
- String value1 = client.readStringUntil('\r\n');
- client.print(group2);
- String value2 = client.readStringUntil('\r\n');
- delay(1000);
- }
- /*
- int GetLocoName() {
- String cisloLok = "";
- if(client.available()){
- client.flush();
- }
- client.print("GetLocoName");/*
- String lokomotiva = client.readStringUntil('\r\n');
- char buf[12];
- lokomotiva.toCharArray(buf, 12);
- for(int i=0; i<12; i++) {
- if(isDigit(buf[i]) == true) {
- cisloLok += buf[i];
- }
- }
- return cisloLok.toInt();*/
- void GetAll(int *seznam) {
- for(int i=0; i<velikost; i++) {
- String vypis = "GetCV(" + String(seznam[i]) + ",0)";
- client.print(vypis);
- }
- }
- void Prijem() {
- while(client.available() > 0) {
- const char s[2] = "=";
- char str[200];
- char str2[80];
- char *a;
- char *b;
- String stringi = client.readStringUntil('\r\n');
- stringi.toCharArray(str, 200);
- a = strtok(str, s);
- int id = String(a).toInt();
- if(id < 500) {
- b = strtok(NULL, s);
- int valueFloat = 0 ;
- valueFloat = (String(b)).toInt();
- testovaci = valueFloat;
- hodnoty[(int)id] = testovaci;
- //Serial.println(String(id) + " = " + String((int)hodnoty[id]) + " = " + String(valueFloat));
- }
- else if(id == 501) {
- b = strtok(NULL, s);
- String(b).toCharArray(str2, 80);
- int j=0;
- for(int i=0; i<80; i++) {
- if(isDigit(str2[i]) == true) {
- if(str2[i] == '1') {
- skupina1[j] = 1;
- }
- else {
- skupina1[j] = 0;
- }
- j++;
- }
- }
- }
- else if(id == 502) {
- b = strtok(NULL, s);
- String(b).toCharArray(str2, 80);
- int j=0;
- for(int i=0; i<80; i++) {
- if(isDigit(str2[i]) == true) {
- if(str2[i] == '1') {
- skupina2[j] = 1;
- }
- else {
- skupina2[j] = 0;
- }
- j++;
- }
- }
- }
- else if(id == 600) {
- b = strtok(NULL, s);
- String cisloLoko = "";
- for(int i=0; i<12; i++) {
- if(isDigit(b[i]) == true) {
- cisloLoko += b[i];
- }
- }
- locoName = cisloLoko.toInt();
- }
- }
- }
- /*
- int GetCVint(int ovladac, int stav, int minStav) {
- const char s[2] = "=";
- char str[20];
- char *a;
- char *b;
- String vypis = "GetCV(" + String(ovladac) + "," + String(stav) + ")";
- if(client.available() > 0){
- client.flush();
- }
- client.print(vypis);
- String value = client.readStringUntil('\r\n');
- if(value != "") {
- value.toCharArray(str, 20);
- a = strtok(str, s);
- int id = atoi(a);
- b = strtok(NULL, s);
- int valueInt = atoi(b);
- if(ovladac = id) {
- return valueInt;
- }
- else {
- Serial.print("Chyba příjem! ");
- Serial.print(id);
- Serial.print(" ");
- Serial.println(minStav);
- delay(15);
- client.flush();
- return minStav;
- }
- }
- else {
- Serial.print("Chyba! ");
- Serial.print(ovladac);
- Serial.print(" ");
- Serial.print(minStav);
- Serial.print(" ");
- Serial.println(value);
- delay(15);
- client.flush();
- return minStav;
- }
- }
- */
- void ConnectClient() {
- // if the server's disconnected, restart the client:
- if (!client.connected()) {
- Serial.println();
- Serial.println("disconnecting.");
- client.stop();
- delay(1000);
- // do nothing:
- while (true) {
- if (client.connect(server, 8888)) {
- Serial.println("connected");
- break;
- }
- }
- }
- }
- int * calcCheckSum (int* pack, int packSize)
- {
- int sum = 0;
- for (int i=0; i < (packSize-1); i++) sum += pack[i];
- sum = sum & 0xFF;
- pack[packSize-1] = 0x100 - sum;
- return pack;
- }
- unsigned int pw [] = { 1, 10, 100, 1000, 10000 } ;
- int ExtractDigit(int V, int P)
- {
- return V / pw[P] % 10 ;
- }
- void loop() {
- if(delay2.elapsed()) {
- GetAll(&potrebneKontrolky[0]);
- client.print("GetGroupValues(1)");
- client.print("GetGroupValues(2)");
- client.print("GetLocoName");
- delay2.start(15);
- }
- Prijem();
- if(delay1.elapsed()) {
- int hodnota = ExtractDigit(locoName, 3);
- if(hodnota == 2) {
- rychlostMax = 140;
- }
- else if(hodnota == 3) {
- rychlostMax = 120;
- }
- else {
- rychlostMax = 160;
- }
- actualHourHEX = DECtoHEX[hodnoty[406]];
- actualMinuteHEX = DECtoHEX[hodnoty[407]];
- actualsecondHEX = DECtoHEX[hodnoty[408]];
- actualMonthHEX = DECtoHEX[2];
- actualdayHEX = DECtoHEX[3];
- actualyearHEX = DECtoHEX[19];
- byte rychlostHEX = hodnoty[9];
- byte ukazatelARRHEX = hodnoty[209];
- byte rychlostMaxHEX = rychlostMax;
- digitalWrite(3, 1);
- byte msg[27];
- msg[0] = 0x19; //počet bajtů ve zpráve
- msg[1] = 0x01; //kód povelu 01
- msg[2] = 0x00; // Stavové slovo zprávy
- msg[3] = actualsecondHEX; //Vteřiny
- msg[4] = actualMinuteHEX; //minuty
- msg[5] = actualHourHEX; // Hodiny
- msg[6] = actualdayHEX; //den
- msg[7] = actualMonthHEX; //Měsíc
- msg[8] = actualyearHEX; //Rok
- msg[9] = 0x12; //dráha //0x88
- msg[10] = 0x65; //0x03
- msg[11] = 0x49; //0x03
- msg[12] = 0x99; //0x02
- msg[13] = rychlostHEX; //Rychlost akt
- msg[14] = ukazatelARRHEX; //Rychlost nav
- msg[15] = rychlostMaxHEX; //Max V
- msg[16] = 0x00; //Tlak
- msg[17] = 0x00; //další analog
- msg[18] = 0x00; //1. čís
- msg[19] = 0x00; //2. čís
- msg[20] = 0x00; //3. čís
- msg[21] = 0x00; //4. čís
- msg[22] = 0x00; //5. čís
- msg[23] = 0x00; //Chyb bajt
- msg[24] = 0x00; //traťová značka
- msg[25] = 0x00; //Traťová značka
- uint8_t CRC = 0;
- for(int8_t i = 0; i< 25; i++){
- CRC -= msg[i];
- }
- msg[26] = CRC;
- Serial2.write(msg,27);
- digitalWrite(3, 0);
- delay1.start(90);
- }
- while(Serial2.available()) {
- Serial.print("MamData");
- }
- ConnectClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment