Local copy

Dependencies:   C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed

Fork of IBMIoTClientExampleForLPC1768 by Sam Danbury

Committer:
samdanbury
Date:
Mon Jun 23 20:12:18 2014 +0000
Revision:
2:25ddff75a8c7
Parent:
0:6276e9f72327
Child:
4:e8de333c1cb4
reads in a config file as well as utilises the new topic space and clientId changes

Who changed what in which revision?

UserRevisionLine numberNew 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 {
samdanbury 0:6276e9f72327 59 //Connect to the network
samdanbury 0:6276e9f72327 60 EthernetInterface eth;
samdanbury 0:6276e9f72327 61 eth.init();
samdanbury 0:6276e9f72327 62 eth.connect();
samdanbury 0:6276e9f72327 63
samdanbury 0:6276e9f72327 64 //Obtain mac address of mbed
samdanbury 0:6276e9f72327 65 string mac = eth.getMACAddress();
samdanbury 0:6276e9f72327 66
samdanbury 0:6276e9f72327 67 //Remove colons from mac address
samdanbury 0:6276e9f72327 68 mac.erase(remove(mac.begin(), mac.end(), ':'), mac.end());
samdanbury 0:6276e9f72327 69
samdanbury 0:6276e9f72327 70 //Start thread to read data from joystick
samdanbury 0:6276e9f72327 71 Thread jThd(joystickThread);
samdanbury 0:6276e9f72327 72 joystickPos = "CENTRE";
samdanbury 0:6276e9f72327 73
samdanbury 0:6276e9f72327 74 QuickstartClient* c = new QuickstartClient(mac);
samdanbury 0:6276e9f72327 75
samdanbury 0:6276e9f72327 76 while(1)
samdanbury 0:6276e9f72327 77 {
samdanbury 0:6276e9f72327 78 //Initialize lcd
samdanbury 0:6276e9f72327 79 lcd.cls();
samdanbury 0:6276e9f72327 80 lcd.locate(0,0);
samdanbury 0:6276e9f72327 81
samdanbury 0:6276e9f72327 82 lcd.printf("Mac address: %s\n", mac);
samdanbury 0:6276e9f72327 83
samdanbury 0:6276e9f72327 84 //Flash led to show message has been sent successfully
samdanbury 0:6276e9f72327 85 led1 = 1;
samdanbury 0:6276e9f72327 86
samdanbury 0:6276e9f72327 87 //Construct quickstart message with desired datapoints
samdanbury 0:6276e9f72327 88 QuickstartMessage* m = new QuickstartMessage();
samdanbury 0:6276e9f72327 89 m->add("accelX", MMA.x());
samdanbury 0:6276e9f72327 90 m->add("accelY", MMA.y());
samdanbury 0:6276e9f72327 91 m->add("accelZ", MMA.z());
samdanbury 0:6276e9f72327 92 m->add("temp", tmp.read());
samdanbury 0:6276e9f72327 93 m->add("joystick", joystickPos);
samdanbury 0:6276e9f72327 94
samdanbury 0:6276e9f72327 95 pot1 = ain1;
samdanbury 0:6276e9f72327 96 pot2 = ain2;
samdanbury 0:6276e9f72327 97 m->add("potentiometer1", pot1);
samdanbury 0:6276e9f72327 98 m->add("potentiometer2", pot2);
samdanbury 0:6276e9f72327 99
samdanbury 0:6276e9f72327 100 //Message is converted from datapoints into json format and then published
samdanbury 0:6276e9f72327 101 c->publish(m->convertToJson());
samdanbury 0:6276e9f72327 102
samdanbury 0:6276e9f72327 103 if (m) {
samdanbury 0:6276e9f72327 104 delete m;
samdanbury 0:6276e9f72327 105 }
samdanbury 0:6276e9f72327 106
samdanbury 0:6276e9f72327 107 led1 = 0;
samdanbury 0:6276e9f72327 108
samdanbury 0:6276e9f72327 109 //Message published every second
samdanbury 0:6276e9f72327 110 wait(1);
samdanbury 0:6276e9f72327 111 }
samdanbury 0:6276e9f72327 112
samdanbury 0:6276e9f72327 113 eth.disconnect();
samdanbury 0:6276e9f72327 114 }
samdanbury 0:6276e9f72327 115
samdanbury 0:6276e9f72327 116 void joystickThread(void const *args) {
samdanbury 0:6276e9f72327 117 while (1) {
samdanbury 0:6276e9f72327 118 if (Down)
samdanbury 0:6276e9f72327 119 joystickPos = "DOWN";
samdanbury 0:6276e9f72327 120 else if (Left)
samdanbury 0:6276e9f72327 121 joystickPos = "LEFT";
samdanbury 0:6276e9f72327 122 else if (Click)
samdanbury 0:6276e9f72327 123 joystickPos = "CLICK";
samdanbury 0:6276e9f72327 124 else if (Up)
samdanbury 0:6276e9f72327 125 joystickPos = "UP";
samdanbury 0:6276e9f72327 126 else if (Right)
samdanbury 0:6276e9f72327 127 joystickPos = "RIGHT";
samdanbury 0:6276e9f72327 128 else
samdanbury 0:6276e9f72327 129 joystickPos = "CENTRE";
samdanbury 0:6276e9f72327 130 }
samdanbury 0:6276e9f72327 131 }