Advertisement
RuiViana

Ower_Monitor_Ok

Oct 16th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. ///////////////////////////////////////////
  2. // TCC - USF Campinas/SP - Eng. Elétrica //
  3. // Qualidade de Energia //
  4. //---------------------------------------//
  5. // Denis Eduardo Donadon Torres //
  6. ///////////////////////////////////////////
  7.  
  8. #include <Arduino.h>
  9. #include "EmonLibPro.h"
  10. #include <DS1307.h> // Carrega a biblioteca do RTC DS1307 (Real Time Clock)
  11. //#include <SD.h> // Carrega a biblioteca SD
  12. #include <SdFat.h> // Carrega a biblioteca SD
  13. int userCommand;
  14. DS1307 rtc(A4, A5); // Modulo RTC DS1307 ligado as portas A4 e A5 do Arduino
  15. const int chipSelect = 10; // Pino do Arduino conectado ao pino CS do modulo
  16. float Q; // Potência Reativa
  17.  
  18. EmonLibPro Emon;
  19. SdFat SD;
  20. //------------------------------------------------------------
  21. void setup()
  22. {
  23. Serial.begin(9600);
  24.  
  25. /////////////////////////////////////////////////////////////
  26. // Testa se o cartão cartão SD está encaixado corretamente //
  27. /////////////////////////////////////////////////////////////
  28. if (!SD.begin(chipSelect))
  29. {
  30. Serial.println();
  31. Serial.println("Falha ao acessar o cartao SD!");
  32. Serial.println("Verifique o cartao e reinicie o Analisador...");
  33. Serial.println();
  34. return;
  35. }
  36. Serial.println();
  37. Serial.println("Cartao iniciado corretamente!");
  38. Serial.println();
  39.  
  40. Emon = EmonLibPro(); // Required
  41. Emon.begin(); // Required
  42.  
  43. printMenu();
  44. }
  45. //------------------------------------------------------------------------------
  46. void loop()
  47. {
  48. if((userCommand == 1) && (Emon.FlagCALC_READY))
  49. {
  50. Emon.calculateResult();
  51. for (byte i=0;i<CURRENTCOUNT;i++)
  52. {
  53. printResults(i);
  54. }
  55. }
  56. if((userCommand == 2) && (Emon.FlagCYCLE_FULL)) {
  57. for (byte i=0; i<VOLTSCOUNT + CURRENTCOUNT;i++){
  58. Serial.print("ADC");
  59. Serial.print(0+i);
  60. Serial.print(",");
  61. serialDataTable(i);
  62. }
  63. Emon.FlagCYCLE_FULL = false; //Must reset after read to know next batch.
  64. }
  65.  
  66. if((userCommand == 3) && (Emon.FlagCALC_READY)) {
  67. Emon.printStatus();
  68. userCommand = 0;
  69. }
  70.  
  71. if ((userCommand == 4) && (EmonLibPro::FlagOutOfTime)) { // This should never be true. Reduce sampling rate if it is!
  72. Serial.println("$"); // Alarm that previous ADC processing has run out of time before timer event. Must decrease SAMPLESPSEC
  73. }
  74.  
  75. if(Serial.available())
  76. {
  77. userCommand=Serial.parseInt();
  78. Serial.print("Voce digitou: ");
  79. Serial.println(userCommand);
  80. Serial.println();
  81. Serial.println("Ok, seguem abaixo:");
  82. Serial.println();
  83. Serial.println("Dados: ");
  84. Serial.println();
  85. }
  86. }
  87. //--------------------------------------------------------------------------
  88. void printMenu()
  89. {
  90. Serial.println("ANALISADOR DE QUALIDADE DE ENERGIA");
  91. Serial.println();
  92. Serial.println("Digite o n 1 e tecle ENTER para iniciar!");
  93. Serial.println("Aguardando seu comando...");
  94. }
  95. //----------------------------------------------------------------------------
  96. // Print calculated results
  97. void printResults(byte i)
  98. {
  99. Q = sqrt(Emon.ResultP[i].P * Emon.ResultP[i].P + Emon.ResultP[i].S * Emon.ResultP[i].S); // Calcula a Potêcia Reativa ( Q = RAIZ(Potência Ativa² + Potência Aparente² ).
  100.  
  101. Serial.print(rtc.getDOWStr(FORMAT_LONG));
  102. Serial.print(", ");
  103. Serial.print(rtc.getDateStr(FORMAT_SHORT));
  104. Serial.print(", ");
  105. Serial.print("as ");
  106. Serial.print(rtc.getTimeStr());
  107. Serial.print(" hrs");
  108. Serial.println();
  109.  
  110. Serial.print("FP: ");
  111. Serial.print(Emon.ResultP[i].F);
  112. Serial.print("\t");
  113.  
  114. Serial.print("Freq: ");
  115. Serial.print(Emon.ResultV[0].HZ);
  116. Serial.print(" Hz\t");
  117.  
  118. Serial.print("Volts: ");
  119. Serial.print(Emon.ResultV[0].U);
  120. Serial.print(" Vac\t");
  121.  
  122. Serial.print("Corrente: ");
  123. Serial.print(Emon.ResultP[i].I);
  124. Serial.print(" A\t");
  125.  
  126. Serial.print("Pot Ativa: ");
  127. Serial.print(Emon.ResultP[i].P);
  128. Serial.print(" W\t");
  129.  
  130. Serial.print("Pot Aparente: ");
  131. Serial.print(Emon.ResultP[i].S);
  132. Serial.print(" VA\t");
  133.  
  134. Serial.print("Pot Reativa: ");
  135. Serial.print(Q);
  136. Serial.println(" var");
  137.  
  138. //////////////////////////////////////////////
  139. // Cria o arquivo arquivo .xls no cartão SD //
  140. //////////////////////////////////////////////
  141.  
  142. File dataFile = SD.open("Analise.txt", FILE_WRITE); // * Atenção, o nome do arquivo não pode ter caracteres inválidos como ()/{}[].,;* nem com acentos!!! *
  143. if (dataFile) // Grava os dados no arquivo
  144. {
  145. dataFile.print(rtc.getDOWStr(FORMAT_LONG));
  146. dataFile.print(", ");
  147. dataFile.print(rtc.getDateStr(FORMAT_SHORT));
  148. dataFile.print(", ");
  149. dataFile.print("as ");
  150. dataFile.print(rtc.getTimeStr());
  151. dataFile.print(" hrs");
  152. dataFile.println();
  153.  
  154. dataFile.print("FP: ");
  155. dataFile.print(Emon.ResultP[i].F);
  156. dataFile.print("\t");
  157.  
  158. dataFile.print("Freq: ");
  159. dataFile.print(Emon.ResultV[0].HZ);
  160. dataFile.print(" Hz\t");
  161.  
  162. dataFile.print("Volts: ");
  163. dataFile.print(Emon.ResultV[0].U);
  164. dataFile.print(" V\t");
  165.  
  166. dataFile.print("Corrente: ");
  167. dataFile.print(Emon.ResultP[i].I);
  168. dataFile.print(" A\t");
  169.  
  170. dataFile.print("Pot Ativa: ");
  171. dataFile.print(Emon.ResultP[i].P);
  172. dataFile.print(" W\t");
  173.  
  174. dataFile.print("Pot Aparente: ");
  175. dataFile.print(Emon.ResultP[i].S);
  176. dataFile.print(" VA\t");
  177.  
  178. dataFile.print("Potencia Reativa: ");
  179. dataFile.print(Q);
  180. dataFile.print(" var");
  181. dataFile.println(); // pula a linha
  182. dataFile.println(); // pula a linha
  183.  
  184. dataFile.close();
  185. }
  186. else
  187. {
  188. // Mensagem de erro caso ocorra algum problema na criação do arquivo
  189. Serial.println("Erro ao criar o arquivo .txt!");
  190. Serial.println("Verifique o cartao e reinicie o Analisador...");
  191. Serial.println();
  192. }
  193.  
  194. #ifdef USEPLL
  195. if(!Emon.pllUnlocked) Serial.print(" Locked");
  196. #endif
  197. Serial.println("\t");
  198. }
  199. //-----------------------------------------------------------------------------------
  200. // Exposes the internal sampled values for all samples on a cycle
  201. // Usefull to display the signal on a graph.
  202. void serialDataTable(byte b)
  203. {
  204. int sizearr = (Emon.SamplesPerCycleTotal/Emon.CyclesPerTotal);
  205. if (sizearr > CYCLEARRSIZE) sizearr = CYCLEARRSIZE;
  206. for (byte i=0;i<=sizearr;i++)
  207. {
  208. Serial.print(Emon.Sample[b].CycleArr[i]);
  209. Serial.print(",");
  210. }
  211. Serial.println("\t");
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement