pro_client_device
Dependencies: C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
main.cpp@20:8778deb906b2, 2017-01-12 (annotated)
- Committer:
- occle
- Date:
- Thu Jan 12 15:51:01 2017 +0000
- Revision:
- 20:8778deb906b2
- Parent:
- 18:94da9de96d54
pro_client_device
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | |
samdanbury | 6:37b6d0d56190 | 2 | #include "LM75B.h" |
samdanbury | 6:37b6d0d56190 | 3 | #include "MMA7660.h" |
samdanbury | 6:37b6d0d56190 | 4 | #include "MQTTEthernet.h" |
samdanbury | 6:37b6d0d56190 | 5 | #include "C12832.h" |
samdanbury | 6:37b6d0d56190 | 6 | #include "Arial12x12.h" |
samdanbury | 6:37b6d0d56190 | 7 | #include "rtos.h" |
samdanbury | 6:37b6d0d56190 | 8 | |
samdanbury | 6:37b6d0d56190 | 9 | |
mazgch | 11:7a6df9a2dcdc | 10 | #if defined(TARGET_UBLOX_C027) |
mazgch | 11:7a6df9a2dcdc | 11 | #warning "Compiling for mbed C027" |
mazgch | 11:7a6df9a2dcdc | 12 | #include "C027.h" |
mazgch | 11:7a6df9a2dcdc | 13 | #elif defined(TARGET_LPC1768) |
samdanbury | 6:37b6d0d56190 | 14 | #warning "Compiling for mbed LPC1768" |
samdanbury | 6:37b6d0d56190 | 15 | #include "LPC1768.h" |
samdanbury | 6:37b6d0d56190 | 16 | #elif defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 17 | #warning "Compiling for mbed K64F" |
samdanbury | 6:37b6d0d56190 | 18 | #include "K64F.h" |
samdanbury | 6:37b6d0d56190 | 19 | #endif |
samdanbury | 6:37b6d0d56190 | 20 | |
occle | 20:8778deb906b2 | 21 | /*--INITS-------------------------------------------------------------------------------*/ |
occle | 20:8778deb906b2 | 22 | EthernetInterface eth; //create ethernet |
occle | 20:8778deb906b2 | 23 | UDPSocket sock; //creat Ethernet socket |
occle | 20:8778deb906b2 | 24 | Endpoint server; //create endpoint |
occle | 20:8778deb906b2 | 25 | InterruptIn joyclick(D4); //joystick click as interrupt |
occle | 20:8778deb906b2 | 26 | AnalogIn pot1 (A0); //left potentiometer |
jsutton | 14:1f961d19f3cf | 27 | |
samdanbury | 6:37b6d0d56190 | 28 | char* joystickPos = "CENTRE"; |
samdanbury | 6:37b6d0d56190 | 29 | int blink_interval = 0; |
samdanbury | 6:37b6d0d56190 | 30 | |
occle | 20:8778deb906b2 | 31 | /*--CONSTANTS---------------------------------------------------------------------------*/ |
occle | 20:8778deb906b2 | 32 | const int PORT = 7; |
occle | 20:8778deb906b2 | 33 | static const char* SERVER_IP = "192.168.1.110"; //IP of server board |
occle | 20:8778deb906b2 | 34 | static const char* CLIENT_IP = "192.168.1.102"; //IP of client board |
occle | 20:8778deb906b2 | 35 | static const char* MASK = "255.255.255.0"; //mask |
occle | 20:8778deb906b2 | 36 | static const char* GATEWAY = "192.168.1.1"; //gateway |
jsutton | 14:1f961d19f3cf | 37 | |
occle | 20:8778deb906b2 | 38 | /*--VARIABLES---------------------------------------------------------------------------*/ |
occle | 20:8778deb906b2 | 39 | int n; //size of received message |
occle | 20:8778deb906b2 | 40 | char in_buffer[1]; //create receive buffer |
occle | 20:8778deb906b2 | 41 | char counter[1] = {0}; //sample send buffer |
occle | 20:8778deb906b2 | 42 | int menuItem = 0; |
occle | 20:8778deb906b2 | 43 | int servertemp; //temperature of the server device |
occle | 20:8778deb906b2 | 44 | float idealtemp; //desired temperature |
occle | 20:8778deb906b2 | 45 | /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/ |
occle | 20:8778deb906b2 | 46 | /*****************************************************************************INIT_ETH***/ |
occle | 20:8778deb906b2 | 47 | void init_eth(void) |
samdanbury | 6:37b6d0d56190 | 48 | { |
occle | 20:8778deb906b2 | 49 | eth.init(CLIENT_IP, MASK, GATEWAY); //set up IP |
occle | 20:8778deb906b2 | 50 | eth.connect(); //connect ethernet |
occle | 20:8778deb906b2 | 51 | |
occle | 20:8778deb906b2 | 52 | sock.init(); //initialize socket |
occle | 20:8778deb906b2 | 53 | |
occle | 20:8778deb906b2 | 54 | server.set_address(SERVER_IP, PORT); //set address of server |
occle | 20:8778deb906b2 | 55 | |
occle | 20:8778deb906b2 | 56 | } //end init_eth() |
occle | 20:8778deb906b2 | 57 | /******************************************************************************END_ETH***/ |
occle | 20:8778deb906b2 | 58 | void end_eth(void) |
samdanbury | 6:37b6d0d56190 | 59 | { |
occle | 20:8778deb906b2 | 60 | sock.close(); //close socket |
occle | 20:8778deb906b2 | 61 | eth.disconnect(); //close Ethernet |
occle | 20:8778deb906b2 | 62 | |
occle | 20:8778deb906b2 | 63 | } //end end_eth() |
samdanbury | 6:37b6d0d56190 | 64 | |
occle | 20:8778deb906b2 | 65 | /*****************************************************************************TEMP***/ |
occle | 20:8778deb906b2 | 66 | void TEMP() |
samdanbury | 6:37b6d0d56190 | 67 | { |
occle | 20:8778deb906b2 | 68 | counter[0] = (int)pot1*30; |
occle | 20:8778deb906b2 | 69 | sock.sendTo(server, counter, sizeof(counter)); //send message |
occle | 20:8778deb906b2 | 70 | |
occle | 20:8778deb906b2 | 71 | n = sock.receiveFrom(server, in_buffer, sizeof(in_buffer)); //receive message from server |
occle | 20:8778deb906b2 | 72 | in_buffer[n] = '\0'; //add \0 to end of message |
occle | 20:8778deb906b2 | 73 | servertemp = in_buffer[0]; |
samdanbury | 6:37b6d0d56190 | 74 | } |
occle | 20:8778deb906b2 | 75 | /*****************************************************************************printMenu***/ |
samdanbury | 6:37b6d0d56190 | 76 | void printMenu(int menuItem) |
samdanbury | 6:37b6d0d56190 | 77 | { |
icraggs | 18:94da9de96d54 | 78 | static char last_line1[30] = "", last_line2[30] = ""; |
icraggs | 18:94da9de96d54 | 79 | char line1[30] = "", line2[30] = ""; |
icraggs | 18:94da9de96d54 | 80 | |
samdanbury | 6:37b6d0d56190 | 81 | switch (menuItem) |
samdanbury | 6:37b6d0d56190 | 82 | { |
samdanbury | 6:37b6d0d56190 | 83 | case 0: |
occle | 20:8778deb906b2 | 84 | sprintf(line1, "Smart home system"); |
icraggs | 18:94da9de96d54 | 85 | sprintf(line2, "Scroll with joystick"); |
samdanbury | 6:37b6d0d56190 | 86 | break; |
samdanbury | 6:37b6d0d56190 | 87 | case 1: |
occle | 20:8778deb906b2 | 88 | sprintf(line1, "Use left dial to"); |
occle | 20:8778deb906b2 | 89 | sprintf(line2, "set ideal temperature"); |
samdanbury | 6:37b6d0d56190 | 90 | break; |
samdanbury | 6:37b6d0d56190 | 91 | case 2: |
occle | 20:8778deb906b2 | 92 | sprintf(line1, "Ideal temperature"); |
occle | 20:8778deb906b2 | 93 | sprintf(line2, "set to = %.0f", (float)pot1*30); |
samdanbury | 6:37b6d0d56190 | 94 | break; |
samdanbury | 6:37b6d0d56190 | 95 | case 3: |
occle | 20:8778deb906b2 | 96 | sprintf(line1, "Temperature = %d", servertemp); |
occle | 20:8778deb906b2 | 97 | if(servertemp <(float)pot1*30) |
occle | 20:8778deb906b2 | 98 | sprintf(line2, "Heating ON"); |
icraggs | 16:2420bfbf5f1c | 99 | else |
occle | 20:8778deb906b2 | 100 | sprintf(line2, "Heating OFF"); |
samdanbury | 6:37b6d0d56190 | 101 | break; |
occle | 20:8778deb906b2 | 102 | |
samdanbury | 6:37b6d0d56190 | 103 | } |
icraggs | 18:94da9de96d54 | 104 | |
icraggs | 18:94da9de96d54 | 105 | if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0) |
icraggs | 18:94da9de96d54 | 106 | { |
icraggs | 18:94da9de96d54 | 107 | lcd.cls(); |
icraggs | 18:94da9de96d54 | 108 | lcd.locate(0, 0); |
icraggs | 18:94da9de96d54 | 109 | lcd.printf(line1); |
icraggs | 18:94da9de96d54 | 110 | strncpy(last_line1, line1, sizeof(last_line1)); |
icraggs | 18:94da9de96d54 | 111 | |
icraggs | 18:94da9de96d54 | 112 | lcd.locate(0,16); |
icraggs | 18:94da9de96d54 | 113 | lcd.printf(line2); |
icraggs | 18:94da9de96d54 | 114 | strncpy(last_line2, line2, sizeof(last_line2)); |
icraggs | 18:94da9de96d54 | 115 | } |
samdanbury | 6:37b6d0d56190 | 116 | } |
samdanbury | 6:37b6d0d56190 | 117 | |
occle | 20:8778deb906b2 | 118 | /*****************************************************************************setMenu***/ |
samdanbury | 6:37b6d0d56190 | 119 | void setMenu() |
samdanbury | 6:37b6d0d56190 | 120 | { |
jsutton | 14:1f961d19f3cf | 121 | |
samdanbury | 6:37b6d0d56190 | 122 | if (Down) |
samdanbury | 6:37b6d0d56190 | 123 | { |
samdanbury | 6:37b6d0d56190 | 124 | joystickPos = "DOWN"; |
occle | 20:8778deb906b2 | 125 | if (menuItem >= 0 && menuItem < 3) |
samdanbury | 6:37b6d0d56190 | 126 | printMenu(++menuItem); |
samdanbury | 6:37b6d0d56190 | 127 | } |
samdanbury | 6:37b6d0d56190 | 128 | else if (Left) |
samdanbury | 6:37b6d0d56190 | 129 | joystickPos = "LEFT"; |
samdanbury | 6:37b6d0d56190 | 130 | else if (Click) |
samdanbury | 6:37b6d0d56190 | 131 | joystickPos = "CLICK"; |
samdanbury | 6:37b6d0d56190 | 132 | else if (Up) |
samdanbury | 6:37b6d0d56190 | 133 | { |
samdanbury | 6:37b6d0d56190 | 134 | joystickPos = "UP"; |
occle | 20:8778deb906b2 | 135 | if (menuItem <= 3 && menuItem > 0) |
samdanbury | 6:37b6d0d56190 | 136 | printMenu(--menuItem); |
samdanbury | 6:37b6d0d56190 | 137 | } |
samdanbury | 6:37b6d0d56190 | 138 | else if (Right) |
samdanbury | 6:37b6d0d56190 | 139 | joystickPos = "RIGHT"; |
samdanbury | 6:37b6d0d56190 | 140 | else |
samdanbury | 6:37b6d0d56190 | 141 | joystickPos = "CENTRE"; |
samdanbury | 6:37b6d0d56190 | 142 | } |
samdanbury | 6:37b6d0d56190 | 143 | |
occle | 20:8778deb906b2 | 144 | /*****************************************************************************menu_loop***/ |
jsutton | 13:85801e3b83d3 | 145 | void menu_loop(void const *args) |
jsutton | 13:85801e3b83d3 | 146 | { |
jsutton | 14:1f961d19f3cf | 147 | int count = 0; |
icraggs | 16:2420bfbf5f1c | 148 | while(true) |
icraggs | 16:2420bfbf5f1c | 149 | { |
jsutton | 13:85801e3b83d3 | 150 | setMenu(); |
icraggs | 16:2420bfbf5f1c | 151 | if (++count % 10 == 0) |
jsutton | 14:1f961d19f3cf | 152 | printMenu(menuItem); |
jsutton | 14:1f961d19f3cf | 153 | Thread::wait(100); |
jsutton | 13:85801e3b83d3 | 154 | } |
jsutton | 13:85801e3b83d3 | 155 | } |
jsutton | 13:85801e3b83d3 | 156 | |
samdanbury | 6:37b6d0d56190 | 157 | |
samdanbury | 6:37b6d0d56190 | 158 | /** |
samdanbury | 6:37b6d0d56190 | 159 | * Display a message on the LCD screen prefixed with IBM IoT Cloud |
samdanbury | 6:37b6d0d56190 | 160 | */ |
samdanbury | 6:37b6d0d56190 | 161 | void displayMessage(char* message) |
samdanbury | 6:37b6d0d56190 | 162 | { |
samdanbury | 6:37b6d0d56190 | 163 | lcd.cls(); |
samdanbury | 6:37b6d0d56190 | 164 | lcd.locate(0,0); |
occle | 20:8778deb906b2 | 165 | lcd.printf("Welcome"); |
samdanbury | 6:37b6d0d56190 | 166 | lcd.locate(0,16); |
samdanbury | 6:37b6d0d56190 | 167 | lcd.printf(message); |
samdanbury | 6:37b6d0d56190 | 168 | } |
samdanbury | 6:37b6d0d56190 | 169 | |
samdanbury | 6:37b6d0d56190 | 170 | |
samdanbury | 6:37b6d0d56190 | 171 | |
samdanbury | 6:37b6d0d56190 | 172 | int main() |
samdanbury | 6:37b6d0d56190 | 173 | { |
samdanbury | 6:37b6d0d56190 | 174 | lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen |
samdanbury | 6:37b6d0d56190 | 175 | |
samdanbury | 6:37b6d0d56190 | 176 | led2 = LED2_OFF; // K64F: turn off the main board LED |
samdanbury | 6:37b6d0d56190 | 177 | |
jsutton | 13:85801e3b83d3 | 178 | Thread menu_thread(menu_loop); |
jsutton | 13:85801e3b83d3 | 179 | |
occle | 20:8778deb906b2 | 180 | |
icraggs | 16:2420bfbf5f1c | 181 | |
icraggs | 16:2420bfbf5f1c | 182 | |
samdanbury | 6:37b6d0d56190 | 183 | int count = 0; |
samdanbury | 6:37b6d0d56190 | 184 | while (true) |
samdanbury | 6:37b6d0d56190 | 185 | { |
samdanbury | 6:37b6d0d56190 | 186 | if (++count == 100) |
samdanbury | 6:37b6d0d56190 | 187 | { // Publish a message every second |
occle | 20:8778deb906b2 | 188 | |
samdanbury | 6:37b6d0d56190 | 189 | |
samdanbury | 6:37b6d0d56190 | 190 | if (blink_interval == 0) |
samdanbury | 6:37b6d0d56190 | 191 | led2 = LED2_OFF; |
samdanbury | 6:37b6d0d56190 | 192 | else if (count % blink_interval == 0) |
samdanbury | 6:37b6d0d56190 | 193 | led2 = !led2; |
occle | 20:8778deb906b2 | 194 | |
occle | 20:8778deb906b2 | 195 | TEMP(); |
occle | 20:8778deb906b2 | 196 | idealtemp=(float)pot1*30; |
occle | 20:8778deb906b2 | 197 | } |
samdanbury | 6:37b6d0d56190 | 198 | } |
samdanbury | 6:37b6d0d56190 | 199 | } |