módulo WIFI para comunicación de KL25Z midiendo corriente a través de una sonda amperimétrica con Android App neurGAI

Dependencies:   WiflyInterface mbed

Fork of wiflyServer by Gorka Bueno

Files at this revision

API Documentation at this revision

Comitter:
gbm
Date:
Thu Mar 17 09:29:32 2016 +0000
Parent:
0:6d78e05ac19d
Child:
2:d5b61d09976a
Commit message:
m?dulo WIFI para comunicaci?n de KL25Z midiendo corriente a trav?s de una sonda amperim?trica con Android App neurGAI

Changed in this revision

WiflyInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
softserial.lib Show diff for this revision Revisions of this file
--- a/WiflyInterface.lib	Sun Feb 28 17:19:31 2016 +0000
+++ b/WiflyInterface.lib	Thu Mar 17 09:29:32 2016 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/gbm/code/WiflyInterface/#fa0fb985d6be
+http://developer.mbed.org/users/samux/code/WiflyInterface/#108340829acc
--- a/main.cpp	Sun Feb 28 17:19:31 2016 +0000
+++ b/main.cpp	Thu Mar 17 09:29:32 2016 +0000
@@ -1,81 +1,108 @@
-/*
-// este es el programa a ejecutar en ordenador, como cliente (tutoriales de java): https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
+//#define DEBUG
+#include "mbed.h"
+#include "WiflyInterface.h"
 
-import java.io.*;
-import java.net.*;
+#if (defined(DEBUG))
+#define DBG(x, ...) std::printf("[neurGAI : DBG]"x"\r\n", ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[neurGAI : WARN]"x"\r\n", ##__VA_ARGS__);
+#define ERR(x, ...) std::printf("[neurGAI : ERR]"x"\r\n", ##__VA_ARGS__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#endif
+
+#define ECHO_SERVER_PORT   7
 
-public class EchoClient {
-    public static void main(String[] args) throws IOException {
-        
-        if (args.length != 2) {
-            System.err.println(
-                "Usage: java EchoClient <host name> <port number>");
-            System.exit(1);
-        }
+WiflyInterface wifly(D14, D15, D5, LED1, "NeurGai", "neurGai2016", WPA);
+
+Ticker medidor;
+
+AnalogIn sonda(A2);                     // la sonda amperimétrica debe conectarse entre los pines A2 y PTE30
+AnalogOut salida_offset(PTE30);
 
-        String hostName = args[0];
-        int portNumber = Integer.parseInt(args[1]);
+int frecuencia_muestreo = 5000;         // con 8000 Hz hace bien el muestreo, pero el volcado del debug no es completo
+int numero_muestra = 0;
+float valor_muestra;
+float v_offset = 0;
+float v_offset_medido = 0;
+float potencia = 0;
+float potencia_medida = 0;
+float potencia_230V =0;
+float corriente_medida = 0;
+bool medido = false;
+bool medido_y_enviado = false;
 
-        try (
-            Socket echoSocket = new Socket(hostName, portNumber);
-            PrintWriter out =
-                new PrintWriter(echoSocket.getOutputStream(), true);
-            BufferedReader in =
-                new BufferedReader(
-                    new InputStreamReader(echoSocket.getInputStream()));
-            BufferedReader stdIn =
-                new BufferedReader(
-                    new InputStreamReader(System.in))
-        ) {
-            String userInput;
-            while ((userInput = stdIn.readLine()) != null) {
-                out.println(userInput);
-                System.out.println("echo: " + in.readLine());
-            }
-        } catch (UnknownHostException e) {
-            System.err.println("Don't know about host " + hostName);
-            System.exit(1);
-        } catch (IOException e) {
-            System.err.println("Couldn't get I/O for the connection to " +
-                hostName);
-            System.err.println(e);
-            System.exit(1);
-        } 
+float pendiente = 201.206437;
+float offset = 0;
+    
+int num_medida = 0;
+
+TCPSocketServer server;
+TCPSocketConnection client;
+
+char buffer[256];       // string con los datos
+
+
+void medir()
+{
+    numero_muestra++;
+    valor_muestra = sonda.read();
+    v_offset = v_offset + valor_muestra;
+    potencia = potencia + pow((valor_muestra - v_offset_medido), 2);
+    if (numero_muestra == frecuencia_muestreo)  // hemos llegado a un segundo
+    {
+        medidor.detach();
+        potencia_medida = potencia / frecuencia_muestreo;
+        corriente_medida = sqrt(potencia_medida);
+        v_offset_medido = v_offset / frecuencia_muestreo;
+        v_offset = 0;
+        potencia = 0;
+        numero_muestra = 0;
+        medido = true;
+        potencia_230V = (corriente_medida * pendiente + offset) * 230;
+        sprintf(buffer, "#%i&%f*\n", num_medida, potencia_230V);
+        DBG("%s %i", buffer, strlen(buffer));
+        int numDatosEnviados = client.send_all(buffer, strlen(buffer));
+        medido = false;
+        num_medida++;
+        medido_y_enviado = true;
+        DBG("datos enviados : %i", numDatosEnviados);
     }
 }
 
-*/
-
-#include "mbed.h"
-#include "WiflyInterface.h"
- 
-#define ECHO_SERVER_PORT   7
- 
-WiflyInterface wifly(D3, D2, D5, LED1, "wifisarea", "wifi0712", WPA);
- 
 int main (void)
 {
-    printf("\nEmpezando...\n\r");
+    DBG("Empezando...");
     wifly.init(); // use DHCP
     while (!wifly.connect()); // join the network
-    printf("\nLa direccion IP es %s\n\r", wifly.getIPAddress());
+    DBG("\nLa direccion IP es %s", wifly.getIPAddress());
  
-    TCPSocketServer server;
     server.bind(ECHO_SERVER_PORT);
     server.listen();
  
-    printf("\nEsperando conexion...\n");
-    TCPSocketConnection client;
+    DBG("\nEsperando conexion...");
     server.accept(client);
-    printf("\nServidor aceptado...\n");
-    wait(1);    //si no se le mete un pequeño retardo, se queda colgado. También vale con el printf anterior
+    DBG("\nServidor aceptado...");
+    wait(1);    //si no se le mete un pequeño retardo, se queda colgado. También vale con el DBG anterior
     
-    char buffer[256];
+    //configura el offset de voltaje a sumar a la sonda
+    salida_offset = 0.5;
+    
     while (true) {
+        DBG("Esperando dato...");
         int n = client.receive(buffer, sizeof(buffer));
         if (n <= 0) continue;
+        DBG("Recibido dato: %s Longitud %i", buffer, n);
         buffer[n] = 0;
- 
-        client.send_all(buffer, n);
+        if (strcmp(buffer, "Pot?\n") == 0) 
+
+        // activa el muestreo
+        medido_y_enviado = false;
+        medidor.attach(&medir, 1.0/frecuencia_muestreo);
+            
+        // spin in a main loop. medidor will interrupt it to call medir
+        while (!medido_y_enviado) 
+            wait(.1);
     }
-}
+}
\ No newline at end of file
--- a/softserial.lib	Sun Feb 28 17:19:31 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/gbm/code/softserial/#480d9575ac47