a demo to post your sensor data from ARCH GPRS to Xively
Fork of USB2UART by
ARCH GPRS With Xively
- Xively is an on-line database service allowing developers to connect sensor-derived data (e.g. energy and environment data from objects, devices & buildings) to the Web and to build their own applications based on that data,to know more, visit https://xively.com/
- Here, we use ARCH GPRS to post our sensor data(temperature/light/humidity etc.) to Xively.
ARCH GPRS Introduction
- Arch GPRS is a wireless network module based on EG-10, it can achive remote data colletion function to communicate with outside world by GPRS network.
- Arch GPRS has standard Arduino interface and Grove connectors. It’s convenient to connect existing Shields and Grove products to Arch GPRS.
- You can use the solar panel to charge for battery, and own to its low-power design, so Arch GPRS can work normally in outdoor.
- For more information, please visit http://www.seeedstudio.com/depot/arch-gprs-p-1657.html?cPath=6_11
Code
- Description: this demo is used as posting value of sound sensor to Xively every 10 seconds.
- Attention: you need to replace the PHONE_NUMBER/FEED_ID/SENSOR_ID/XIVELY_KEY with yours.
main.cpp
#include "gprs.h" #define PHONE_NUMBER "139****7382" #define IP_ADDRSS "216.52.233.120" //api.xively.com #define PORT "80" //port addr #define FEED_ID "your feeed ID" #define SENSOR_ID "your sensor ID" #define XIVELY_KEY "your Xively Key" #define REQUEST_LENGTH 36 #define DATA_LENGTH 90 #define HEAD_LEN 270 #define NETWORK_APN "CMNET" //replace APN in your country #define PINPWR P1_2 // power on EG 10, low enable #define PINONOFF P1_7 // switch of EG10, low enable, low for 2s to turn on EG10 DigitalOut eg10_pwr(PINPWR); DigitalOut eg10_on(PINONOFF); GPRS gprs(USBTX, USBRX,115200,PHONE_NUMBER); AnalogIn soundSensor(P0_11); void EG10_PowerUp(void) { eg10_pwr = 1; eg10_on = 1; wait(2); eg10_pwr = 0; eg10_on = 0; wait(2); eg10_on = 1; wait(2); } int putDataToXively(float sensorValue) { char request[REQUEST_LENGTH]; char dataStream[DATA_LENGTH]; char head[HEAD_LEN]; snprintf(request,REQUEST_LENGTH,"PUT /v2/feeds/%s HTTP/1.1\r\n",FEED_ID); snprintf(dataStream,DATA_LENGTH,"{\"version\":\"1.0.0\", \"datastreams\" : [{ \"id\" : \"%s\", \"current_value\" : \"%f\"}]}\r\n",SENSOR_ID,sensorValue); int dataLen = strlen(dataStream); snprintf(head,HEAD_LEN,"%sHost: api.xively.com\r\nX-ApiKey: %s\r\nUser-Agent: Xively-Arduino-Lib/1.0\r\nContent-Length: %d\r\n\r\n%s",request,XIVELY_KEY,dataLen,dataStream); if(0 != gprs.networkInit(NETWORK_APN,NULL,NULL)){ //APN,User,PassWd return -1; } if(0 != gprs.connectTCP(IP_ADDRSS,PORT)) { goto STOP; } wait(5); if(0 != gprs.sendTCPData(head)) { goto STOP; } STOP: gprs.closeTCP(); return 0; } int main() { EG10_PowerUp(); while(0 != gprs.init()) { wait(2); } while(1) { putDataToXively(10*soundSensor.read()); wait(10); } }
main.cpp@5:97b82fe2c712, 2014-01-22 (annotated)
- Committer:
- lawliet
- Date:
- Wed Jan 22 02:06:06 2014 +0000
- Revision:
- 5:97b82fe2c712
- Parent:
- 3:40d09aa56b86
version 2.1 (update with GPRS library)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 3:40d09aa56b86 | 1 | #include "gprs.h" |
yihui | 0:8c4eea221dcf | 2 | |
lawliet | 3:40d09aa56b86 | 3 | #define PHONE_NUMBER "139****7382" |
lawliet | 3:40d09aa56b86 | 4 | #define IP_ADDRSS "216.52.233.120" //api.xively.com |
lawliet | 3:40d09aa56b86 | 5 | #define PORT "80" //port addr |
lawliet | 3:40d09aa56b86 | 6 | #define FEED_ID "your feeed ID" |
lawliet | 3:40d09aa56b86 | 7 | #define SENSOR_ID "your sensor ID" |
lawliet | 3:40d09aa56b86 | 8 | #define XIVELY_KEY "your Xively Key" |
lawliet | 3:40d09aa56b86 | 9 | #define REQUEST_LENGTH 36 |
lawliet | 3:40d09aa56b86 | 10 | #define DATA_LENGTH 90 |
lawliet | 3:40d09aa56b86 | 11 | #define HEAD_LEN 270 |
lawliet | 3:40d09aa56b86 | 12 | |
lawliet | 5:97b82fe2c712 | 13 | #define NETWORK_APN "CMNET" //replace APN in your country |
lawliet | 5:97b82fe2c712 | 14 | |
lawliet | 3:40d09aa56b86 | 15 | #define PINPWR P1_2 // power on EG 10, low enable |
lawliet | 3:40d09aa56b86 | 16 | #define PINONOFF P1_7 // switch of EG10, low enable, low for 2s to turn on EG10 |
lawliet | 3:40d09aa56b86 | 17 | |
lawliet | 3:40d09aa56b86 | 18 | DigitalOut eg10_pwr(PINPWR); |
lawliet | 3:40d09aa56b86 | 19 | DigitalOut eg10_on(PINONOFF); |
lawliet | 3:40d09aa56b86 | 20 | GPRS gprs(USBTX, USBRX,115200,PHONE_NUMBER); |
lawliet | 3:40d09aa56b86 | 21 | AnalogIn soundSensor(P0_11); |
yihui | 0:8c4eea221dcf | 22 | |
lawliet | 3:40d09aa56b86 | 23 | void EG10_PowerUp(void) |
lawliet | 3:40d09aa56b86 | 24 | { |
lawliet | 3:40d09aa56b86 | 25 | eg10_pwr = 1; |
lawliet | 3:40d09aa56b86 | 26 | eg10_on = 1; |
lawliet | 3:40d09aa56b86 | 27 | wait(2); |
lawliet | 3:40d09aa56b86 | 28 | eg10_pwr = 0; |
lawliet | 3:40d09aa56b86 | 29 | eg10_on = 0; |
lawliet | 3:40d09aa56b86 | 30 | wait(2); |
lawliet | 3:40d09aa56b86 | 31 | eg10_on = 1; |
lawliet | 3:40d09aa56b86 | 32 | wait(2); |
lawliet | 3:40d09aa56b86 | 33 | } |
lawliet | 3:40d09aa56b86 | 34 | |
lawliet | 3:40d09aa56b86 | 35 | int putDataToXively(float sensorValue) |
yihui | 0:8c4eea221dcf | 36 | { |
lawliet | 3:40d09aa56b86 | 37 | char request[REQUEST_LENGTH]; |
lawliet | 3:40d09aa56b86 | 38 | char dataStream[DATA_LENGTH]; |
lawliet | 3:40d09aa56b86 | 39 | char head[HEAD_LEN]; |
lawliet | 3:40d09aa56b86 | 40 | snprintf(request,REQUEST_LENGTH,"PUT /v2/feeds/%s HTTP/1.1\r\n",FEED_ID); |
lawliet | 3:40d09aa56b86 | 41 | snprintf(dataStream,DATA_LENGTH,"{\"version\":\"1.0.0\", \"datastreams\" : [{ \"id\" : \"%s\", \"current_value\" : \"%f\"}]}\r\n",SENSOR_ID,sensorValue); |
lawliet | 3:40d09aa56b86 | 42 | int dataLen = strlen(dataStream); |
lawliet | 3:40d09aa56b86 | 43 | snprintf(head,HEAD_LEN,"%sHost: api.xively.com\r\nX-ApiKey: %s\r\nUser-Agent: Xively-Arduino-Lib/1.0\r\nContent-Length: %d\r\n\r\n%s",request,XIVELY_KEY,dataLen,dataStream); |
lawliet | 5:97b82fe2c712 | 44 | if(0 != gprs.networkInit(NETWORK_APN,NULL,NULL)){ //APN,User,PassWd |
lawliet | 5:97b82fe2c712 | 45 | return -1; |
lawliet | 5:97b82fe2c712 | 46 | } |
lawliet | 3:40d09aa56b86 | 47 | if(0 != gprs.connectTCP(IP_ADDRSS,PORT)) { |
lawliet | 3:40d09aa56b86 | 48 | goto STOP; |
yihui | 0:8c4eea221dcf | 49 | } |
lawliet | 3:40d09aa56b86 | 50 | wait(5); |
lawliet | 3:40d09aa56b86 | 51 | if(0 != gprs.sendTCPData(head)) { |
lawliet | 3:40d09aa56b86 | 52 | goto STOP; |
lawliet | 3:40d09aa56b86 | 53 | } |
lawliet | 3:40d09aa56b86 | 54 | STOP: |
lawliet | 3:40d09aa56b86 | 55 | gprs.closeTCP(); |
lawliet | 3:40d09aa56b86 | 56 | return 0; |
yihui | 0:8c4eea221dcf | 57 | } |
yihui | 0:8c4eea221dcf | 58 | |
yihui | 0:8c4eea221dcf | 59 | int main() |
yihui | 0:8c4eea221dcf | 60 | { |
lawliet | 3:40d09aa56b86 | 61 | EG10_PowerUp(); |
lawliet | 3:40d09aa56b86 | 62 | while(0 != gprs.init()) { |
lawliet | 3:40d09aa56b86 | 63 | wait(2); |
lawliet | 3:40d09aa56b86 | 64 | } |
lawliet | 3:40d09aa56b86 | 65 | while(1) { |
lawliet | 3:40d09aa56b86 | 66 | putDataToXively(10*soundSensor.read()); |
lawliet | 3:40d09aa56b86 | 67 | wait(10); |
yihui | 0:8c4eea221dcf | 68 | } |
yihui | 0:8c4eea221dcf | 69 | } |