![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
hg
Dependencies: LoRaTransmitter mbed BME280 SX1272Lib
Diff: main.cpp
- Revision:
- 1:30ce00fbab55
- Parent:
- 0:cc2b234b6255
- Child:
- 2:2bcb1840d680
- Child:
- 3:4e51739d3a97
--- a/main.cpp Wed May 11 14:56:17 2022 +0000 +++ b/main.cpp Fri May 13 15:45:57 2022 +0000 @@ -61,7 +61,6 @@ AnalogIn voltage(PC_5); AnalogIn current(A1); Ticker measure; -Ticker lcd_print; Ticker send; Grove_LCD_RGB_Backlight lcd(I2C_SDA, I2C_SCL); @@ -107,42 +106,56 @@ int8_t SnrValue = 0.0; //float measured_sinus = 0.0; int16_t j = 0; -float voltage_sinus[200], current_sinus[200]; -volatile float Vsum = 0,Vmax =0, Vmean = 0, Imax =0, Isum = 0, Imean = 0, P = 0, Ud = 0, U = 0, Id = 0, I = 0; +float voltage_sinus, current_sinus; +volatile float Vsum = 0,Vmax =0, Vmean = 0, Imax =0, Isum = 0, Imean = 0, Ud = 0, Id = 0, cosi = 0; +volatile double P = 0, P2 = 0, S2 = 0, P1=0, S1=0; + uint16_t strSize = 0; char str[BUFFER_SIZE]; -char monitor[20]="0", monitor1[20]="0"; +char monitor[16]="0", monitor1[16]="0"; + + void meas (void) { - - - if (j>=200) + j++; + voltage_sinus = voltage*2200; + current_sinus = current*5; + Vsum+=voltage_sinus; + Isum+=current_sinus; + if (voltage_sinus>Vmax) + Vmax=voltage_sinus; + if (current_sinus>Imax) + Imax=current_sinus; + + P=P+(((voltage_sinus-Vmean)*(current_sinus-Imean))*(0.0002/3600000)); + P2=P2+((voltage_sinus-Vmean)*(current_sinus-Imean)); + if((((voltage_sinus-Vmean)*(current_sinus-Imean))*(0.0002/3600000))>0) + S2=S2+((voltage_sinus-Vmean)*(current_sinus-Imean)); + + + + if (j>=300) { j = 0; - Vmean=Vsum/(200); - Imean = Isum/(200); - Vsum=0; - Isum=0; + Vmean=Vsum/(300); + Imean = Isum/(300); + P1=P2/300; S1=S2/300; Ud=(Vmax-Vmean)/1.41; Id=(Imax-Imean)/1.41; - - Vsum = 0; Isum = 0; U = 0; I = 0; Vmax = 0; Imax = 0; + Vmax = 0; Imax = 0; + + cosi = P2/S2; + P2=0; S2=0; } - voltage_sinus[j] = voltage*2200; - current_sinus[j] = current*5; - if (voltage_sinus[j]>Vmax) - Vmax=voltage_sinus[j]; - if (current_sinus[j]>Imax) - Imax=current_sinus[j]; - P=P+(((voltage_sinus[j]-Vmean)*(current_sinus[j]-Imean))*(0.0002/3600000)); - - Vsum+=voltage_sinus[j]; - Isum+=current_sinus[j]; - - j++; - +} + +void transmit (void) +{ + strSize = sprintf ( str, "W=%10.4fkWh, cos fi=%5.4f, P1=%5.4f, S1=%5.4f", P, cosi, P2, S2); + State = TX; + } @@ -151,7 +164,7 @@ int main( void ) { - uint16_t count = 0; + @@ -230,15 +243,11 @@ measure.attach_us(&meas, 200); + send.attach(&transmit, 30); while( 1 ) { - count++; - if(count>=30) - { - strSize = sprintf ( str, "U=%3.0fV, I=%3.2fA, W=%10.4fkWh", Ud, Id, P); - State = TX; - } + lcd.setRGB(0, 255, 0); @@ -246,155 +255,36 @@ sprintf(monitor, "U=%3.0fV, I=%3.2fA", Ud, Id); - sprintf(monitor1, "P=%10.4f kWh", P); + sprintf(monitor1, "P=%8.2f kWh", P); // Print a message to the LCD. lcd.locate(0, 0); lcd.print(monitor); lcd.locate(0, 1); lcd.print(monitor1); - - switch( State ) + switch( State ) { case RX: - /*if( isMaster == true ) - { - if( BufferSize > 0 ) - { - if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) - { - led = !led; - debug( "...Pong\r\n" ); - // Send the next PING frame - strcpy( ( char* )Buffer, ( char* )PingMsg ); - // We fill the buffer with numbers for the payload - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - } - else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) - { // A master already exists then become a slave - debug( "...Ping\r\n" ); - led = !led; - isMaster = false; - // Send the next PONG frame - strcpy( ( char* )Buffer, ( char* )PongMsg ); - // We fill the buffer with numbers for the payload - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - } - else // valid reception but neither a PING or a PONG message - { // Set device as master ans start again - isMaster = true; - Radio.Rx( RX_TIMEOUT_VALUE ); - } - } - } - else - { - if( BufferSize > 0 ) - { - if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) - { - led = !led; - debug( "...Ping\r\n" ); - // Send the reply to the PING string - strcpy( ( char* )Buffer, ( char* )PongMsg ); - // We fill the buffer with numbers for the payload - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - } - else // valid reception but not a PING as expected - { // Set device as master and start again - isMaster = true; - Radio.Rx( RX_TIMEOUT_VALUE ); - } - } - }*/ + //Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case TX: strcpy(( char* )Buffer, ( char* ) str); - - /*led = !led; - if( isMaster == true ) - { - debug( "Ping...\r\n" ); - } - else - { - debug( "Pong...\r\n" ); - }*/ - /*for( i = strSize; i < BufferSize; i++ ) - { - Buffer[i] = i - strSize; - }*/ - wait_ms( 10 ); - Radio.Send( Buffer, BUFFER_SIZE ); + Radio.Send((uint8_t *) Buffer, BufferSize ); //Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case RX_TIMEOUT: - /*if( isMaster == true ) - { - // Send the next PING frame - strcpy( ( char* )Buffer, ( char* )PingMsg ); - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - } - else - { - Radio.Rx( RX_TIMEOUT_VALUE ); - }*/ Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case RX_ERROR: - // We have received a Packet with a CRC error, send reply as if packet was correct - /*if( isMaster == true ) - { - // Send the next PING frame - strcpy( ( char* )Buffer, ( char* )PingMsg ); - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - } - else - { - // Send the next PONG frame - strcpy( ( char* )Buffer, ( char* )PongMsg ); - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - }*/ - //Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case TX_TIMEOUT: - //Radio.Rx( RX_TIMEOUT_VALUE ); + Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case LOWPOWER: @@ -403,7 +293,8 @@ State = LOWPOWER; break; } - wait(1); + + wait(5); } }