Dependencies:   C12832 Client

Committer:
thomasluca
Date:
Sat Nov 07 15:12:35 2020 +0000
Revision:
14:a05e12de308b
Parent:
13:368c6181b81e
Deleted Client class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thomasluca 13:368c6181b81e 1 #include "mbed.h"
thomasluca 13:368c6181b81e 2 #include "LM75B.h"
thomasluca 13:368c6181b81e 3 #include "C12832.h"
thomasluca 13:368c6181b81e 4 #include "EthernetInterface.h"
thomasluca 13:368c6181b81e 5 #include "StateMachine.h"
thomasluca 13:368c6181b81e 6 #include <vector>
thomasluca 13:368c6181b81e 7 #include <string>
thomasluca 13:368c6181b81e 8
thomasluca 13:368c6181b81e 9 StateMachine::StateMachine()
thomasluca 13:368c6181b81e 10 {
thomasluca 13:368c6181b81e 11 lcd = new C12832 (D11, D13, D12, D7, D10);
thomasluca 13:368c6181b81e 12 tempSensor = new LM75B (D14,D15);
thomasluca 13:368c6181b81e 13 pot1 = new AnalogIn (A0);
thomasluca 13:368c6181b81e 14 currentState = INIT;
thomasluca 13:368c6181b81e 15 }
thomasluca 13:368c6181b81e 16
thomasluca 13:368c6181b81e 17 StateMachine::~StateMachine()
thomasluca 13:368c6181b81e 18 {
thomasluca 13:368c6181b81e 19 delete lcd;
thomasluca 13:368c6181b81e 20 delete tempSensor;
thomasluca 13:368c6181b81e 21 delete pot1;
thomasluca 13:368c6181b81e 22 }
thomasluca 13:368c6181b81e 23
thomasluca 13:368c6181b81e 24 void StateMachine::start()
thomasluca 13:368c6181b81e 25 {
thomasluca 13:368c6181b81e 26 while(!containsID()) {
thomasluca 13:368c6181b81e 27 switch(currentState) {
thomasluca 13:368c6181b81e 28 case INIT:
thomasluca 13:368c6181b81e 29 actionInit();
thomasluca 13:368c6181b81e 30 break;
thomasluca 13:368c6181b81e 31 case RECEIVE:
thomasluca 13:368c6181b81e 32 actionReceive();
thomasluca 13:368c6181b81e 33 currentState = TRANSMIT;
thomasluca 13:368c6181b81e 34 break;
thomasluca 13:368c6181b81e 35 case TRANSMIT:
thomasluca 13:368c6181b81e 36 actionTransmit();
thomasluca 13:368c6181b81e 37 currentState = RECEIVE;
thomasluca 13:368c6181b81e 38 break;
thomasluca 13:368c6181b81e 39 default:
thomasluca 13:368c6181b81e 40 currentState = CERROR;
thomasluca 13:368c6181b81e 41 break;
thomasluca 13:368c6181b81e 42 }
thomasluca 13:368c6181b81e 43 }
thomasluca 13:368c6181b81e 44 }
thomasluca 13:368c6181b81e 45
thomasluca 13:368c6181b81e 46 void StateMachine::actionInit()
thomasluca 13:368c6181b81e 47 {
thomasluca 13:368c6181b81e 48 do {
thomasluca 13:368c6181b81e 49 uint8_t PWM = pot1.read();
thomasluca 13:368c6181b81e 50 if (PWM == 1.0) {
thomasluca 13:368c6181b81e 51 currentState = RECEIVE;
thomasluca 13:368c6181b81e 52 } else if (PWM == 0.0) {
thomasluca 13:368c6181b81e 53 currentState = TRANSMIT;
thomasluca 13:368c6181b81e 54 }
thomasluca 13:368c6181b81e 55 } while (currentState == INIT);
thomasluca 13:368c6181b81e 56 }
thomasluca 13:368c6181b81e 57
thomasluca 13:368c6181b81e 58 void StateMachine::actionReceive()
thomasluca 13:368c6181b81e 59 {
thomasluca 13:368c6181b81e 60 printf("Server:\n\r");
thomasluca 13:368c6181b81e 61
thomasluca 13:368c6181b81e 62 EthernetInterface eth;
thomasluca 13:368c6181b81e 63 eth.set_network("192.168.0.20","255.255.255.0","192.168.0.1");
thomasluca 13:368c6181b81e 64 eth.connect();
thomasluca 13:368c6181b81e 65
thomasluca 13:368c6181b81e 66 printf("The Server IP address is '%s'\n\r", eth.get_ip_address());
thomasluca 13:368c6181b81e 67
thomasluca 13:368c6181b81e 68 TCPServer srv(&eth);
thomasluca 13:368c6181b81e 69 srv.bind(4000);
thomasluca 13:368c6181b81e 70 srv.listen();
thomasluca 13:368c6181b81e 71
thomasluca 13:368c6181b81e 72 while(true) {
thomasluca 13:368c6181b81e 73 TCPSocket client;
thomasluca 13:368c6181b81e 74 SocketAddress client_addr;
thomasluca 13:368c6181b81e 75 char *buffer = "Hello TCP client!\r\n";
thomasluca 13:368c6181b81e 76
thomasluca 13:368c6181b81e 77 srv.accept(&client, &client_addr);
thomasluca 13:368c6181b81e 78 printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), client_addr.get_port());
thomasluca 13:368c6181b81e 79
thomasluca 13:368c6181b81e 80 client.send(buffer, 256);
thomasluca 13:368c6181b81e 81
thomasluca 13:368c6181b81e 82 client.recv(srbuffer, sizeof srbuffer);
thomasluca 13:368c6181b81e 83
thomasluca 13:368c6181b81e 84 uint8_t upper = srbuffer[0];
thomasluca 13:368c6181b81e 85 uint8_t lower = srbuffer[1];
thomasluca 13:368c6181b81e 86 uint8_t PWM = srbuffer[2];
thomasluca 13:368c6181b81e 87
thomasluca 13:368c6181b81e 88
thomasluca 13:368c6181b81e 89 float temp = ((upper << 8) | lower) / 256.00;
thomasluca 13:368c6181b81e 90
thomasluca 13:368c6181b81e 91 printf("Temperature is %f\n", temp);
thomasluca 13:368c6181b81e 92 printf("%d\n",PWM);
thomasluca 13:368c6181b81e 93 for (uint8_t i = 3; i < 127; i++) {
thomasluca 13:368c6181b81e 94 if (srbuffer[i] != '0') {
thomasluca 13:368c6181b81e 95 printf("%d\n", srbuffer[i]);
thomasluca 13:368c6181b81e 96 }
thomasluca 13:368c6181b81e 97
thomasluca 13:368c6181b81e 98 }
thomasluca 13:368c6181b81e 99 client.close();
thomasluca 13:368c6181b81e 100 }
thomasluca 13:368c6181b81e 101 }
thomasluca 13:368c6181b81e 102
thomasluca 13:368c6181b81e 103 void StateMachine::actionTransmit()
thomasluca 13:368c6181b81e 104 {
thomasluca 13:368c6181b81e 105
thomasluca 13:368c6181b81e 106 EthernetInterface eth;
thomasluca 13:368c6181b81e 107 eth.set_network("192.168.0.20","255.255.255.0","192.168.0.1");
thomasluca 13:368c6181b81e 108 eth.connect();
thomasluca 13:368c6181b81e 109
thomasluca 13:368c6181b81e 110 printf("The client IP address is '%s'\n\r", eth.get_ip_address());
thomasluca 13:368c6181b81e 111
thomasluca 13:368c6181b81e 112 TCPSocket socket;
thomasluca 13:368c6181b81e 113 socket.open(&eth);
thomasluca 13:368c6181b81e 114 socket.connect("192.168.0.28",4000);
thomasluca 13:368c6181b81e 115
thomasluca 13:368c6181b81e 116 if (srbuffer.size() == 0) {
thomasluca 13:368c6181b81e 117 uint16_t temperature =(tempSensor.read());
thomasluca 13:368c6181b81e 118 char upperbyte = (temperature >> 8) & 0x00FF;
thomasluca 13:368c6181b81e 119 char lowerbyte = temperature & 0x00FF;
thomasluca 13:368c6181b81e 120 char pwm = (char)(pot1);
thomasluca 13:368c6181b81e 121
thomasluca 13:368c6181b81e 122 srbuffer.push(upperbyte);
thomasluca 13:368c6181b81e 123 srbuffer.push(lowerbyte);
thomasluca 13:368c6181b81e 124 srbuffer.push(pwm);
thomasluca 13:368c6181b81e 125 srbuffer.push(ownID));
thomasluca 13:368c6181b81e 126
thomasluca 13:368c6181b81e 127 // print hex value of temperature on lcd
thomasluca 13:368c6181b81e 128 lcd.printf("Temp %.1x\n", tempSensor.read());
thomasluca 13:368c6181b81e 129 } else {
thomasluca 13:368c6181b81e 130 srbuffer.push(ownID);
thomasluca 13:368c6181b81e 131 }
thomasluca 13:368c6181b81e 132
thomasluca 13:368c6181b81e 133
thomasluca 13:368c6181b81e 134 printf(srbuffer);
thomasluca 13:368c6181b81e 135 socket.send(srbuffer, srbuffer.size());
thomasluca 13:368c6181b81e 136 socket.close();
thomasluca 13:368c6181b81e 137 }
thomasluca 13:368c6181b81e 138
thomasluca 13:368c6181b81e 139 bool StateMachine::containsID()
thomasluca 13:368c6181b81e 140 {
thomasluca 13:368c6181b81e 141 if(srbuffer.size() >= 3) {
thomasluca 13:368c6181b81e 142 for (uint8_t i = 3; i < srbuffer.size() - 1; i++) {
thomasluca 13:368c6181b81e 143 if (srbuffer[i] == ownID) {
thomasluca 13:368c6181b81e 144 return true;
thomasluca 13:368c6181b81e 145 }
thomasluca 13:368c6181b81e 146 }
thomasluca 13:368c6181b81e 147 }
thomasluca 13:368c6181b81e 148 return false;
thomasluca 13:368c6181b81e 149 }