Skip to content

Instantly share code, notes, and snippets.

@zarya
Created October 2, 2014 08:37
Show Gist options
  • Save zarya/f6c94d37d861a9079710 to your computer and use it in GitHub Desktop.
Save zarya/f6c94d37d861a9079710 to your computer and use it in GitHub Desktop.
ESP8266 toggle GPIO pin example
diff --git a/user/at_ipCmd.c b/user/at_ipCmd.c
index 7c3be3c..4ae7fd3 100644
--- a/user/at_ipCmd.c
+++ b/user/at_ipCmd.c
@@ -24,9 +24,11 @@ uint16_t at_tranLen; //now is 256
os_timer_t at_delayChack;
BOOL IPMODE;
-static BOOL at_ipMux = FALSE;
+static BOOL at_ipMux = TRUE;
static BOOL disAllFlag = FALSE;
+uint8_t LED = 0;
+
static at_linkConType pLink[at_linkMax];
static uint8_t sendingID;
static BOOL serverEn = FALSE;
@@ -43,6 +45,30 @@ static struct espconn *pUdpServer;
static void at_tcpclient_discon_cb(void *arg);
+void ICACHE_FLASH_ATTR
+at_user_input(void *arg, char *pdata, unsigned short len)
+{
+ struct espconn *pespconn = (struct espconn *)arg;
+ at_linkConType *linkTemp = (at_linkConType *)pespconn->reverse;
+ char temp[32];
+ if (pdata[0] == 'L' && pdata[1] == 'E' && pdata[2] == 'D') {
+ if (pdata[3] == '1') {
+ uart0_sendStr("\r\nSet led ON\r\n");
+ gpio_output_set(BIT2, 0, BIT2, 0);
+ LED = 1;
+ } else if (pdata[3] == '0') {
+ uart0_sendStr("\r\nSet led OFF\r\n");
+ gpio_output_set(0, BIT2, BIT2, 0);
+ LED = 0;
+ }
+ else
+ {
+ os_sprintf(temp, "LED: %d\r\n",LED);
+ espconn_sent(pLink[linkTemp->linkId].pCon, temp, strlen(temp));
+ }
+ }
+}
+
/**
* @brief Test commad of get module ip.
* @param id: commad id number
@@ -235,6 +261,8 @@ at_tcpclient_recv(void *arg, char *pdata, unsigned short len)
linkTemp->linkId, len);
uart0_sendStr(temp);
uart0_tx_buffer(pdata, len);
+ //Run my function
+ at_user_input(arg,pdata,len);
}
else if(IPMODE == FALSE)
{
@@ -1065,6 +1093,7 @@ at_queryCmdCipmux(uint8_t id)
void ICACHE_FLASH_ATTR
at_setupCmdCipmux(uint8_t id, char *pPara)
{
+ uart0_sendStr(pPara);
uint8_t muxTemp;
if(mdState == m_linked)
{
@@ -1271,6 +1300,7 @@ at_udpserver_recv(void *arg, char *pusrdata, unsigned short len)
linkTemp->linkId, len);
uart0_sendStr(temp);
uart0_tx_buffer(pusrdata, len);
+ at_user_input(arg, pusrdata,len);
at_backOk;
}
diff --git a/user/at_ipCmd.h b/user/at_ipCmd.h
index 2f2c468..4ad2415 100644
--- a/user/at_ipCmd.h
+++ b/user/at_ipCmd.h
@@ -19,6 +19,8 @@ typedef struct
struct espconn *pCon;
}at_linkConType;
+void at_user_input(void *arg, char *pdata, unsigned short len);
+
void at_testCmdCifsr(uint8_t id);
void at_setupCmdCifsr(uint8_t id, char *pPara);
void at_exeCmdCifsr(uint8_t id);
diff --git a/user/user_main.c b/user/user_main.c
index 13eeaf2..ea97ffc 100644
--- a/user/user_main.c
+++ b/user/user_main.c
@@ -7,9 +7,48 @@ extern uint8_t at_wifiMode;
void user_init(void)
{
+ //Init uart
uart_init(BIT_RATE_115200, BIT_RATE_115200);
+
+ //Set GPIO2 to output mode
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
+
+ //Set GPIO2 low
+ gpio_output_set(0, BIT2, BIT2, 0);
+
at_wifiMode = wifi_get_opmode();
os_printf("\r\nready!!!\r\n");
uart0_sendStr("\r\nready\r\n");
at_init();
+
+ int xdel;
+ for(xdel=0;xdel<5000000;xdel++)
+ {
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ }
+
+ uart0_sendStr("Setting MUX to 1:");
+ at_setupCmdCipmux(15, "=1");
+
+ for(xdel=0;xdel<5000000;xdel++)
+ {
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ }
+
+ at_queryCmdCipmux(15);
+
+ for(xdel=0;xdel<5000000;xdel++)
+ {
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ asm("nop"); asm("nop"); asm("nop");
+ }
+ uart0_sendStr("Starting TCP server:");
+ at_setupCmdCipserver(16, "=1,9999");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment