Advertisement
pippero

Untitled

Sep 30th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. SoftwareSerial mySerial(10, 11); // RX, TX
  6. LiquidCrystal_I2C lcd(0x27, 16, 2);
  7. unsigned long t1, dt;
  8. int stato = 0;
  9.  
  10.  
  11.  
  12.  
  13. struct mydata {
  14. char cmd;
  15. int count;
  16. float temp;
  17. char str[20];
  18. int st_size ;
  19. };
  20.  
  21. mydata dato;
  22.  
  23. void setup() {
  24.  
  25. pinMode(2, OUTPUT);
  26. lcd.init();
  27. lcd.backlight();
  28. mySerial.begin(9600);
  29. Serial.begin(9600);
  30. Serial.print(" - ");
  31. lcd.setCursor(0, 0);
  32. lcd.print("Hello, world!");
  33. }
  34.  
  35. byte buff[30];
  36. int i = 0;
  37.  
  38. void loop() {
  39. switch (stato) {
  40. case 0:
  41. recv();
  42. break;
  43. case 1:
  44. ack();
  45. break;
  46. }
  47. }
  48.  
  49. void recv() {
  50. dt = millis() - t1;// messo per evitare che sia
  51. if (dt > 300) { //fermo in ascolto
  52. //timeout!
  53. Serial.println("timeout");
  54. stato = 1;
  55. }
  56. if (mySerial.available()) {
  57. byte n = mySerial.read();
  58. // if (n == 'a') {
  59. if ((char)n == '\n') {
  60. //mySerial.write((const uint8_t *)&dato, sizeof(struct mydata));
  61. Serial.println("rx");
  62. digitalWrite(2, HIGH);
  63. delay(500);
  64. digitalWrite(2, LOW);
  65. stato = 1;
  66. memcpy(&dato, buff, i);
  67.  
  68. Serial.print(dato.cmd); Serial.print(" - ");
  69. Serial.print(dato.count); Serial.print(" - ");
  70. Serial.print(dato.temp); Serial.print("\n");
  71. Serial.print(dato.str); Serial.print("\n");
  72. Serial.print(dato.st_size); Serial.print("\n");
  73. Serial.print("ricevuti: ");
  74. Serial.print(i);
  75. Serial.println(" bytes - END");
  76. mySerial.flush();
  77. if (dato.st_size == i) {
  78. Serial.println("dati ok ");
  79. lcd.setCursor(1, 1);
  80. lcd.print(dato.temp);
  81. dato.st_size = 0;
  82. }
  83. else {
  84. Serial.println("ERRORE");
  85. digitalWrite(3, HIGH);
  86. delay(500);
  87. digitalWrite(3, LOW);
  88. lcd.setCursor(1, 1);
  89. lcd.print("ERRORE");
  90. }
  91. i = 0;
  92. }
  93. else {
  94. buff[i] = n;
  95. i++;
  96. }
  97. }
  98. }
  99. //}
  100. void ack() {
  101. delay(300);
  102. mySerial.write('k');
  103. digitalWrite(2, LOW);
  104. Serial.println("ack");
  105. mySerial.flush();
  106. stato = 0;
  107. t1 = millis();
  108. }
  109.  
  110.  
  111. //while (mySerial.available()) {
  112. // byte n = mySerial.read();
  113. //if ((char)n == '\n') {
  114. // lcd.setCursor(1, 1);
  115. //lcd.print(dato.temp);
  116. //mySerial.write((const uint8_t *)&dato, sizeof(struct mydata));
  117.  
  118. //memcpy(&dato, buff, i);
  119.  
  120. // Serial.print(dato.cmd); Serial.print(" - ");
  121. // Serial.print(dato.count); Serial.print(" - ");
  122. //Serial.print(dato.temp); Serial.print("\n");
  123. //Serial.print(dato.str); Serial.print("\n");
  124.  
  125. //Serial.print("ricevuti: ");
  126. // Serial.print(i);
  127. //Serial.println(" bytes - END");
  128. //i = 0;
  129. //} else {
  130. //buff[i] = n;
  131. //i++;
  132. //}
  133. //}
  134. //mySerial.write((const uint8_t *)&dato, sizeof(struct mydata));
  135. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement