this is a demo code for Seeed ARCH GPRS V2.0 to work with Xively

Dependencies:   GPRSInterface USBDevice mbed libxively

Fork of Seeed_ARCH_GPRS_V2_Xively_HelloWorld by wei zou

Committer:
yihui
Date:
Mon Sep 15 07:13:30 2014 +0000
Revision:
3:3b7881cf8bd0
Parent:
1:95596d36119d
update with latest libs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:8a01089092bd 1 /*
lawliet 0:8a01089092bd 2 main.cpp
lawliet 0:8a01089092bd 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
lawliet 0:8a01089092bd 4
lawliet 0:8a01089092bd 5 Author:lawliet zou(lawliet.zou@gmail.com)
lawliet 0:8a01089092bd 6 2014-4-28
lawliet 0:8a01089092bd 7
lawliet 0:8a01089092bd 8 This library is free software; you can redistribute it and/or
lawliet 0:8a01089092bd 9 modify it under the terms of the GNU Lesser General Public
lawliet 0:8a01089092bd 10 License as published by the Free Software Foundation; either
lawliet 0:8a01089092bd 11 version 2.1 of the License, or (at your option) any later version.
lawliet 0:8a01089092bd 12
lawliet 0:8a01089092bd 13 This library is distributed in the hope that it will be useful,
lawliet 0:8a01089092bd 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
lawliet 0:8a01089092bd 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lawliet 0:8a01089092bd 16 Lesser General Public License for more details.
lawliet 0:8a01089092bd 17
lawliet 0:8a01089092bd 18 You should have received a copy of the GNU Lesser General Public
lawliet 0:8a01089092bd 19 License along with this library; if not, write to the Free Software
lawliet 0:8a01089092bd 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lawliet 0:8a01089092bd 21 */
lawliet 0:8a01089092bd 22 #include "mbed.h"
lawliet 0:8a01089092bd 23 #include "GPRSInterface.h"
lawliet 0:8a01089092bd 24 #include "USBSerial.h"
lawliet 0:8a01089092bd 25 #include "xively.h"
lawliet 0:8a01089092bd 26 #include "xi_err.h"
lawliet 0:8a01089092bd 27
lawliet 0:8a01089092bd 28 #define PIN_PWR P1_2 //power up gprs module
lawliet 0:8a01089092bd 29 #define PIN_PWR_KEY P1_7
lawliet 0:8a01089092bd 30 #define PIN_TX P1_27
lawliet 0:8a01089092bd 31 #define PIN_RX P1_26
lawliet 0:8a01089092bd 32 #define XI_FEED_ID 571464242 // set Xively Feed ID (numerical, no quoutes)
lawliet 0:8a01089092bd 33 #define XI_API_KEY "niUYuSJkjqyzPFwnWqpApm7lLNv8fInUD6ijAoRrikqKFWbg" // set Xively API key (double-quoted string)
lawliet 0:8a01089092bd 34
lawliet 1:95596d36119d 35 AnalogIn soundSensor(P0_12);
lawliet 0:8a01089092bd 36 GPRSInterface gprs(PIN_TX,PIN_RX,115200,"cmnet",NULL,NULL);
lawliet 0:8a01089092bd 37 USBSerial pc(0x1f00, 0x2012, 0x0001, false);
lawliet 0:8a01089092bd 38 DigitalOut power(PIN_PWR);
lawliet 0:8a01089092bd 39 DigitalOut powerKey(PIN_PWR_KEY);
lawliet 0:8a01089092bd 40
lawliet 0:8a01089092bd 41 /* power pin: low enable
lawliet 0:8a01089092bd 42 ___
lawliet 0:8a01089092bd 43 |___
lawliet 0:8a01089092bd 44
lawliet 0:8a01089092bd 45 powerKey pin: you can also power up by long press the powerKey.
lawliet 0:8a01089092bd 46
lawliet 0:8a01089092bd 47 ___
lawliet 0:8a01089092bd 48 ___| |___
lawliet 0:8a01089092bd 49
lawliet 0:8a01089092bd 50 */
lawliet 0:8a01089092bd 51 void gprsPowerUp(void)
lawliet 0:8a01089092bd 52 {
lawliet 0:8a01089092bd 53 power = 1;
lawliet 0:8a01089092bd 54 wait(2);
lawliet 0:8a01089092bd 55 power = 0;
lawliet 0:8a01089092bd 56 wait(2);
lawliet 0:8a01089092bd 57
lawliet 0:8a01089092bd 58 powerKey = 0;
lawliet 0:8a01089092bd 59 wait(1);
lawliet 0:8a01089092bd 60 powerKey = 1;
lawliet 0:8a01089092bd 61 wait(2);
lawliet 0:8a01089092bd 62 powerKey = 0;
lawliet 0:8a01089092bd 63 wait(3);
lawliet 0:8a01089092bd 64 }
lawliet 0:8a01089092bd 65 // Called by ISR
lawliet 0:8a01089092bd 66 void settingsChanged(int baud, int bits, int parity, int stop)
lawliet 0:8a01089092bd 67 {
lawliet 0:8a01089092bd 68 const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
lawliet 0:8a01089092bd 69
lawliet 0:8a01089092bd 70 if (stop != 2) {
lawliet 0:8a01089092bd 71 stop = 1; // stop bit(s) = 1 or 1.5
lawliet 0:8a01089092bd 72 }
lawliet 0:8a01089092bd 73
lawliet 0:8a01089092bd 74 gprs.serialModem.baud(baud);
lawliet 0:8a01089092bd 75 gprs.serialModem.format(bits, parityTable[parity], stop);
lawliet 0:8a01089092bd 76 }
lawliet 0:8a01089092bd 77
lawliet 0:8a01089092bd 78 int main()
lawliet 0:8a01089092bd 79 {
lawliet 0:8a01089092bd 80 pc.attach(settingsChanged);
lawliet 0:8a01089092bd 81 gprsPowerUp();
lawliet 0:8a01089092bd 82 wait(10);
lawliet 0:8a01089092bd 83
lawliet 0:8a01089092bd 84 gprs.init(); //Use DHCP
lawliet 0:8a01089092bd 85 // attempt DHCP
lawliet 0:8a01089092bd 86 while(false == gprs.connect()) {
lawliet 0:8a01089092bd 87 wait(2);
lawliet 0:8a01089092bd 88 pc.printf("gprs connect error\n");
lawliet 0:8a01089092bd 89 }
lawliet 0:8a01089092bd 90 // successful DHCP
lawliet 0:8a01089092bd 91 pc.printf("IP Address is %s\n", gprs.getIPAddress());
lawliet 0:8a01089092bd 92
lawliet 0:8a01089092bd 93 xi_feed_t feed;
lawliet 0:8a01089092bd 94 memset( &feed, NULL, sizeof( xi_feed_t ) );
lawliet 0:8a01089092bd 95
lawliet 0:8a01089092bd 96 feed.feed_id = XI_FEED_ID;
lawliet 0:8a01089092bd 97 feed.datastream_count = 1;
lawliet 0:8a01089092bd 98
lawliet 0:8a01089092bd 99 feed.datastreams[0].datapoint_count = 1;
lawliet 0:8a01089092bd 100 xi_datastream_t* sound_datastream = &feed.datastreams[0];
lawliet 0:8a01089092bd 101 strcpy( sound_datastream->datastream_id, "Sound" );
lawliet 0:8a01089092bd 102 xi_datapoint_t* current_sound = &sound_datastream->datapoints[0];
lawliet 0:8a01089092bd 103
lawliet 0:8a01089092bd 104 // create the cosm library context
lawliet 0:8a01089092bd 105 xi_context_t* xi_context = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
lawliet 0:8a01089092bd 106
lawliet 0:8a01089092bd 107 // check if everything works
lawliet 0:8a01089092bd 108 if( xi_context == NULL ) {
lawliet 0:8a01089092bd 109 return -1;
lawliet 0:8a01089092bd 110 }
lawliet 0:8a01089092bd 111 while(1) {
lawliet 1:95596d36119d 112 float val = 100*soundSensor.read();
lawliet 1:95596d36119d 113 pc.printf("val = %f\n",val);
lawliet 1:95596d36119d 114 xi_set_value_f32(current_sound, val);
lawliet 0:8a01089092bd 115 xi_feed_update( xi_context, &feed );
lawliet 0:8a01089092bd 116 wait( 10 );
lawliet 0:8a01089092bd 117 }
lawliet 0:8a01089092bd 118 }