/************************************************************************************************** * @brief Set mode for ESP8266 * @param[in] mode Mode for the esp8266 * @return true if success **************************************************************************************************/ bool esp8266_wifi::esp8266_mode(uint8_t mode) { esp8266_reset(); delay_ms(10000); esp8266_flushin(); delay_ms(2000); esp8266_send((int8_t *)"ATE0\r\n",strlen("ATE0\r\n")); esp8266_flushin(); delay_ms(3000); if(!esp8266_sendCheckReply((int8_t *)"AT+CWMODE=1\r\n",(int8_t *) "OK")) { printf("AT+CWMODE=1 error\n"); return false; } delay_ms(2000); return true; }
/************************************************************************************************** * @brief Restart the ESP module * @return true if the module restarted properly **************************************************************************************************/ bool esp8266_wifi::esp8266_reset(void) { printf("Inside reset function\n"); esp8266_send((int8_t *)"AT+RST\r\n",strlen("AT+RST\r\n")); return true; }
/************************************************************************************************** * @brief Send a block of data via UART peripheral * This sends the AT+CIPSEND command followed byte data to the ESP module. * @param[in] data :Pointer to Transmit buffer * datalen : data length * @return true if success **************************************************************************************************/ bool esp8266_wifi::esp8266_sendData(int8_t *data,uint8_t datalen) { char tempBuffer[256] = {0}; sprintf(tempBuffer,"AT+CIPSEND=%d\r\n",datalen); esp8266_send((int8_t*)tempBuffer,(uint8_t)strlen(tempBuffer)); delay_ms(2000); //while (esp8266_getch() != '>'); printf("Datalength is %d\n",datalen); printf("Data is %s\n",data); esp8266_send(data,datalen); printf("data sent\n"); return true; }
/************************************************************************************************** * @brief Receive response from ESP8266 * @param[in] send : pointer Data to be sent * multiline: If multiple lines are expected in response * @return Received data length **************************************************************************************************/ uint8_t esp8266_wifi::esp8266_getReply(int8_t *send, bool multiline) { uint8_t readLength =0; esp8266_send(send,strlen((const char *)send)); delay_ms(4000); readLength = esp_uartReceive(replybuffer,sizeof(replybuffer),NONE_BLOCKING, multiline); printf("%s\n",replybuffer); return readLength; }
/*JSON{ "type" : "method", "class" : "ESPWifi", "name" : "connect", "generate" : "jswrap_esp8266_connect", "params" : [ ["ap","JsVar","Access point name"], ["key","JsVar","WPA2 key (or undefined for unsecured connection)"], ["callback","JsVar","Function to call back with connection status. It has one argument which is one of 'connect'/'disconnect'/'dhcp'"] ], "return" : ["bool",""] } Connect to an access point */ bool jswrap_esp8266_connect(JsVar *wlanObj, JsVar *vAP, JsVar *vKey, JsVar *callback) { NOT_USED(wlanObj); JsNetwork net; if (!networkGetFromVar(&net)) return false; // 'AT+CWMODE=1\r' ? seems to be the default JsVar *msg = jsvVarPrintf("AT+CWJAP=%q,%q\r", vAP, vKey); esp8266_send(msg); jsvUnLock(msg); if (!esp8266_wait_for("OK",500, false)) return false; networkFree(&net); if (callback) jsiQueueEvents(callback, 0, 0); return true; }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266", "name" : "connect", "generate" : "jswrap_esp8266_connect_device", "params" : [ ["serial","JsVar","The Serial port used for communications with the ESP8266 (must already be setup)"], ["callback","JsVar","Function to call back when connected"] ], "return" : ["JsVar","An ESP8266 Object"], "return_object" : "ESP8266" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_esp8266_connect_device(JsVar *usart, JsVar *callback) { IOEventFlags usartDevice; usartDevice = jsiGetDeviceFromClass(usart); if (!DEVICE_IS_USART(usartDevice)) { jsExceptionHere(JSET_ERROR, "Expecting USART device, got %q", usart); return 0; } JsNetwork net; networkCreate(&net, JSNETWORKTYPE_ESP8266); net.data.device = usartDevice; networkSet(&net); JsVar *wifiObj = 0; JsVar *cmd = jsvNewFromString("AT+RST\r\n"); esp8266_send(cmd); jsvUnLock(cmd); if (esp8266_wait_for("OK", 100, false)) { if (esp8266_wait_for("ready", 4000, false)) { networkState = NETWORKSTATE_ONLINE; wifiObj = jspNewObject(0, "ESPWifi"); } else { jsExceptionHere(JSET_ERROR, "Module not ready"); } } else { jsExceptionHere(JSET_ERROR, "No Acknowledgement"); } networkFree(&net); if (callback) jsiQueueEvents(callback, 0, 0); return wifiObj; }
int main(void) { char *text = "abc123456879754461325456478974564153217897654564132156489768545613214567489465413521231324567489797468545646958798"; int payload_length, context_length; bool esp8266_exist=false, connected=false, test_send=true; uint8_t *context; test[0] = 0x81; test[1] = strlen(text); strcpy( (char *)test+2, text ); /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /*##-1- Configure the UART peripheral ######################################*/ /* Put the USART peripheral in the Asynchronous mode (UART Mode) */ /* UART1 configured as follow: - Word Length = 8 Bits - Stop Bit = One Stop bit - Parity = None - BaudRate = 9600 baud - Hardware flow control disabled (RTS and CTS signals) */ UartHandle.Instance = USARTx; UartHandle.Init.BaudRate = 115200; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; UartHandle.Init.Parity = UART_PARITY_NONE; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; if(HAL_UART_Init(&UartHandle) != HAL_OK) { Error_Handler(); } /* The board receives the message and sends it back */ /*##-2- Put UART peripheral in reception process ###########################*/ if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer8266, RXBUFFERSIZE) != HAL_OK) { while(1); } if( esp8266_init() ){ esp8266_exist = true; if( !esp8266_cmd("AT+CWMODE=2\r\n", "no change\r\n", 100) && !esp8266_cmd("AT+CWMODE=2\r\n", "\r\nOK\r\n", 100) ){ while(1); } if( !esp8266_cmd("AT+CIPMUX=1\r\n", "\r\nOK\r\n", 100) ){ while(1); } if( !esp8266_cmd("AT+CIPSERVER=1,8080\r\n", "\r\nOK\r\n", 200) ){ while(1); } } while (1){ switch( esp8266_parse(payload, &payload_length) ){ //maybe more than one event in the messages, but parse only one each time case NO_EVENT : break; case LINK : break; case UNLINK : connected = 0; break; case PAYLOAD : if( 0 == strncmp((const char *)payload, "GET /chat", 9) ){ //handshake web_soket_handshake( (char *)payload ); }else if( 0x81 == *payload ){ //websocket data rm_mask(payload, payload_length, &context, &context_length); if( 0 == strncmp( (char *)context, "login:"******"fanlong: stop", 13 ) ){ test_send = false; }else if( 0 == strncmp( (char *)context, "fanlong: start", 14 ) ){ test_send = true; } } break; case SEND_OK : break; case RST : break; default : break; } /* at least one char will be received in 1 ms; if you ignore this delay, maybe you update_length() just after one message ended and the next is coming. coment it if and only if you are sure there is no message just begin; */ HAL_Delay(1); if( update_length() ){ continue;// still have message } //now I'm sure there is no more message will come if( connected && test_send ){ //make sure rxbuffer is empty esp8266_send(0, test, strlen(text)+2); HAL_Delay(100); } } }