Ciao Cyberths,
lo sketch ha come base quello fornito da Cosm, nel loop ho inserito il mio Sketch (o meglio diversi sketch presi dalla rete, una brodaglia insomma).
Se non creo troppo stress posto qui sotto il codice....
Secondo me Cosm dovrebbe farmi vedere una segnale lineare e non con sbalzi da 0 a x, lo sketch si collega ogni 10 secondi a Cosm, mentre lo sketch stesso misura 5000 volte prima di passare il tutto a Cosm (almeno credo).
Ho tagliato la prima parte dello sketch altrimenti nn potevo postarlo tutto
Saluti Remo
------------------------------Snip--------------------------------------------
void setup() {
// start serial port:
Serial.begin(115200);
analogReference (EXTERNAL);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// DHCP failed, so use a fixed IP address:
Ethernet.begin(mac, ip);
}
}
void loop() {
analogReference (EXTERNAL);
// massimo delle sinus positive
analogReference (EXTERNAL);
ValA0 = analogRead(A0) ;
ValA1 = analogRead(A1) ;
ValA2 = analogRead(A2) ;
Vpartitore = analogRead(A3);
// record the maximum sensor value
if (ValA0 > sensorMax1) {
sensorMax1 = ValA0;
}
// record the minimum sensor value
if (ValA0 < sensorMin1) {
sensorMin1 = ValA0;
}
// record the maximum sensor value
if (ValA1 > sensorMax2) {
sensorMax2 = ValA1;
}
// record the minimum sensor value
if (ValA1 < sensorMin2) {
sensorMin2 = ValA1;
}
// record the maximum sensor value
if (ValA2 > sensorMax3) {
sensorMax3 = ValA2;
}
// record the minimum sensor value
if (ValA2 < sensorMin3) {
sensorMin3 = ValA2;
}
// Serial.println(ValA0);
// if (ValA0 < 510.72 ){ValA0 = 512 + (510.72 - ValA0); }
// if (ValA1 < 510.72 ){ValA1 = 512 + (510.72 - ValA1); }
// if (ValA2 < 510.72 ){ValA2 = 512 + (510.72 - ValA2); }
L1 = L1 + sensorMax1 ;
L2 = L2 + sensorMax2 ;
L3 = L3 + sensorMax3 ;
Cicli++ ;
// VIENE FATTA LA MEDIA SU 5000 Letture
if (Cicli == 5000){
L1 = (L1 / 5000.000)-510.000 ;
L2 = (L2 / 5000.000)-510.000 ;
L3 = (L3 / 5000.000)-510.000 ;
Serial.println(L1,4);
Serial.println(L2,4);
Serial.println(L3,4);
Vpartitore = analogRead(A3)*0.0032265625;
Voltaggio1 = (((L1 * 0.0032265625)-0));
Voltaggio2 = (((L2 * 0.0032265625)-0));
Voltaggio3 = (((L3 * 0.0032265625)-0));
Corrente1 = sqrt(((Voltaggio1*30)/3.3)0.9);
Corrente2 = sqrt(((Voltaggio230)/3.3)0.9);
Corrente3 = sqrt(((Voltaggio330)/3.3)*0.9);
Potenza1 = Corrente12400.9;
Potenza2 = Corrente22400.9;
Potenza3 = Corrente32400.9;
Ptot = (Potenza1+Potenza2+Potenza3);
Vpartitore = analogRead(A3)*0.0032265625;
Serial.println();
Serial.print( " Voltaggio 1 --> ");
Serial.print(Voltaggio1,4);
Serial.print( " Ampere --> ");
Serial.print(Corrente1,4);
Serial.print( " Potenza --> ");
Serial.println(Potenza1,4);
Serial.print( " Voltaggio 2 --> ");
Serial.print(Voltaggio2,4);
Serial.print( " Ampere --> ");
Serial.print(Corrente2,4);
Serial.print( " Potenza --> ");
Serial.print(Potenza2,4);
Serial.print( " Potenta Totale --> ");
Serial.println(Ptot,4);
Serial.print( " Voltaggio 3 --> ");
Serial.print(Voltaggio3,4);
Serial.print( " Ampere --> ");
Serial.print(Corrente3,4);
Serial.print( " Potenza --> ");
Serial.println(Potenza3,4);
Serial.println();
Serial.print( " Tensione sul Partitore --> ");
Serial.println(Vpartitore,4);
L1 = 0 ;
L2 = 0 ;
L3 = 0 ;
sensorMax1 = 0;
sensorMax2 = 0;
sensorMax3 = 0;
sensorMin1 = 0;
sensorMin2 = 0;
sensorMin3 = 0;
Cicli = 0 ;}
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(Ptot,Potenza1,Potenza2,Potenza3);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData(float thisData,float thisData1,float thisData2,float thisData3) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.cosm.com");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = 8 + getLength(thisData);
client.println(thisLength);
// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
client.print("A_MaxPower,");
client.println(thisData);
client.print("B_Power_L1,");
client.println(thisData1);
client.print("C_Power_L2,");
client.println(thisData2);
client.print("D_Power_L3,");
client.println(thisData3);
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
--------------snip-----------------------------