a demo to post your sensor data from ARCH GPRS to Xively

Dependencies:   GPRS mbed

Fork of USB2UART by Yihui Xiong

ARCH GPRS With Xively

IOT Architecture

  • 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

  • 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);
    }
}
Committer:
yihui
Date:
Wed Dec 25 03:04:09 2013 +0000
Revision:
2:427b69ad737c
Parent:
1:efa9f62a12c4
Child:
3:40d09aa56b86
add led indicator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:8c4eea221dcf 1 /**
yihui 0:8c4eea221dcf 2 * USB to UART Bridge
yihui 0:8c4eea221dcf 3 */
yihui 0:8c4eea221dcf 4
yihui 0:8c4eea221dcf 5 #include "mbed.h"
yihui 0:8c4eea221dcf 6 #include "USBSerial.h"
yihui 0:8c4eea221dcf 7
yihui 0:8c4eea221dcf 8 Serial uart(USBTX, USBRX);
yihui 0:8c4eea221dcf 9 USBSerial pc;
yihui 2:427b69ad737c 10 DigitalOut led1(LED1);
yihui 2:427b69ad737c 11 DigitalOut led2(LED2);
yihui 2:427b69ad737c 12 DigitalOut led3(LED3);
yihui 0:8c4eea221dcf 13
yihui 0:8c4eea221dcf 14 // Called by ISR
yihui 0:8c4eea221dcf 15 void settingsChanged(int baud, int bits, int parity, int stop)
yihui 0:8c4eea221dcf 16 {
yihui 0:8c4eea221dcf 17 const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
yihui 0:8c4eea221dcf 18
yihui 2:427b69ad737c 19
yihui 2:427b69ad737c 20 led1 = 1;
yihui 0:8c4eea221dcf 21 if (stop != 2) {
yihui 0:8c4eea221dcf 22 stop = 1; // stop bit(s) = 1 or 1.5
yihui 0:8c4eea221dcf 23 }
yihui 0:8c4eea221dcf 24 uart.baud(baud);
yihui 0:8c4eea221dcf 25 uart.format(bits, parityTable[parity], stop);
yihui 2:427b69ad737c 26 led1 = 0;
yihui 0:8c4eea221dcf 27 }
yihui 0:8c4eea221dcf 28
yihui 0:8c4eea221dcf 29 int main()
yihui 0:8c4eea221dcf 30 {
yihui 0:8c4eea221dcf 31 pc.attach(settingsChanged);
yihui 0:8c4eea221dcf 32
yihui 0:8c4eea221dcf 33 while (1) {
yihui 0:8c4eea221dcf 34 while (uart.readable()) {
yihui 2:427b69ad737c 35 led2 = 1;
yihui 2:427b69ad737c 36 pc.putc(uart.getc());
yihui 2:427b69ad737c 37 led2 = 0;
yihui 0:8c4eea221dcf 38 }
yihui 0:8c4eea221dcf 39
yihui 0:8c4eea221dcf 40 while (pc.readable()) {
yihui 2:427b69ad737c 41 led3 = 1;
yihui 0:8c4eea221dcf 42 uart.putc(pc.getc());
yihui 2:427b69ad737c 43 led3 = 0;
yihui 0:8c4eea221dcf 44 }
yihui 0:8c4eea221dcf 45 }
yihui 0:8c4eea221dcf 46 }