test
Dependencies: BH1750 DHT DS1820 W5500Interface MQ135 NTPClient SENSOR WebSocketClient mbed
main.cpp@2:31b004b66644, 2016-09-19 (annotated)
- Committer:
- readysteadygo2006
- Date:
- Mon Sep 19 18:53:09 2016 +0000
- Revision:
- 2:31b004b66644
- Parent:
- 0:7b09d7308cf7
Updated with w5500
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
readysteadygo2006 | 0:7b09d7308cf7 | 1 | #include "mbed.h" |
readysteadygo2006 | 0:7b09d7308cf7 | 2 | #include "SENSOR.h" |
readysteadygo2006 | 0:7b09d7308cf7 | 3 | |
readysteadygo2006 | 0:7b09d7308cf7 | 4 | #include "HTTPClient.h" |
readysteadygo2006 | 0:7b09d7308cf7 | 5 | #include "ESP8266Interface.h" |
readysteadygo2006 | 0:7b09d7308cf7 | 6 | #include "TCPSocketConnection.h" |
readysteadygo2006 | 0:7b09d7308cf7 | 7 | #include "Websocket.h" |
readysteadygo2006 | 0:7b09d7308cf7 | 8 | //#include "NTPClient.h" |
readysteadygo2006 | 0:7b09d7308cf7 | 9 | |
readysteadygo2006 | 0:7b09d7308cf7 | 10 | #define SENSOR_ARRAY_SIZE 6 |
readysteadygo2006 | 0:7b09d7308cf7 | 11 | |
readysteadygo2006 | 0:7b09d7308cf7 | 12 | SENSOR *sensor_array[SENSOR_ARRAY_SIZE]; |
readysteadygo2006 | 0:7b09d7308cf7 | 13 | Serial pc(USBTX, USBRX); // tx, rx |
readysteadygo2006 | 0:7b09d7308cf7 | 14 | ESP8266Interface wifi(PA_11,PA_12,PB_12,"EE-mehrg7","MFPHOME0708",115200); // TX,RX,Reset,SSID,Password,Baud |
readysteadygo2006 | 0:7b09d7308cf7 | 15 | //NTPClient ntp; |
readysteadygo2006 | 0:7b09d7308cf7 | 16 | |
readysteadygo2006 | 0:7b09d7308cf7 | 17 | |
readysteadygo2006 | 0:7b09d7308cf7 | 18 | int main() { |
readysteadygo2006 | 0:7b09d7308cf7 | 19 | pc.baud(921600); |
readysteadygo2006 | 0:7b09d7308cf7 | 20 | |
readysteadygo2006 | 2:31b004b66644 | 21 | #if defined(TARGET_NUCLEO_F401RE) |
readysteadygo2006 | 2:31b004b66644 | 22 | SPI spi(PB_15, PB_14, PB_13); // mosi, miso, sclk |
readysteadygo2006 | 2:31b004b66644 | 23 | EthernetInterface eth(&spi, PB_1, PB_2); // spi, cs, reset |
readysteadygo2006 | 2:31b004b66644 | 24 | #warning "setup for F401RE" |
readysteadygo2006 | 2:31b004b66644 | 25 | #endif |
readysteadygo2006 | 0:7b09d7308cf7 | 26 | |
readysteadygo2006 | 0:7b09d7308cf7 | 27 | //Create sensors |
readysteadygo2006 | 0:7b09d7308cf7 | 28 | // pc.printf("Creating sensor 0\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 29 | sensor_array[0] = new SENSOR( PB_9, PB_8, NC, NC, PA_0, PA_9); //I2C1,A0 |
readysteadygo2006 | 0:7b09d7308cf7 | 30 | // pc.printf("Creating sensor 1\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 31 | sensor_array[1] = new SENSOR( PB_3, PB_10, NC, NC, PA_1, PC_7); //I2C2,A1 |
readysteadygo2006 | 0:7b09d7308cf7 | 32 | // pc.printf("Creating sensor 2\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 33 | sensor_array[2] = new SENSOR( PB_4, PA_8, NC, NC, PA_4, PB_6); //I2C3,A2 |
readysteadygo2006 | 0:7b09d7308cf7 | 34 | // pc.printf("Creating sensor 3\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 35 | sensor_array[3] = new SENSOR( PA_3, NC, NC, NC, PB_0, PA_7); //GPIO,A3 |
readysteadygo2006 | 0:7b09d7308cf7 | 36 | // pc.printf("Creating sensor 4\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 37 | sensor_array[4] = new SENSOR( PA_2, NC, NC, NC, PC_1, PA_6); //GPIO,A4 |
readysteadygo2006 | 0:7b09d7308cf7 | 38 | // pc.printf("Creating sensor 5\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 39 | sensor_array[5] = new SENSOR( PA_10, NC, NC, NC, PC_0, PA_5); //GPIO,A5 |
readysteadygo2006 | 0:7b09d7308cf7 | 40 | |
readysteadygo2006 | 0:7b09d7308cf7 | 41 | //Statically connect sensors |
readysteadygo2006 | 0:7b09d7308cf7 | 42 | // pc.printf("Connecting sensor 0\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 43 | // sensor_array[0]->detectSensor(BH1750_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 44 | sensor_array[0]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 45 | // pc.printf("Connecting sensor 1\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 46 | sensor_array[1]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 47 | // sensor_array[1]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 48 | // pc.printf("Connecting sensor 2\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 49 | sensor_array[2]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 50 | // sensor_array[2]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 51 | // pc.printf("Connecting sensor 3\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 52 | // sensor_array[3]->detectSensor(MQ135_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 53 | sensor_array[3]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 54 | // pc.printf("Connecting sensor 4\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 55 | sensor_array[4]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 56 | // sensor_array[4]->detectSensor(ML8511_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 57 | // pc.printf("Connecting sensor 5\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 58 | // sensor_array[5]->detectSensor(DHT11_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 59 | sensor_array[5]->detectSensor(NO_SENSOR); |
readysteadygo2006 | 0:7b09d7308cf7 | 60 | |
readysteadygo2006 | 0:7b09d7308cf7 | 61 | string sensor_reading; |
readysteadygo2006 | 0:7b09d7308cf7 | 62 | string sensor_logentry; |
readysteadygo2006 | 0:7b09d7308cf7 | 63 | sensorStatus sensor_status; |
readysteadygo2006 | 0:7b09d7308cf7 | 64 | sensorType sensor_type; |
readysteadygo2006 | 0:7b09d7308cf7 | 65 | |
readysteadygo2006 | 2:31b004b66644 | 66 | pc.printf("pre init eth\r\n", eth.getMACAddress()); |
readysteadygo2006 | 2:31b004b66644 | 67 | eth.init("192.168.1.202", "255.255.255.0", "192.168.1.254"); //Use DHCP |
readysteadygo2006 | 2:31b004b66644 | 68 | pc.printf("Initialized, MAC: %s\r\n", eth.getMACAddress()); |
readysteadygo2006 | 2:31b004b66644 | 69 | eth.connect(); |
readysteadygo2006 | 2:31b004b66644 | 70 | pc.printf("Connected, IP: %s, MASK: %s, GW: %s\r\n",eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
readysteadygo2006 | 0:7b09d7308cf7 | 71 | |
readysteadygo2006 | 0:7b09d7308cf7 | 72 | /* |
readysteadygo2006 | 0:7b09d7308cf7 | 73 | if (ntp.setTime("0.uk.pool.ntp.org") == 0) |
readysteadygo2006 | 0:7b09d7308cf7 | 74 | { |
readysteadygo2006 | 0:7b09d7308cf7 | 75 | time_t ctTime; |
readysteadygo2006 | 0:7b09d7308cf7 | 76 | ctTime = time(NULL); |
readysteadygo2006 | 0:7b09d7308cf7 | 77 | printf("Time is set to : %s \r\n", ctime(&ctTime)); |
readysteadygo2006 | 0:7b09d7308cf7 | 78 | } |
readysteadygo2006 | 0:7b09d7308cf7 | 79 | else |
readysteadygo2006 | 0:7b09d7308cf7 | 80 | { |
readysteadygo2006 | 0:7b09d7308cf7 | 81 | printf("Error getting time \r\n"); |
readysteadygo2006 | 0:7b09d7308cf7 | 82 | } |
readysteadygo2006 | 0:7b09d7308cf7 | 83 | wait(2); |
readysteadygo2006 | 0:7b09d7308cf7 | 84 | */ |
readysteadygo2006 | 0:7b09d7308cf7 | 85 | |
readysteadygo2006 | 0:7b09d7308cf7 | 86 | pc.printf("trying to connect to ws://192.168.1.84:8000/\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 87 | Websocket ws("ws://192.168.1.84:8000/"); |
readysteadygo2006 | 0:7b09d7308cf7 | 88 | |
readysteadygo2006 | 0:7b09d7308cf7 | 89 | if(!ws.connect()) { |
readysteadygo2006 | 0:7b09d7308cf7 | 90 | pc.printf("Failed to connect to ws://192.168.1.84:8000/\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 91 | } else { |
readysteadygo2006 | 0:7b09d7308cf7 | 92 | pc.printf("Starting reading sensor array\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 93 | |
readysteadygo2006 | 0:7b09d7308cf7 | 94 | while(1) { |
readysteadygo2006 | 0:7b09d7308cf7 | 95 | for(int sensor_index = 0; sensor_index < SENSOR_ARRAY_SIZE; sensor_index++) { |
readysteadygo2006 | 0:7b09d7308cf7 | 96 | pc.printf("Reading sensor %d\n\r", sensor_index); |
readysteadygo2006 | 0:7b09d7308cf7 | 97 | sensor_type = sensor_array[sensor_index]->getSensorType(); |
readysteadygo2006 | 0:7b09d7308cf7 | 98 | pc.printf("Sensor type: %d\n\r", sensor_type); |
readysteadygo2006 | 0:7b09d7308cf7 | 99 | sensor_status = sensor_array[sensor_index]->readSensor(sensor_reading); |
readysteadygo2006 | 0:7b09d7308cf7 | 100 | pc.printf("Sensor status: %d\n\r", sensor_status); |
readysteadygo2006 | 0:7b09d7308cf7 | 101 | switch (sensor_status) { |
readysteadygo2006 | 0:7b09d7308cf7 | 102 | case SENSOR_SUCCESS: |
readysteadygo2006 | 0:7b09d7308cf7 | 103 | pc.printf("Reading sensor(%d) of type(%d), status(%d): %s\n\r", sensor_index, (int)sensor_type, (int)sensor_status, sensor_reading.c_str()); |
readysteadygo2006 | 0:7b09d7308cf7 | 104 | break; |
readysteadygo2006 | 0:7b09d7308cf7 | 105 | case SENSOR_UNCONNECTED: |
readysteadygo2006 | 0:7b09d7308cf7 | 106 | break; |
readysteadygo2006 | 0:7b09d7308cf7 | 107 | default: |
readysteadygo2006 | 0:7b09d7308cf7 | 108 | pc.printf("Problem reading (%d) of type(%d), status(%d): %s\n\r", sensor_index, (int)sensor_type, (int)sensor_status, sensor_reading.c_str()); |
readysteadygo2006 | 0:7b09d7308cf7 | 109 | break; |
readysteadygo2006 | 0:7b09d7308cf7 | 110 | break; |
readysteadygo2006 | 0:7b09d7308cf7 | 111 | } |
readysteadygo2006 | 0:7b09d7308cf7 | 112 | } |
readysteadygo2006 | 0:7b09d7308cf7 | 113 | pc.printf("send: %s\n\r", sensor_reading.c_str()); |
readysteadygo2006 | 0:7b09d7308cf7 | 114 | char msg[1000]; |
readysteadygo2006 | 0:7b09d7308cf7 | 115 | strcpy(msg, sensor_reading.c_str()); |
readysteadygo2006 | 0:7b09d7308cf7 | 116 | ws.send(msg); |
readysteadygo2006 | 0:7b09d7308cf7 | 117 | pc.printf("\n\r\n\r"); |
readysteadygo2006 | 0:7b09d7308cf7 | 118 | wait(1); |
readysteadygo2006 | 0:7b09d7308cf7 | 119 | } |
readysteadygo2006 | 0:7b09d7308cf7 | 120 | } |
readysteadygo2006 | 0:7b09d7308cf7 | 121 | |
readysteadygo2006 | 0:7b09d7308cf7 | 122 | |
readysteadygo2006 | 0:7b09d7308cf7 | 123 | ws.close(); |
readysteadygo2006 | 0:7b09d7308cf7 | 124 | |
readysteadygo2006 | 0:7b09d7308cf7 | 125 | while(true) {} |
readysteadygo2006 | 0:7b09d7308cf7 | 126 | |
readysteadygo2006 | 0:7b09d7308cf7 | 127 | } |