Red Light Work

Dependencies:   EthernetInterface QEI_hw QEIx4 mbed-rtos mbed realtimeMMLib

Fork of realtimeMM_V3 by Graham Nicholson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MMiniFileReader.h"
00003 #include "rtos.h"
00004 #include "EthernetInterface.h"
00005 #include "MBedStation.h"
00006 #include "sensor_base.h"
00007 #include "Coms.h"
00008  
00009 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00010 Serial pc(USBTX, USBRX);
00011 iniFile iniconfig;
00012 char * ComsType;
00013 DigitalOut light1(LED1);
00014 DigitalOut light3(LED3);
00015 DigitalOut Red(p8);
00016 PwmOut Green(p21);
00017 EthernetInterface ethtmp;
00018 TCPSocketConnection socktmp;
00019 Coms MMComs;
00020 int connected = 0;
00021 int datasendtime;
00022 char datasample[250];
00023 char * datasample1;
00024 char * datasample2;
00025 char * datasample3;
00026 char * datasample4;
00027 
00028 MBedStation MBed1;
00029 
00030 sensor_pulse SensorTest(p6);
00031     
00032 int main() 
00033 {    
00034     int error;
00035     Red=1;
00036     light3=1;
00037     Green.period(0.2);
00038     Green=0;
00039     light1=0;
00040         
00041     MBed1.setup();
00042     
00043     
00044     sensor_onoff* sensor0;
00045     sensor0 = &MBed1.sensor_0;
00046 
00047     sensor_vin* sensor1;
00048     sensor1 = &MBed1.sensor_1;
00049 
00050     sensor_vin* sensor2;
00051     sensor2 = &MBed1.sensor_2;
00052 
00053     sensor_pulse SensorTest(p6);
00054     SensorTest.sensor_id = 2;
00055     SensorTest.enabled = true;
00056     //sensor_pulse* sensor3;
00057     //sensor3->IntIn.rise(sensor3, &sensor_pulse::ISR1);
00058     //sensor3 = &MBed1.sensor_3;
00059 
00060     //Ini file reading
00061     iniFileReader filereader;
00062     
00063     iniconfig = filereader.ReadFile("/local/ini.txt");
00064 
00065     ComsType=iniconfig.Station.ComsType;
00066     
00067     pc.printf("IPAddress=%s",iniconfig.Station.IPAddress);
00068     pc.printf("Coms Type=%s",ComsType);
00069     datasendtime = atoi(iniconfig.Station.DataSendTime);
00070     pc.printf("datasendtime=%i\r\n",datasendtime); 
00071 
00072     while (true) { //Main Program Loop - allows for re-establishing coms
00073 
00074         //int retrys = 5;       
00075     
00076         // /* Ethernet connection
00077          if (strncmp(ComsType,"Ethernet",8)==0) {
00078             printf("\r\n");   
00079             printf("Initialising ethernet...\r\n");
00080         
00081             error = MMComs.EthernetInitialise(iniconfig.Station.IPAddress, iniconfig.Station.NetworkMask, iniconfig.Station.DefaultGateway);
00082         
00083             if (error)  {
00084                 printf("Error: Could not initialise ethernet (code %i)...\r\n", error);
00085                 while(true);
00086             }
00087             printf("MAC Address is %s\r\n", MMComs.getMACAddress());
00088             printf("Obtaining IP address...\r\n");
00089             
00090             error = MMComs.EthernetConnect();
00091             if (error) {
00092                 printf("Error: Could not obtain IP address (code %i) \r\n", error);
00093                 while(true);
00094             }
00095             
00096             printf("IP Address is %s\r\n", ethtmp.getIPAddress());
00097             printf("Network Mask is %s\r\n", ethtmp.getNetworkMask());
00098             printf("Default Gateway is %s\r\n", ethtmp.getGateway());  
00099             printf("Connecting...\r\n");
00100         
00101         }
00102         else {
00103             printf("ethernet not required...\r\n");
00104         }
00105         
00106         //retrys = 5;
00107 
00108        // End of Ethernet connection
00109 
00110 
00111 
00112         //retrys = 0;
00113     
00114         printf("Monitoring \r\n");
00115         Green=0.01;
00116         light1=1;
00117     
00118         //Start of Monitoring Loop
00119         while (true)
00120         {
00121             Red=0;
00122             light3=0;
00123             datasample1 = sensor0->read_data();
00124             datasample2 = sensor1->read_data();
00125             datasample3 = sensor2->read_data();
00126             //datasample4 = sensor3->read_data();
00127             //sensor3->reset();
00128             datasample4 = SensorTest.read_data();
00129             SensorTest.reset();
00130             //printf("Testr: %s\n", datasample4);
00131                
00132             sprintf(datasample,"<<1%s,%s>>\n",datasample1,datasample4);
00133             //sprintf(datasample,"<<1%s,%s,%s>>\n",datasample1,datasample2,datasample3);
00134             printf(datasample);  
00135         
00136             if (strncmp(ComsType,"Ethernet",8)==0) {
00137                 printf("Trying to Connect to %s Port: %s \r\n",iniconfig.Server.IPAddress,iniconfig.Server.Port);
00138                 
00139                 Red=1;
00140                 light3=1;
00141                 
00142                 error = MMComs.EthernetSocketConnect(iniconfig.Server.IPAddress, atoi(iniconfig.Server.Port));            
00143                 if (error) {
00144                     printf("Connect Failed\r\n");
00145                     continue;
00146                     //break;
00147                 }
00148                 
00149                 error = MMComs.EthernetSocketSendAll(datasample, sizeof(datasample)-1);
00150                 if (error) {
00151                     printf("Send Failed\r\n");
00152                     //continue;
00153                     //break;
00154                 }
00155                 else {
00156                     Red=0;                    
00157                     light3=0;
00158                     
00159                     char buffer[300];
00160                     ////////////////start of receiving status
00161                     int ret;
00162                     while (true) {
00163                         ret = MMComs.EthernetSocketReceive(buffer, sizeof(buffer)-1);
00164                         //ret = sock.receive(buffer, sizeof(buffer)-1);
00165                         if (ret <= 0) break;
00166                         buffer[ret] = '\0';
00167                         MBed1.ProcessResponse(buffer);
00168                         printf("%d",atoi(buffer));
00169                         printf("Received %d chars from server:\r\n%s\r\n", ret, buffer);
00170                     }
00171                     
00172                 }
00173                 MMComs.EthernetSocketClose();            
00174  
00175             }
00176         
00177             
00178             Thread::wait(datasendtime);
00179       
00180         } // End of Monitoring Loop
00181 
00182     } //End of Main Programme Loop
00183     
00184     //eth.disconnect();
00185             
00186 }
00187