Problema codice dati dei sensori

Ciao a tutti. Vorrei chiedervi un aiuto per la compilazione di questo codice. Vorrei poter trasmettere sul server di arduino i dati raccolti dal

  • sensore di temperatura ,
  • distanza percepita dal sensore ad ultrasuoni hc sr04 e
  • l'angolo su cui è rivolto il servomotore
    in formato JSON. Con questi dati(esclusa temperatura), vorrei realizzare un sonar accessibile da remoto modificando uno dei tanti progetti che si trovano su internet.

Intanto però vorrei almeno riuscire a trasmettere i dati correttamente, cosa che purtroppo non avviene. Il codice che ho messo insieme attualmente è questo:

#include <SPI.h>                                    //Include these libraries
#include <Ethernet.h>                               
#include <Servo.h>


byte mac[] = {0x00, 0x08, 0xDC, 0x1C, 0xB8, 0x4C};  
IPAddress ip(192,168,0,100);                         
EthernetServer server(80);                        

//Definizione dei PIN Temperatura
const int sensorPin = A0;
const float baselineTemp = 20.0;

// Definizione PIN Sonar
static int trigger = 5;
static int echo = 6;

//variabili utilizzate per calcolare la distanza
long durata; //tempo che impieghera' il suono a percorrere una certa distanza
long distanza;  //la distanza che ha percorso il suono

Servo myServo;
int i = 0;

//*************************************************************************************************
// setup function
//=================================================================================================
void setup() {
  Serial.begin(9600);                               //Begin Serial communication (baud rate = 9600).


//settiamo il funzionamento dei pin
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);

//metto a LOW l'ingresso del PIN echo e del PIN trigger
  digitalWrite(echo, LOW);
  digitalWrite(trigger, LOW); 
 
  myServo.attach(4);

//inizializzo le variabili
  durata = 0;
  distanza = 0;  
  
 
  
  
  Ethernet.begin(mac, ip);                          //Initialise the ethernet library and network settings: https://www.arduino.cc/en/Reference/EthernetBegin
  server.begin();                                   //Tell the server to begin listening for incoming connections (on port 8081 in this example)
  Serial.print("Server IP address : ");
  Serial.println(Ethernet.localIP());               //If you see the IP address within the Serial monitor - you know that the server started successfully
}


//*************************************************************************************************
// loop function
//=================================================================================================
void loop() {
  // rotates the servo motor from 15 to 165 degrees
  for(int i=15;i<=165;i++){  
  myServo.write(i);
  delay(30);
  
  }
  
  for(int i=165;i>15;i--){  
  myServo.write(i);
  delay(30);
  
  }

  
  int sensorVal = analogRead(sensorPin);
  float voltage = (sensorVal/1024.0) * 5.0;
  float temperature = (voltage -.5)*100;


  //Invio un impulso HIGH sul pin del trigger
  digitalWrite(trigger, HIGH);
  //lo lascio al valore HIGH per 10 microsecondi
  delayMicroseconds(10);
  //lo riporto allo stato LOW
  digitalWrite(trigger, LOW);
  //ottengo il numero di microsecondi per i quali il PIN echo e' rimasto allo stato HIGH
  //per fare questo utilizzo la funzione pulseIn()
  durata = pulseIn(echo, HIGH);
 
  // La velocita' del suono e' di 340 metri al secondo, o 29 microsecondi al centimetro.
  // il nostro impulso viaggia in andata e ritorno, quindi per calcoalre la distanza
  // tra il sensore e il nostro ostacolo occorre fare:
  distanza = durata / 29 / 2; 
  
  
  EthernetClient client = server.available();      // assign any newly connected Web Browsers to the "client" variable.
  
  if(client.connected()){
    Serial.println("Client Connesso");
    
    while(client.available()){
      //Serial.write(client.read());               // Uncomment if you want to write the request from the Browser (CLIENT) to the SERIAL MONITOR (and comment out the next line)
      client.read();                               // This line will clear the communication buffer between the client and the server.
    }
    
    //Send the Server response header back to the browser.
    client.println("HTTP/1.1 200 OK");           // This tells the browser that the request to provide data was accepted
    client.println("Access-Control-Allow-Origin: *");  //Tells the browser it has accepted its request for data from a different domain (origin).
    client.println("Content-Type: application/json;charset=utf-8");  //Lets the browser know that the data will be in a JSON format
    client.println("Server: Arduino");           // The data is coming from an Arduino Web Server (this line can be omitted)
    client.println("Connection: close");         // Will close the connection at the end of data transmission.
    client.println();                            // You need to include this blank line - it tells the browser that it has reached the end of the Server reponse header.
    
    //Transmit the Analog Readings to the Web Browser in JSON format
    //Example Transmission: [{"key":0, "value":300},{"key":1, "value":320},{"key":2, "value":143},{"key":3, "value":24},{"key":4, "value":760},{"key":5, "value":470}]
    client.print("[");                           // This is tha starting bracket of the JSON data
    //for(int i=0; i<6; i++){                      // Transmit analog readings from Analog Pin 0 to Analog Pin 5
      client.print("{\"key\": ");
      client.print(0);                           // The key for Analog pin 0 (A0) is equal to 0   eg.  "key":0
      client.print(", \"value\": ");
      client.print(temperature);               // The value is equal to the Analog reading from that pin.  eg. "value":300
      
      client.print("}, ");                      // All other values will have a comma after the bracket.
      client.print("{\"key\": ");
      client.print(1);
      client.print(", \"value\": ");
      client.print(distanza);  
 
      client.print("}, ");                      // All other values will have a comma after the bracket.
      client.print("{\"key\": ");
      client.print(2);
      client.print(", \"value\": ");
      client.print(i); 
 
      client.print("}"); 
    
    
    
    client.println("]");                         // This is the final bracket of the JSON data
    client.stop();                               // This method terminates the connection to the client
    Serial.println("Client has closed");         // Print the message to the Serial monitor to indicate that the client connection has closed.
  }
}

Per verificare il funzionamento fino ad oraho utilizzato questo sito
http://arduinobasics.blogspot.it/p/arduinobasics.html

finchè si trattava di trasmettere la temperatura funzionava bene. Con l'aggiunta degli altri sensori però vedo che si blocca. Qualsiasi aiuto o consiglio è ben accetto, grazie in anticipo a tutti :slight_smile:

up

Cosa si blocca? Sito, codice?
Potrebbe essere anche il problema quel ciclo for con delay