seeedstudio ARCH GPRS Demo. post data to yeelink.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002   main.cpp, iot_mbed demo
00003   2013 Copyright (c) Seeed Technology Inc.  All right reserved.
00004 
00005   Author:Loovee
00006   2013-7-21
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021 */
00022 
00023 #include "tcp_yeelink.h"
00024 #include "mbed.h"
00025 #include "tcp_yeelink_dfs.h"
00026 #include "ARCH_GPRS_HW.h"
00027 #include "i2c_uart.h"
00028 #include "ARCH_GPRS_Sleep.h"
00029 
00030 AnalogIn light_sensor(GROVE_ADC_1);
00031 
00032 #define BROADCAST_TIME            100                            // broadcast time : 100s
00033 
00034 // you can get this information in www.yeelink.net
00035 #define HTTP_POST_URL "http://api.yeelink.net/v1.0/device/4190/sensor/6074/datapoints"
00036 
00037 //#define HTTP_POST_URL_BAT "http://api.yeelink.net/v1.0/device/4190/sensor/6089/datapoints"
00038 
00039 #define YEELINK_APIKEY "38645582d54121679dee8104f140c29a"
00040 
00041 void delay_ms(long ms)
00042 {
00043     if(ms <= 900)
00044     {
00045         wait_ms(ms);
00046         wdt_sleep.feed();
00047     }
00048     else
00049     {
00050         ms -= 900;
00051         delay_ms(900);
00052     }
00053 }
00054 
00055 int getAnalog()
00056 {
00057     long sum = 0;
00058     for(int i=0; i<32; i++)
00059     {
00060         sum += light_sensor.read_u16();
00061     }
00062     sum = sum >> 5;
00063     sum = sum >> 6;
00064     return sum;
00065 }
00066 
00067 void power_on()
00068 {
00069     IOT.init(HTTP_POST_URL, YEELINK_APIKEY);
00070     START:
00071     DBG("begin to start\r\n");
00072     iot_hw.EG10_PWROFF();                           // eg10 power off
00073     wait(0.5);
00074     wdt_sleep.feed();
00075     iot_hw.EG10_PWRON();                            // eg10 power on
00076     wait(0.5);
00077     wdt_sleep.feed();
00078 
00079     if(iot_hw.init()==1)
00080     {
00081         iot_hw.EG10StateLed(1);
00082         DBG("hardware init ok\r\n");
00083     }
00084     else
00085     {
00086         DBG("hardware init fail\r\n");
00087         DBG("hardware init again\r\n");
00088         goto START;
00089     }
00090     wdt_sleep.feed();
00091 }
00092 
00093 #define LOWPWR        1
00094 
00095 void iot_demo()
00096 {
00097     wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC);
00098 
00099     PWRON:
00100     power_on();
00101     for(int i=0; i<10; i++)
00102     {
00103         wdt_sleep.feed();
00104         wait(0.9);
00105         wdt_sleep.feed();
00106     }
00107     int dtaSw = 0;
00108     while(1)
00109     {
00110 
00111         int dtaVal = getAnalog()/10;
00112 
00113         if(!IOT.postDtaToYeelink(HTTP_POST_URL, YEELINK_APIKEY, dtaVal))
00114         {
00115             DBG("post data err\r\n");
00116             wdt_sleep.feed();
00117             goto PWRON;
00118         }
00119         else
00120         {
00121             DBG("post data ok!\r\n");
00122         }
00123 
00124         iot_hw.userLed(1, 1);
00125         wait(.2);
00126         iot_hw.userLed(1, 0);
00127 
00128         for(int i=0; i<10; i++)
00129         {
00130             wdt_sleep.feed();
00131             wait(0.5);
00132         }
00133 #if LOWPWR
00134         DBG("goto sleep for 100s");
00135         iot_hw.EG10_PWROFF();
00136         iot_hw.grovePwrOff();
00137         wdt_sleep.sleep(BROADCAST_TIME);
00138 
00139         DBG("wake");
00140         goto PWRON;
00141 #else
00142 
00143         wait(5);
00144 #endif
00145     }
00146 }
00147 
00148 void wdt_sleep_demo()
00149 {
00150     DBG("begin to poweron\r\n");
00151     // power_on();
00152     wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC);
00153 
00154     // start led
00155     for(int i=0; i<5; i++)
00156     {
00157         iot_hw.userLed(2, 1);wait_ms(100);
00158         iot_hw.userLed(2, 0);wait_ms(100);
00159     }
00160 
00161 
00162     // cut power
00163     for(int i=0; i<10; i++)
00164     {
00165         wait(0.1);
00166         wdt_sleep.feed();
00167     }
00168     iot_hw.EG10_PWROFF();
00169     iot_hw.grovePwrOff();
00170 
00171     while(1)
00172     {
00173 #if 0
00174         DBG("sleep\r\n");
00175         wait(0.1);
00176         //wdt_sleep.gotoSleep();
00177         DBG("wake\r\n");
00178         for(int i=0; i<5; i++)
00179         {
00180             wait(1);
00181             wdt_sleep.feed();
00182         }
00183 #else
00184         DBG("sleep 5s\r\n");
00185         wait(0.1);
00186         wdt_sleep.sleep(5);
00187 
00188         DBG("wake\r\n");
00189         for(int i=0; i<10; i++)
00190         {
00191             wait(0.5);
00192             wdt_sleep.feed();
00193         }
00194 
00195 #endif
00196     }
00197 }
00198 
00199 
00200 int main(void)
00201 {
00202     //wdt_sleep_demo();
00203     iot_demo();
00204 }
00205 
00206 /*********************************************************************************************************
00207   END FILE
00208 *********************************************************************************************************/