-
Dependencies: EthernetInterfaceWithHostname HygroClip2-LPC mbed-rtos mbed
Fork of TCPSocket_HelloWorld by
Diff: main.cpp
- Revision:
- 17:85a4dfbe02cb
- Parent:
- 11:59dcefdda506
- Child:
- 19:d49dacbf64de
--- a/main.cpp Tue Oct 20 02:03:15 2015 +0000 +++ b/main.cpp Mon May 02 14:09:38 2016 +0000 @@ -1,31 +1,91 @@ #include "mbed.h" +#include "rtos.h" #include "EthernetInterface.h" +#include "HygroClip2.h" + +EthernetInterface eth; +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +typedef struct { + float temperature; + float humidity; + float dewpoint; +} SensorData; + +Mail<SensorData, 32> sensorMailBox; + +void network(void const * arg) +{ + while(true) + { + osEvent event = sensorMailBox.get(); + if (event.status == osEventMail) + { + led3 = 1; + SensorData * data = static_cast<SensorData*>(event.value.p); + + TCPSocketConnection sock; + int connected = sock.connect("192.168.100.100", 1337); + if (connected == 0) + { + char transmissionBuffer[300]; + int lenght = snprintf(transmissionBuffer, 200, "GET /relay?temperature=%.2f&humidity=%.2f&dewpoint=%.2f&id=1 HTTP/1.0\n\n", data->temperature, data->humidity,data->dewpoint); + printf(transmissionBuffer); + sock.send_all(transmissionBuffer, lenght); + } + sock.close(); + + sensorMailBox.free(data); + led3 = 0; + } + } +} int main() { - EthernetInterface eth; - eth.init(); //Use DHCP - eth.connect(); - printf("IP Address is %s\n", eth.getIPAddress()); + // Start ethernet module + int initOK = eth.init(); + if (initOK != 0) + { + while(true); + } + - TCPSocketConnection sock; - sock.connect("mbed.org", 80); + // Connect to network + int connectOK = eth.connect(); + if (connectOK != 0) + { + while(true); + } + led1 = 1; - char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; - sock.send_all(http_cmd, sizeof(http_cmd)-1); + // Create and start ethernet task + Thread transmissionTask(network); + HygroClip2 sensor(p9,p10); - char buffer[300]; - int ret; - while (true) { - ret = sock.receive(buffer, sizeof(buffer)-1); - if (ret <= 0) - break; - buffer[ret] = '\0'; - printf("Received %d chars from server:\n%s\n", ret, buffer); + while(!sensor.isDataValid()) + { + sensor.update(); + Thread::wait(500); } - - sock.close(); + led2 = 1; - eth.disconnect(); - - while(1) {} + while(true) + { + led4 = 1; + if (sensor.isDataValid()) + { + SensorData * data = sensorMailBox.alloc(); + data->temperature = sensor.getTemperature(); + data->humidity = sensor.getHumidity(); + data->dewpoint = sensor.getDewPoint(); + sensorMailBox.put(data); + } + sensor.update(); + Thread::wait(50); + led4 = 0; + Thread::wait(9950); + } }