Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface LM75B mbed-rtos mbed
Diff: Slave.cpp
- Revision:
- 1:db9b9bec0133
- Child:
- 2:ecc0c5c14bc1
diff -r 29eeb74e55c6 -r db9b9bec0133 Slave.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Slave.cpp Wed Mar 14 14:38:43 2018 +0000 @@ -0,0 +1,125 @@ +#include "slave.h" +#include "string.h" +#include "temperature.h" +#include "potentiometer.h" +#include "lcdcontrol.h" +#include "led.h" +#include "buzzer.h" + +Temperature temperature; +Potentiometer potentiometer; +LcdControl screen; +Led led; +Buzzer buzzer; + +Slave::Slave() + :answer() +{ + //Constructor +} + +void Slave::slaveRun(const char* ip) +{ + EthernetInterface ethernet; + ethernet.init(ip,"255.255.255.0", "192.168.0.1"); + ethernet.connect(); + printf("Your ip is: %s", ethernet.getIPAddress()); + + UDPSocket slave; + slave.bind(4000); + Endpoint master; + + while(1){ + char buffer[512] = {0}; + printf("\nWaiting for UDP packet...\n\r"); + + int n = slave.receiveFrom(master, buffer, sizeof(buffer)); + buffer[n] = '\0'; + + printf("Received packet from: %s\n\r", master.get_address()); + printf("Received: '%s'\r\n", buffer); + actionSlave(buffer); + + slave.sendTo(master, answer, 50); + answerReset(); + } +} + +void Slave::actionSlave(const char* bufferke){ + char* command = strtok((char*)bufferke, " "); + char* action = strtok(NULL, " "); + if(strcmp(command, "GET") == 0) + { + getRequest(action); + } + else if(strcmp(command, "PUT") == 0) + { + putRequest(action); + } + else + { + answerAppend("ACK 4.0 ID"); + } +} + +void Slave::getRequest(char* action) +{ + char temp[8]; + if(strcmp(action, "/temperature") == 0) + { + sprintf(temp, "%.3f", temperature.getTemperature()); + answerAppend("ACK 2.05 ID "); + answerAppend(temp); + } + else + if(strcmp(action, "/potentiometer") == 0) + { + sprintf(temp, "%.0f", potentiometer.getPotentiometer()*255); + answerAppend("ACK 2.05 ID "); + answerAppend(temp); + } + else + { + answerAppend("ACK 4.0 ID"); + } +} + +void Slave::putRequest(char* action) +{ + if(strcmp(action, "/lcd") == 0) + { + answerAppend("ACK 2.04 ID"); + screen.lcdSlave(strtok(NULL, "\0")); + } + else + if(strcmp(action, "/led") == 0) + { + answerAppend("ACK 2.04 ID"); + led.setColor(strtok(NULL, " ")); + } + else + if(strcmp(action, "/buzzer") == 0) + { + answerAppend("ACK 2.04 ID"); + int one = atoi(strtok(NULL, " -")); + int two = atoi(strtok(NULL, " -")); + printf("%d %d", one,two); + buzzer.playBuzzer(one, two); + } + else + { + answerAppend("ACK 4.0 ID"); + } +} + +void Slave::answerAppend(char* append) +{ + strcat(answer, append); +} + +void Slave::answerReset(){ + for(int i = 0; i < 512; i++) + { + answer[i] = NULL; + } +} \ No newline at end of file