![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
v3 Solution
Dependencies: EthernetInterface QEI_hw QEIx4 mbed-rtos mbed realtimeMMLib
main.cpp@2:c55e3a06e308, 2017-10-02 (annotated)
- Committer:
- ChrisAydon
- Date:
- Mon Oct 02 15:25:17 2017 +0000
- Revision:
- 2:c55e3a06e308
- Parent:
- 0:70c9f7c6844b
- Child:
- 3:32876c57fa57
Added extra light to Display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GTNicholson | 0:70c9f7c6844b | 1 | #include "mbed.h" |
GTNicholson | 0:70c9f7c6844b | 2 | #include "MMiniFileReader.h" |
GTNicholson | 0:70c9f7c6844b | 3 | #include "rtos.h" |
GTNicholson | 0:70c9f7c6844b | 4 | #include "EthernetInterface.h" |
GTNicholson | 0:70c9f7c6844b | 5 | #include "MBedStation.h" |
GTNicholson | 0:70c9f7c6844b | 6 | #include "sensor_base.h" |
GTNicholson | 0:70c9f7c6844b | 7 | #include "Coms.h" |
GTNicholson | 0:70c9f7c6844b | 8 | |
GTNicholson | 0:70c9f7c6844b | 9 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
GTNicholson | 0:70c9f7c6844b | 10 | Serial pc(USBTX, USBRX); |
GTNicholson | 0:70c9f7c6844b | 11 | iniFile iniconfig; |
GTNicholson | 0:70c9f7c6844b | 12 | char * ComsType; |
GTNicholson | 0:70c9f7c6844b | 13 | DigitalOut light1(LED1); |
GTNicholson | 0:70c9f7c6844b | 14 | DigitalOut light3(LED3); |
GTNicholson | 0:70c9f7c6844b | 15 | DigitalOut Red(p8); |
GTNicholson | 0:70c9f7c6844b | 16 | PwmOut Green(p21); |
GTNicholson | 0:70c9f7c6844b | 17 | EthernetInterface ethtmp; |
GTNicholson | 0:70c9f7c6844b | 18 | TCPSocketConnection socktmp; |
GTNicholson | 0:70c9f7c6844b | 19 | Coms MMComs; |
GTNicholson | 0:70c9f7c6844b | 20 | int connected = 0; |
GTNicholson | 0:70c9f7c6844b | 21 | int datasendtime; |
GTNicholson | 0:70c9f7c6844b | 22 | char datasample[250]; |
GTNicholson | 0:70c9f7c6844b | 23 | char * datasample1; |
GTNicholson | 0:70c9f7c6844b | 24 | char * datasample2; |
GTNicholson | 0:70c9f7c6844b | 25 | char * datasample3; |
GTNicholson | 0:70c9f7c6844b | 26 | char * datasample4; |
GTNicholson | 0:70c9f7c6844b | 27 | |
GTNicholson | 0:70c9f7c6844b | 28 | MBedStation MBed1; |
GTNicholson | 0:70c9f7c6844b | 29 | |
GTNicholson | 0:70c9f7c6844b | 30 | sensor_pulse SensorTest(p6); |
GTNicholson | 0:70c9f7c6844b | 31 | |
GTNicholson | 0:70c9f7c6844b | 32 | int main() |
GTNicholson | 0:70c9f7c6844b | 33 | { |
GTNicholson | 0:70c9f7c6844b | 34 | int error; |
GTNicholson | 0:70c9f7c6844b | 35 | Red=1; |
GTNicholson | 0:70c9f7c6844b | 36 | light3=1; |
GTNicholson | 0:70c9f7c6844b | 37 | Green.period(1); |
GTNicholson | 0:70c9f7c6844b | 38 | Green=0; |
GTNicholson | 0:70c9f7c6844b | 39 | light1=0; |
GTNicholson | 0:70c9f7c6844b | 40 | |
GTNicholson | 0:70c9f7c6844b | 41 | MBed1.setup(); |
GTNicholson | 0:70c9f7c6844b | 42 | |
GTNicholson | 0:70c9f7c6844b | 43 | |
GTNicholson | 0:70c9f7c6844b | 44 | sensor_onoff* sensor0; |
GTNicholson | 0:70c9f7c6844b | 45 | sensor0 = &MBed1.sensor_0; |
GTNicholson | 0:70c9f7c6844b | 46 | |
GTNicholson | 0:70c9f7c6844b | 47 | sensor_vin* sensor1; |
GTNicholson | 0:70c9f7c6844b | 48 | sensor1 = &MBed1.sensor_1; |
GTNicholson | 0:70c9f7c6844b | 49 | |
GTNicholson | 0:70c9f7c6844b | 50 | sensor_vin* sensor2; |
GTNicholson | 0:70c9f7c6844b | 51 | sensor2 = &MBed1.sensor_2; |
GTNicholson | 0:70c9f7c6844b | 52 | |
GTNicholson | 0:70c9f7c6844b | 53 | sensor_pulse SensorTest(p6); |
GTNicholson | 0:70c9f7c6844b | 54 | SensorTest.sensor_id = 2; |
GTNicholson | 0:70c9f7c6844b | 55 | SensorTest.enabled = true; |
GTNicholson | 0:70c9f7c6844b | 56 | //sensor_pulse* sensor3; |
GTNicholson | 0:70c9f7c6844b | 57 | //sensor3->IntIn.rise(sensor3, &sensor_pulse::ISR1); |
GTNicholson | 0:70c9f7c6844b | 58 | //sensor3 = &MBed1.sensor_3; |
GTNicholson | 0:70c9f7c6844b | 59 | |
GTNicholson | 0:70c9f7c6844b | 60 | //Ini file reading |
GTNicholson | 0:70c9f7c6844b | 61 | iniFileReader filereader; |
GTNicholson | 0:70c9f7c6844b | 62 | |
GTNicholson | 0:70c9f7c6844b | 63 | iniconfig = filereader.ReadFile("/local/ini.txt"); |
GTNicholson | 0:70c9f7c6844b | 64 | |
GTNicholson | 0:70c9f7c6844b | 65 | ComsType=iniconfig.Station.ComsType; |
GTNicholson | 0:70c9f7c6844b | 66 | |
GTNicholson | 0:70c9f7c6844b | 67 | pc.printf("IPAddress=%s",iniconfig.Station.IPAddress); |
GTNicholson | 0:70c9f7c6844b | 68 | pc.printf("Coms Type=%s",ComsType); |
GTNicholson | 0:70c9f7c6844b | 69 | datasendtime = atoi(iniconfig.Station.DataSendTime); |
GTNicholson | 0:70c9f7c6844b | 70 | pc.printf("datasendtime=%i\r\n",datasendtime); |
GTNicholson | 0:70c9f7c6844b | 71 | |
GTNicholson | 0:70c9f7c6844b | 72 | while (true) { //Main Program Loop - allows for re-establishing coms |
GTNicholson | 0:70c9f7c6844b | 73 | |
GTNicholson | 0:70c9f7c6844b | 74 | //int retrys = 5; |
GTNicholson | 0:70c9f7c6844b | 75 | |
GTNicholson | 0:70c9f7c6844b | 76 | // /* Ethernet connection |
GTNicholson | 0:70c9f7c6844b | 77 | if (strncmp(ComsType,"Ethernet",8)==0) { |
GTNicholson | 0:70c9f7c6844b | 78 | printf("\r\n"); |
GTNicholson | 0:70c9f7c6844b | 79 | printf("Initialising ethernet...\r\n"); |
GTNicholson | 0:70c9f7c6844b | 80 | |
GTNicholson | 0:70c9f7c6844b | 81 | error = MMComs.EthernetInitialise(iniconfig.Station.IPAddress, iniconfig.Station.NetworkMask, iniconfig.Station.DefaultGateway); |
GTNicholson | 0:70c9f7c6844b | 82 | |
GTNicholson | 0:70c9f7c6844b | 83 | if (error) { |
GTNicholson | 0:70c9f7c6844b | 84 | printf("Error: Could not initialise ethernet (code %i)...\r\n", error); |
GTNicholson | 0:70c9f7c6844b | 85 | while(true); |
GTNicholson | 0:70c9f7c6844b | 86 | } |
GTNicholson | 0:70c9f7c6844b | 87 | printf("MAC Address is %s\r\n", MMComs.getMACAddress()); |
GTNicholson | 0:70c9f7c6844b | 88 | printf("Obtaining IP address...\r\n"); |
GTNicholson | 0:70c9f7c6844b | 89 | |
GTNicholson | 0:70c9f7c6844b | 90 | error = MMComs.EthernetConnect(); |
GTNicholson | 0:70c9f7c6844b | 91 | if (error) { |
GTNicholson | 0:70c9f7c6844b | 92 | printf("Error: Could not obtain IP address (code %i) \r\n", error); |
GTNicholson | 0:70c9f7c6844b | 93 | while(true); |
GTNicholson | 0:70c9f7c6844b | 94 | } |
GTNicholson | 0:70c9f7c6844b | 95 | |
GTNicholson | 0:70c9f7c6844b | 96 | printf("IP Address is %s\r\n", ethtmp.getIPAddress()); |
GTNicholson | 0:70c9f7c6844b | 97 | printf("Network Mask is %s\r\n", ethtmp.getNetworkMask()); |
GTNicholson | 0:70c9f7c6844b | 98 | printf("Default Gateway is %s\r\n", ethtmp.getGateway()); |
GTNicholson | 0:70c9f7c6844b | 99 | printf("Connecting...\r\n"); |
GTNicholson | 0:70c9f7c6844b | 100 | |
GTNicholson | 0:70c9f7c6844b | 101 | } |
GTNicholson | 0:70c9f7c6844b | 102 | else { |
GTNicholson | 0:70c9f7c6844b | 103 | printf("ethernet not required...\r\n"); |
GTNicholson | 0:70c9f7c6844b | 104 | } |
GTNicholson | 0:70c9f7c6844b | 105 | |
GTNicholson | 0:70c9f7c6844b | 106 | //retrys = 5; |
GTNicholson | 0:70c9f7c6844b | 107 | |
GTNicholson | 0:70c9f7c6844b | 108 | // End of Ethernet connection |
GTNicholson | 0:70c9f7c6844b | 109 | |
GTNicholson | 0:70c9f7c6844b | 110 | |
GTNicholson | 0:70c9f7c6844b | 111 | |
GTNicholson | 0:70c9f7c6844b | 112 | //retrys = 0; |
GTNicholson | 0:70c9f7c6844b | 113 | |
GTNicholson | 0:70c9f7c6844b | 114 | printf("Monitoring \r\n"); |
GTNicholson | 0:70c9f7c6844b | 115 | Green=0.01; |
GTNicholson | 0:70c9f7c6844b | 116 | light1=1; |
GTNicholson | 0:70c9f7c6844b | 117 | |
GTNicholson | 0:70c9f7c6844b | 118 | //Start of Monitoring Loop |
GTNicholson | 0:70c9f7c6844b | 119 | while (true) |
GTNicholson | 0:70c9f7c6844b | 120 | { |
GTNicholson | 0:70c9f7c6844b | 121 | Red=0; |
GTNicholson | 0:70c9f7c6844b | 122 | light3=0; |
GTNicholson | 0:70c9f7c6844b | 123 | datasample1 = sensor0->read_data(); |
GTNicholson | 0:70c9f7c6844b | 124 | datasample2 = sensor1->read_data(); |
GTNicholson | 0:70c9f7c6844b | 125 | datasample3 = sensor2->read_data(); |
GTNicholson | 0:70c9f7c6844b | 126 | //datasample4 = sensor3->read_data(); |
GTNicholson | 0:70c9f7c6844b | 127 | //sensor3->reset(); |
GTNicholson | 0:70c9f7c6844b | 128 | datasample4 = SensorTest.read_data(); |
GTNicholson | 0:70c9f7c6844b | 129 | SensorTest.reset(); |
GTNicholson | 0:70c9f7c6844b | 130 | //printf("Testr: %s\n", datasample4); |
GTNicholson | 0:70c9f7c6844b | 131 | |
GTNicholson | 0:70c9f7c6844b | 132 | sprintf(datasample,"<<1%s,%s>>\n",datasample1,datasample4); |
GTNicholson | 0:70c9f7c6844b | 133 | //sprintf(datasample,"<<1%s,%s,%s>>\n",datasample1,datasample2,datasample3); |
GTNicholson | 0:70c9f7c6844b | 134 | printf(datasample); |
GTNicholson | 0:70c9f7c6844b | 135 | |
GTNicholson | 0:70c9f7c6844b | 136 | if (strncmp(ComsType,"Ethernet",8)==0) { |
GTNicholson | 0:70c9f7c6844b | 137 | printf("Trying to Connect to %s Port: %s \r\n",iniconfig.Server.IPAddress,iniconfig.Server.Port); |
GTNicholson | 0:70c9f7c6844b | 138 | |
GTNicholson | 0:70c9f7c6844b | 139 | Red=1; |
GTNicholson | 0:70c9f7c6844b | 140 | light3=1; |
GTNicholson | 0:70c9f7c6844b | 141 | |
GTNicholson | 0:70c9f7c6844b | 142 | error = MMComs.EthernetSocketConnect(iniconfig.Server.IPAddress, atoi(iniconfig.Server.Port)); |
GTNicholson | 0:70c9f7c6844b | 143 | if (error) { |
GTNicholson | 0:70c9f7c6844b | 144 | printf("Connect Failed\r\n"); |
GTNicholson | 0:70c9f7c6844b | 145 | continue; |
GTNicholson | 0:70c9f7c6844b | 146 | //break; |
GTNicholson | 0:70c9f7c6844b | 147 | } |
GTNicholson | 0:70c9f7c6844b | 148 | |
GTNicholson | 0:70c9f7c6844b | 149 | error = MMComs.EthernetSocketSendAll(datasample, sizeof(datasample)-1); |
GTNicholson | 0:70c9f7c6844b | 150 | if (error) { |
GTNicholson | 0:70c9f7c6844b | 151 | printf("Send Failed\r\n"); |
GTNicholson | 0:70c9f7c6844b | 152 | //continue; |
GTNicholson | 0:70c9f7c6844b | 153 | //break; |
GTNicholson | 0:70c9f7c6844b | 154 | } |
GTNicholson | 0:70c9f7c6844b | 155 | else { |
GTNicholson | 0:70c9f7c6844b | 156 | Red=0; |
GTNicholson | 0:70c9f7c6844b | 157 | light3=0; |
GTNicholson | 0:70c9f7c6844b | 158 | |
GTNicholson | 0:70c9f7c6844b | 159 | char buffer[300]; |
GTNicholson | 0:70c9f7c6844b | 160 | ////////////////start of receiving status |
GTNicholson | 0:70c9f7c6844b | 161 | int ret; |
GTNicholson | 0:70c9f7c6844b | 162 | while (true) { |
GTNicholson | 0:70c9f7c6844b | 163 | ret = MMComs.EthernetSocketReceive(buffer, sizeof(buffer)-1); |
GTNicholson | 0:70c9f7c6844b | 164 | //ret = sock.receive(buffer, sizeof(buffer)-1); |
GTNicholson | 0:70c9f7c6844b | 165 | if (ret <= 0) break; |
GTNicholson | 0:70c9f7c6844b | 166 | buffer[ret] = '\0'; |
ChrisAydon | 2:c55e3a06e308 | 167 | MBed1.ProcessResponse(buffer); |
ChrisAydon | 2:c55e3a06e308 | 168 | printf("%d",atoi(buffer)); |
GTNicholson | 0:70c9f7c6844b | 169 | printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); |
GTNicholson | 0:70c9f7c6844b | 170 | } |
GTNicholson | 0:70c9f7c6844b | 171 | |
GTNicholson | 0:70c9f7c6844b | 172 | } |
GTNicholson | 0:70c9f7c6844b | 173 | MMComs.EthernetSocketClose(); |
GTNicholson | 0:70c9f7c6844b | 174 | |
GTNicholson | 0:70c9f7c6844b | 175 | } |
GTNicholson | 0:70c9f7c6844b | 176 | |
GTNicholson | 0:70c9f7c6844b | 177 | |
GTNicholson | 0:70c9f7c6844b | 178 | Thread::wait(datasendtime); |
GTNicholson | 0:70c9f7c6844b | 179 | |
GTNicholson | 0:70c9f7c6844b | 180 | } // End of Monitoring Loop |
GTNicholson | 0:70c9f7c6844b | 181 | |
GTNicholson | 0:70c9f7c6844b | 182 | } //End of Main Programme Loop |
GTNicholson | 0:70c9f7c6844b | 183 | |
GTNicholson | 0:70c9f7c6844b | 184 | //eth.disconnect(); |
GTNicholson | 0:70c9f7c6844b | 185 | |
GTNicholson | 0:70c9f7c6844b | 186 | } |
GTNicholson | 0:70c9f7c6844b | 187 |