Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Monitor de Voltagem
- *
- * Monitora a voltagem de um sistema, utilizando divisão de voltagem.
- * Voltagem máxima: 20v
- * Utilizar Resistor 3K entre V+ da bateria e a entrada analógica.
- * Utilizar Resistor 1K no GND e V- da bateria, interligando na porta analogica
- *
- * Autor:
- * Alexandre Jeronimo Correa
- * ajcorrea@gmail.com
- * 26/10/2011
- *
- * Changelog:
- * 26/10/2011 - ajcorrea - Versao inicial
- * Esta versao suporta apenas comunicacao serial.
- * 26/10/2011 - ajcorrea - Versao 0.2
- * Suporte a Ethernet Shield. O suporte inicial funciona
- * da seguinte maneira, o client conecta no ip e porta
- * do shield, o servidor envia a voltagem e termina a conexao.
- */
- #define ETHERNET 1
- #ifdef ETHERNET
- //Includes - Ethernet.h e SPI.h necessarios para Ethernet
- #include <Ethernet.h>
- #include <SPI.h>
- #endif
- //variaveis
- const int referenceVolts = 5; //valor de referencia, maximo aceito pela ADC
- const int maxVoltage = 20; //valor maximo a ser medido
- const int R1 = 3000; // valor do resistor1, 3000 ohms (3k)
- const int R2 = 1000; // valor do resistor2, 1000 ohms (1k)
- const int resistorFactor = 255 / (R2/(R1 + R2)); //Fator de resistencia
- const int batteryPin = 0; // Porta analogica que recebe o positivo da bateria (V+)
- #ifdef ETHERNET
- //variaveis sistema ethernet
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Mac address do shield
- byte ip[] = { 192, 168, 2, 177 }; //Ip address do shield
- byte gateway[] = { 192, 168, 2, 2 }; //gateway da rede
- byte subnet[] = { 255, 255, 255, 0 }; //mascara da rede
- const int Porta = 23; //Porta TCP de Acesso
- //Inicializacao do servidor
- Server server(Porta);
- #endif
- void setup()
- {
- Serial.begin(9600); //Inicia comunicacao serial.
- #ifdef ETHERNET
- Ethernet.begin(mac,ip,gateway,subnet);
- server.begin();
- delay(1000);
- #endif
- }
- float printVolts(int Pin) {
- int value = analogRead(Pin);
- float volts = (value / resistorFactor) * referenceVolts;
- volts = (((volts / 1024) * -1) * (maxVoltage / referenceVolts));
- return volts;
- }
- void loop()
- {
- float amostragem = 0.00;
- amostragem = printVolts(batteryPin);
- Serial.println(amostragem);
- #ifdef ETHERNET
- Client client = server.available();
- if (client) {
- client.println(amostragem);
- }
- #endif
- delay(500); //Pausa de 500ms
- #ifdef ETHERNET
- if (client) {
- client.stop();
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement