Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
src/main.cpp@8:e58e10ca4352, 2014-07-01 (annotated)
- Committer:
- rajathishere
- Date:
- Tue Jul 01 10:08:39 2014 +0000
- Revision:
- 8:e58e10ca4352
- Parent:
- 7:2c5d0dbd7985
- Child:
- 9:cbabd85f2d5d
Clean up and reading username and password from device.cfg
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); |
samdanbury | 0:6276e9f72327 | 76 | |
rajathishere | 7:2c5d0dbd7985 | 77 | //c->subscribe(); |
rajathishere | 7:2c5d0dbd7985 | 78 | char value[10]; |
rajathishere | 7:2c5d0dbd7985 | 79 | //string data_points = ""; |
samdanbury | 0:6276e9f72327 | 80 | while(1) |
samdanbury | 0:6276e9f72327 | 81 | { |
samdanbury | 0:6276e9f72327 | 82 | //Initialize lcd |
rajathishere | 8:e58e10ca4352 | 83 | lcd.cls(); |
rajathishere | 8:e58e10ca4352 | 84 | lcd.locate(0,0); |
rajathishere | 8:e58e10ca4352 | 85 | lcd.printf("Mac address: %s\n", mac); |
samdanbury | 0:6276e9f72327 | 86 | //Flash led to show message has been sent successfully |
samdanbury | 0:6276e9f72327 | 87 | led1 = 1; |
rajathishere | 7:2c5d0dbd7985 | 88 | string data_points = "\"myName\": \"IoT mbed\""; |
samdanbury | 0:6276e9f72327 | 89 | //Construct quickstart message with desired datapoints |
rajathishere | 6:a022f983f94b | 90 | //QuickstartMessage* m = new QuickstartMessage(); |
rajathishere | 6:a022f983f94b | 91 | |
rajathishere | 6:a022f983f94b | 92 | //m->add("accelX", MMA.x()); |
rajathishere | 7:2c5d0dbd7985 | 93 | sprintf(value, "%0.4f", MMA.x()); |
rajathishere | 7:2c5d0dbd7985 | 94 | data_points+=",\"accelX\":"; |
rajathishere | 7:2c5d0dbd7985 | 95 | data_points+=value; |
rajathishere | 6:a022f983f94b | 96 | //m->add("accelY", MMA.y()); |
rajathishere | 7:2c5d0dbd7985 | 97 | sprintf(value, "%0.4f", MMA.y()); |
rajathishere | 7:2c5d0dbd7985 | 98 | data_points+=",\"accelY\":"; |
rajathishere | 7:2c5d0dbd7985 | 99 | data_points+=value; |
rajathishere | 6:a022f983f94b | 100 | //m->add("accelZ", MMA.z()); |
rajathishere | 7:2c5d0dbd7985 | 101 | sprintf(value, "%0.4f", MMA.z()); |
rajathishere | 7:2c5d0dbd7985 | 102 | data_points+=",\"accelZ\":"; |
rajathishere | 7:2c5d0dbd7985 | 103 | data_points+=value; |
rajathishere | 6:a022f983f94b | 104 | //m->add("temp", tmp.read()); |
rajathishere | 7:2c5d0dbd7985 | 105 | sprintf(value, "%0.4f", tmp.read()); |
rajathishere | 7:2c5d0dbd7985 | 106 | data_points+=",\"temp\":"; |
rajathishere | 7:2c5d0dbd7985 | 107 | data_points+=value; |
rajathishere | 6:a022f983f94b | 108 | //m->add("joystick", joystickPos); |
rajathishere | 7:2c5d0dbd7985 | 109 | data_points+=",\"joystick\":" + joystickPos; |
rajathishere | 7:2c5d0dbd7985 | 110 | pot1 = ain1; |
rajathishere | 7:2c5d0dbd7985 | 111 | pot2 = ain2; |
rajathishere | 6:a022f983f94b | 112 | //m->add("potentiometer1", pot1); |
rajathishere | 7:2c5d0dbd7985 | 113 | sprintf(value, "%0.4f", pot1); |
rajathishere | 7:2c5d0dbd7985 | 114 | data_points+=",\"potentiometer1\":"; |
rajathishere | 7:2c5d0dbd7985 | 115 | data_points+=value; |
rajathishere | 6:a022f983f94b | 116 | //m->add("potentiometer2", pot2); |
rajathishere | 7:2c5d0dbd7985 | 117 | sprintf(value, "%0.4f", pot1); |
rajathishere | 7:2c5d0dbd7985 | 118 | data_points+=",\"potentiometer1\":"; |
rajathishere | 7:2c5d0dbd7985 | 119 | data_points+=value; |
rajathishere | 7:2c5d0dbd7985 | 120 | |
rajathishere | 7:2c5d0dbd7985 | 121 | string message = "{\"d\":{" + data_points + "}}"; |
samdanbury | 0:6276e9f72327 | 122 | |
samdanbury | 0:6276e9f72327 | 123 | //Message is converted from datapoints into json format and then published |
rajathishere | 7:2c5d0dbd7985 | 124 | c->publish(message);//c->publish(m->convertToJson()); |
rajathishere | 6:a022f983f94b | 125 | //if (m) { |
rajathishere | 6:a022f983f94b | 126 | // delete m; |
rajathishere | 6:a022f983f94b | 127 | //} |
samdanbury | 0:6276e9f72327 | 128 | |
samdanbury | 0:6276e9f72327 | 129 | led1 = 0; |
samdanbury | 0:6276e9f72327 | 130 | |
samdanbury | 0:6276e9f72327 | 131 | //Message published every second |
samdanbury | 0:6276e9f72327 | 132 | wait(1); |
samdanbury | 0:6276e9f72327 | 133 | } |
samdanbury | 0:6276e9f72327 | 134 | |
samdanbury | 0:6276e9f72327 | 135 | eth.disconnect(); |
samdanbury | 0:6276e9f72327 | 136 | } |
samdanbury | 0:6276e9f72327 | 137 | |
samdanbury | 0:6276e9f72327 | 138 | void joystickThread(void const *args) { |
samdanbury | 0:6276e9f72327 | 139 | while (1) { |
samdanbury | 0:6276e9f72327 | 140 | if (Down) |
samdanbury | 0:6276e9f72327 | 141 | joystickPos = "DOWN"; |
samdanbury | 0:6276e9f72327 | 142 | else if (Left) |
samdanbury | 0:6276e9f72327 | 143 | joystickPos = "LEFT"; |
samdanbury | 0:6276e9f72327 | 144 | else if (Click) |
samdanbury | 0:6276e9f72327 | 145 | joystickPos = "CLICK"; |
samdanbury | 0:6276e9f72327 | 146 | else if (Up) |
samdanbury | 0:6276e9f72327 | 147 | joystickPos = "UP"; |
samdanbury | 0:6276e9f72327 | 148 | else if (Right) |
samdanbury | 0:6276e9f72327 | 149 | joystickPos = "RIGHT"; |
samdanbury | 0:6276e9f72327 | 150 | else |
samdanbury | 0:6276e9f72327 | 151 | joystickPos = "CENTRE"; |
samdanbury | 0:6276e9f72327 | 152 | } |
rajathishere | 6:a022f983f94b | 153 | } |