Advertisement
RuiViana

RTC_SD_Card_WIFI

Sep 2nd, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. #include <SD.h>
  2. #include <SPI.h>
  3. #include <WiFi.h>
  4.  
  5. #include <DS1307.h>
  6. DS1307 rtc(A4, A5);
  7.  
  8. Sd2Card SDcard;
  9. SdVolume volume;
  10. const int chipSelect = 4;
  11. char ssid[] = "*******"; // your network SSID (name)
  12. char pass[] = "********"; // your network password
  13. int keyIndex = 0; // your network key Index number (needed only for WEP)
  14. //Sensor de Corrrente DC 10A
  15. //PINO DO SENSOR
  16. const int sensorPin = A0;
  17. //DECLARA��O DA VARI�VEL QUE REALIZA A LEITURA DA CORRENTE
  18. float sensorValue = 0;
  19. float currentValue = 0;
  20. //DECLARA��O DA CONSTANTE DC 5/1023
  21. float voltsporUnidade = 0.0048875855327468;
  22. float ruido = 0.00;
  23. // Sensor de Tens�o
  24. float tensaoA5;
  25. float aRef = 4.48;
  26. float relacaoA5 = 11.1134;
  27. #define AMOSTRAS 12
  28. //WIFI
  29. int status = WL_IDLE_STATUS;
  30. WiFiServer server(80);
  31. //----------------------------
  32. void setup()
  33. {
  34. rtc.halt(false); //Aciona o relogio
  35. //As linhas abaixo setam a data e hora do modulo
  36. //e podem ser comentada apos a primeira utilizacao
  37. rtc.setDOW(SUNDAY); //Define o dia da semana
  38. rtc.setTime(16, 50, 45); //Define o horario
  39. rtc.setDate(2, 9, 2016); //Define o dia, mes e ano
  40.  
  41.  
  42. Serial.begin(9600);
  43. if (!SD.begin(chipSelect))
  44. {
  45. Serial.println("Falha ao acessar o cartao !");
  46. return;
  47. }
  48. Serial.println("Cartao iniciado corretamente !");
  49. Serial.println();
  50. while (!Serial) {
  51. }
  52. if (WiFi.status() == WL_NO_SHIELD)
  53. {
  54. Serial.println("WiFi shield not present");
  55. // don't continue:
  56. while (true);
  57. }
  58. String fv = WiFi.firmwareVersion();
  59. if (fv != "1.1.0")
  60. {
  61. Serial.println("Please upgrade the firmware");
  62. }
  63. while (status != WL_CONNECTED)
  64. {
  65. Serial.print("Attempting to connect to SSID: ");
  66. Serial.println(ssid);
  67. status = WiFi.begin(ssid, pass);
  68. delay(5000);
  69. }
  70. server.begin();
  71. printWifiStatus();
  72. }
  73. //-----------------------------------
  74. float lePorta(uint8_t portaAnalogica)
  75. {
  76. float total = 0;
  77. for (int i = 0; i < AMOSTRAS; i++)
  78. {
  79. total += 1.0 * analogRead(portaAnalogica);
  80. delay(5);
  81. }
  82. return total / (float)AMOSTRAS;
  83. }
  84. //-------------------------------
  85. void mostraTensoes()
  86. {
  87. Serial.print("Tensao: ");
  88. Serial.print(tensaoA5 * relacaoA5);
  89. Serial.print ("\n");
  90. }
  91. //------------------------------
  92. void loop()
  93. {
  94. File dataFile = SD.open("arquivo.txt", FILE_WRITE);
  95. tensaoA5 = (lePorta(A5) * aRef) / 1023;
  96. mostraTensoes();
  97. delay(1000);
  98. // mostra o resultado no terminal
  99. currentValue = 0; // REINICIA O VALOR ATUAL E ATUALIZA NA PROXIMA LEITURA
  100. //INICIA A ANALISE DOS VALAORES PARA MEDIR A CORRENTE CONSUMIDA
  101. for (int index = 0; index < 5000; index++)
  102. {
  103. sensorValue = analogRead(sensorPin); // REALIZA A LEITURA DO SENSOR NO PINO A0
  104. sensorValue = (sensorValue - 510.85) * voltsporUnidade; //AJUSTAR VALOR ENTRE 510-512
  105. currentValue = currentValue + (sensorValue / 100) * 1000; // SENSOR DE 10A, A SA�DA DO SENSOR 100mV POR AMPER
  106. delay(1);
  107. }
  108. currentValue = currentValue / 5000; // 5000mV
  109. Serial.print("corrente = " ); // MOSTRAR RESULTADO NO TERMINAL
  110. currentValue = currentValue - ruido;
  111. Serial.print(currentValue, 3);
  112. Serial.println(" Amp");
  113. Serial.print("\n" );
  114. delay(10);
  115. float Potencia = currentValue * (tensaoA5 * relacaoA5);
  116. Serial.print("Potencia= ");
  117. Serial.print(Potencia);
  118. Serial.print("\n");
  119. delay(2000);
  120. if (dataFile) // Parte do cartao
  121. {
  122. dataFile.print("Corrente: ");
  123. dataFile.println(currentValue);
  124. dataFile.println(" Amp");
  125. dataFile.print("Tensao: ");
  126. dataFile.println(tensaoA5 * relacaoA5);
  127. dataFile.print("Potencia: ");
  128. dataFile.println(Potencia);
  129.  
  130. dataFile.print("Data: ");
  131. dataFile.println(rtc.getTimeStr());
  132. dataFile.print("Hora: ");
  133. dataFile.println(rtc.getDateStr(FORMAT_SHORT));
  134.  
  135. dataFile.close();
  136. }
  137. else
  138. {
  139. Serial.println("Erro ao abrir arquivo.txt !"); // Mensagem de erro caso ocorra algum problema na abertura do arquivo
  140. }
  141. WiFiClient client = server.available(); // Parte do Wifi
  142. if (client)
  143. {
  144. Serial.println("new client");
  145. boolean currentLineIsBlank = true; // an http request ends with a blank line
  146. while (client.connected())
  147. {
  148. if (client.available())
  149. {
  150. char c = client.read();
  151. Serial.write(c);
  152. if (c == '\n' && currentLineIsBlank) // send a standard http response header
  153. {
  154. client.println("HTTP/1.1 200 OK");
  155. client.println("Content-Type: text/html");
  156. client.println("Connection: close");
  157. client.println("Refresh: 5");
  158. client.println();
  159. client.println("<!DOCTYPE HTML>");
  160. client.println("<html>");
  161. client.print("Corrente: ");
  162. client.print(currentValue);
  163. client.println("<br />");
  164. client.print("Tensao: ");
  165. client.print(tensaoA5 * relacaoA5);
  166. client.println("<br />");
  167. client.print("Potencia: ");
  168. client.print(Potencia);
  169. client.println("<br />");
  170. client.println("</html>");
  171. break;
  172. }
  173. if (c == '\n')
  174. {
  175. currentLineIsBlank = true; // you're starting a new line
  176. }
  177. else if (c != '\r')
  178. {
  179. currentLineIsBlank = false; // you've gotten a character on the current line
  180. }
  181. }
  182. }
  183. delay(1);
  184. client.stop();
  185. Serial.println("client disonnected");
  186. }
  187. }
  188. //---------------------------------
  189. void printWifiStatus()
  190. {
  191. IPAddress ip = WiFi.localIP();
  192. Serial.print("IP Address: ");
  193. Serial.println(ip);
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement