├── .gitattributes ├── .gitignore ├── LeweiClient.cpp ├── LeweiClient.h ├── examples ├── JSY109Local │ └── JSY109Local.ino ├── JSY109Remote │ └── JSY109Remote.ino ├── Upload_Temperature_Humidity_Using_Sht11 │ └── Upload_Temperature_Humidity_Using_Sht11.ino ├── append │ └── append.ino ├── append_send │ └── append_send.ino └── particle │ └── DSM501A.ino ├── keywords.txt └── readme.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /LeweiClient.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #ifdef __ART__ 7 | #define malloc rt_malloc 8 | #define realloc rt_realloc 9 | #define free rt_free 10 | 11 | #define DEBUG_PRINTF rt_kprintf 12 | #else 13 | // taken from http://playground.arduino.cc/Main/Printf 14 | #include 15 | static void _print(char *fmt, ... ) 16 | { 17 | char tmp[128]; // resulting string limited to 128 chars 18 | va_list args; 19 | va_start (args, fmt ); 20 | vsnprintf(tmp, 128, fmt, args); 21 | va_end (args); 22 | Serial.print(tmp); 23 | } 24 | #define DEBUG_PRINTF _print 25 | #endif 26 | 27 | //#define DEBUG_PRINTF(...) 28 | 29 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 30 | char server[] = "open.lewei50.com"; 31 | 32 | LeWeiClient::LeWeiClient(const char * user_key, const char * gateway) 33 | { 34 | char *ptr = head; 35 | int head_length = 0; 36 | int tmp; 37 | 38 | user_data = NULL; 39 | user_str_length = 0; 40 | 41 | begin = false; 42 | end = false; 43 | 44 | // build head. 45 | tmp = sprintf(ptr, 46 | "POST /api/V1/Gateway/UpdateSensors/%s HTTP/1.1\r\n", 47 | gateway); 48 | head_length += tmp; 49 | ptr += tmp; 50 | 51 | // build userkey. 52 | tmp = sprintf(ptr, 53 | "userkey: %s\r\n", 54 | user_key); 55 | head_length += tmp; 56 | ptr += tmp; 57 | 58 | // build Host. 59 | tmp = sprintf(ptr, "Host: open.lewei50.com \r\n"); 60 | head_length += tmp; 61 | ptr += tmp; 62 | 63 | // build User-Agent. 64 | tmp = sprintf(ptr, "User-Agent: RT-Thread ART\r\n"); 65 | head_length += tmp; 66 | ptr += tmp; 67 | 68 | if (Ethernet.begin(mac) == 0) 69 | { 70 | DEBUG_PRINTF("Failed to configure Ethernet using DHCP\r\n"); 71 | } 72 | else 73 | { 74 | DEBUG_PRINTF("Ethernet configuration OK\r\n"); 75 | } 76 | } 77 | 78 | LeWeiClient::LeWeiClient(const char * user_key, const char *gateway,IPAddress ip,IPAddress dns,IPAddress gw,IPAddress subnet) 79 | { 80 | char *ptr = head; 81 | int head_length = 0; 82 | int tmp; 83 | 84 | user_data = NULL; 85 | user_str_length = 0; 86 | 87 | begin = false; 88 | end = false; 89 | 90 | // build head. 91 | tmp = sprintf(ptr, 92 | "POST /api/V1/Gateway/UpdateSensors/%s HTTP/1.1\r\n", 93 | gateway); 94 | head_length += tmp; 95 | ptr += tmp; 96 | 97 | // build userkey. 98 | tmp = sprintf(ptr, 99 | "userkey: %s\r\n", 100 | user_key); 101 | head_length += tmp; 102 | ptr += tmp; 103 | 104 | // build Host. 105 | tmp = sprintf(ptr, "Host: open.lewei50.com \r\n"); 106 | head_length += tmp; 107 | ptr += tmp; 108 | 109 | // build User-Agent. 110 | tmp = sprintf(ptr, "User-Agent: RT-Thread ART\r\n"); 111 | head_length += tmp; 112 | ptr += tmp; 113 | 114 | Ethernet.begin(mac,ip,dns,gw,subnet); 115 | } 116 | 117 | int LeWeiClient::append(const char * name, int value) 118 | { 119 | int length; 120 | int tmp; 121 | char * ptr; 122 | 123 | if(begin == false) 124 | { 125 | user_data = (char *)malloc(2); 126 | if(user_data == NULL) 127 | { 128 | return -1; 129 | } 130 | 131 | user_data[0] = '['; 132 | user_data[1] = 0; 133 | user_str_length = 1; 134 | 135 | begin = true; 136 | } 137 | 138 | if(user_data == NULL) 139 | { 140 | return -1; 141 | } 142 | 143 | length = 23; /* >>{"Name":"","Value":""},<< */ 144 | length += 8; /* name */ 145 | length += 10; /* value */ 146 | 147 | ptr = (char *)realloc(user_data, user_str_length + length + 1); 148 | if(ptr == NULL) 149 | { 150 | return -1; 151 | } 152 | user_data = ptr; 153 | 154 | ptr = user_data + user_str_length; 155 | 156 | length = sprintf(ptr, 157 | "{\"Name\":\"%s\",\"Value\":\"%d\"},", 158 | name, 159 | value); 160 | 161 | user_str_length += length; 162 | DEBUG_PRINTF("append:%s\r\n", ptr); 163 | } 164 | 165 | int LeWeiClient::append(const char * name, double value) 166 | { 167 | int length; 168 | int tmp; 169 | char * ptr; 170 | 171 | if(begin == false) 172 | { 173 | user_data = (char *)malloc(2); 174 | if(user_data == NULL) 175 | { 176 | DEBUG_PRINTF("malloc(2) ERROR!\r\n"); 177 | return -1; 178 | } 179 | 180 | user_data[0] = '['; 181 | user_data[1] = 0; 182 | user_str_length = 1; 183 | 184 | begin = true; 185 | } 186 | 187 | if(user_data == NULL) 188 | { 189 | DEBUG_PRINTF("user_data == NULL!\r\n"); 190 | return -1; 191 | } 192 | 193 | length = 23; /* >>{"Name":"","Value":""},<< */ 194 | length += 8; /* name */ 195 | length += 20; /* value: ab.cd */ 196 | 197 | ptr = (char *)realloc(user_data, user_str_length + length + 1); 198 | if(ptr == NULL) 199 | { 200 | DEBUG_PRINTF("realloc == NULL!\r\n"); 201 | return -1; 202 | } 203 | user_data = ptr; "", 204 | 205 | ptr = user_data + user_str_length; 206 | 207 | length = sprintf(ptr, 208 | "{\"Name\":\"%s\",\"Value\":\"%d.%02u\"},", 209 | name, 210 | (int)value, (long)(abs(value)*100+0.5) % 100); 211 | 212 | 213 | user_str_length += length; 214 | DEBUG_PRINTF("append:%s\r\n", ptr); 215 | } 216 | 217 | int LeWeiClient::send() 218 | { 219 | int result = 0; 220 | char value_str[6] = "00.00"; 221 | int total_len = 0; 222 | int tmp_len; 223 | 224 | 225 | if (client.connect(server, 80)) 226 | { 227 | // send the HTTP PUT request: 228 | client.print(head); 229 | 230 | client.print("Content-Length: "); 231 | client.println(strlen(user_data) + 1); // ']' 232 | 233 | // last pieces of the HTTP PUT request: 234 | client.println("Connection: close"); 235 | client.println(); 236 | 237 | // post data 238 | client.print(user_data); 239 | client.println("]"); 240 | 241 | DEBUG_PRINTF("data:%s]\r\n", user_data); 242 | 243 | result = 0; 244 | goto send_exit; 245 | } 246 | else 247 | { 248 | DEBUG_PRINTF("connect failed!\r\n"); 249 | result = -1; 250 | goto send_exit; 251 | } 252 | 253 | send_exit: 254 | client.stop(); 255 | 256 | begin = false; 257 | end = false; 258 | 259 | if(user_data != NULL) 260 | { 261 | free(user_data); 262 | } 263 | 264 | return result; 265 | } 266 | 267 | -------------------------------------------------------------------------------- /LeweiClient.h: -------------------------------------------------------------------------------- 1 | #ifndef __LEWEICLIENT_H__ 2 | #define __LEWEICLIENT_H__ 3 | 4 | #include 5 | 6 | class LeWeiClient 7 | { 8 | public: 9 | LeWeiClient(const char * user_key, const char *gateway); 10 | LeWeiClient(const char * user_key, const char *gateway,IPAddress ip,IPAddress dns,IPAddress gw,IPAddress subnet); 11 | // int begin(); 12 | int append(const char * name, int value); 13 | int append(const char * name, double value); 14 | // int end(); 15 | int send(); 16 | private: 17 | char head[160]; 18 | 19 | char * user_data; 20 | int user_str_length; 21 | 22 | bool begin; 23 | bool end; 24 | 25 | EthernetClient client; 26 | }; 27 | 28 | #endif /* end of include guard: __LEWEICLIENT_H__ */ 29 | -------------------------------------------------------------------------------- /examples/JSY109Local/JSY109Local.ino: -------------------------------------------------------------------------------- 1 | // LeWei AC Power Meter Module test program 2013.07.12 2 | // Change log: 3 | // 2013.07.12 v1 release 4 | 5 | /* FIXME: not yet being used */ 6 | unsigned long interframe_delay = 2; /* Modbus t3.5 = 2 ms */ 7 | 8 | /* 9 | * preset_multiple_registers: Modbus function 16. Write the data from an 10 | * array into the holding registers of a slave. 11 | * INPUTS 12 | * slave: modbus slave id number 13 | * start_addr: address of the slave's first register (+1) 14 | * reg_count: number of consecutive registers to preset 15 | * data: array of words (ints) with the data to write into the slave 16 | * RETURNS: the number of bytes received as response on success, or 17 | * 0 if no bytes received (i.e. response timeout) 18 | * -1 to -4 (modbus exception code) 19 | * -5 for other errors (port error, etc.). 20 | */ 21 | 22 | int preset_multiple_registers(int slave, int start_addr, 23 | int reg_count, int *data); 24 | 25 | /* 26 | * read_holding_registers: Modbus function 3. Read the holding registers 27 | * in a slave and put the data into an array 28 | * INPUTS 29 | * slave: modbus slave id number 30 | * start_addr: address of the slave's first register (+1) 31 | * count: number of consecutive registers to read 32 | * dest: array of words (ints) on which the read data is to be stored 33 | * dest_size: size of the array, which should be at least 'count' 34 | * RETURNS: the number of bytes received as response on success, or 35 | * 0 if no valid response received (i.e. response timeout, bad crc) 36 | * -1 to -4 (modbus exception code) 37 | * -5 for other errors (port error, etc.). 38 | */ 39 | 40 | int read_holding_registers(int slave, int start_addr, int count, 41 | int *dest, int dest_size); 42 | 43 | 44 | /* 45 | open.lewei50.com sensor client 46 | */ 47 | 48 | 49 | 50 | // #include 51 | // #include 52 | // #include 53 | 54 | // #define USERKEY "051066e888f24fa489d04e64d83bd2b8" // replace your key here 55 | // #define LW_GATEWAY "01" 56 | 57 | // LeWeiClient *lwc; 58 | 59 | /* 60 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 61 | boolean lastConnected = false; // state of the connection last time through the main loop 62 | const unsigned long postingInterval = 30*1000; //delay between updates to cosm.com 63 | 64 | 65 | 66 | unsigned long duration; 67 | unsigned long starttime; 68 | unsigned long sampletime_ms = 30000; 69 | unsigned long lowpulseoccupancy = 0; 70 | float ratio = 0; 71 | double concentration = 0; 72 | */ 73 | 74 | int pin = 8; 75 | 76 | void setup() { 77 | 78 | // start serial port: 79 | Serial.begin(4800); 80 | pinMode(8,INPUT); 81 | 82 | delay(1000); 83 | 84 | // lwc = new LeWeiClient(USERKEY, LW_GATEWAY); 85 | 86 | // starttime = millis(); 87 | } 88 | /* filter program : 20130521 */ 89 | #define FILTERLEN 10 90 | 91 | unsigned long Array_Average( unsigned long* Array,int length) 92 | { 93 | int x; 94 | unsigned long returnVal; 95 | unsigned long result=0; 96 | for(x=0;xsindex) 113 | { 114 | sindex++; 115 | Serial.println(sindex); 116 | sfiterArray[sindex]=lowpulse; 117 | Serial.println("filter1 END"); 118 | return lowpulse; 119 | } 120 | else 121 | { 122 | for(x=0;xappend("01", Voltage); 207 | lwc->append("02", Amp); 208 | lwc->append("03", Watt); 209 | lwc->append("04", Kwh); 210 | lwc->append("05", Pf); 211 | lwc->append("06", Cabon); 212 | 213 | 214 | lwc->send(); 215 | */ 216 | delay(5000); 217 | 218 | 219 | } 220 | 221 | // this method makes a HTTP connection to the server: 222 | 223 | 224 | /**************************************************************************** 225 | * BEGIN MODBUS RTU MASTER FUNCTIONS 226 | ****************************************************************************/ 227 | 228 | //#define TIMEOUT 1000 /* 1 second */ 229 | #define TIMEOUT 10000 /* 10 second */ 230 | #define MAX_READ_REGS 125 231 | #define MAX_WRITE_REGS 125 232 | #define MAX_RESPONSE_LENGTH 256 233 | #define PRESET_QUERY_SIZE 256 234 | /* errors */ 235 | #define PORT_ERROR -5 236 | 237 | /* 238 | CRC 239 | 240 | INPUTS: 241 | buf -> Array containing message to be sent to controller. 242 | start -> Start of loop in crc counter, usually 0. 243 | cnt -> Amount of bytes in message being sent to controller/ 244 | OUTPUTS: 245 | temp -> Returns crc byte for message. 246 | COMMENTS: 247 | This routine calculates the crc high and low byte of a message. 248 | Note that this crc is only used for Modbus, not Modbus+ etc. 249 | ****************************************************************************/ 250 | 251 | unsigned int crc(unsigned char *buf, int start, int cnt) 252 | { 253 | int i, j; 254 | unsigned temp, temp2, flag; 255 | 256 | temp = 0xFFFF; 257 | 258 | for (i = start; i < cnt; i++) { 259 | temp = temp ^ buf[i]; 260 | 261 | for (j = 1; j <= 8; j++) { 262 | flag = temp & 0x0001; 263 | temp = temp >> 1; 264 | if (flag) 265 | temp = temp ^ 0xA001; 266 | } 267 | } 268 | 269 | /* Reverse byte order. */ 270 | 271 | temp2 = temp >> 8; 272 | temp = (temp << 8) | temp2; 273 | temp &= 0xFFFF; 274 | 275 | return (temp); 276 | } 277 | 278 | 279 | /*********************************************************************** 280 | * 281 | * The following functions construct the required query into 282 | * a modbus query packet. 283 | * 284 | ***********************************************************************/ 285 | 286 | #define REQUEST_QUERY_SIZE 6 /* the following packets require */ 287 | #define CHECKSUM_SIZE 2 /* 6 unsigned chars for the packet plus */ 288 | /* 2 for the checksum. */ 289 | 290 | void build_request_packet(int slave, int function, int start_addr, 291 | int count, unsigned char *packet) 292 | { 293 | packet[0] = slave; 294 | packet[1] = function; 295 | start_addr -= 1; 296 | packet[2] = start_addr >> 8; 297 | packet[3] = start_addr & 0x00ff; 298 | packet[4] = count >> 8; 299 | packet[5] = count & 0x00ff; 300 | 301 | //below test only 302 | // packet[0] =0x01; 303 | // packet[1] = 0x03; 304 | // packet[2] = 0; 305 | // packet[3] = 0x48; 306 | // packet[4] = 0; 307 | // packet[5] = 0x02; 308 | } 309 | 310 | /************************************************************************* 311 | * 312 | * modbus_query( packet, length) 313 | * 314 | * Function to add a checksum to the end of a packet. 315 | * Please note that the packet array must be at least 2 fields longer than 316 | * string_length. 317 | **************************************************************************/ 318 | 319 | void modbus_query(unsigned char *packet, size_t string_length) 320 | { 321 | int temp_crc; 322 | 323 | temp_crc = crc(packet, 0, string_length); 324 | 325 | packet[string_length++] = temp_crc >> 8; 326 | packet[string_length++] = temp_crc & 0x00FF; 327 | packet[string_length] = 0; 328 | } 329 | 330 | 331 | 332 | /*********************************************************************** 333 | * 334 | * send_query(query_string, query_length ) 335 | * 336 | * Function to send a query out to a modbus slave. 337 | ************************************************************************/ 338 | 339 | int send_query(unsigned char *query, size_t string_length) 340 | { 341 | 342 | int i; 343 | 344 | modbus_query(query, string_length); 345 | string_length += 2; 346 | 347 | for (i = 0; i < string_length; i++) { 348 | // Serial.print(query[i], HEX); //Orginal 349 | Serial.write(query[i]); //JingLi 350 | 351 | } 352 | /* without the following delay, the reading of the response might be wrong 353 | * apparently, */ 354 | delay(200); /* FIXME: value to use? */ 355 | 356 | return i; /* it does not mean that the write was succesful, though */ 357 | } 358 | 359 | 360 | /*********************************************************************** 361 | * 362 | * receive_response( array_for_data ) 363 | * 364 | * Function to monitor for the reply from the modbus slave. 365 | * This function blocks for timeout seconds if there is no reply. 366 | * 367 | * Returns: Total number of characters received. 368 | ***********************************************************************/ 369 | 370 | int receive_response(unsigned char *received_string) 371 | { 372 | 373 | int bytes_received = 0; 374 | int i = 0; 375 | /* wait for a response; this will block! */ 376 | while(Serial.available() == 0) { 377 | delay(1); 378 | if (i++ > TIMEOUT) 379 | return bytes_received; 380 | } 381 | delay(200); 382 | /* FIXME: does Serial.available wait 1.5T or 3.5T before exiting the loop? */ 383 | while(Serial.available()) { 384 | received_string[bytes_received] = Serial.read(); 385 | // Serial.print(bytes_received); //only test 386 | // Serial.print("-"); //only test 387 | // Serial.println(received_string[bytes_received]); //only test 388 | bytes_received++; 389 | if (bytes_received >= MAX_RESPONSE_LENGTH) 390 | return PORT_ERROR; 391 | } 392 | //Serial.print("bytes_received="); 393 | //Serial.println(bytes_received); 394 | return (bytes_received); 395 | } 396 | 397 | 398 | /********************************************************************* 399 | * 400 | * modbus_response( response_data_array, query_array ) 401 | * 402 | * Function to the correct response is returned and that the checksum 403 | * is correct. 404 | * 405 | * Returns: string_length if OK 406 | * 0 if failed 407 | * Less than 0 for exception errors 408 | * 409 | * Note: All functions used for sending or receiving data via 410 | * modbus return these return values. 411 | * 412 | **********************************************************************/ 413 | 414 | int modbus_response(unsigned char *data, unsigned char *query) 415 | { 416 | int response_length; 417 | int i; 418 | unsigned int crc_calc = 0; 419 | unsigned int crc_received = 0; 420 | unsigned char recv_crc_hi; 421 | unsigned char recv_crc_lo; 422 | 423 | do { // repeat if unexpected slave replied 424 | response_length = receive_response(data); 425 | } 426 | while ((response_length > 0) && (data[0] != query[0])); 427 | // for (i = 0; i 0) 480 | raw_response_length -= 2; 481 | 482 | if (raw_response_length > 0) { 483 | /* FIXME: data[2] * 2 ???!!! data[2] isn't already the byte count (number of registers * 2)?! */ 484 | for (i = 0; 485 | i < (data[2] * 2) && i < (raw_response_length / 2); 486 | i++) { 487 | 488 | /* shift reg hi_byte to temp */ 489 | temp = data[3 + i * 2] << 8; 490 | /* OR with lo_byte */ 491 | temp = temp | data[4 + i * 2]; 492 | 493 | dest[i] = temp; 494 | } 495 | } 496 | return (raw_response_length); 497 | } 498 | 499 | 500 | /*********************************************************************** 501 | * 502 | * preset_response 503 | * 504 | * Gets the raw data from the input stream. 505 | * 506 | ***********************************************************************/ 507 | 508 | int preset_response(unsigned char *query) 509 | { 510 | unsigned char data[MAX_RESPONSE_LENGTH]; 511 | int raw_response_length; 512 | 513 | raw_response_length = modbus_response(data, query); 514 | 515 | return (raw_response_length); 516 | } 517 | 518 | 519 | /************************************************************************ 520 | * 521 | * read_holding_registers 522 | * 523 | * Read the holding registers in a slave and put the data into 524 | * an array. 525 | * 526 | *************************************************************************/ 527 | 528 | int read_holding_registers(int slave, int start_addr, int count, 529 | int *dest, int dest_size) 530 | { 531 | int function = 0x03; /* Function: Read Holding Registers */ 532 | int ret; 533 | 534 | unsigned char packet[REQUEST_QUERY_SIZE + CHECKSUM_SIZE]; 535 | 536 | if (count > MAX_READ_REGS) { 537 | count = MAX_READ_REGS; 538 | } 539 | 540 | build_request_packet(slave, function, start_addr, count, packet); 541 | 542 | if (send_query(packet, REQUEST_QUERY_SIZE) > -1) { 543 | ret = read_reg_response(dest, dest_size, packet); 544 | } 545 | else { 546 | 547 | ret = -1; 548 | } 549 | 550 | return (ret); 551 | } 552 | 553 | 554 | /************************************************************************ 555 | * 556 | * preset_multiple_registers 557 | * 558 | * Write the data from an array into the holding registers of a 559 | * slave. 560 | * 561 | *************************************************************************/ 562 | 563 | int preset_multiple_registers(int slave, int start_addr, 564 | int reg_count, int *data) 565 | { 566 | int function = 0x10; /* Function 16: Write Multiple Registers */ 567 | int byte_count, i, packet_size = 6; 568 | int ret; 569 | 570 | unsigned char packet[PRESET_QUERY_SIZE]; 571 | 572 | if (reg_count > MAX_WRITE_REGS) { 573 | reg_count = MAX_WRITE_REGS; 574 | } 575 | 576 | build_request_packet(slave, function, start_addr, reg_count, packet); 577 | byte_count = reg_count * 2; 578 | packet[6] = (unsigned char)byte_count; 579 | 580 | for (i = 0; i < reg_count; i++) { 581 | packet_size++; 582 | packet[packet_size] = data[i] >> 8; 583 | packet_size++; 584 | packet[packet_size] = data[i] & 0x00FF; 585 | } 586 | 587 | packet_size++; 588 | if (send_query(packet, packet_size) > -1) { 589 | ret = preset_response(packet); 590 | } 591 | else { 592 | ret = -1; 593 | } 594 | 595 | return (ret); 596 | } 597 | 598 | 599 | 600 | 601 | -------------------------------------------------------------------------------- /examples/JSY109Remote/JSY109Remote.ino: -------------------------------------------------------------------------------- 1 | // LeWei AC Power Meter trail success2013.06.25 2 | // LeWei AC Power Meter (ZongBiao60A)trail syccess 2013.06.30 18:50pm 3 | // Fixed a bug of string boundary and removed "start_addr -= 1". 4 | // Sending to LeWei one time every 4 Modbus reading. 2013.07.28 22:50 zlz 5 | // 6 | // 4 Parameter: watt / kwh / Amp / Voltage / Pf 7 | 8 | /* FIXME: not yet being used */ 9 | unsigned long interframe_delay = 2; /* Modbus t3.5 = 2 ms */ 10 | 11 | /* 12 | * preset_multiple_registers: Modbus function 16. Write the data from an 13 | * array into the holding registers of a slave. 14 | * INPUTS 15 | * slave: modbus slave id number 16 | * start_addr: address of the slave's first register (+1) 17 | * reg_count: number of consecutive registers to preset 18 | * data: array of words (ints) with the data to write into the slave 19 | * RETURNS: the number of bytes received as response on success, or 20 | * 0 if no bytes received (i.e. response timeout) 21 | * -1 to -4 (modbus exception code) 22 | * -5 for other errors (port error, etc.). 23 | */ 24 | 25 | int preset_multiple_registers(int slave, int start_addr, 26 | int reg_count, int *data); 27 | 28 | /* 29 | * read_holding_registers: Modbus function 3. Read the holding registers 30 | * in a slave and put the data into an array 31 | * INPUTS 32 | * slave: modbus slave id number 33 | * start_addr: address of the slave's first register (+1) 34 | * count: number of consecutive registers to read 35 | * dest: array of words (ints) on which the read data is to be stored 36 | * dest_size: size of the array, which should be at least 'count' 37 | * RETURNS: the number of bytes received as response on success, or 38 | * 0 if no valid response received (i.e. response timeout, bad crc) 39 | * -1 to -4 (modbus exception code) 40 | * -5 for other errors (port error, etc.). 41 | */ 42 | 43 | int read_holding_registers(int slave, int start_addr, int count, 44 | int *dest, int dest_size); 45 | 46 | 47 | /* 48 | open.lewei50.com sensor client 49 | */ 50 | 51 | 52 | 53 | #include 54 | #include 55 | #include 56 | 57 | #define USERKEY "a377c51c9c074c9791ec9ce1c6f69990" // replace your key here 58 | #define LW_GATEWAY "02" 59 | 60 | LeWeiClient *lwc; 61 | 62 | 63 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 64 | boolean lastConnected = false; // state of the connection last time through the main loop 65 | const unsigned long postingInterval = 30*1000; //delay between updates to cosm.com 66 | 67 | 68 | //int pin = 8; 69 | unsigned long duration; 70 | unsigned long starttime; 71 | unsigned long sampletime_ms = 30000; 72 | unsigned long lowpulseoccupancy = 0; 73 | float ratio = 0; 74 | double concentration = 0; 75 | 76 | void setup() { 77 | 78 | // start serial port: 79 | Serial.begin(4800); 80 | // pinMode(8,INPUT); 81 | 82 | //delay(10000); 83 | delay(3000); 84 | 85 | lwc = new LeWeiClient(USERKEY, LW_GATEWAY); 86 | 87 | starttime = millis(); 88 | } 89 | /* filter program : 20130521 */ 90 | /* 91 | #define FILTERLEN 10 92 | 93 | unsigned long Array_Average( unsigned long* Array,int length) 94 | { 95 | int x; 96 | unsigned long returnVal; 97 | unsigned long result=0; 98 | for(x=0;xsindex) 115 | { 116 | sindex++; 117 | Serial.println(sindex); 118 | sfiterArray[sindex]=lowpulse; 119 | Serial.println("filter1 END"); 120 | return lowpulse; 121 | } 122 | else 123 | { 124 | for(x=0;x in loop(), iii="); 163 | Serial.println(iii); 164 | /* example, this will write some data in the first 10 registers of slave 1 */ 165 | // retval = preset_multiple_registers(1,1,10, data); 166 | 167 | // data[0] = retval; 168 | // data[1]++; 169 | // data[8]=0xdead; 170 | // data[9] = 0xbeaf; 171 | // delay(500); 172 | //int read_holding_registers(int slave, int start_addr, int count,int *dest, int dest_size); 173 | // retval = read_holding_registers(2,1, 1,tt,6); 174 | delay(1000); //before read_holding_registers, delay is necessary! 175 | retval = read_holding_registers(1, 0x48, 6, tt, 1); // 1:5,,2:7,,3:9 //the last param is not useful here. 176 | //retval=999; 177 | // delay(1000); 178 | Serial.print("| receive flag(retval)="); 179 | Serial.println(retval); 180 | 181 | 182 | int Voltage = tt[0]; 183 | Voltage = Voltage / 100; 184 | float Amp = tt[1]; 185 | Amp = Amp / 1000; 186 | int Watt = tt[2]; 187 | //long y=x0*65536+x1; 188 | unsigned int Kwhh = (unsigned int)tt[3]; 189 | //unsigned int Kwhh = (unsigned int)65535; //test maximum 190 | unsigned int Kwhl = (unsigned int)tt[4]; 191 | unsigned long kwhA = (unsigned long) Kwhh *65536 + Kwhl; 192 | // unsigned long kwhA = Kwhh <<16 + Kwhl; 193 | float Kwh = kwhA; 194 | Kwh = Kwh / 3200; 195 | // double Kwh = kwhA / 3200; //Kwh = kwh / 32; 196 | // int Kwh = tt[4]; 197 | float Pf = tt[5]; 198 | Pf = Pf / 1000; 199 | 200 | /* 201 | unsigned int CarbonH = (unsigned int)tt[6]; 202 | Serial.print("CarbonH="); Serial.println(CarbonH, HEX); 203 | unsigned int CarbonL = (unsigned int)tt[7]; 204 | Serial.print("CarbonL="); Serial.println(CarbonL, HEX); 205 | unsigned long CarbonA = (unsigned long) CarbonH *65536 + CarbonL; 206 | float Carbon = ((float) CarbonA)/1000; //Unit:Kg. 207 | */ 208 | Serial.print("|"); Serial.print(Voltage); 209 | Serial.print("|"); Serial.print(Amp); 210 | Serial.print("|"); Serial.print(Watt); 211 | Serial.print("|"); Serial.print(Kwh); 212 | Serial.print("|"); Serial.print(Pf); 213 | //Serial.print("|"); Serial.print(Carbon); 214 | 215 | Serial.println(""); 216 | Serial.print("Kwh="); Serial.println(Kwh); 217 | 218 | 219 | 220 | 221 | // 4 Parameter: watt / kwh / Amp / Voltage / Pf 222 | lwc->append("PC", Kwh); 223 | lwc->append("P", Watt); 224 | lwc->append("I", Amp); 225 | lwc->append("U", Voltage); 226 | lwc->append("PF", Pf); 227 | // lwc->append("06", Cabon); 228 | Serial.println("lwc->append completed"); 229 | lwc->send(); 230 | Serial.println("lwc->send"); 231 | 232 | // Serial.print("Before end of for/while loop, iii="); 233 | // Serial.println(iii); 234 | if(iii==3 && lw_flag >0){ 235 | // lwc->send(); 236 | lw_flag = 0; 237 | // Serial.println("lwc->send"); 238 | } 239 | delay(13250); //delay about 15s in total per loop. 240 | 241 | iii++; 242 | //Serial.print("Before End of main loop, iii=" ); 243 | //Serial.println(iii); 244 | // Serial.print("end loop\n"); 245 | 246 | } //end of main loop. 247 | // this method makes a HTTP connection to the server: 248 | 249 | 250 | /**************************************************************************** 251 | * BEGIN MODBUS RTU MASTER FUNCTIONS 252 | ****************************************************************************/ 253 | 254 | //#define TIMEOUT 1000 /* 1 second */ 255 | #define TIMEOUT 10000 /* 10 second */ 256 | #define MAX_READ_REGS 125 257 | #define MAX_WRITE_REGS 125 258 | #define MAX_RESPONSE_LENGTH 256 259 | #define PRESET_QUERY_SIZE 256 260 | /* errors */ 261 | #define PORT_ERROR -5 262 | 263 | /* 264 | CRC 265 | 266 | INPUTS: 267 | buf -> Array containing message to be sent to controller. 268 | start -> Start of loop in crc counter, usually 0. 269 | cnt -> Amount of bytes in message being sent to controller/ 270 | OUTPUTS: 271 | temp -> Returns crc byte for message. 272 | COMMENTS: 273 | This routine calculates the crc high and low byte of a message. 274 | Note that this crc is only used for Modbus, not Modbus+ etc. 275 | ****************************************************************************/ 276 | 277 | unsigned int crc(unsigned char *buf, int start, int cnt) 278 | { 279 | int i, j; 280 | unsigned temp, temp2, flag; 281 | 282 | temp = 0xFFFF; 283 | 284 | for (i = start; i < cnt; i++) { 285 | temp = temp ^ buf[i]; 286 | 287 | for (j = 1; j <= 8; j++) { 288 | flag = temp & 0x0001; 289 | temp = temp >> 1; 290 | if (flag) 291 | temp = temp ^ 0xA001; 292 | } 293 | } 294 | 295 | /* Reverse byte order. */ 296 | 297 | temp2 = temp >> 8; 298 | temp = (temp << 8) | temp2; 299 | temp &= 0xFFFF; 300 | 301 | return (temp); 302 | } 303 | 304 | 305 | /*********************************************************************** 306 | * 307 | * The following functions construct the required query into 308 | * a modbus query packet. 309 | * 310 | ***********************************************************************/ 311 | 312 | #define REQUEST_QUERY_SIZE 6 /* the following packets require */ 313 | #define CHECKSUM_SIZE 2 /* 6 unsigned chars for the packet plus */ 314 | /* 2 for the checksum. */ 315 | 316 | void build_request_packet(int slave, int function, int start_addr, 317 | int count, unsigned char *packet) 318 | { 319 | packet[0] = slave; 320 | packet[1] = function; 321 | // start_addr -= 1; zlz removed. 322 | packet[2] = start_addr >> 8; 323 | packet[3] = start_addr & 0x00ff; 324 | packet[4] = count >> 8; 325 | packet[5] = count & 0x00ff; 326 | 327 | //below test only 328 | // packet[0] =0x01; 329 | // packet[1] = 0x03; 330 | // packet[2] = 0; 331 | // packet[3] = 0x48; 332 | // packet[4] = 0; 333 | // packet[5] = 0x02; 334 | } 335 | 336 | /************************************************************************* 337 | * 338 | * modbus_query( packet, length) 339 | * 340 | * Function to add a checksum to the end of a packet. 341 | * Please note that the packet array must be at least 2 fields longer than 342 | * string_length. 343 | **************************************************************************/ 344 | 345 | void modbus_query(unsigned char *packet, size_t string_length) 346 | { 347 | int temp_crc; 348 | 349 | // temp_crc = 0x45DE; //debug zzz 350 | temp_crc = crc(packet, 0, string_length); 351 | 352 | packet[string_length++] = temp_crc >> 8; 353 | packet[string_length++] = temp_crc & 0x00FF; 354 | packet[string_length] = 0; 355 | } 356 | 357 | 358 | 359 | /*********************************************************************** 360 | * 361 | * send_query(query_string, query_length ) 362 | * 363 | * Function to send a query out to a modbus slave. 364 | ************************************************************************/ 365 | 366 | int send_query(unsigned char *query, size_t string_length) 367 | { 368 | 369 | int i; 370 | 371 | //below test only 372 | /* 373 | query[0] =0x01; 374 | query[1] = 0x03; 375 | query[2] = 0; 376 | query[3] = 0x48; 377 | query[4] = 0; 378 | query[5] = 0x06; 379 | query[6] = 0x45; 380 | query[7] = 0xDE; 381 | */ 382 | modbus_query(query, string_length); 383 | string_length += 2; 384 | 385 | //DEBUG zlz 386 | // for (i = 0; i < string_length; i++) { 387 | // Serial.print(" ") ; Serial.print(query[i], HEX); //zlz 388 | // } 389 | // delay(2000); 390 | 391 | for (i = 0; i < string_length; i++) { 392 | // Serial.print(query[i], HEX); //Orginal 393 | Serial.write(query[i]); //JingLi 394 | } 395 | /* without the following delay, the reading of the response might be wrong 396 | * apparently, */ 397 | delay(200); /* FIXME: value to use? */ 398 | 399 | return i; /* it does not mean that the write was succesful, though */ 400 | } 401 | 402 | 403 | /*********************************************************************** 404 | * 405 | * receive_response( array_for_data ) 406 | * 407 | * Function to monitor for the reply from the modbus slave. 408 | * This function blocks for timeout seconds if there is no reply. 409 | * 410 | * Returns: Total number of characters received. 411 | ***********************************************************************/ 412 | 413 | int receive_response(unsigned char *received_string) 414 | { 415 | int bytes_received = 0; 416 | int i = 0; 417 | 418 | /* wait for a response; this will block! */ 419 | while(Serial.available() == 0) { 420 | delay(1); 421 | if (i++ > TIMEOUT) 422 | return bytes_received; 423 | } 424 | delay(200); 425 | /* FIXME: does Serial.available wait 1.5T or 3.5T before exiting the loop? */ 426 | while(Serial.available()) { 427 | received_string[bytes_received] = Serial.read(); //ORIGINAL. 428 | // Serial.print(bytes_received); //only test 429 | // Serial.print("-"); //only test 430 | // Serial.println(received_string[bytes_received]); //only test 431 | bytes_received++; 432 | if (bytes_received >= MAX_RESPONSE_LENGTH) 433 | return PORT_ERROR; 434 | } 435 | //Serial.print("bytes_received="); 436 | //Serial.println(bytes_received); 437 | return (bytes_received); 438 | } 439 | 440 | 441 | /********************************************************************* 442 | * 443 | * modbus_response( response_data_array, query_array ) 444 | * 445 | * Function to the correct response is returned and that the checksum 446 | * is correct. 447 | * 448 | * Returns: string_length if OK 449 | * 0 if failed 450 | * Less than 0 for exception errors 451 | * 452 | * Note: All functions used for sending or receiving data via 453 | * modbus return these return values. 454 | * 455 | **********************************************************************/ 456 | 457 | int modbus_response(unsigned char *data, unsigned char *query) 458 | { 459 | int response_length; 460 | int i; 461 | unsigned int crc_calc = 0; 462 | unsigned int crc_received = 0; 463 | unsigned char recv_crc_hi; 464 | unsigned char recv_crc_lo; 465 | 466 | do { // repeat if unexpected slave replied 467 | response_length = receive_response(data); 468 | } 469 | while ((response_length > 0) && (data[0] != query[0])); 470 | // for (i = 0; i 0) 523 | raw_response_length -= 2; 524 | 525 | if (raw_response_length > 0) { 526 | /* FIXME: data[2] * 2 ???!!! data[2] isn't already the byte count (number of registers * 2)?! */ 527 | for (i = 0; 528 | i < (data[2] * 2) && i < (raw_response_length / 2); 529 | i++) { 530 | 531 | /* shift reg hi_byte to temp */ 532 | temp = data[3 + i * 2] << 8; 533 | /* OR with lo_byte */ 534 | temp = temp | data[4 + i * 2]; 535 | 536 | dest[i] = temp; 537 | } 538 | } 539 | return (raw_response_length); 540 | } 541 | 542 | 543 | /*********************************************************************** 544 | * 545 | * preset_response 546 | * 547 | * Gets the raw data from the input stream. 548 | * 549 | ***********************************************************************/ 550 | 551 | int preset_response(unsigned char *query) 552 | { 553 | unsigned char data[MAX_RESPONSE_LENGTH]; 554 | int raw_response_length; 555 | 556 | raw_response_length = modbus_response(data, query); 557 | 558 | return (raw_response_length); 559 | } 560 | 561 | 562 | /************************************************************************ 563 | * 564 | * read_holding_registers 565 | * 566 | * Read the holding registers in a slave and put the data into 567 | * an array. 568 | * 569 | *************************************************************************/ 570 | 571 | int read_holding_registers(int slave, int start_addr, int count, 572 | int *dest, int dest_size) 573 | { 574 | int function = 0x03; /* Function: Read Holding Registers */ 575 | int ret; 576 | 577 | // unsigned char packet[REQUEST_QUERY_SIZE + CHECKSUM_SIZE]; 578 | unsigned char packet[REQUEST_QUERY_SIZE + CHECKSUM_SIZE +1]; //The bug. zlz corrected. Need "+1"! 579 | // as checksum 2bytes, 1more byte = '\0'; 580 | 581 | if (count > MAX_READ_REGS) { 582 | count = MAX_READ_REGS; 583 | } 584 | 585 | //Serial.println("build_request_packet."); 586 | build_request_packet(slave, function, start_addr, count, packet); 587 | 588 | //Serial.print("send_query(packet, REQUEST_QUERY_SIZE)"); Serial.println(REQUEST_QUERY_SIZE); 589 | if (send_query(packet, REQUEST_QUERY_SIZE) > -1) { 590 | // if(1==1 ) { //debug 591 | // Serial.print("read_reg_response(dest, dest_size, packet)"); Serial.println(dest_size); 592 | ret = read_reg_response(dest, dest_size, packet); 593 | // ret=999;//zzz 594 | } 595 | else { 596 | 597 | ret = -1; 598 | } 599 | 600 | return (ret); 601 | } 602 | 603 | 604 | /************************************************************************ 605 | * 606 | * preset_multiple_registers 607 | * 608 | * Write the data from an array into the holding registers of a 609 | * slave. 610 | * 611 | *************************************************************************/ 612 | 613 | int preset_multiple_registers(int slave, int start_addr, 614 | int reg_count, int *data) 615 | { 616 | int function = 0x10; /* Function 16: Write Multiple Registers */ 617 | int byte_count, i, packet_size = 6; 618 | int ret; 619 | 620 | unsigned char packet[PRESET_QUERY_SIZE]; 621 | 622 | if (reg_count > MAX_WRITE_REGS) { 623 | reg_count = MAX_WRITE_REGS; 624 | } 625 | 626 | build_request_packet(slave, function, start_addr, reg_count, packet); 627 | byte_count = reg_count * 2; 628 | packet[6] = (unsigned char)byte_count; 629 | 630 | for (i = 0; i < reg_count; i++) { 631 | packet_size++; 632 | packet[packet_size] = data[i] >> 8; 633 | packet_size++; 634 | packet[packet_size] = data[i] & 0x00FF; 635 | } 636 | 637 | packet_size++; 638 | if (send_query(packet, packet_size) > -1) { 639 | ret = preset_response(packet); 640 | } 641 | else { 642 | ret = -1; 643 | } 644 | 645 | return (ret); 646 | } 647 | 648 | -------------------------------------------------------------------------------- /examples/Upload_Temperature_Humidity_Using_Sht11/Upload_Temperature_Humidity_Using_Sht11.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Devicebit open platform sensor client 3 | This code is in the public domain. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // Specify data and clock connections and instantiate SHT1x object 12 | #define dataPin 4 13 | #define clockPin 5 14 | SHT1x sht1x(dataPin, clockPin); 15 | 16 | //put your api key here,find it in lewei50.com->my account->account setting 17 | #define LW_USERKEY "YOUR_API_KEY_HERE" 18 | //put your gateway number here,01 as default 19 | #define LW_GATEWAY "01" 20 | 21 | //delay between updates 22 | #define POST_INTERVAL (30*1000) 23 | 24 | LeWeiClient *lwc; 25 | 26 | 27 | void setup() { 28 | // start serial port: 29 | Serial.begin(9600); 30 | // hope no exception here 31 | lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY); 32 | } 33 | 34 | void loop() { 35 | // read the analog sensor: 36 | //int sensorReading = analogRead(A0); 37 | 38 | // if there's incoming data from the net connection. 39 | // send it out the serial port. This is for debugging 40 | // purposes only: 41 | 42 | if (lwc) { 43 | Serial.println("read data "); 44 | 45 | float temp_c; 46 | float temp_f; 47 | float humidity; 48 | 49 | // Read values from the sensor 50 | temp_c = sht1x.readTemperatureC(); 51 | temp_f = sht1x.readTemperatureF(); 52 | humidity = sht1x.readHumidity(); 53 | //t1,t2.. must using the same name setting on web server. 54 | lwc->append("temp", temp_c); 55 | lwc->append("hum", humidity); 56 | //Serial.print("*** data send ***"); 57 | lwc->send(); 58 | //Grammar changed by Wei&Anonymous ;) 59 | Serial.println("*** send completed ***"); 60 | 61 | delay(POST_INTERVAL); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /examples/append/append.ino: -------------------------------------------------------------------------------- 1 | /* 2 | open.lewei50.com sensor client 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #define USERKEY "xxx" // replace your key here 9 | 10 | 11 | #define LW_GATEWAY "01" 12 | 13 | 14 | LeWeiClient *lwc; 15 | 16 | 17 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 18 | boolean lastConnected = false; // state of the connection last time through the main loop 19 | const unsigned long postingInterval = 30*1000; //delay between updates to cosm.com 20 | 21 | 22 | int pin = 8; 23 | unsigned long duration; 24 | unsigned long starttime; 25 | unsigned long sampletime_ms = 30000; 26 | unsigned long lowpulseoccupancy = 0; 27 | float ratio = 0; 28 | double concentration = 0; 29 | 30 | void setup() { 31 | 32 | // start serial port: 33 | Serial.begin(9600); 34 | pinMode(8,INPUT); 35 | 36 | lwc = new LeWeiClient(USERKEY, LW_GATEWAY); 37 | 38 | starttime = millis(); 39 | } 40 | /* filter program : 20130521 */ 41 | #define FILTERLEN 10 42 | 43 | unsigned long Array_Average( unsigned long* Array,int length) 44 | { 45 | int x; 46 | unsigned long returnVal; 47 | unsigned long result=0; 48 | for(x=0;xsindex) 65 | { 66 | sindex++; 67 | Serial.println(sindex); 68 | sfiterArray[sindex]=lowpulse; 69 | Serial.println("filter1 END"); 70 | return lowpulse; 71 | } 72 | else 73 | { 74 | for(x=0;xappend("p1", 100.33); 101 | lwc->append("abc", 10000); 102 | lwc->append("def", 25.33); 103 | 104 | 105 | lwc->send(); 106 | delay(5000); 107 | } 108 | 109 | // this method makes a HTTP connection to the server: 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /examples/append_send/append_send.ino: -------------------------------------------------------------------------------- 1 | /* 2 | lewei50 open platform sensor client 3 | This code is in the public domain. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #define LW_USERKEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 11 | #define LW_GATEWAY "01" 12 | 13 | //delay between updates 14 | #define POST_INTERVAL (30*1000) 15 | 16 | 17 | //IPAddress ip(192,168,1, 15); 18 | //IPAddress mydns(8,8,8,8); 19 | //IPAddress gw(192,168,1,1); 20 | //IPAddress subnet(255,255,255,0); 21 | 22 | LeWeiClient *lwc; 23 | 24 | 25 | void setup() { 26 | // start serial port: 27 | Serial.begin(9600); 28 | // hope no exception here 29 | lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY); 30 | //lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY,ip,mydns,gw,subnet); 31 | } 32 | 33 | void loop() { 34 | // read the analog sensor: 35 | //int sensorReading = analogRead(A0); 36 | 37 | // if there's incoming data from the net connection. 38 | // send it out the serial port. This is for debugging 39 | // purposes only: 40 | 41 | if (lwc) { 42 | Serial.print("*** start data collection "); 43 | 44 | //t1,t2.. must using the same name setting on web server. 45 | lwc->append("t1", 123); 46 | lwc->append("t2", 456); 47 | Serial.print("*** data send ***"); 48 | lwc->send(); 49 | //Grammar changed by Wei&Anonymous ;) 50 | Serial.print("*** send completed ***"); 51 | 52 | delay(POST_INTERVAL); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /examples/particle/DSM501A.ino: -------------------------------------------------------------------------------- 1 | /* 2 | open.lewei50.com sensor client 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #define USERKEY "xxxxxxxxxxxx" // replace your key here 9 | 10 | 11 | #define LW_GATEWAY "01" 12 | 13 | 14 | LeWeiClient *lwc; 15 | 16 | 17 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 18 | boolean lastConnected = false; // state of the connection last time through the main loop 19 | const unsigned long postingInterval = 30*1000; //delay between updates to cosm.com 20 | 21 | 22 | int pin = 8; 23 | unsigned long duration; 24 | unsigned long starttime; 25 | unsigned long sampletime_ms = 30000; 26 | unsigned long lowpulseoccupancy = 0; 27 | float ratio = 0; 28 | 29 | double concentration = 0; 30 | double concentrationNofilter=0; 31 | 32 | void setup() { 33 | 34 | // start serial port: 35 | Serial.begin(9600); 36 | pinMode(8,INPUT); 37 | 38 | lwc = new LeWeiClient(USERKEY, LW_GATEWAY); 39 | 40 | starttime = millis(); 41 | } 42 | /* filter program : 20130521 */ 43 | #define FILTERLEN 10 44 | 45 | unsigned long Array_Average( unsigned long* Array,int length) 46 | { 47 | int x; 48 | unsigned long returnVal; 49 | unsigned long result=0; 50 | for(x=0;xsindex) 67 | { 68 | sindex++; 69 | Serial.println(sindex); 70 | sfiterArray[sindex]=lowpulse; 71 | Serial.println("filter1 END"); 72 | return lowpulse; 73 | } 74 | else 75 | { 76 | for(x=0;x sampletime_ms) 113 | { 114 | Serial.print("before filter:"); 115 | Serial.println(lowpulseoccupancy); 116 | /*start:get the result without filter*/ 117 | ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100 118 | concentrationNofilter = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve 119 | /*end*/ 120 | 121 | lowpulseoccupancy=Filter1(lowpulseoccupancy); 122 | 123 | //Serial.print("behind filter:"); 124 | //Serial.println(lowpulseoccupancy); 125 | ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100 126 | concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve 127 | Serial.print(lowpulseoccupancy); 128 | Serial.print(","); 129 | Serial.print(ratio); 130 | Serial.print(","); 131 | Serial.println(concentration); 132 | lowpulseoccupancy = 0; 133 | //initiate the http post 134 | sampling=0; 135 | transfering=1; 136 | } 137 | } 138 | // http post begin 139 | if(1==transfering) 140 | { 141 | lwc->append("p1", concentration); 142 | lwc->append("p2",concentrationNofilter); 143 | 144 | 145 | lwc->send(); 146 | Serial.print("leweiclient send:"); 147 | Serial.println(concentration); 148 | transfering=0; 149 | sampling=1; 150 | starttime=millis(); 151 | } 152 | 153 | 154 | } 155 | 156 | // this method makes a HTTP connection to the server: 157 | 158 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For LeweiTcpClient 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | LeweiClient KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | append KEYWORD2 15 | send KEYWORD2 16 | ####################################### 17 | # Constants (LITERAL1) 18 | ####################################### 19 | 20 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | LeweiClient --------------------------------------------------------------------------------