Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- SoftwareSerial mySerial(10, 11); // RX, TX
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- unsigned long t1, dt;
- int stato = 0;
- struct mydata {
- char cmd;
- int count;
- float temp;
- char str[20];
- int st_size ;
- };
- mydata dato;
- void setup() {
- pinMode(2, OUTPUT);
- lcd.init();
- lcd.backlight();
- mySerial.begin(9600);
- Serial.begin(9600);
- Serial.print(" - ");
- lcd.setCursor(0, 0);
- lcd.print("Hello, world!");
- }
- byte buff[30];
- int i = 0;
- void loop() {
- switch (stato) {
- case 0:
- recv();
- break;
- case 1:
- ack();
- break;
- }
- }
- void recv() {
- dt = millis() - t1;// messo per evitare che sia
- if (dt > 300) { //fermo in ascolto
- //timeout!
- Serial.println("timeout");
- stato = 1;
- }
- if (mySerial.available()) {
- byte n = mySerial.read();
- // if (n == 'a') {
- if ((char)n == '\n') {
- //mySerial.write((const uint8_t *)&dato, sizeof(struct mydata));
- Serial.println("rx");
- digitalWrite(2, HIGH);
- delay(500);
- digitalWrite(2, LOW);
- stato = 1;
- memcpy(&dato, buff, i);
- Serial.print(dato.cmd); Serial.print(" - ");
- Serial.print(dato.count); Serial.print(" - ");
- Serial.print(dato.temp); Serial.print("\n");
- Serial.print(dato.str); Serial.print("\n");
- Serial.print(dato.st_size); Serial.print("\n");
- Serial.print("ricevuti: ");
- Serial.print(i);
- Serial.println(" bytes - END");
- mySerial.flush();
- if (dato.st_size == i) {
- Serial.println("dati ok ");
- lcd.setCursor(1, 1);
- lcd.print(dato.temp);
- dato.st_size = 0;
- }
- else {
- Serial.println("ERRORE");
- digitalWrite(3, HIGH);
- delay(500);
- digitalWrite(3, LOW);
- lcd.setCursor(1, 1);
- lcd.print("ERRORE");
- }
- i = 0;
- }
- else {
- buff[i] = n;
- i++;
- }
- }
- }
- //}
- void ack() {
- delay(300);
- mySerial.write('k');
- digitalWrite(2, LOW);
- Serial.println("ack");
- mySerial.flush();
- stato = 0;
- t1 = millis();
- }
- //while (mySerial.available()) {
- // byte n = mySerial.read();
- //if ((char)n == '\n') {
- // lcd.setCursor(1, 1);
- //lcd.print(dato.temp);
- //mySerial.write((const uint8_t *)&dato, sizeof(struct mydata));
- //memcpy(&dato, buff, i);
- // Serial.print(dato.cmd); Serial.print(" - ");
- // Serial.print(dato.count); Serial.print(" - ");
- //Serial.print(dato.temp); Serial.print("\n");
- //Serial.print(dato.str); Serial.print("\n");
- //Serial.print("ricevuti: ");
- // Serial.print(i);
- //Serial.println(" bytes - END");
- //i = 0;
- //} else {
- //buff[i] = n;
- //i++;
- //}
- //}
- //mySerial.write((const uint8_t *)&dato, sizeof(struct mydata));
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement