Jim Abraham / Mbed 2 deprecated SumpPumpMonitor-WIZwiki-W7500

Dependencies:   STATIC_COLORS WIZnetInterface mbed

Fork of HTTP_SDCard_File_Server_WIZwiki-W7500 by FOURNET Olivier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main_SPM.cpp Source File

main_SPM.cpp

00001 /*
00002 ==========================================================================
00003 Project:     Sump Pump Monitor
00004 Description: Measure pump current to indicate run status. (10amp pump) 
00005 Date:        11-6-16
00006 Author:      Jim Abraham
00007 Board:       WIZwiki-W7500
00008 Rev:
00009 ==========================================================================
00010 */
00011 
00012 #include "mbed.h"
00013 #include "EthernetInterface.h"
00014 #include <stdio.h>
00015 #include <string.h>
00016 
00017 #define USE_DHCP    0  // 0= no dhcp
00018 #define MAC     "\x00\x08\xDC\x11\x34\x78"
00019 #define IP      "192.168.0.12"
00020 #define MASK    "255.255.255.0"
00021 #define GATEWAY "192.168.0.254"
00022 #define HTTPD_SERVER_PORT   80
00023 #define HTTPD_MAX_REQ_LENGTH   1023
00024 #define HTTPD_MAX_HDR_LENGTH   255
00025 #define HTTPD_MAX_FNAME_LENGTH   127
00026 #define HTTPD_MAX_DNAME_LENGTH   127
00027 
00028 #if defined(TARGET_WIZwiki_W7500)
00029 Serial uart(USBTX, USBRX);
00030 #include "static_colors.h"
00031 // LED R     : server listning status
00032 // LED GREEN : socket connecting status Ok
00033 // LED BLUE  : socket connecting status Busy
00034 #endif
00035 
00036 EthernetInterface eth;
00037 TCPSocketServer server;
00038 TCPSocketConnection client;
00039 
00040 //========================================================
00041 // Data Variables.
00042 //========================================================
00043 int LP1,LP2,LP3;
00044 float AMP_Reading_on_AO, AMPreadingHI, AMPreading;
00045 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00046 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00047 
00048 //-----------------------------------------------------------
00049 // Initialize a pins to perform analog input
00050 //-----------------------------------------------------------
00051 AnalogIn   ain0(A0);
00052 
00053 //-----------------------------------------------------------
00054 void Serial_Interface_init(void)
00055 //-----------------------------------------------------------
00056 {
00057  // Serial Interface eth;
00058     // Serial port configuration: 
00059     // 9600 baud, 8-bit data, no parity, stop bit
00060     uart.baud(9600);
00061     uart.format(8, SerialBase::None, 1);
00062     uart.printf("Initializing\n");
00063     wait(1.0);   
00064 }
00065 
00066 //-----------------------------------------------------------
00067 void ethernet_init(void)
00068 //-----------------------------------------------------------
00069 {
00070  //    EthernetInterface eth;
00071     uart.printf("Initializing Ethernet\n");
00072     #if USE_DHCP
00073     //eth.init Use DHCP
00074     // Use DHCP for WIZnetInterface
00075     int ret = eth.init((uint8_t*)MAC);    
00076     uart.printf("Connecting DHCP\n");
00077     #else
00078     // IP,mask,Gateway
00079     int ret = eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  
00080     uart.printf("Connecting (IP,mask,Gateway)\n");
00081     #endif
00082     wait(1.0);
00083     // Check Ethernet Link-Done
00084     uart.printf("Check Ethernet Link\r\n");
00085     
00086     if(eth.link() == true) 
00087     { 
00088      uart.printf("- Ethernet PHY Link - Done\r\n");
00089      COLOR(_RED_);
00090     }
00091     else 
00092     {
00093         uart.printf("- Ethernet PHY Link - Fail\r\n");
00094         COLOR(_BLACK_);
00095     }
00096     wait(1.0);
00097     if(!ret) 
00098     {
00099         uart.printf("Initialized, MAC: %s\r\n", eth.getMACAddress());
00100         ret = eth.connect();
00101      
00102      if(!ret) 
00103      {
00104         uart.printf("IP: %s, MASK: %s, GW: %s\r\n",
00105         eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00106         COLOR(_CYAN_);
00107      } 
00108      else 
00109      {
00110         uart.printf("Error ethernet.connect() - ret = %d\r\n", ret);
00111         COLOR(_YELLOW_);
00112         exit(0);
00113      }
00114     } 
00115     else 
00116     {
00117         uart.printf("Error ethernet.init() - ret = %d\r\n", ret);
00118      COLOR(_BLACK_);
00119      exit(0);
00120     }  
00121       
00122     wait(1.0);
00123  // TCPSocketServer server;
00124     server.bind(HTTPD_SERVER_PORT);
00125     server.listen();
00126     uart.printf("Server Listening\n");   
00127 }
00128 
00129 //-----------------------------------------------------------
00130 void VAC_Zero_Cross(void)
00131 //-----------------------------------------------------------
00132 {
00133   LP1=0;                 // Indicator to exit while loop
00134   if (LP2==1)
00135     {   
00136 //Waits for the waveform to be close to 'zero' (500 adc)
00137       while(LP1==0)
00138       {
00139         wait_us(100);
00140         AMPreading=ain0.read();
00141 
00142 //check its within range. (+/- 5)
00143         if ((AMPreading < 0.5) && (AMPreading > 0.4)) 
00144         {
00145            LP1=1;
00146         }
00147       }
00148     }
00149   LP2=1;
00150 }
00151 
00152 //-----------------------------------------------------------
00153  void Measure_AMPS(void)
00154 //-----------------------------------------------------------
00155 // Measure AC Current.
00156 // 12bit ADC, 10MHz = 0.1usec conversion time.
00157 // 12bit ADC (0-3.3v = 0-4095 values) 3.3/4096 = 0.81mv
00158 // 1.65v = 2047 = 0 amps.
00159 //-----------------------------------------------------------
00160 {
00161     AMPreadingHI=0.0;
00162     for (LP3=0; LP3<100; LP3++)
00163     {
00164        wait_us(200);
00165       AMPreading=ain0.read();
00166       if (AMPreadingHI < AMPreading) AMPreadingHI  =  AMPreading;
00167     }
00168     
00169     if (AMPreadingHI <0.51)
00170     {
00171        COLOR(_BLUE_);
00172     }
00173     else
00174     {
00175        COLOR(_GREEN_);
00176     }
00177     
00178     AMP_Reading_on_AO = AMPreadingHI;
00179     uart.printf("$%2.2f!", AMPreading);
00180     sprintf(httpHeader,"$%2.2f!", AMP_Reading_on_AO); 
00181     client.send(httpHeader,strlen(httpHeader));       //send amps to PC.
00182 }
00183 
00184 
00185 //===================================================================
00186 int main(void)
00187 //===================================================================
00188 {
00189 //RGB LED: 
00190 //WHITE = program running.
00191 //RED   = not connected to PC.
00192 //GRN   = pump running.
00193 //BLUE  = pump not running.
00194 
00195     Serial_Interface_init(); 
00196     ethernet_init();
00197     LP1=0;                 // Indicator to exit while loop
00198     LP2=0;
00199 //-----------------------------------------------------------   
00200     while(true) 
00201 //-----------------------------------------------------------
00202     {
00203         uart.printf("\nWait for new connection...\r\n");
00204         server.accept(client);
00205         client.set_blocking(false, 1500); // Timeout after (1.5)s
00206         uart.printf("Connection from: %s\r\n", client.get_address());
00207         
00208 //-----------------------------------------------------------
00209         while(true) 
00210 //-----------------------------------------------------------
00211         {            
00212             if(!client.is_connected())
00213              {
00214                COLOR(_RED_);
00215                break;        //exit while
00216              }
00217                 // Mesure ADC 0 - Check Sump Pump Run Status
00218                 VAC_Zero_Cross();
00219                 Measure_AMPS();              
00220                 wait(1.0);
00221                 COLOR(_WHITE_);
00222         }
00223 //-----------------------------------------------------------
00224         client.close();  //close connection to pc app.
00225         ethernet_init();
00226     }
00227 }