MQTT+G SENSOR
Dependencies: EthernetInterface FXOS8700Q HTTPClient HelloMQTT MQTT cantcoap mbed-rtos mbed
Fork of HelloMQTT by
main.cpp@18:67520755e27e, 2015-08-12 (annotated)
- Committer:
- ericliang
- Date:
- Wed Aug 12 02:43:32 2015 +0000
- Revision:
- 18:67520755e27e
- Parent:
- 17:31ed13e8a394
MQTT_G_Sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 1:a1d5c7a6acbc | 1 | /******************************************************************************* |
icraggs | 1:a1d5c7a6acbc | 2 | * Copyright (c) 2014 IBM Corp. |
icraggs | 1:a1d5c7a6acbc | 3 | * |
icraggs | 1:a1d5c7a6acbc | 4 | * All rights reserved. This program and the accompanying materials |
icraggs | 1:a1d5c7a6acbc | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
icraggs | 1:a1d5c7a6acbc | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
icraggs | 1:a1d5c7a6acbc | 7 | * |
icraggs | 1:a1d5c7a6acbc | 8 | * The Eclipse Public License is available at |
icraggs | 1:a1d5c7a6acbc | 9 | * http://www.eclipse.org/legal/epl-v10.html |
icraggs | 1:a1d5c7a6acbc | 10 | * and the Eclipse Distribution License is available at |
icraggs | 1:a1d5c7a6acbc | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
icraggs | 1:a1d5c7a6acbc | 12 | * |
icraggs | 1:a1d5c7a6acbc | 13 | * Contributors: |
icraggs | 1:a1d5c7a6acbc | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
icraggs | 1:a1d5c7a6acbc | 15 | *******************************************************************************/ |
icraggs | 2:638c854c0695 | 16 | |
icraggs | 2:638c854c0695 | 17 | /** |
icraggs | 2:638c854c0695 | 18 | This is a sample program to illustrate the use of the MQTT Client library |
icraggs | 2:638c854c0695 | 19 | on the mbed platform. The Client class requires two classes which mediate |
icraggs | 2:638c854c0695 | 20 | access to system interfaces for networking and timing. As long as these two |
icraggs | 2:638c854c0695 | 21 | classes provide the required public programming interfaces, it does not matter |
icraggs | 2:638c854c0695 | 22 | what facilities they use underneath. In this program, they use the mbed |
icraggs | 2:638c854c0695 | 23 | system libraries. |
icraggs | 2:638c854c0695 | 24 | |
icraggs | 2:638c854c0695 | 25 | */ |
icraggs | 1:a1d5c7a6acbc | 26 | |
icraggs | 2:638c854c0695 | 27 | |
icraggs | 8:a3e3113054a1 | 28 | #include "MQTTEthernet.h" |
icraggs | 2:638c854c0695 | 29 | #include "MQTTClient.h" |
ericliang | 17:31ed13e8a394 | 30 | #include "FXOS8700Q.h" |
ericliang | 17:31ed13e8a394 | 31 | #include "HTTPClient.h" |
ericliang | 17:31ed13e8a394 | 32 | #include "WISEAgent.h" |
icraggs | 2:638c854c0695 | 33 | |
icraggs | 2:638c854c0695 | 34 | int arrivedcount = 0; |
icraggs | 2:638c854c0695 | 35 | |
ericliang | 17:31ed13e8a394 | 36 | #define FXOS8700 1 |
ericliang | 17:31ed13e8a394 | 37 | |
ericliang | 17:31ed13e8a394 | 38 | // FXOS8700 |
ericliang | 17:31ed13e8a394 | 39 | //FXOS8700Q acc( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield |
ericliang | 17:31ed13e8a394 | 40 | //FXOS8700Q mag( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield |
ericliang | 17:31ed13e8a394 | 41 | FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
ericliang | 17:31ed13e8a394 | 42 | FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
ericliang | 17:31ed13e8a394 | 43 | |
ericliang | 17:31ed13e8a394 | 44 | |
ericliang | 17:31ed13e8a394 | 45 | #define SampelTime 1 /// 1 sec |
ericliang | 17:31ed13e8a394 | 46 | #define SampleCount 30 |
ericliang | 17:31ed13e8a394 | 47 | #define MQTT_BROKER_IP "172.22.12.206" |
ericliang | 17:31ed13e8a394 | 48 | //#define MQTT_BROKER_IP "192.168.1.13" |
ericliang | 17:31ed13e8a394 | 49 | #define MQTT_CLIENT_ID "Eric" |
ericliang | 17:31ed13e8a394 | 50 | #define QUARK_THREAD 0.2 |
ericliang | 17:31ed13e8a394 | 51 | #define SMALL_VAR 1 |
ericliang | 17:31ed13e8a394 | 52 | |
ericliang | 17:31ed13e8a394 | 53 | float X_LOGs[SampleCount]; |
ericliang | 17:31ed13e8a394 | 54 | float Y_LOGs[SampleCount]; |
ericliang | 17:31ed13e8a394 | 55 | float Z_LOGs[SampleCount]; |
ericliang | 17:31ed13e8a394 | 56 | |
ericliang | 17:31ed13e8a394 | 57 | float stdDevX = 0.0; |
ericliang | 17:31ed13e8a394 | 58 | float stdDevY = 0.0; |
ericliang | 17:31ed13e8a394 | 59 | float stdDevZ = 0.0; |
ericliang | 17:31ed13e8a394 | 60 | |
ericliang | 17:31ed13e8a394 | 61 | float g_coeff_X = 0.0; |
ericliang | 17:31ed13e8a394 | 62 | float g_coeff_Y = 0.0; |
ericliang | 17:31ed13e8a394 | 63 | float g_coeff_Z = 0.0; |
ericliang | 17:31ed13e8a394 | 64 | |
ericliang | 17:31ed13e8a394 | 65 | float g_G_Value = 0.0; |
ericliang | 17:31ed13e8a394 | 66 | |
ericliang | 17:31ed13e8a394 | 67 | float meanX = 0.0; |
ericliang | 17:31ed13e8a394 | 68 | float meanY = 0.0; |
ericliang | 17:31ed13e8a394 | 69 | float meanZ = 0.0; |
ericliang | 17:31ed13e8a394 | 70 | |
ericliang | 17:31ed13e8a394 | 71 | int log_index = 0; |
ericliang | 17:31ed13e8a394 | 72 | |
ericliang | 17:31ed13e8a394 | 73 | float X_BASE = 0; |
ericliang | 17:31ed13e8a394 | 74 | float Y_BASE = 0; |
ericliang | 17:31ed13e8a394 | 75 | float Z_BASE = 0; |
ericliang | 17:31ed13e8a394 | 76 | |
ericliang | 17:31ed13e8a394 | 77 | |
ericliang | 17:31ed13e8a394 | 78 | #ifdef FXOS8700 |
ericliang | 17:31ed13e8a394 | 79 | Serial pc(USBTX, USBRX); |
ericliang | 17:31ed13e8a394 | 80 | |
ericliang | 17:31ed13e8a394 | 81 | MotionSensorDataUnits mag_data; |
ericliang | 17:31ed13e8a394 | 82 | MotionSensorDataUnits acc_data; |
ericliang | 17:31ed13e8a394 | 83 | |
ericliang | 17:31ed13e8a394 | 84 | MotionSensorDataCounts mag_raw; |
ericliang | 17:31ed13e8a394 | 85 | MotionSensorDataCounts acc_raw; |
ericliang | 17:31ed13e8a394 | 86 | #endif |
ericliang | 17:31ed13e8a394 | 87 | |
ericliang | 17:31ed13e8a394 | 88 | float standard_deviation(float data[], int n, float *mean ); |
ericliang | 17:31ed13e8a394 | 89 | void CalaulateXYZStatisticValue(); |
ericliang | 17:31ed13e8a394 | 90 | |
ericliang | 17:31ed13e8a394 | 91 | void Get_G_SensorValue(float *praX, float *praY, float *praZ ) |
ericliang | 17:31ed13e8a394 | 92 | { |
ericliang | 17:31ed13e8a394 | 93 | #ifdef FXOS8700 |
ericliang | 17:31ed13e8a394 | 94 | acc.getAxis(acc_data); |
ericliang | 17:31ed13e8a394 | 95 | *praX = acc_data.x; |
ericliang | 17:31ed13e8a394 | 96 | *praY = acc_data.y; |
ericliang | 17:31ed13e8a394 | 97 | *praZ = acc_data.z; |
ericliang | 17:31ed13e8a394 | 98 | #endif |
ericliang | 17:31ed13e8a394 | 99 | } |
ericliang | 17:31ed13e8a394 | 100 | |
icraggs | 8:a3e3113054a1 | 101 | |
icraggs | 9:5beb8609e9f7 | 102 | void messageArrived(MQTT::MessageData& md) |
icraggs | 2:638c854c0695 | 103 | { |
icraggs | 9:5beb8609e9f7 | 104 | MQTT::Message &message = md.message; |
icraggs | 9:5beb8609e9f7 | 105 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
icraggs | 9:5beb8609e9f7 | 106 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
icraggs | 2:638c854c0695 | 107 | ++arrivedcount; |
ericliang | 17:31ed13e8a394 | 108 | } |
ericliang | 17:31ed13e8a394 | 109 | |
ericliang | 17:31ed13e8a394 | 110 | |
ericliang | 17:31ed13e8a394 | 111 | void SaveLogRingBuf( float faX, float faY, float fzZ ) |
ericliang | 17:31ed13e8a394 | 112 | { |
ericliang | 17:31ed13e8a394 | 113 | X_LOGs[log_index] = faX; |
ericliang | 17:31ed13e8a394 | 114 | Y_LOGs[log_index] = faY; |
ericliang | 17:31ed13e8a394 | 115 | Z_LOGs[log_index] = fzZ; |
ericliang | 17:31ed13e8a394 | 116 | log_index++; |
ericliang | 17:31ed13e8a394 | 117 | if( log_index >= SampleCount ) |
ericliang | 17:31ed13e8a394 | 118 | log_index = 0; |
ericliang | 17:31ed13e8a394 | 119 | |
ericliang | 17:31ed13e8a394 | 120 | } |
ericliang | 17:31ed13e8a394 | 121 | |
ericliang | 17:31ed13e8a394 | 122 | |
ericliang | 17:31ed13e8a394 | 123 | |
ericliang | 17:31ed13e8a394 | 124 | void CorrectGSensor() |
ericliang | 17:31ed13e8a394 | 125 | { |
ericliang | 17:31ed13e8a394 | 126 | float faX=0,faXt=0, faY=0, faYt=0, faZ=0, faZt=0; |
ericliang | 17:31ed13e8a394 | 127 | |
ericliang | 17:31ed13e8a394 | 128 | int count = 0; |
ericliang | 17:31ed13e8a394 | 129 | |
ericliang | 17:31ed13e8a394 | 130 | printf("Start Correct G-Sensor \n"); |
ericliang | 17:31ed13e8a394 | 131 | while(1) { |
ericliang | 17:31ed13e8a394 | 132 | count++; |
ericliang | 17:31ed13e8a394 | 133 | |
ericliang | 17:31ed13e8a394 | 134 | Get_G_SensorValue(&faX,&faY,&faZ); |
ericliang | 17:31ed13e8a394 | 135 | SaveLogRingBuf(faX, faY, faZ); |
ericliang | 17:31ed13e8a394 | 136 | |
ericliang | 17:31ed13e8a394 | 137 | faXt += faX; |
ericliang | 17:31ed13e8a394 | 138 | faYt += faY; |
ericliang | 17:31ed13e8a394 | 139 | faZt += faZ; |
ericliang | 17:31ed13e8a394 | 140 | if(count >= SampleCount ) { |
ericliang | 17:31ed13e8a394 | 141 | X_BASE = faXt / count; |
ericliang | 17:31ed13e8a394 | 142 | Y_BASE = faYt / count; |
ericliang | 17:31ed13e8a394 | 143 | Z_BASE = faZt / count; |
ericliang | 17:31ed13e8a394 | 144 | printf("Stop to Correct G-Sensor Base X=%1.4f Y= %1.4f Z=%1.4f\n", X_BASE, Y_BASE, Z_BASE); |
ericliang | 17:31ed13e8a394 | 145 | break; |
ericliang | 17:31ed13e8a394 | 146 | } |
ericliang | 17:31ed13e8a394 | 147 | //wait(SampelTime); |
ericliang | 17:31ed13e8a394 | 148 | wait(0.1); |
ericliang | 17:31ed13e8a394 | 149 | } |
ericliang | 17:31ed13e8a394 | 150 | CalaulateXYZStatisticValue(); |
ericliang | 17:31ed13e8a394 | 151 | } |
ericliang | 17:31ed13e8a394 | 152 | |
ericliang | 17:31ed13e8a394 | 153 | void CalaulateXYZStatisticValue() |
ericliang | 17:31ed13e8a394 | 154 | { |
ericliang | 17:31ed13e8a394 | 155 | stdDevX = standard_deviation(X_LOGs,SampleCount,&meanX); |
ericliang | 17:31ed13e8a394 | 156 | g_coeff_X = stdDevX / meanX; |
ericliang | 17:31ed13e8a394 | 157 | |
ericliang | 17:31ed13e8a394 | 158 | stdDevY = standard_deviation(Y_LOGs,SampleCount,&meanY); |
ericliang | 17:31ed13e8a394 | 159 | g_coeff_Y = stdDevY / meanY; |
ericliang | 17:31ed13e8a394 | 160 | |
ericliang | 17:31ed13e8a394 | 161 | stdDevZ = standard_deviation(Z_LOGs,SampleCount,&meanZ); |
ericliang | 17:31ed13e8a394 | 162 | g_coeff_Z = stdDevZ / meanZ; |
ericliang | 17:31ed13e8a394 | 163 | |
ericliang | 17:31ed13e8a394 | 164 | printf(" X St=%1.4f Cof=%1.4f\n Y St=%1.4f Cof=%1.4f\n Z St=%1.4f Cof=%1.4f\n",stdDevX,g_coeff_X,stdDevY,g_coeff_Y,stdDevZ,g_coeff_Z); |
ericliang | 17:31ed13e8a394 | 165 | } |
ericliang | 17:31ed13e8a394 | 166 | |
ericliang | 17:31ed13e8a394 | 167 | |
ericliang | 17:31ed13e8a394 | 168 | void CheckCalibration() |
ericliang | 17:31ed13e8a394 | 169 | { |
ericliang | 17:31ed13e8a394 | 170 | int small_coeff = 0; |
ericliang | 17:31ed13e8a394 | 171 | |
ericliang | 17:31ed13e8a394 | 172 | if( ( g_coeff_X < 1.0 && g_coeff_X > -1.0) && ( g_coeff_Y < 1.0 && g_coeff_Y > -1.0) && (g_coeff_Z < 1.0 && g_coeff_Z > -1.0) ) |
ericliang | 17:31ed13e8a394 | 173 | small_coeff = 1; |
ericliang | 17:31ed13e8a394 | 174 | |
ericliang | 17:31ed13e8a394 | 175 | |
ericliang | 17:31ed13e8a394 | 176 | if( g_G_Value > 0.2 ) { |
ericliang | 17:31ed13e8a394 | 177 | if( small_coeff == 1 ) { |
ericliang | 17:31ed13e8a394 | 178 | printf("Device's bais be changed in Correct Mode\n"); |
ericliang | 17:31ed13e8a394 | 179 | CorrectGSensor(); |
ericliang | 17:31ed13e8a394 | 180 | } |
ericliang | 17:31ed13e8a394 | 181 | } |
icraggs | 2:638c854c0695 | 182 | } |
icraggs | 0:0cae29831d01 | 183 | |
icraggs | 2:638c854c0695 | 184 | |
ericliang | 17:31ed13e8a394 | 185 | void ResetLogBuf() |
ericliang | 17:31ed13e8a394 | 186 | { |
ericliang | 17:31ed13e8a394 | 187 | int i = 0; |
ericliang | 17:31ed13e8a394 | 188 | |
ericliang | 17:31ed13e8a394 | 189 | for(i=0; i<SampleCount; i++) { |
ericliang | 17:31ed13e8a394 | 190 | X_LOGs[i]=0.0; |
ericliang | 17:31ed13e8a394 | 191 | Y_LOGs[i]=0.0; |
ericliang | 17:31ed13e8a394 | 192 | Z_LOGs[i]=0.0; |
ericliang | 17:31ed13e8a394 | 193 | } |
ericliang | 17:31ed13e8a394 | 194 | } |
ericliang | 17:31ed13e8a394 | 195 | |
ericliang | 17:31ed13e8a394 | 196 | void ShowCalibrationValue() |
ericliang | 17:31ed13e8a394 | 197 | { |
ericliang | 17:31ed13e8a394 | 198 | float faX=0,faX2=0, faY=0, faY2=0, faZ=0, faZ2=0; |
ericliang | 17:31ed13e8a394 | 199 | float g1, g2; |
ericliang | 17:31ed13e8a394 | 200 | int count = 0; |
ericliang | 17:31ed13e8a394 | 201 | while(1) { |
ericliang | 17:31ed13e8a394 | 202 | Get_G_SensorValue(&faX,&faY,&faZ); |
ericliang | 17:31ed13e8a394 | 203 | |
ericliang | 17:31ed13e8a394 | 204 | count++; |
ericliang | 17:31ed13e8a394 | 205 | faZ2 = faZ - Z_BASE; |
ericliang | 17:31ed13e8a394 | 206 | faX2 = faX - X_BASE; |
ericliang | 17:31ed13e8a394 | 207 | faY2 = faY - Y_BASE; |
ericliang | 17:31ed13e8a394 | 208 | g1 = faX2 * faX2 + faY2 * faY2 + faZ2 * faZ2; |
ericliang | 17:31ed13e8a394 | 209 | g2 = sqrt(g1); |
ericliang | 17:31ed13e8a394 | 210 | printf(" %1.4f %1.4f %1.4f %1.4f\n", faX2, faY2, faZ2, g2 ); |
ericliang | 17:31ed13e8a394 | 211 | wait(SampelTime); |
ericliang | 17:31ed13e8a394 | 212 | if( count >= SampleCount ) |
ericliang | 17:31ed13e8a394 | 213 | break; |
ericliang | 17:31ed13e8a394 | 214 | } |
ericliang | 17:31ed13e8a394 | 215 | } |
ericliang | 17:31ed13e8a394 | 216 | |
ericliang | 17:31ed13e8a394 | 217 | |
ericliang | 17:31ed13e8a394 | 218 | float standard_deviation(float data[], int n, float *Mean ) |
ericliang | 17:31ed13e8a394 | 219 | { |
ericliang | 17:31ed13e8a394 | 220 | float mean=0.0; |
ericliang | 17:31ed13e8a394 | 221 | float sum_deviation=0.0; |
ericliang | 17:31ed13e8a394 | 222 | int i; |
ericliang | 17:31ed13e8a394 | 223 | for(i=0; i<n;++i) |
ericliang | 17:31ed13e8a394 | 224 | { |
ericliang | 17:31ed13e8a394 | 225 | mean+=data[i]; |
ericliang | 17:31ed13e8a394 | 226 | } |
ericliang | 17:31ed13e8a394 | 227 | mean=mean/n; |
ericliang | 17:31ed13e8a394 | 228 | *Mean = mean; |
ericliang | 17:31ed13e8a394 | 229 | for(i=0; i<n;++i) |
ericliang | 17:31ed13e8a394 | 230 | sum_deviation+=(data[i]-mean)*(data[i]-mean); |
ericliang | 17:31ed13e8a394 | 231 | return sqrt(sum_deviation/n); |
ericliang | 17:31ed13e8a394 | 232 | } |
ericliang | 17:31ed13e8a394 | 233 | |
ericliang | 17:31ed13e8a394 | 234 | |
ericliang | 17:31ed13e8a394 | 235 | char *RegistJson = "{\"susiCommData\":{\"devID\":\"%s\",\"hostname\":\"mbed\",\"sn\":\"%s\",\"mac\":\"%s\",\"version\":\"3.1.0.440\",\"type\":\"SenHub\",\"product\":\"mbed\",\"manufacture\":\"Adv\",\"status\":%d,\"commCmd\":1,\"requestID\":21,\"agentID\":\"%s\",\"handlerName\":\"general\",\"sendTS\":%d}}\n"; |
ericliang | 17:31ed13e8a394 | 236 | |
ericliang | 17:31ed13e8a394 | 237 | char *OSInfoJson = "{\"susiCommData\":{\"osInfo\":{\"cagentVersion\":\"3.1.0.440\",\"cagentType\":\"IoTGW\",\"osVersion\":\"\",\"biosVersion\":\"1\",\"platformName\":\"\",\"processorName\":\"\",\"osArch\":\"RTOS\",\"totalPhysMemKB\":101240,\"macs\":\"14:DA:E9:96:BE:05\",\"IP\":\"%s\"},\"commCmd\":116,\"requestID\":109,\"agentID\":\"%s\",\"handlerName\":\"general\",\"sendTS\":%d}}\n"; |
ericliang | 17:31ed13e8a394 | 238 | |
ericliang | 17:31ed13e8a394 | 239 | #if 1 |
icraggs | 2:638c854c0695 | 240 | int main(int argc, char* argv[]) |
icraggs | 2:638c854c0695 | 241 | { |
ericliang | 17:31ed13e8a394 | 242 | float faX=0,faX2=0, faY=0, faY2=0, faZ=0, faZ2=0; |
ericliang | 17:31ed13e8a394 | 243 | acc.enable(); |
ericliang | 17:31ed13e8a394 | 244 | float g1=0; |
ericliang | 17:31ed13e8a394 | 245 | g_G_Value = 0.0; |
ericliang | 17:31ed13e8a394 | 246 | char *mac = MQTT_CLIENT_ID; |
ericliang | 17:31ed13e8a394 | 247 | char buf[340]; |
ericliang | 17:31ed13e8a394 | 248 | |
ericliang | 17:31ed13e8a394 | 249 | |
icraggs | 8:a3e3113054a1 | 250 | MQTTEthernet ipstack = MQTTEthernet(); |
icraggs | 2:638c854c0695 | 251 | |
ericliang | 17:31ed13e8a394 | 252 | //char* topic = "sen/g-sensor"; |
ericliang | 17:31ed13e8a394 | 253 | char topic[128]={0}; |
ericliang | 17:31ed13e8a394 | 254 | |
ericliang | 17:31ed13e8a394 | 255 | |
icraggs | 8:a3e3113054a1 | 256 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
ericliang | 17:31ed13e8a394 | 257 | MQTT::Message message; |
icraggs | 3:7a6a899de7cc | 258 | |
ericliang | 17:31ed13e8a394 | 259 | char* hostname = MQTT_BROKER_IP; |
icraggs | 6:e4c690c45021 | 260 | int port = 1883; |
ericliang | 17:31ed13e8a394 | 261 | printf("Connecting to %s:%d\n", hostname, port); |
icraggs | 6:e4c690c45021 | 262 | int rc = ipstack.connect(hostname, port); |
icraggs | 6:e4c690c45021 | 263 | if (rc != 0) |
ericliang | 17:31ed13e8a394 | 264 | printf("rc from TCP connect is %d\n", rc); |
ericliang | 17:31ed13e8a394 | 265 | else |
ericliang | 17:31ed13e8a394 | 266 | printf("TCP connect %s OK\n", hostname); |
ericliang | 17:31ed13e8a394 | 267 | |
icraggs | 6:e4c690c45021 | 268 | |
ericliang | 17:31ed13e8a394 | 269 | //snprintf(buf,sizeof(topic),RegistJson,DEV_UNID, DEV_UNID, DEV_UNID, 0, DEV_UNID, 1436160081000); |
ericliang | 17:31ed13e8a394 | 270 | |
ericliang | 17:31ed13e8a394 | 271 | printf("%s\n",buf); |
ericliang | 17:31ed13e8a394 | 272 | |
icraggs | 6:e4c690c45021 | 273 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
ericliang | 17:31ed13e8a394 | 274 | data.MQTTVersion = 3; |
ericliang | 17:31ed13e8a394 | 275 | data.clientID.cstring= DEV_UNID;//mac; |
ericliang | 17:31ed13e8a394 | 276 | //data.username.cstring = "ral"; |
ericliang | 17:31ed13e8a394 | 277 | //data.password.cstring = "123"; |
ericliang | 17:31ed13e8a394 | 278 | // willmessage |
ericliang | 17:31ed13e8a394 | 279 | //data.willFlag = '1'; |
ericliang | 17:31ed13e8a394 | 280 | //data.will.topicName.cstring = DEF_WILLMSG_TOPIC; |
ericliang | 17:31ed13e8a394 | 281 | //data.will.message.cstring = buf; |
icraggs | 2:638c854c0695 | 282 | |
ericliang | 17:31ed13e8a394 | 283 | |
ericliang | 17:31ed13e8a394 | 284 | if ((rc = client.connect(data)) != 0) { |
ericliang | 17:31ed13e8a394 | 285 | printf("rc from MQTT connect is %d\n", rc); |
ericliang | 17:31ed13e8a394 | 286 | return rc; |
ericliang | 17:31ed13e8a394 | 287 | } |
ericliang | 17:31ed13e8a394 | 288 | |
ericliang | 17:31ed13e8a394 | 289 | memset(topic, 0, sizeof(topic)); |
ericliang | 17:31ed13e8a394 | 290 | memset(buf, 0, sizeof(buf)); |
ericliang | 17:31ed13e8a394 | 291 | |
ericliang | 17:31ed13e8a394 | 292 | snprintf(topic,sizeof(topic),WA_PUB_CONNECT_TOPIC,DEV_UNID); |
ericliang | 17:31ed13e8a394 | 293 | snprintf(buf,sizeof(buf),RegistJson,DEV_UNID, DEV_UNID, DEV_UNID, 1, DEV_UNID, 1436160081020); |
ericliang | 17:31ed13e8a394 | 294 | |
icraggs | 2:638c854c0695 | 295 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 296 | message.retained = false; |
icraggs | 2:638c854c0695 | 297 | message.dup = false; |
icraggs | 2:638c854c0695 | 298 | message.payload = (void*)buf; |
icraggs | 2:638c854c0695 | 299 | message.payloadlen = strlen(buf)+1; |
ericliang | 17:31ed13e8a394 | 300 | |
ericliang | 17:31ed13e8a394 | 301 | |
ericliang | 17:31ed13e8a394 | 302 | printf("len=%d\n",message.payloadlen); |
ericliang | 17:31ed13e8a394 | 303 | if( rc = client.publish(topic, message) != 0 ) { |
ericliang | 17:31ed13e8a394 | 304 | printf("rc from MQTT publish topic=%s rc= %d\n", topic, rc); |
ericliang | 17:31ed13e8a394 | 305 | return rc; |
ericliang | 17:31ed13e8a394 | 306 | }else { |
ericliang | 17:31ed13e8a394 | 307 | printf("rc topic2 ok\n"); |
ericliang | 17:31ed13e8a394 | 308 | } |
ericliang | 17:31ed13e8a394 | 309 | wait(SampelTime); |
ericliang | 17:31ed13e8a394 | 310 | |
ericliang | 17:31ed13e8a394 | 311 | memset(topic, 0, sizeof(topic)); |
ericliang | 17:31ed13e8a394 | 312 | memset(buf, 0, sizeof(buf)); |
ericliang | 17:31ed13e8a394 | 313 | |
ericliang | 17:31ed13e8a394 | 314 | EthernetInterface eth = ipstack.getEth(); |
ericliang | 17:31ed13e8a394 | 315 | |
ericliang | 17:31ed13e8a394 | 316 | snprintf(topic,sizeof(topic),WA_PUB_ACTION_TOPIC,DEV_UNID); |
ericliang | 17:31ed13e8a394 | 317 | snprintf(buf,sizeof(buf),OSInfoJson,eth.getIPAddress(), DEV_UNID, 1436160081030); |
ericliang | 17:31ed13e8a394 | 318 | |
icraggs | 2:638c854c0695 | 319 | message.payloadlen = strlen(buf)+1; |
ericliang | 17:31ed13e8a394 | 320 | printf("len=%d\n",message.payloadlen); |
ericliang | 17:31ed13e8a394 | 321 | if( rc = client.publish(topic, message) != 0 ) { |
ericliang | 17:31ed13e8a394 | 322 | printf("rc from MQTT publish topic=%s rc= %d\n", topic, rc); |
ericliang | 17:31ed13e8a394 | 323 | return rc; |
ericliang | 17:31ed13e8a394 | 324 | }else { |
ericliang | 17:31ed13e8a394 | 325 | printf("rc topic3 ok\n"); |
ericliang | 17:31ed13e8a394 | 326 | } |
ericliang | 17:31ed13e8a394 | 327 | //Init(ipstack); |
ericliang | 17:31ed13e8a394 | 328 | |
ericliang | 17:31ed13e8a394 | 329 | |
ericliang | 17:31ed13e8a394 | 330 | //if( WISEAgentConnect( eth.getIPAddress(), DEV_UNID)!= 0 ) { //eth.getMACAddress()); |
ericliang | 17:31ed13e8a394 | 331 | // printf("Connect to WISECloud Fail\n"); |
ericliang | 17:31ed13e8a394 | 332 | // }else |
ericliang | 17:31ed13e8a394 | 333 | // printf("Connected to WISECloud =%s\n",MQTT_BROKER_IP); |
icraggs | 2:638c854c0695 | 334 | |
ericliang | 17:31ed13e8a394 | 335 | while(1){ |
ericliang | 17:31ed13e8a394 | 336 | wait(SampelTime); |
ericliang | 17:31ed13e8a394 | 337 | printf("111\n"); |
ericliang | 17:31ed13e8a394 | 338 | } |
ericliang | 17:31ed13e8a394 | 339 | #if 0 |
ericliang | 17:31ed13e8a394 | 340 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
ericliang | 17:31ed13e8a394 | 341 | data.MQTTVersion = 3; |
ericliang | 17:31ed13e8a394 | 342 | //mac = eth.getMACAddress(); |
ericliang | 17:31ed13e8a394 | 343 | data.clientID.cstring= DEV_UNID;//mac; |
ericliang | 17:31ed13e8a394 | 344 | printf("\nMAC =%s IP=%s\n", data.clientID.cstring, eth.getIPAddress() ); |
ericliang | 17:31ed13e8a394 | 345 | //data.username.cstring = "testuser"; |
ericliang | 17:31ed13e8a394 | 346 | //data.password.cstring = "testpassword"; |
ericliang | 17:31ed13e8a394 | 347 | if ((rc = client.connect(data)) != 0) |
ericliang | 17:31ed13e8a394 | 348 | printf("rc from MQTT connect is %d\n", rc); |
ericliang | 17:31ed13e8a394 | 349 | |
ericliang | 17:31ed13e8a394 | 350 | //if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) |
ericliang | 17:31ed13e8a394 | 351 | // printf("rc from MQTT subscribe is %d\n", rc); |
ericliang | 17:31ed13e8a394 | 352 | |
ericliang | 17:31ed13e8a394 | 353 | MQTT::Message message; |
ericliang | 17:31ed13e8a394 | 354 | char buf[100]; |
ericliang | 17:31ed13e8a394 | 355 | while(1) { |
ericliang | 17:31ed13e8a394 | 356 | Get_G_SensorValue(&faX,&faY,&faZ); |
icraggs | 12:086a9314e8a5 | 357 | |
ericliang | 17:31ed13e8a394 | 358 | SaveLogRingBuf(faX,faY,faZ); |
ericliang | 17:31ed13e8a394 | 359 | CalaulateXYZStatisticValue(); |
ericliang | 17:31ed13e8a394 | 360 | |
ericliang | 17:31ed13e8a394 | 361 | faZ2 = faZ - Z_BASE; |
ericliang | 17:31ed13e8a394 | 362 | faX2 = faX - X_BASE; |
ericliang | 17:31ed13e8a394 | 363 | faY2 = faY - Y_BASE; |
ericliang | 17:31ed13e8a394 | 364 | g1 = faX2 * faX2 + faY2 * faY2 + faZ2 * faZ2; |
ericliang | 17:31ed13e8a394 | 365 | g_G_Value = sqrt(g1); |
ericliang | 17:31ed13e8a394 | 366 | //printf(" %1.4f %1.4f %1.4f %1.4f\n", faX2, faY2, faZ2, g_G_Value ); |
ericliang | 17:31ed13e8a394 | 367 | |
ericliang | 17:31ed13e8a394 | 368 | sprintf(buf, "%s %1.4f %1.4f %1.4f %1.4f\n", mac, faX2, faY2, faZ2, g_G_Value ); |
ericliang | 17:31ed13e8a394 | 369 | |
ericliang | 17:31ed13e8a394 | 370 | CheckCalibration(); |
ericliang | 17:31ed13e8a394 | 371 | |
ericliang | 17:31ed13e8a394 | 372 | message.qos = MQTT::QOS0; |
ericliang | 17:31ed13e8a394 | 373 | message.retained = false; |
ericliang | 17:31ed13e8a394 | 374 | message.dup = false; |
ericliang | 17:31ed13e8a394 | 375 | message.payload = (void*)buf; |
icraggs | 12:086a9314e8a5 | 376 | message.payloadlen = strlen(buf)+1; |
icraggs | 16:28d062c5522b | 377 | rc = client.publish(topic, message); |
ericliang | 17:31ed13e8a394 | 378 | |
ericliang | 17:31ed13e8a394 | 379 | wait(SampelTime); |
ericliang | 17:31ed13e8a394 | 380 | |
ericliang | 17:31ed13e8a394 | 381 | memset(buf,0,100); |
icraggs | 12:086a9314e8a5 | 382 | } |
ericliang | 17:31ed13e8a394 | 383 | |
icraggs | 2:638c854c0695 | 384 | |
icraggs | 8:a3e3113054a1 | 385 | if ((rc = client.disconnect()) != 0) |
ericliang | 17:31ed13e8a394 | 386 | printf("rc from disconnect was %d\n", rc); |
ericliang | 17:31ed13e8a394 | 387 | |
ericliang | 17:31ed13e8a394 | 388 | #endif |
icraggs | 2:638c854c0695 | 389 | |
icraggs | 2:638c854c0695 | 390 | ipstack.disconnect(); |
icraggs | 2:638c854c0695 | 391 | |
ericliang | 17:31ed13e8a394 | 392 | //printf("Version %.2f: finish %d msgs\n", version, arrivedcount); |
ericliang | 17:31ed13e8a394 | 393 | //printf("Finishing with %d messages received\n", arrivedcount); |
icraggs | 2:638c854c0695 | 394 | |
icraggs | 0:0cae29831d01 | 395 | return 0; |
icraggs | 0:0cae29831d01 | 396 | } |
ericliang | 17:31ed13e8a394 | 397 | #else // HTTP |
ericliang | 17:31ed13e8a394 | 398 | |
ericliang | 17:31ed13e8a394 | 399 | EthernetInterface eth; |
ericliang | 17:31ed13e8a394 | 400 | HTTPClient http; |
ericliang | 17:31ed13e8a394 | 401 | char str[512]; |
ericliang | 17:31ed13e8a394 | 402 | char Instr[512]; |
ericliang | 17:31ed13e8a394 | 403 | |
ericliang | 17:31ed13e8a394 | 404 | void MCB_HTTPPOST(const char *url, const char *data ) |
ericliang | 17:31ed13e8a394 | 405 | { |
ericliang | 17:31ed13e8a394 | 406 | sprintf(str, "%s", data); |
ericliang | 17:31ed13e8a394 | 407 | HTTPText outText(str); |
ericliang | 17:31ed13e8a394 | 408 | HTTPText inText(Instr, 512); |
ericliang | 17:31ed13e8a394 | 409 | |
ericliang | 17:31ed13e8a394 | 410 | int ret = http.post(url, outText, &inText); |
ericliang | 17:31ed13e8a394 | 411 | if (!ret) |
ericliang | 17:31ed13e8a394 | 412 | { |
ericliang | 17:31ed13e8a394 | 413 | printf("Executed PUT successfully - read %d characters\n", strlen(Instr)); |
ericliang | 17:31ed13e8a394 | 414 | printf("Result: %s\n", str); |
ericliang | 17:31ed13e8a394 | 415 | } |
ericliang | 17:31ed13e8a394 | 416 | else |
ericliang | 17:31ed13e8a394 | 417 | { |
ericliang | 17:31ed13e8a394 | 418 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
ericliang | 17:31ed13e8a394 | 419 | } |
ericliang | 17:31ed13e8a394 | 420 | } |
ericliang | 17:31ed13e8a394 | 421 | |
ericliang | 17:31ed13e8a394 | 422 | int main(int argc, char* argv[]) |
ericliang | 17:31ed13e8a394 | 423 | { |
ericliang | 17:31ed13e8a394 | 424 | float faX=0,faX2=0, faY=0, faY2=0, faZ=0, faZ2=0; |
ericliang | 17:31ed13e8a394 | 425 | acc.enable(); |
ericliang | 17:31ed13e8a394 | 426 | float g1=0; |
ericliang | 17:31ed13e8a394 | 427 | g_G_Value = 0.0; |
ericliang | 17:31ed13e8a394 | 428 | char buf[100]; |
ericliang | 17:31ed13e8a394 | 429 | |
ericliang | 17:31ed13e8a394 | 430 | ResetLogBuf(); |
ericliang | 17:31ed13e8a394 | 431 | |
ericliang | 17:31ed13e8a394 | 432 | CorrectGSensor(); |
ericliang | 17:31ed13e8a394 | 433 | |
ericliang | 17:31ed13e8a394 | 434 | |
ericliang | 17:31ed13e8a394 | 435 | eth.init(); //Use DHCP |
ericliang | 17:31ed13e8a394 | 436 | |
ericliang | 17:31ed13e8a394 | 437 | |
ericliang | 17:31ed13e8a394 | 438 | eth.connect(); |
ericliang | 17:31ed13e8a394 | 439 | |
ericliang | 17:31ed13e8a394 | 440 | |
ericliang | 17:31ed13e8a394 | 441 | while(1) { |
ericliang | 17:31ed13e8a394 | 442 | Get_G_SensorValue(&faX,&faY,&faZ); |
ericliang | 17:31ed13e8a394 | 443 | |
ericliang | 17:31ed13e8a394 | 444 | SaveLogRingBuf(faX,faY,faZ); |
ericliang | 17:31ed13e8a394 | 445 | CalaulateXYZStatisticValue(); |
ericliang | 17:31ed13e8a394 | 446 | |
ericliang | 17:31ed13e8a394 | 447 | faZ2 = faZ - Z_BASE; |
ericliang | 17:31ed13e8a394 | 448 | faX2 = faX - X_BASE; |
ericliang | 17:31ed13e8a394 | 449 | faY2 = faY - Y_BASE; |
ericliang | 17:31ed13e8a394 | 450 | g1 = faX2 * faX2 + faY2 * faY2 + faZ2 * faZ2; |
ericliang | 17:31ed13e8a394 | 451 | g_G_Value = sqrt(g1); |
ericliang | 17:31ed13e8a394 | 452 | //printf(" %1.4f %1.4f %1.4f %1.4f\n", faX2, faY2, faZ2, g_G_Value ); |
ericliang | 17:31ed13e8a394 | 453 | |
ericliang | 17:31ed13e8a394 | 454 | //sprintf(buf, "%s %1.4f %1.4f %1.4f %1.4f\n", mac, faX, faY, faZ, g_G_Value ); |
ericliang | 17:31ed13e8a394 | 455 | sprintf(buf,"x,,%1.4f",faX2); |
ericliang | 17:31ed13e8a394 | 456 | MCB_HTTPPOST("http://api.mediatek.com/mcs/v2/devices/DtIA7o7q/datapoints.csv",buf); |
ericliang | 17:31ed13e8a394 | 457 | memset(buf,0,100); |
ericliang | 17:31ed13e8a394 | 458 | |
ericliang | 17:31ed13e8a394 | 459 | |
ericliang | 17:31ed13e8a394 | 460 | sprintf(buf,"y,,%1.4f",faY2); |
ericliang | 17:31ed13e8a394 | 461 | MCB_HTTPPOST("http://api.mediatek.com/mcs/v2/devices/DtIA7o7q/datapoints.csv",buf); |
ericliang | 17:31ed13e8a394 | 462 | |
ericliang | 17:31ed13e8a394 | 463 | sprintf(buf,"z,,%1.4f",faZ2); |
ericliang | 17:31ed13e8a394 | 464 | MCB_HTTPPOST("http://api.mediatek.com/mcs/v2/devices/DtIA7o7q/datapoints.csv",buf); |
ericliang | 17:31ed13e8a394 | 465 | |
ericliang | 17:31ed13e8a394 | 466 | sprintf(buf,"g,,%1.4f",g_G_Value); |
ericliang | 17:31ed13e8a394 | 467 | MCB_HTTPPOST("http://api.mediatek.com/mcs/v2/devices/DtIA7o7q/datapoints.csv",buf); |
ericliang | 17:31ed13e8a394 | 468 | |
ericliang | 17:31ed13e8a394 | 469 | CheckCalibration(); |
ericliang | 17:31ed13e8a394 | 470 | |
ericliang | 17:31ed13e8a394 | 471 | // HTTPClient Send |
ericliang | 17:31ed13e8a394 | 472 | |
ericliang | 17:31ed13e8a394 | 473 | wait(SampelTime); |
ericliang | 17:31ed13e8a394 | 474 | |
ericliang | 17:31ed13e8a394 | 475 | memset(buf,0,100); |
ericliang | 17:31ed13e8a394 | 476 | } |
ericliang | 17:31ed13e8a394 | 477 | |
ericliang | 17:31ed13e8a394 | 478 | eth.disconnect(); |
ericliang | 17:31ed13e8a394 | 479 | |
ericliang | 17:31ed13e8a394 | 480 | |
ericliang | 17:31ed13e8a394 | 481 | return 0; |
ericliang | 17:31ed13e8a394 | 482 | } |
ericliang | 17:31ed13e8a394 | 483 | #endif |
ericliang | 17:31ed13e8a394 | 484 | |
ericliang | 17:31ed13e8a394 | 485 |