elgunhasan

Untitled

Jul 31st, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. /**
  2. *
  3. * Written by: Miradil Zeynalli
  4. * Company: Sumaks Technologies
  5. * Project: P10 displays for Tax Ministry
  6. * Date: 29.06.2019
  7. * IDE Version: 1.8.9
  8. *
  9. * Used external libraries:
  10. * UIPEthernet - https://github.com/UIPEthernet/UIPEthernet
  11. *
  12. * Purpose:
  13. * This code reads data from ethernet entered by user.
  14. * Then this data is parsed and collected otherwise, into
  15. * two other packets of data, which are sent by two
  16. * SoftwareSerials (for each part of display)
  17. *
  18. */
  19.  
  20. #include <SPI.h>
  21. #include <SoftwareSerial.h>
  22. #include <UIPEthernet.h>
  23. #include <avr/wdt.h>
  24.  
  25. #define RX_TOP 3
  26. #define TX_TOP 5
  27. #define RX_BOTTOM 6
  28. #define TX_BOTTOM 9
  29.  
  30. const uint32_t TIMEOUT = 70;
  31.  
  32. /*
  33. #ifdef MACON3_FULDPX
  34.  
  35. #undef MACON3_FULDPX
  36. #define MACON3_FULDPX 1
  37.  
  38. #endif
  39.  
  40. #ifdef PHCON1_PDPXMD
  41.  
  42. #undef PHCON1_PDPXMD
  43. #define PHCON1_PDPXMD 1
  44.  
  45. #endif
  46. */
  47.  
  48. byte mac[] = {
  49. 0xDE, 0xAD, 0xAA, 0x18, 0x3B, 0x1A
  50. };
  51.  
  52. //IPAddress ip(192, 168, 1, 18);
  53. //IPAddress myDns(8, 8, 8, 8);
  54. //IPAddress gateway(192, 168, 1, 1);
  55. //IPAddress subnet(255, 255, 255, 0);
  56.  
  57. IPAddress ip(172, 20, 57, 250);
  58. IPAddress myDns(192, 168, 250, 248);
  59. IPAddress gateway(172, 20, 57, 1);
  60. IPAddress subnet(255, 255, 255, 0);
  61.  
  62. EthernetServer server(23); // port 23
  63. boolean alreadyConnected = false; // whether or not the client was connected previously
  64.  
  65. SoftwareSerial top(RX_TOP, TX_TOP); // for top 2 rows of display
  66. SoftwareSerial bottom(RX_BOTTOM, TX_BOTTOM); // for bottom 2 rows of display
  67.  
  68. char *up1, *up2, *down1, *down2, *line1, *line2;
  69.  
  70. void setup() {
  71.  
  72. watchdogSetup();
  73. // initialize the ethernet device
  74.  
  75. Ethernet.begin(mac, ip, myDns, gateway, subnet);
  76. wdt_reset();
  77. // start listening for clients
  78. server.begin();
  79. wdt_reset();
  80.  
  81. Serial.begin(9600);
  82. top.begin(9600); // for top side of display
  83. bottom.begin(9600); // for bottom side of display
  84.  
  85. Serial.print("Local IP address: ");
  86. Serial.println(Ethernet.localIP());
  87. }
  88.  
  89. void loop() {
  90. // wait for a new client:
  91. wdt_reset();
  92. parse_data();
  93. }
  94.  
  95. void watchdogSetup(void)
  96. {
  97. wdt_reset();
  98. cli();
  99. WDTCSR |= (1 << WDCE) | (1 << WDE);
  100. WDTCSR = (1 << WDIE) | (1 << WDE) | (1 << WDP2) | (1 << WDP1); // 1 sec wdt_timer
  101. sei();
  102. }
  103.  
  104. String read_data()
  105. {
  106. wdt_reset();
  107. EthernetClient client = server.available();
  108. String cmd = "";
  109. wdt_reset();
  110.  
  111. if (client)
  112. {
  113. cmd = "";
  114. while (client.available() > 0)
  115. {
  116. cmd += (char) client.read();
  117. wdt_reset();
  118. }
  119.  
  120. client.println("DATA Recieved!");
  121.  
  122. return cmd.substring(0, cmd.length() - 2); // skip last '\r\n' character
  123. }
  124.  
  125. return "";
  126. }
  127.  
  128. void parse_data()
  129. {
  130. uint64_t prev = millis();
  131. String d;
  132.  
  133. do
  134. {
  135. d = read_data();
  136. wdt_reset();
  137.  
  138. } while (((millis() - prev) < (TIMEOUT * 1000)) && (d.length() < 2));
  139.  
  140. Serial.print("received data: ");
  141. Serial.println(d);
  142.  
  143. if(d.length() < 2) // no valid data got in TIMEOUT period
  144. while(1); // force wdt
  145.  
  146.  
  147. wdt_reset();
  148. // tokenize string
  149. up1 = strtok((char *) d.c_str(), ",");
  150. up2 = strtok(NULL, ",");
  151. line1 = strtok(NULL, ",");
  152. down1 = strtok(NULL, ",");
  153. down2 = strtok(NULL, ",");
  154. line2 = strtok(NULL, ",");
  155.  
  156. wdt_reset();
  157. // send data to respective parts of display
  158. top.println(String(up1) + "," + String(up2) + "," + String(line1));
  159. bottom.println(String(down1) + "," + String(down2) + "," + String(line2));
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment