Nelson Neves
/
CurrentCostXML
CurrentCost EnviR XML Serial Data Emulator
main.cpp@0:d4524f1e749d, 2012-04-06 (annotated)
- Committer:
- botdream
- Date:
- Fri Apr 06 00:08:25 2012 +0000
- Revision:
- 0:d4524f1e749d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:d4524f1e749d | 1 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 2 | // CurrentCost XML Serial Data Emulator |
botdream | 0:d4524f1e749d | 3 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 4 | // Simulating OptiSmart TX device with 2 configured sensors (0 and 9) |
botdream | 0:d4524f1e749d | 5 | // and additional IAM power plug sensor (1): |
botdream | 0:d4524f1e749d | 6 | // |
botdream | 0:d4524f1e749d | 7 | // http://www.currentcost.com/cc128/xml.htm |
botdream | 0:d4524f1e749d | 8 | // http://currentcost.com/support/viewtopic.php?f=24&t=1284 |
botdream | 0:d4524f1e749d | 9 | // https://docs.google.com/document/pub?id=1-Saz_uA8r6HJzGNoNTF2zpwz7BcjTTwpf0HOlhCpuGY |
botdream | 0:d4524f1e749d | 10 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 11 | // Objectives: to emulate CurrentCost (EnviR Device) functionality to send XML data |
botdream | 0:d4524f1e749d | 12 | // via USB Serial Port to be used to develop custom software application. |
botdream | 0:d4524f1e749d | 13 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 14 | #include "mbed.h" |
botdream | 0:d4524f1e749d | 15 | #include <string> |
botdream | 0:d4524f1e749d | 16 | using std::string; |
botdream | 0:d4524f1e749d | 17 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 18 | |
botdream | 0:d4524f1e749d | 19 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 20 | // USB serial port setup - debug messages |
botdream | 0:d4524f1e749d | 21 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 22 | Serial pc(USBTX, USBRX); // tx, rx |
botdream | 0:d4524f1e749d | 23 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 24 | |
botdream | 0:d4524f1e749d | 25 | DigitalOut myled(LED1); |
botdream | 0:d4524f1e749d | 26 | |
botdream | 0:d4524f1e749d | 27 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 28 | // Main |
botdream | 0:d4524f1e749d | 29 | //--------------------------------------------------------------------------------------------- |
botdream | 0:d4524f1e749d | 30 | int main() |
botdream | 0:d4524f1e749d | 31 | { |
botdream | 0:d4524f1e749d | 32 | uint16_t impulse = 30733; |
botdream | 0:d4524f1e749d | 33 | uint16_t power; |
botdream | 0:d4524f1e749d | 34 | |
botdream | 0:d4524f1e749d | 35 | // setup time structure for Wed, 28 Oct 2009 11:35:37 |
botdream | 0:d4524f1e749d | 36 | char buffer[32]; |
botdream | 0:d4524f1e749d | 37 | struct tm t; |
botdream | 0:d4524f1e749d | 38 | t.tm_sec = 01; // 0-59 |
botdream | 0:d4524f1e749d | 39 | t.tm_min = 35; // 0-59 |
botdream | 0:d4524f1e749d | 40 | t.tm_hour = 23; // 0-23 |
botdream | 0:d4524f1e749d | 41 | t.tm_mday = 5; // 1-31 |
botdream | 0:d4524f1e749d | 42 | t.tm_mon = 4; // 0-11 |
botdream | 0:d4524f1e749d | 43 | t.tm_year = 112; // year since 1900 |
botdream | 0:d4524f1e749d | 44 | |
botdream | 0:d4524f1e749d | 45 | string data1 = "<msg><src>CC128-v1.29</src><dsb>00381</dsb><time>DATETIME</time><tmpr>26.1</tmpr><sensor>9</sensor><id>02063</id><type>2</type><imp>IMPULSE</imp><ipu>1000</ipu></msg>"; |
botdream | 0:d4524f1e749d | 46 | string data2 = "<msg><src>CC128-v1.29</src><dsb>00381</dsb><time>DATETIME</time><tmpr>26.0</tmpr><sensor>1</sensor><id>00815</id><type>1</type><ch1><watts>POWER</watts></ch1></msg>"; |
botdream | 0:d4524f1e749d | 47 | string data3 = "<msg><src>CC128-v1.29</src><dsb>00381</dsb><time>DATETIME</time><tmpr>26.0</tmpr><sensor>0</sensor><id>03386</id><type>1</type><ch1><watts>POWER</watts></ch1></msg>"; |
botdream | 0:d4524f1e749d | 48 | string parsedata; |
botdream | 0:d4524f1e749d | 49 | |
botdream | 0:d4524f1e749d | 50 | pc.baud(57600); |
botdream | 0:d4524f1e749d | 51 | pc.printf("\r\n--- CurrentCost XML Data Emulator---\r\n"); |
botdream | 0:d4524f1e749d | 52 | |
botdream | 0:d4524f1e749d | 53 | // convert to timestamp and display (1256729737) |
botdream | 0:d4524f1e749d | 54 | time_t seconds = mktime(&t); |
botdream | 0:d4524f1e749d | 55 | //pc.printf("Time as seconds since January 1, 1970 = %d\r\n", seconds); |
botdream | 0:d4524f1e749d | 56 | |
botdream | 0:d4524f1e749d | 57 | while(1) |
botdream | 0:d4524f1e749d | 58 | { |
botdream | 0:d4524f1e749d | 59 | myled = 1; |
botdream | 0:d4524f1e749d | 60 | |
botdream | 0:d4524f1e749d | 61 | // increments datetime |
botdream | 0:d4524f1e749d | 62 | seconds = seconds + 1; |
botdream | 0:d4524f1e749d | 63 | // gets datetime formated data |
botdream | 0:d4524f1e749d | 64 | strftime(buffer, 32, "%H:%M:%S", localtime(&seconds)); |
botdream | 0:d4524f1e749d | 65 | // replaces data with current datetime |
botdream | 0:d4524f1e749d | 66 | parsedata = data1; |
botdream | 0:d4524f1e749d | 67 | parsedata.replace(parsedata.find("DATETIME"), string("DATETIME").length(), buffer); |
botdream | 0:d4524f1e749d | 68 | // format impulse |
botdream | 0:d4524f1e749d | 69 | sprintf(buffer, "%010u", impulse); // 0000030733 |
botdream | 0:d4524f1e749d | 70 | parsedata.replace(parsedata.find("IMPULSE"), string("IMPULSE").length(), buffer); |
botdream | 0:d4524f1e749d | 71 | impulse = impulse + 1; |
botdream | 0:d4524f1e749d | 72 | |
botdream | 0:d4524f1e749d | 73 | // sends XML data |
botdream | 0:d4524f1e749d | 74 | pc.printf("%s\r\n",parsedata.c_str()); |
botdream | 0:d4524f1e749d | 75 | wait(1); |
botdream | 0:d4524f1e749d | 76 | |
botdream | 0:d4524f1e749d | 77 | myled = 0; |
botdream | 0:d4524f1e749d | 78 | |
botdream | 0:d4524f1e749d | 79 | // increments datetime |
botdream | 0:d4524f1e749d | 80 | seconds = seconds + 1; |
botdream | 0:d4524f1e749d | 81 | // gets datetime formated data |
botdream | 0:d4524f1e749d | 82 | strftime(buffer, 32, "%H:%M:%S", localtime(&seconds)); |
botdream | 0:d4524f1e749d | 83 | // replaces data with current datetime |
botdream | 0:d4524f1e749d | 84 | parsedata = data2; |
botdream | 0:d4524f1e749d | 85 | parsedata.replace(parsedata.find("DATETIME"), string("DATETIME").length(), buffer); |
botdream | 0:d4524f1e749d | 86 | // format power |
botdream | 0:d4524f1e749d | 87 | sprintf(buffer, "%05u", power); // 00715 |
botdream | 0:d4524f1e749d | 88 | parsedata.replace(parsedata.find("POWER"), string("POWER").length(), buffer); |
botdream | 0:d4524f1e749d | 89 | power = rand()%2500; |
botdream | 0:d4524f1e749d | 90 | |
botdream | 0:d4524f1e749d | 91 | // sends XML data |
botdream | 0:d4524f1e749d | 92 | pc.printf("%s\r\n",parsedata.c_str()); |
botdream | 0:d4524f1e749d | 93 | wait(1); |
botdream | 0:d4524f1e749d | 94 | |
botdream | 0:d4524f1e749d | 95 | |
botdream | 0:d4524f1e749d | 96 | // increments datetime |
botdream | 0:d4524f1e749d | 97 | seconds = seconds + 1; |
botdream | 0:d4524f1e749d | 98 | // gets datetime formated data |
botdream | 0:d4524f1e749d | 99 | strftime(buffer, 32, "%H:%M:%S", localtime(&seconds)); |
botdream | 0:d4524f1e749d | 100 | // replaces data with current datetime |
botdream | 0:d4524f1e749d | 101 | parsedata = data3; |
botdream | 0:d4524f1e749d | 102 | parsedata.replace(parsedata.find("DATETIME"), string("DATETIME").length(), buffer); |
botdream | 0:d4524f1e749d | 103 | // format power |
botdream | 0:d4524f1e749d | 104 | sprintf(buffer, "%05u", power); // 00715 |
botdream | 0:d4524f1e749d | 105 | parsedata.replace(parsedata.find("POWER"), string("POWER").length(), buffer); |
botdream | 0:d4524f1e749d | 106 | power = rand()%2500; |
botdream | 0:d4524f1e749d | 107 | |
botdream | 0:d4524f1e749d | 108 | // sends XML data |
botdream | 0:d4524f1e749d | 109 | pc.printf("%s\r\n",parsedata.c_str()); |
botdream | 0:d4524f1e749d | 110 | wait(1); |
botdream | 0:d4524f1e749d | 111 | } |
botdream | 0:d4524f1e749d | 112 | //return -1; |
botdream | 0:d4524f1e749d | 113 | } |
botdream | 0:d4524f1e749d | 114 | //--------------------------------------------------------------------------------------------- |