Conversione dati per trasmissione verso Emoncms

Salve a tutti,

ho un piccolo problemino quando mando i dati che recupero in Modbus da un contatore.

unsigned int readRegs1[30];
unsigned int readRegs2[34];
unsigned int readRegs3[59];
unsigned int readRegs4[59];
unsigned int readRegs5[59];
unsigned int readRegs6[59];
unsigned int readRegs7[59];
unsigned int readRegs8[59];
unsigned int readRegs9[44];
SimpleTimer timer;

float Voltage =0;
float Current =0;
float PowerFactor =0;
float ActivePower =0;
float Frequency =0;
float ActiveEnergy =0;

void setup()
{
 pinMode(SS_PIN, OUTPUT);
 digitalWrite(SS_PIN, HIGH);
 pinMode(10, OUTPUT);  // Set the CS pin as an output
 digitalWrite(10, 1); // Turn off the W5100 chip! (wait for configuration)
 pinMode(4, OUTPUT);       // Set the SDcard CS pin as an output
 digitalWrite(4, 1);   // Turn off the SD card! (wait for configuration)  
 Wire.begin(); 
 Serial.begin (9600);
  
 timer.setInterval(30000, RepeatTask);
   
 modbus_construct(packet1, 1, READ_HOLDING_REGISTERS, 0, 30, readRegs1);
 modbus_construct(packet2, 1, READ_HOLDING_REGISTERS, 31, 34, readRegs2);
 modbus_construct(packet3, 1, READ_HOLDING_REGISTERS, 256, 59, readRegs3);
 modbus_construct(packet4, 1, READ_HOLDING_REGISTERS, 316, 59, readRegs4);
 modbus_construct(packet5, 1, READ_HOLDING_REGISTERS, 512, 59, readRegs5);
 modbus_construct(packet6, 1, READ_HOLDING_REGISTERS, 572, 59, readRegs6);
 modbus_construct(packet7, 1, READ_HOLDING_REGISTERS, 768, 59, readRegs7);
 modbus_construct(packet8, 1, READ_HOLDING_REGISTERS, 828, 59, readRegs8);
 modbus_construct(packet9, 1, READ_HOLDING_REGISTERS, 1024, 44, readRegs9);

 modbus_configure(&Serial1, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);

Per metterli in assieme ho fatto queste funzioni:

unsigned long Compact3Word(int FirstWord, unsigned int Array[])
  { int a = FirstWord, b = FirstWord + 1, c = FirstWord + 2;
    unsigned long dataA =0, dataB =0; 
    dataA = Array[b] + (Array[a] * 0x10000);
    dataB = Array[c] + (dataA * 0x10000); 
    return dataB;
}

unsigned long Compact2Word(int FirstWord, unsigned int Array[])
  { int a = FirstWord, b = FirstWord + 1;
    unsigned long dataC =0; 
    dataC = Array[b] + (Array[a] * 0x10000);
    return dataC;   
}

Dopo diché prima di spedirle al Server :

//Create Data to Emoncms.org
    
      Voltage = (Compact2Word(12,readRegs1))/1000;
      Current = (Compact2Word(22,readRegs1))/1000;
      PowerFactor = readRegs1[27]/1000;
      ActivePower = Compact3Word(6,readRegs2);
      Frequency = readRegs2[33]/100;
      ActiveEnergy = (Compact3Word(9,readRegs3))/1000;
      
      
    //send values
    sendData();

    Serial.println(Voltage);
    Serial.println(Current);
    Serial.println(PowerFactor);
    Serial.println(ActivePower);
    Serial.println(Frequency);
    Serial.println(ActiveEnergy);

La trasmissione funziona ma sul Serial Monitor e sul Server ho questi risultato, la prima trama sono tutti i dati ricomposti e stampati senza messa in forma (ho colorato quelli che ritocco):

231014;2595;955;572791;599527;16260;49980;522940;0;303276;0;196608;0;37111;0;0;0;522942;0;303278;0;196608;0;37111;0;0;0;0;0;0;0;0;0;0;0;0;0;522910;0;303274;0;0;0;0;0;0;0;522939;303274;259802;37111;4294901760;16/12/14;16:55:0

Connecting... 231.00 2.00 0.00 572791.00 499.00 522.00

Come mai ho solo degli 00 dopo il punto? :astonished:

Perché devi forzare la divisione intera in float. Dividi per 1000.0

Grazie PaoloP,
il mio C é molto ma molto arrugginito :frowning:

Adesso su Serial Monitor ho questo risultato:

227513;3203;64591;688407;728770;4294877304;50000;539598;0;309419;0;262144;0;37275;0;0;0;539600;0;309419;0;262144;0;37275;0;0;0;0;0;0;0;0;0;0;0;0;0;539565;0;309419;0;0;0;0;0;0;0;539594;309419;271095;37275;4294901760;16/12/14;19:15:18
Connecting…
227.51
3.20
64.59
688.41
50.00
53.96
HTTP/1.1 200 OK
Date: Tue, 16 Dec 2014 18:15:05 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.15
Content-Length: 2
Connection: close
Content-Type: application/json
ok
Disconnecting…

Quindi adesso dalla parte mia tutto ok, resta il problema di Emoncms… non tutti i valori sono con la virgola… :sweat_smile:

Non conosco Emoncms. Dovresti chiedere a Pitusso, è più esperto in quel campo. 8)