Nelson Neves / Mbed 2 deprecated CurrentCostXML

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //---------------------------------------------------------------------------------------------
00002 // CurrentCost XML Serial Data Emulator
00003 //---------------------------------------------------------------------------------------------
00004 // Simulating OptiSmart TX device with 2 configured sensors (0 and 9) 
00005 // and additional IAM power plug sensor (1):
00006 //
00007 // http://www.currentcost.com/cc128/xml.htm
00008 // http://currentcost.com/support/viewtopic.php?f=24&t=1284
00009 // https://docs.google.com/document/pub?id=1-Saz_uA8r6HJzGNoNTF2zpwz7BcjTTwpf0HOlhCpuGY
00010 //---------------------------------------------------------------------------------------------
00011 // Objectives: to emulate CurrentCost (EnviR Device) functionality to send XML data 
00012 // via USB Serial Port to be used to develop custom software application.
00013 //---------------------------------------------------------------------------------------------
00014 #include "mbed.h"
00015 #include <string>
00016 using std::string;
00017 //---------------------------------------------------------------------------------------------
00018 
00019 //---------------------------------------------------------------------------------------------
00020 // USB serial port setup - debug messages
00021 //---------------------------------------------------------------------------------------------
00022 Serial pc(USBTX, USBRX); // tx, rx  
00023 //---------------------------------------------------------------------------------------------  
00024 
00025 DigitalOut myled(LED1);
00026 
00027 //---------------------------------------------------------------------------------------------
00028 // Main
00029 //---------------------------------------------------------------------------------------------
00030 int main() 
00031 { 
00032   uint16_t impulse = 30733;
00033   uint16_t power;
00034   
00035   // setup time structure for Wed, 28 Oct 2009 11:35:37
00036   char buffer[32];
00037   struct tm t;
00038   t.tm_sec = 01;    // 0-59
00039   t.tm_min = 35;    // 0-59
00040   t.tm_hour = 23;   // 0-23
00041   t.tm_mday = 5;   // 1-31
00042   t.tm_mon = 4;     // 0-11
00043   t.tm_year = 112;  // year since 1900
00044 
00045   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>";
00046   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>";
00047   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>";
00048   string parsedata;  
00049 
00050   pc.baud(57600);  
00051   pc.printf("\r\n--- CurrentCost XML Data Emulator---\r\n");  
00052   
00053   // convert to timestamp and display (1256729737)
00054   time_t seconds = mktime(&t);
00055   //pc.printf("Time as seconds since January 1, 1970 = %d\r\n", seconds);            
00056 
00057   while(1) 
00058   {        
00059     myled = 1;
00060    
00061     // increments datetime
00062     seconds = seconds + 1;
00063     // gets datetime formated data
00064     strftime(buffer, 32, "%H:%M:%S", localtime(&seconds));
00065     // replaces data with current datetime    
00066     parsedata = data1;
00067     parsedata.replace(parsedata.find("DATETIME"), string("DATETIME").length(), buffer);
00068     // format impulse        
00069     sprintf(buffer, "%010u", impulse); // 0000030733    
00070     parsedata.replace(parsedata.find("IMPULSE"), string("IMPULSE").length(), buffer);
00071     impulse = impulse + 1;   
00072         
00073     // sends XML data
00074     pc.printf("%s\r\n",parsedata.c_str());
00075     wait(1);
00076 
00077     myled = 0;
00078     
00079     // increments datetime
00080     seconds = seconds + 1;
00081     // gets datetime formated data
00082     strftime(buffer, 32, "%H:%M:%S", localtime(&seconds));
00083     // replaces data with current datetime    
00084     parsedata = data2;
00085     parsedata.replace(parsedata.find("DATETIME"), string("DATETIME").length(), buffer);
00086     // format power
00087     sprintf(buffer, "%05u", power); // 00715
00088     parsedata.replace(parsedata.find("POWER"), string("POWER").length(), buffer);
00089     power = rand()%2500;
00090     
00091     // sends XML data
00092     pc.printf("%s\r\n",parsedata.c_str());
00093     wait(1);        
00094     
00095     
00096     // increments datetime
00097     seconds = seconds + 1;
00098     // gets datetime formated data
00099     strftime(buffer, 32, "%H:%M:%S", localtime(&seconds));
00100     // replaces data with current datetime    
00101     parsedata = data3;
00102     parsedata.replace(parsedata.find("DATETIME"), string("DATETIME").length(), buffer);
00103     // format power
00104     sprintf(buffer, "%05u", power); // 00715
00105     parsedata.replace(parsedata.find("POWER"), string("POWER").length(), buffer);
00106     power = rand()%2500;
00107 
00108     // sends XML data
00109     pc.printf("%s\r\n",parsedata.c_str());
00110     wait(1);
00111   }    
00112   //return -1;
00113 }
00114 //---------------------------------------------------------------------------------------------