hs loovee
/
ARCH_GPRS_Demo
seeedstudio ARCH GPRS Demo. post data to yeelink.
Revision 0:6d297fe482af, committed 2013-08-23
- Comitter:
- loovee
- Date:
- Fri Aug 23 02:31:06 2013 +0000
- Commit message:
- seeedstudio Arch Gprs Demo. ; ; post data to yeelink.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ARCH_GPRS_HW/ARCH_GPRS_HW.cpp Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,139 @@ +/* + IOT_hw.cpp + 2013 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + 2013-7-21 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "mbed.h" +#include "tcp_yeelink.h" +#include "tcp_yeelink_dfs.h" +#include "ARCH_GPRS_HW.h" +#include "ARCH_GPRS_HW_DFS.h" + +DigitalOut eg10_pwr(PINPWR); +DigitalOut eg10_on(PINONOFF); +DigitalInOut eg10_state(PINEGSTATE); +DigitalOut eg10_rst(PINSYSRST); + +DigitalOut led1(PINLED1); +DigitalOut led2(PINLED2); +DigitalOut led3(PINLED3); +DigitalOut led4(PINLED4); + +DigitalOut grove_pwr(GROVE_PWR); +DigitalIn charging(PIN_CHRGING); +DigitalIn chargdone(PIN_CHRGDONE); + +AnalogIn vol_bat(PIN_READBAT); + +void IOT_HW::EG10_PWRON() +{ + eg10_pwr = 0; +} + +void IOT_HW::EG10_PWROFF() +{ + eg10_pwr = 1; +} + +void IOT_HW::EG10_ON() +{ + eg10_on = 0; + wait(2); + eg10_on = 1; +} + +int IOT_HW::init() +{ + init_io(); + EG10_PWROFF(); + wait(2); // wait 2s + EG10_PWRON(); + EG10_PWRON(); + EG10_ON(); + return (IOT.waitString("M2MRSSI", 30)); +} + +void IOT_HW::init_io() +{ + eg10_pwr = 1; + eg10_on = 1; +} + +int IOT_HW::getEG10_State() +{ + eg10_state.input(); + eg10_state.mode(PullDown); + if(eg10_state.read()) + { + return 1; + } + return 0; +} + +void IOT_HW::EG10StateLed(unsigned char state) +{ + eg10_state.output(); + eg10_state = 1-state; +} + +void IOT_HW::userLed(unsigned char led, unsigned char state) +{ + // if(state!=0 || state!=1)return; + + if(1==led)led1 = state; + else if(2==led)led2 = state; + else if(3==led)led3 = state; + else if(4==led)led4 = state; + +} + +int IOT_HW::readChrgState() +{ + if(!charging.read())return CHRGDONE; + if(!chargdone.read())return CHRGING; + return NOCHRG; +} + +float IOT_HW::readBatVol() +{ + unsigned int tmp = vol_bat.read_u16(); + + return tmp; + float dr = tmp; + dr = dr/65536.0*3.3*2.0; + return dr; + // return ((float tmp)/65536.0*3.3*2.0); +} + +void IOT_HW::grovePwrOn() +{ + grove_pwr = 0; +} + +void IOT_HW::grovePwrOff() +{ + grove_pwr = 1; +} + + +IOT_HW iot_hw; +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ARCH_GPRS_HW/ARCH_GPRS_HW.h Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,55 @@ +/* + IOT_hw.h + 2013 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + 2013-7-21 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __ARCH_GPRS_HW_H__ +#define __ARCH_GPRS_HW_H__ + +#include "ARCH_GPRS_HW_DFS.h" + +class IOT_HW{ + +private: + +public: + int init(); // init all + void init_io(); // init io + void EG10_PWRON(); // power on eg10 + void EG10_PWROFF(); // power off eg10, cut the power + void EG10_ON(); // turn on eg10 + void EG10_OFF(); // turn off eg10 + int getEG10_State(); // get state of eg10 + void EG10StateLed(unsigned char state); // control led + + int readChrgState(); + float readBatVol(); + void userLed(unsigned char led, unsigned char state); + void grovePwrOn(); + void grovePwrOff(); +}; + +extern IOT_HW iot_hw; + +#endif + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ARCH_GPRS_HW/ARCH_GPRS_HW_DFS.h Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,67 @@ +/* + IOT_hw_dfs.h + 2013 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + 2013-7-21 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __ARCH_GPRS_HW_DFS_H__ +#define __ARCH_GPRS_HW_DFS_H__ + +// pin define +#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 +#define PINTX P0_18 // EG10 TX +#define PINRX P0_19 // EG10 RX +#define PINSYSRST P1_6 // EG10 RST,high enable +#define PINEGSTATE P1_15 // in and out, read eg10 state, HIGH: on, LOW: off + + +// grove pin +#define GROVE_I2C_SCL P0_4 // grove i2c pin +#define GROVE_I2C_SDA P0_5 // grove i2c sda + +#define GROVE_UART_RX P0_14 // grove uart rx +#define GROVE_UART_TX P0_13 // grove uart tx + +#define GROVE_ADC_1 P0_12 // grove ADC, the outside one +#define GROVE_ADC_2 P0_11 // grove ADC, the other one + +#define GROVE_PWR P1_3 // control all grove power, low enable + +// charge circuit +#define PIN_CHRGING P1_1 // charging, low enable +#define PIN_CHRGDONE P1_0 // charge done +#define PIN_READBAT P0_23 // battery voltage in, the value should x2 + +// led pin +#define PINLED1 P1_8 +#define PINLED2 P1_9 +#define PINLED3 P1_10 +#define PINLED4 P1_11 + +// charge state +#define CHRGING 1 +#define CHRGDONE 2 +#define NOCHRG 3 + +#endif + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2C_UART/i2c_uart.cpp Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,23 @@ +#include "mbed.h" +#include "i2c_uart.h" +#include "ARCH_GPRS_HW_DFS.h" + +#define ADDRESS 8 + +I2C i2c_debug(GROVE_I2C_SDA, GROVE_I2C_SCL); + + +void debug_i2c(char *dta) +{ + int len = strlen(dta); + i2c_debug.write(ADDRESS, dta, len); + //wait_ms(10); +} + +void debug_i2c(char dta) +{ + char dta1[5]; + sprintf(dta1, "%c", dta); + debug_i2c(dta1); +} +//
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2C_UART/i2c_uart.h Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,17 @@ +#ifndef __I2C_UART_H__ +#define __I2C_UART_H__ + +#define __Debug 1 + +#if __Debug +#define DBG(X) debug_i2c(X) +#else +#define DBG(X) +#endif + + +void debug_i2c(char *dta); + + +void debug_i2c(char dta); +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SLEEP/ARCH_GPRS_Sleep.cpp Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,90 @@ +#include "mbed.h" +#include "ARCH_GPRS_Sleep.h" +#include "ARCH_GPRS_HW.h" + +void Stalker3_0_sleep::gotoSleep() // goto sleep mode, untill wdt interrupt +{ + LPC_PMU->PCON |= 0x01; /* ????????? */ + LPC_SYSCON->PDSLEEPCFG |= (1UL << 3); /* ??BOD??????? */ + SCB->SCR &= ~(1UL << 2); /* ?????? */ + __wfi(); +} + +void Stalker3_0_sleep::wdtClkSetup(unsigned long clksrc) +{ + /* Freq = 0.5Mhz, div_sel is 0x1F, divided by 64. WDT_OSC should be 7.8125khz */ + LPC_SYSCON->WDTOSCCTRL = (0x1<<5)|0x1F; + LPC_SYSCON->PDRUNCFG &= ~(0x1<<6); /* Let WDT clock run */ + + /* Enables clock for WDT */ + LPC_SYSCON->SYSAHBCLKCTRL |= (1<<15); + LPC_WWDT->CLKSEL = clksrc; /* Select clock source */ + +} + +void Stalker3_0_sleep::wdtInit(long tc) // init wdt +{ + uint32_t regVal; + + LPC_WWDT->TC = tc; + + regVal = WDEN; + LPC_WWDT->MOD = regVal; + + LPC_WWDT->FEED = 0xAA; /* Feeding sequence */ + LPC_WWDT->FEED = 0x55; + + NVIC_EnableIRQ(WDT_IRQn); + NVIC_SetPriority(WDT_IRQn, 2); + return; + +} + +void Stalker3_0_sleep::init() +{ + +} + +void Stalker3_0_sleep::sleep(long ts) // sleep for ts (s) +{ + + workMode = MODE_SLEEP; + wdtInit(0x2dc6c0); + + for(int i=0; i<ts; i++) + { + gotoSleep(); + } + + workMode = MODE_WORKING; + feed(); +} + +void Stalker3_0_sleep::wakeUp() // wake up from sleep +{ + +} + +void Stalker3_0_sleep::feed() // watch dog feed +{ + LPC_WWDT->FEED = 0xAA; /* Feeding sequence */ + LPC_WWDT->FEED = 0x55; + return; +} + +Stalker3_0_sleep wdt_sleep; + +extern "C"{ + + void WDT_IRQHandler(void) + { + + if(wdt_sleep.workMode == MODE_WORKING) // WORKING MODE, AND NO FEET WDT, RESET!!! + { + NVIC_SystemReset(); + } + LPC_WWDT->MOD &= ~WDTOF; /* clear the time-out flag and interrupt flag */ + LPC_WWDT->MOD &= ~WDINT; /* clear the time-out flag and interrupt flag */ + wdt_sleep.wdtInit(0x2dc6c0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SLEEP/ARCH_GPRS_Sleep.h Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,48 @@ +#ifndef __ARCH_GPRS_SLEEP_H__ +#define __ARCH_GPRS_SLEEP_H__ + + +#define WDTCLK_SRC_IRC_OSC 0 +#define WDTCLK_SRC_WDT_OSC 1 + +#define WDEN (0x1<<0) +#define WDRESET (0x1<<1) +#define WDTOF (0x1<<2) +#define WDINT (0x1<<3) +#define WDPROTECT (0x1<<4) +#define WDLOCKCLK (0x1<<5) + +#define WDT_FEED_VALUE 0x003FFFFF + +#define WINDOW_MODE 0 +#define PROTECT_MODE 0 +#define WATCHDOG_RESET 1 +#define WDLOCK_MODE 0 +#define LOCKCLK_MODE 0 + +#define MODE_SLEEP 0 +#define MODE_WORKING 1 + +class Stalker3_0_sleep{ + + public: + + int workMode; // working mode, sleep or working + + public: + + void gotoSleep(); // goto sleep mode, untill wdt interrupt + void wdtClkSetup(unsigned long clksrc); + + public: + + void init(); + + void sleep(long ts); // sleep for ts (s) + void wakeUp(); // wake up from sleep + void feed(); // watch dog feed + void wdtInit(long tc); // init wdt +}; + +extern Stalker3_0_sleep wdt_sleep; +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TCP_YEELINK/tcp_yeelink.cpp Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,336 @@ +/* + IOT_Mbed.cpp + 2013 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + 2013-7-21 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include <stdio.h> + +#include "mbed.h" +#include "tcp_yeelink.h" +#include "tcp_yeelink_dfs.h" +#include "i2c_uart.h" +#include "ARCH_GPRS_Sleep.h" + + +Serial serial1(P0_19, P0_18); // tx, rx +Timer tcnt; + +void IOT_Mbed::init(char *postURL, char *APIKey) +{ + serial1.baud(115200); + strcpy(yeelinkPostURL, postURL); + sprintf(yeelinkPostHeads, "U-ApiKey: %s\r\n", APIKey); + strcpy(yeelinkDns, "42.96.164.52"); // api.yeelink.net + sprintf(yeelinkPort, "%d", HTTP_DEFAULT_PORT); +} + +void IOT_Mbed::callTest() +{ + sendCmdAndWaitForRest("ATD:10086;\r\n", "OK", 10); +} + +int IOT_Mbed::checkAT(int timeout) +{ + return sendCmdAndWaitForRest("AT\r\n", "OK", timeout); +} + +int IOT_Mbed::waitString(const char *str, int timeout) // time out : s +{ + int len = strlen(str); + int sum=0; + + tcnt.start(); // start timer + + for(;;) + { + if(serial1.readable()) + { + char c = serial1.getc(); + DBG(c); + sum = (c==str[sum]) ? sum+1 : 0; + if(sum == len)break; + } + + if(tcnt.read() > timeout) // time out + { + tcnt.stop(); + tcnt.reset(); + + DBG("time out\r\n"); + return ERRTOUT; + } + + wdt_sleep.feed(); + } + + tcnt.stop(); // stop timer + tcnt.reset(); // clear timer + + while(serial1.readable()) // display the other thing.. + { + char c = serial1.getc(); + DBG(c); + } + + return 1; +} + + +int IOT_Mbed::sendCmdAndWaitForRest(char *dta, const char *resq, int timeout) +{ + sendCmd(dta); + return waitString(resq, timeout); +} + +void IOT_Mbed::sendCmd(char *dta) +{ + serial1.printf("%s\r\n", dta); +} + +int IOT_Mbed::connectTCP() +{ + + sendCmdAndWaitForRest("ATE0\r\n", "OK", 3); + int tout = 0; + while(1) + { + if(sendCmdAndWaitForRest(STROPENGPRS, "OK", 20) == ERRTOUT) + { + DBG("GPRS OPEN ERR, OPEN AGAIN\r\n"); + wait(5); + } + else + { + DBG("GPRS OPEN OK!\r\n"); + break; + } + tout++; + if(tout>5)return 0; + } + + + if(!sendCmdAndWaitForRest(STRSETGPRS, "OK", 20))return 0; + if(!sendCmdAndWaitForRest(STRSETAPN, "OK", 20))return 0;; + + char cipstart[50]; + sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"", yeelinkDns, yeelinkPort); + if(!sendCmdAndWaitForRest(cipstart, "CONNECT OK", 20))return 0;; // connect tcp + + return 1; +} + +int IOT_Mbed::connectTCP(char *ip, char *port) +{ + + sendCmdAndWaitForRest("ATE0\r\n", "OK", 3); + int tout = 0; + + while(1) + { + if(sendCmdAndWaitForRest(STROPENGPRS, "OK", 20) == ERRTOUT) + { + DBG("GPRS OPEN ERR, OPEN AGAIN\r\n"); + wait(5); + } + else + { + DBG("GPRS OPEN OK!\r\n"); + break; + } + tout++; + if(tout>5)return 0; + } + + + if(!sendCmdAndWaitForRest(STRSETGPRS, "OK", 20))return 0; + if(!sendCmdAndWaitForRest(STRSETAPN, "OK", 20))return 0;; + + char cipstart[50]; + sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"", ip, port); + if(!sendCmdAndWaitForRest(cipstart, "CONNECT OK", 20))return 0;; // connect tcp + return 1; + +} + +//send data to tcp +int IOT_Mbed::sendDtaTcp(char *dta, int timeout) +{ + serial1.printf("AT+CIPSEND=%d\r\n", strlen(dta)); + waitString(">", 10); + serial1.printf("%s", dta); + + wait_ms(50); + return waitString("SEND OK", timeout); +} + +bool IOT_Mbed::sendToYeelink_t() +{ + char dtaSend[300]; + sprintf(dtaSend, "%s\r\n%s\r\n%s\r\n%s\r\n%s\r\n%s\r\n\r\n%s\r\n", POST1, POST2, POST3, POST4, POST5, POST6, POST7); + return sendDtaTcp(dtaSend, 20); +} + +void IOT_Mbed::postDtaToYeelink() +{ + +} + +int IOT_Mbed::postDtaToYeelink(char *url, char *apikey, int sensorDta) +{ + return postDtaToYeelink(url, apikey, sensorDta, 0); +} + +int IOT_Mbed::postDtaToYeelink(char *url, char *apikey, float sensorDta, int dec) +{ + char dtaPost[350]; + + char request[100]; + char heads[200]; + char body[100]; + + unsigned int port; + + char host[HTTP_MAX_HOST_LEN]; + char path[HTTP_MAX_PATH_LEN]; + + if (parseURL(url, host, sizeof(host), &port, path, sizeof(path)) != 0) + { + DBG("Failed to parse URL.\r\n"); + return 0; + } + + if(!connectTCP()) + { + DBG("connect to tcp err!\r\n"); + return 0; + } + + if(dec == 0) + { + sprintf(body, "{\"value\": %.0f}\r\n", sensorDta); + } + else if(dec == 1) + { + sprintf(body, "{\"value\": %.1f}\r\n", sensorDta); + } + else if(dec == 2) + { + sprintf(body, "{\"value\": %.2f}\r\n", sensorDta); + } + else + { + sprintf(body, "{\"value\": %.3f}\r\n", sensorDta); + + } + sprintf(request, "POST %s HTTP/1.1\r\n", path); + sprintf(heads, "Host: %s\r\nU-ApiKey: %s\r\nContent-Length: %d\r\nContent-Type: %s\r\n\r\n",host, apikey, strlen(body), CONTENT_TYPE); + sprintf(dtaPost, "%s%s%s", request, heads, body); + + sendDtaTcp(dtaPost, 10); + + while(serial1.readable()) + { + char c = serial1.getc(); + DBG(c); + } + + return sendCmdAndWaitForRest(STRCLOSE, "OK", 20); +} + +int IOT_Mbed::parseURL(const char *url, char *host, int max_host_len, unsigned int *port, char *path, int max_path_len) +{ + char *scheme_ptr = (char *)url; + char *host_ptr = (char *)strstr(url, "://"); + if (host_ptr != NULL) + { + if (strncmp(scheme_ptr, "http://", 7)) + { + DBG("Bad scheme\r\n"); + return -1; + } + host_ptr += 3; + } + else + { + host_ptr = (char *)url; + } + + int host_len = 0; + char *port_ptr = strchr(host_ptr, ':'); + + if (port_ptr != NULL) + { + host_len = port_ptr - host_ptr; + port_ptr++; + if (sscanf(port_ptr, "%hu", port) != 1) + { + DBG("Could not find port.\r\n"); + return -3; + } + } + else + { + *port = HTTP_DEFAULT_PORT; + } + + char *path_ptr = strchr(host_ptr, '/'); + + if (host_len == 0) + { + host_len = path_ptr - host_ptr; + } + + if (max_host_len < (host_len + 1)) + { + DBG("Host buffer is too small.\r\n"); + return -4; + } + + memcpy(host, host_ptr, host_len); + host[host_len] = '\0'; + + int path_len; + + char *fragment_ptr = strchr(host_ptr, '#'); + if (fragment_ptr != NULL) + { + path_len = fragment_ptr - path_ptr; + } + else + { + path_len = strlen(path_ptr); + } + + if (max_path_len < (path_len + 1)) + { + DBG("Path buffer is too small.\r\n"); + return -5; + } + memcpy(path, path_ptr, path_len); + path[path_len] = '\0'; + + return 0; +} + + + +IOT_Mbed IOT; +/********************************************************************************************************* + END FILE +*********************************************************************************************************/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TCP_YEELINK/tcp_yeelink.h Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,72 @@ +/* + IOT_Mbed.h + 2013 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + 2013-7-21 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __TCP_YEELINK_H__ +#define __TCP_YEELINK_H__ + +#include "mbed.h" + +class IOT_Mbed{ + + private: + + //Serial serial1(P0_19, P0_18); // tx, rx + private: + char yeelinkPostURL[100]; + char yeelinkPostHeads[100]; + + char yeelinkDns[20]; + char yeelinkPort[10]; + + private: + + void callTest(); + + int sendCmdAndWaitForRest(char *dta, const char *resq, int timeout); + void sendCmd(char *dta); + int parseURL(const char *url, char *host, int max_host_len, unsigned int *port, char *path, int max_path_len); + + public: + int waitString(const char *str, int timeout); + void init(char *postURL, char *APIKey); + int checkAT(int timeout); + int connectTCP(); + int connectTCP(char *ip, char *port); + void postDtaToYeelink(); + int postDtaToYeelink(char *url, char *apikey, int sensorDta); + int postDtaToYeelink(char *url, char *apikey, float sensorDta, int dec); + + int sendDtaTcp(char *dta, int timeout); + bool sendToYeelink_t(); + + +}; + + +extern IOT_Mbed IOT; + + +#endif + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TCP_YEELINK/tcp_yeelink_dfs.h Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,67 @@ +/* + IOT_MbedDfs.h + 2013 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + 2013-7-21 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __TCP_YEELINK_DFS_H__ +#define __TCP_YEELINK_DFS_H__ + +#define HTTP_DEFAULT_PORT 80 + +#define HTTP_MAX_HOST_LEN 20 +#define HTTP_MAX_PATH_LEN 64 +#define HTTP_MAX_BUF_LEN 100 + +// some useful AT command +#define STRCSQ "AT+CSQ" +#define STROPENGPRS "AT+CGATT=1" +#define STRSETGPRS "AT+CGDCONT=1,\"IP\",\"CMNET\"" +#define STRSETAPN "AT+CSTT=\"CMNET\",\"\",\"\"" +#define STRSETMODE "AT+CIPMODE=0" +#define STRSTARTTCP "AT+CIPSTART=\"TCP\",\"42.96.164.52\",\"80\"" +#define STRCLOSE "AT+CIPCLOSE" +#define STRECHOFF "ATE0" +#define STRSLEEP "AT+ESLP=1" +#define STRWAKE "AT+ESLP=0" + +// err code +#define ERRTOUT 0 +#define OK 1 + + +// post data +#define POST1 "POST /v1.0/device/3091/sensor/4346/datapoints HTTP/1.1" +#define POST2 "Host: api.yeelink.net" +#define POST3 "Connection: close" +#define POST4 "Content-Length: 15" +#define POST5 "Content-Type: text/plain" +#define POST6 "U-ApiKey: 9270322fd7c7683cb9ad198f3464cf0d" + +#define POST7 "{\"value\": 100}" + +#define CONTENT_TYPE " application/x-www-form-urlencoded" + + + +#endif + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,208 @@ +/* + main.cpp, iot_mbed demo + 2013 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:Loovee + 2013-7-21 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "tcp_yeelink.h" +#include "mbed.h" +#include "tcp_yeelink_dfs.h" +#include "ARCH_GPRS_HW.h" +#include "i2c_uart.h" +#include "ARCH_GPRS_Sleep.h" + +AnalogIn light_sensor(GROVE_ADC_1); + +#define BROADCAST_TIME 100 // broadcast time : 100s + +// you can get this information in www.yeelink.net +#define HTTP_POST_URL "http://api.yeelink.net/v1.0/device/4190/sensor/6074/datapoints" + +//#define HTTP_POST_URL_BAT "http://api.yeelink.net/v1.0/device/4190/sensor/6089/datapoints" + +#define YEELINK_APIKEY "38645582d54121679dee8104f140c29a" + +void delay_ms(long ms) +{ + if(ms <= 900) + { + wait_ms(ms); + wdt_sleep.feed(); + } + else + { + ms -= 900; + delay_ms(900); + } +} + +int getAnalog() +{ + long sum = 0; + for(int i=0; i<32; i++) + { + sum += light_sensor.read_u16(); + } + sum = sum >> 5; + sum = sum >> 6; + return sum; +} + +void power_on() +{ + IOT.init(HTTP_POST_URL, YEELINK_APIKEY); + START: + DBG("begin to start\r\n"); + iot_hw.EG10_PWROFF(); // eg10 power off + wait(0.5); + wdt_sleep.feed(); + iot_hw.EG10_PWRON(); // eg10 power on + wait(0.5); + wdt_sleep.feed(); + + if(iot_hw.init()==1) + { + iot_hw.EG10StateLed(1); + DBG("hardware init ok\r\n"); + } + else + { + DBG("hardware init fail\r\n"); + DBG("hardware init again\r\n"); + goto START; + } + wdt_sleep.feed(); +} + +#define LOWPWR 1 + +void iot_demo() +{ + wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC); + + PWRON: + power_on(); + for(int i=0; i<10; i++) + { + wdt_sleep.feed(); + wait(0.9); + wdt_sleep.feed(); + } + int dtaSw = 0; + while(1) + { + + int dtaVal = getAnalog()/10; + + if(!IOT.postDtaToYeelink(HTTP_POST_URL, YEELINK_APIKEY, dtaVal)) + { + DBG("post data err\r\n"); + wdt_sleep.feed(); + goto PWRON; + } + else + { + DBG("post data ok!\r\n"); + } + + iot_hw.userLed(1, 1); + wait(.2); + iot_hw.userLed(1, 0); + + for(int i=0; i<10; i++) + { + wdt_sleep.feed(); + wait(0.5); + } +#if LOWPWR + DBG("goto sleep for 100s"); + iot_hw.EG10_PWROFF(); + iot_hw.grovePwrOff(); + wdt_sleep.sleep(BROADCAST_TIME); + + DBG("wake"); + goto PWRON; +#else + + wait(5); +#endif + } +} + +void wdt_sleep_demo() +{ + DBG("begin to poweron\r\n"); + // power_on(); + wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC); + + // start led + for(int i=0; i<5; i++) + { + iot_hw.userLed(2, 1);wait_ms(100); + iot_hw.userLed(2, 0);wait_ms(100); + } + + + // cut power + for(int i=0; i<10; i++) + { + wait(0.1); + wdt_sleep.feed(); + } + iot_hw.EG10_PWROFF(); + iot_hw.grovePwrOff(); + + while(1) + { +#if 0 + DBG("sleep\r\n"); + wait(0.1); + //wdt_sleep.gotoSleep(); + DBG("wake\r\n"); + for(int i=0; i<5; i++) + { + wait(1); + wdt_sleep.feed(); + } +#else + DBG("sleep 5s\r\n"); + wait(0.1); + wdt_sleep.sleep(5); + + DBG("wake\r\n"); + for(int i=0; i<10; i++) + { + wait(0.5); + wdt_sleep.feed(); + } + +#endif + } +} + + +int main(void) +{ + //wdt_sleep_demo(); + iot_demo(); +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Aug 23 02:31:06 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9c8f0e3462fb \ No newline at end of file