Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
src/main.cpp@7:2c5d0dbd7985, 2014-07-01 (annotated)
- Committer:
- rajathishere
- Date:
- Tue Jul 01 08:22:36 2014 +0000
- Revision:
- 7:2c5d0dbd7985
- Parent:
- 6:a022f983f94b
- Child:
- 8:e58e10ca4352
Publish working with hardcode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 0:6276e9f72327 | 1 | /******************************************************************************* |
samdanbury | 0:6276e9f72327 | 2 | * Copyright (c) 2014 IBM Corporation and other Contributors. |
samdanbury | 0:6276e9f72327 | 3 | * |
samdanbury | 0:6276e9f72327 | 4 | * All rights reserved. This program and the accompanying materials |
samdanbury | 0:6276e9f72327 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
samdanbury | 0:6276e9f72327 | 6 | * which accompanies this distribution, and is available at |
samdanbury | 0:6276e9f72327 | 7 | * http://www.eclipse.org/legal/epl-v10.html |
samdanbury | 0:6276e9f72327 | 8 | * |
samdanbury | 0:6276e9f72327 | 9 | * Contributors: Sam Danbury |
samdanbury | 0:6276e9f72327 | 10 | * IBM - Initial Contribution |
samdanbury | 0:6276e9f72327 | 11 | *******************************************************************************/ |
samdanbury | 0:6276e9f72327 | 12 | |
samdanbury | 0:6276e9f72327 | 13 | #include "stdio.h" |
samdanbury | 0:6276e9f72327 | 14 | #include "mbed.h" |
samdanbury | 0:6276e9f72327 | 15 | #include "rtos.h" |
samdanbury | 0:6276e9f72327 | 16 | #include "EthernetInterface.h" |
samdanbury | 0:6276e9f72327 | 17 | #include "C12832_lcd.h" |
samdanbury | 0:6276e9f72327 | 18 | #include "LM75B.h" |
samdanbury | 0:6276e9f72327 | 19 | #include "MMA7660.h" |
samdanbury | 0:6276e9f72327 | 20 | #include "C12832_lcd.h" |
samdanbury | 0:6276e9f72327 | 21 | #include "QuickstartClient.h" |
samdanbury | 0:6276e9f72327 | 22 | #include "QuickstartMessage.h" |
samdanbury | 0:6276e9f72327 | 23 | |
samdanbury | 0:6276e9f72327 | 24 | #include <string> |
samdanbury | 0:6276e9f72327 | 25 | |
samdanbury | 0:6276e9f72327 | 26 | using namespace std; |
samdanbury | 0:6276e9f72327 | 27 | |
samdanbury | 0:6276e9f72327 | 28 | //LCD |
samdanbury | 0:6276e9f72327 | 29 | C12832_LCD lcd; |
samdanbury | 0:6276e9f72327 | 30 | |
samdanbury | 0:6276e9f72327 | 31 | //LED |
samdanbury | 0:6276e9f72327 | 32 | DigitalOut led1(LED1); |
samdanbury | 0:6276e9f72327 | 33 | |
samdanbury | 0:6276e9f72327 | 34 | //Accelerometer |
samdanbury | 0:6276e9f72327 | 35 | MMA7660 MMA(p28, p27); |
samdanbury | 0:6276e9f72327 | 36 | |
samdanbury | 0:6276e9f72327 | 37 | //Temperature sensor |
samdanbury | 0:6276e9f72327 | 38 | LM75B tmp(p28, p27); |
samdanbury | 0:6276e9f72327 | 39 | |
samdanbury | 0:6276e9f72327 | 40 | //Joystick |
samdanbury | 0:6276e9f72327 | 41 | BusIn Down(p12); |
samdanbury | 0:6276e9f72327 | 42 | BusIn Left(p13); |
samdanbury | 0:6276e9f72327 | 43 | BusOut Click(p14); |
samdanbury | 0:6276e9f72327 | 44 | BusIn Up(p15); |
samdanbury | 0:6276e9f72327 | 45 | BusIn Right(p16); |
samdanbury | 0:6276e9f72327 | 46 | string joystickPos; |
samdanbury | 0:6276e9f72327 | 47 | void joystickThread(void const *args); |
samdanbury | 0:6276e9f72327 | 48 | |
samdanbury | 0:6276e9f72327 | 49 | //Potentiometer |
samdanbury | 0:6276e9f72327 | 50 | AnalogIn ain1(p19); |
samdanbury | 0:6276e9f72327 | 51 | AnalogIn ain2(p20); |
samdanbury | 0:6276e9f72327 | 52 | float pot1; |
samdanbury | 0:6276e9f72327 | 53 | float pot2; |
samdanbury | 0:6276e9f72327 | 54 | |
samdanbury | 2:25ddff75a8c7 | 55 | LocalFileSystem local("local"); |
samdanbury | 2:25ddff75a8c7 | 56 | |
samdanbury | 0:6276e9f72327 | 57 | int main() |
samdanbury | 0:6276e9f72327 | 58 | { |
rajathishere | 1:dd948ce80918 | 59 | //RAJATH's COMMIT |
samdanbury | 0:6276e9f72327 | 60 | //Connect to the network |
samdanbury | 0:6276e9f72327 | 61 | EthernetInterface eth; |
samdanbury | 0:6276e9f72327 | 62 | eth.init(); |
samdanbury | 0:6276e9f72327 | 63 | eth.connect(); |
samdanbury | 0:6276e9f72327 | 64 | |
samdanbury | 0:6276e9f72327 | 65 | //Obtain mac address of mbed |
samdanbury | 0:6276e9f72327 | 66 | string mac = eth.getMACAddress(); |
samdanbury | 0:6276e9f72327 | 67 | |
samdanbury | 0:6276e9f72327 | 68 | //Remove colons from mac address |
samdanbury | 0:6276e9f72327 | 69 | mac.erase(remove(mac.begin(), mac.end(), ':'), mac.end()); |
samdanbury | 0:6276e9f72327 | 70 | |
samdanbury | 0:6276e9f72327 | 71 | //Start thread to read data from joystick |
samdanbury | 0:6276e9f72327 | 72 | Thread jThd(joystickThread); |
samdanbury | 0:6276e9f72327 | 73 | joystickPos = "CENTRE"; |
samdanbury | 0:6276e9f72327 | 74 | |
samdanbury | 0:6276e9f72327 | 75 | QuickstartClient* c = new QuickstartClient(mac); |
rajathishere | 6:a022f983f94b | 76 | lcd.cls(); |
rajathishere | 6:a022f983f94b | 77 | lcd.locate(0,0); |
rajathishere | 6:a022f983f94b | 78 | |
rajathishere | 6:a022f983f94b | 79 | lcd.printf("Mac address: %s\n", mac); |
rajathishere | 6:a022f983f94b | 80 | wait(3.0); |
samdanbury | 0:6276e9f72327 | 81 | |
rajathishere | 7:2c5d0dbd7985 | 82 | //c->subscribe(); |
rajathishere | 7:2c5d0dbd7985 | 83 | char value[10]; |
rajathishere | 7:2c5d0dbd7985 | 84 | //string data_points = ""; |
samdanbury | 0:6276e9f72327 | 85 | while(1) |
samdanbury | 0:6276e9f72327 | 86 | { |
samdanbury | 0:6276e9f72327 | 87 | //Initialize lcd |
samdanbury | 0:6276e9f72327 | 88 | //Flash led to show message has been sent successfully |
samdanbury | 0:6276e9f72327 | 89 | led1 = 1; |
rajathishere | 7:2c5d0dbd7985 | 90 | string data_points = "\"myName\": \"IoT mbed\""; |
samdanbury | 0:6276e9f72327 | 91 | //Construct quickstart message with desired datapoints |
rajathishere | 6:a022f983f94b | 92 | //QuickstartMessage* m = new QuickstartMessage(); |
rajathishere | 6:a022f983f94b | 93 | |
rajathishere | 6:a022f983f94b | 94 | //m->add("accelX", MMA.x()); |
rajathishere | 7:2c5d0dbd7985 | 95 | sprintf(value, "%0.4f", MMA.x()); |
rajathishere | 7:2c5d0dbd7985 | 96 | data_points+=",\"accelX\":"; |
rajathishere | 7:2c5d0dbd7985 | 97 | data_points+=value; |
rajathishere | 6:a022f983f94b | 98 | //m->add("accelY", MMA.y()); |
rajathishere | 7:2c5d0dbd7985 | 99 | sprintf(value, "%0.4f", MMA.y()); |
rajathishere | 7:2c5d0dbd7985 | 100 | data_points+=",\"accelY\":"; |
rajathishere | 7:2c5d0dbd7985 | 101 | data_points+=value; |
rajathishere | 6:a022f983f94b | 102 | //m->add("accelZ", MMA.z()); |
rajathishere | 7:2c5d0dbd7985 | 103 | sprintf(value, "%0.4f", MMA.z()); |
rajathishere | 7:2c5d0dbd7985 | 104 | data_points+=",\"accelZ\":"; |
rajathishere | 7:2c5d0dbd7985 | 105 | data_points+=value; |
rajathishere | 6:a022f983f94b | 106 | //m->add("temp", tmp.read()); |
rajathishere | 7:2c5d0dbd7985 | 107 | sprintf(value, "%0.4f", tmp.read()); |
rajathishere | 7:2c5d0dbd7985 | 108 | data_points+=",\"temp\":"; |
rajathishere | 7:2c5d0dbd7985 | 109 | data_points+=value; |
rajathishere | 6:a022f983f94b | 110 | //m->add("joystick", joystickPos); |
rajathishere | 7:2c5d0dbd7985 | 111 | data_points+=",\"joystick\":" + joystickPos; |
rajathishere | 7:2c5d0dbd7985 | 112 | pot1 = ain1; |
rajathishere | 7:2c5d0dbd7985 | 113 | pot2 = ain2; |
rajathishere | 6:a022f983f94b | 114 | //m->add("potentiometer1", pot1); |
rajathishere | 7:2c5d0dbd7985 | 115 | sprintf(value, "%0.4f", pot1); |
rajathishere | 7:2c5d0dbd7985 | 116 | data_points+=",\"potentiometer1\":"; |
rajathishere | 7:2c5d0dbd7985 | 117 | data_points+=value; |
rajathishere | 6:a022f983f94b | 118 | //m->add("potentiometer2", pot2); |
rajathishere | 7:2c5d0dbd7985 | 119 | sprintf(value, "%0.4f", pot1); |
rajathishere | 7:2c5d0dbd7985 | 120 | data_points+=",\"potentiometer1\":"; |
rajathishere | 7:2c5d0dbd7985 | 121 | data_points+=value; |
rajathishere | 7:2c5d0dbd7985 | 122 | |
rajathishere | 7:2c5d0dbd7985 | 123 | string message = "{\"d\":{" + data_points + "}}"; |
samdanbury | 0:6276e9f72327 | 124 | |
samdanbury | 0:6276e9f72327 | 125 | //Message is converted from datapoints into json format and then published |
rajathishere | 7:2c5d0dbd7985 | 126 | c->publish(message);//c->publish(m->convertToJson()); |
rajathishere | 6:a022f983f94b | 127 | lcd.printf("After"); |
rajathishere | 6:a022f983f94b | 128 | wait(3.0); |
rajathishere | 6:a022f983f94b | 129 | //if (m) { |
rajathishere | 6:a022f983f94b | 130 | // delete m; |
rajathishere | 6:a022f983f94b | 131 | //} |
samdanbury | 0:6276e9f72327 | 132 | |
samdanbury | 0:6276e9f72327 | 133 | led1 = 0; |
samdanbury | 0:6276e9f72327 | 134 | |
samdanbury | 0:6276e9f72327 | 135 | //Message published every second |
samdanbury | 0:6276e9f72327 | 136 | wait(1); |
samdanbury | 0:6276e9f72327 | 137 | } |
samdanbury | 0:6276e9f72327 | 138 | |
samdanbury | 0:6276e9f72327 | 139 | eth.disconnect(); |
samdanbury | 0:6276e9f72327 | 140 | } |
samdanbury | 0:6276e9f72327 | 141 | |
samdanbury | 0:6276e9f72327 | 142 | void joystickThread(void const *args) { |
samdanbury | 0:6276e9f72327 | 143 | while (1) { |
samdanbury | 0:6276e9f72327 | 144 | if (Down) |
samdanbury | 0:6276e9f72327 | 145 | joystickPos = "DOWN"; |
samdanbury | 0:6276e9f72327 | 146 | else if (Left) |
samdanbury | 0:6276e9f72327 | 147 | joystickPos = "LEFT"; |
samdanbury | 0:6276e9f72327 | 148 | else if (Click) |
samdanbury | 0:6276e9f72327 | 149 | joystickPos = "CLICK"; |
samdanbury | 0:6276e9f72327 | 150 | else if (Up) |
samdanbury | 0:6276e9f72327 | 151 | joystickPos = "UP"; |
samdanbury | 0:6276e9f72327 | 152 | else if (Right) |
samdanbury | 0:6276e9f72327 | 153 | joystickPos = "RIGHT"; |
samdanbury | 0:6276e9f72327 | 154 | else |
samdanbury | 0:6276e9f72327 | 155 | joystickPos = "CENTRE"; |
samdanbury | 0:6276e9f72327 | 156 | } |
rajathishere | 6:a022f983f94b | 157 | } |