void user_init(void) { uart_init(BIT_RATE_115200, BIT_RATE_115200); uart0_sendStr("\r\nesp8266 ws2812 driver\r\n"); // int opm = wifi_get_opmode(); // if( opm == 1 ) need_to_switch_opmode = 120; // wifi_set_opmode_current(2); //Uncomment this to force a system restore. // system_restore(); CSSettingsLoad( 0 ); CSPreInit(); pUdpServer = (struct espconn *)os_zalloc(sizeof(struct espconn)); ets_memset( pUdpServer, 0, sizeof( struct espconn ) ); espconn_create( pUdpServer ); pUdpServer->type = ESPCONN_UDP; pUdpServer->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); pUdpServer->proto.udp->local_port = 7777; espconn_regist_recvcb(pUdpServer, udpserver_recv); if( espconn_create( pUdpServer ) ) { while(1) { uart0_sendStr( "\r\nFAULT\r\n" ); } } CSInit(); SetServiceName( "ws2812" ); AddMDNSName( "cn8266" ); AddMDNSName( "ws2812" ); AddMDNSService( "_http._tcp", "An ESP8266 Webserver", 80 ); AddMDNSService( "_ws2812._udp", "WS2812 Driver", 7777 ); AddMDNSService( "_cn8266._udp", "ESP8266 Backend", 7878 ); //Add a process system_os_task(procTask, procTaskPrio, procTaskQueue, procTaskQueueLen); //Timer example os_timer_disarm(&some_timer); os_timer_setfn(&some_timer, (os_timer_func_t *)myTimer, NULL); os_timer_arm(&some_timer, 100, 1); ws2812_init(); uint8_t ledout[] = { 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00 }; ws2812_push( ledout, sizeof( ledout ) ); system_os_post(procTaskPrio, 0, 0 ); }
// ---------------------------------------------------------------------------- // Send data // Parameters: uint8* psent -- Data to send // uint16 length -- Length of data to send // ---------------------------------------------------------------------------- static void create_udp(void) { uint32_t ip; os_timer_disarm(&WiFiLinker); ip = ipaddr_addr(UDPSERVERIP); os_memcpy(ConnUDP.remote_ip, &ip, 4); struct ip_info ipConfig; wifi_get_ip_info(STATION_IF, &ipConfig); os_memcpy(ConnUDP.local_ip, &ipConfig.ip.addr, 4); ConnUDP.local_port = UDPSERVERPORT; ConnUDP.remote_port = UDPSERVERPORT; Conn.proto.udp = &ConnUDP; Conn.type = ESPCONN_UDP; Conn.state = ESPCONN_NONE; espconn_regist_recvcb(&Conn, recv_cb); // register a udp packet receiving callback espconn_regist_sentcb(&Conn, sent_cb); os_printf("Start espconn_connect to " IPSTR ":%d\n", IP2STR(Conn.proto.udp->remote_ip), Conn.proto.udp->remote_port); os_printf("Start espconn_connect from " IPSTR ":%d\n", IP2STR(Conn.proto.udp->local_ip), Conn.proto.udp->local_port); espconn_create(&Conn); // create udp os_timer_setfn(&WiFiLinker, (os_timer_func_t *)wifi_check_ip, NULL); os_timer_arm(&WiFiLinker, 1000, 0); }
static void ICACHE_FLASH_ATTR syslog_dns_callback(const char * hostname, ip_addr_t * addr, void * arg) { sint8 rc; /** * Store the target IP address. */ if (addr != NULL) { // // CONSOLE("syslog: Hostname: %s, IP address: " IPSTR, hostname, IP2STR(addr)); os_memcpy(syslog_conn->proto.udp->remote_ip, addr, 4); // // CONSOLE("syslog: Hostname: %s, IP address: " IPSTR, hostname, IP2STR(addr)); CONSOLE("syslog: local IP address:port = " IPSTR ":%d", IP2STR(syslog_conn->proto.udp->local_ip), syslog_conn->proto.udp->local_port); CONSOLE("syslog: remote IP address:port = " IPSTR ":%d", IP2STR(syslog_conn->proto.udp->remote_ip), syslog_conn->proto.udp->remote_port); syslog_inactive = FALSE; rc = espconn_create(syslog_conn); if (rc == 0) { rc = espconn_regist_sentcb(syslog_conn, syslog_sendto_callback); } if (rc != 0) { // CONSOLE("syslog: create UDP connection failed: %d", (int)rc); } syslog_sendto(); } else { // CONSOLE("syslog: gethostbyname() for '%s' failed!", hostname); } }
static void socket_connect(struct espconn *pesp_conn) { if(pesp_conn == NULL) return; lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse; if(nud == NULL) return; if( pesp_conn->type == ESPCONN_TCP ) { #ifdef CLIENT_SSL_ENABLE if(nud->secure){ espconn_secure_connect(pesp_conn); } else #endif { espconn_connect(pesp_conn); } } else if (pesp_conn->type == ESPCONN_UDP) { espconn_create(pesp_conn); } NODE_DBG("socket_connect is called.\n"); }
//---------------------------- // connect / disconnect //---------------------------- bool ESP8266Client::connect() { // already connecting? if (m_bIsConnecting) { return false; } sint8 res = ESPCONN_OK; // if connected... disconnect first if (m_bIsConnected) { disconnect(); } if (isTcp()) { res = espconn_connect(esp_conn); if (res == ESPCONN_OK) { m_bIsConnecting = true; } } else if (isUdp()) { res = espconn_create(esp_conn); m_bIsConnected = true; m_bIsConnecting = false; } if (res != ESPCONN_OK) { error("could not connect: ", res); } return res == ESPCONN_OK; }
void ICACHE_FLASH_ATTR ntp_get_time() { ntp_t ntp; // set up the udp "connection" pCon = (struct espconn*)os_zalloc(sizeof(struct espconn)); pCon->type = ESPCONN_UDP; pCon->state = ESPCONN_NONE; pCon->proto.udp = (esp_udp*)os_zalloc(sizeof(esp_udp)); pCon->proto.udp->local_port = espconn_port(); pCon->proto.udp->remote_port = 123; os_memcpy(pCon->proto.udp->remote_ip, ntp_server, 4); // create a really simple ntp request packet os_memset(&ntp, 0, sizeof(ntp_t)); ntp.options = 0b00100011; // leap = 0, version = 4, mode = 3 (client) // set timeout timer os_timer_disarm(&ntp_timeout); os_timer_setfn(&ntp_timeout, (os_timer_func_t*)ntp_udp_timeout, pCon); os_timer_arm(&ntp_timeout, NTP_TIMEOUT_MS, 0); // send the ntp request espconn_create(pCon); espconn_regist_recvcb(pCon, ntp_udp_recv); espconn_sent(pCon, (uint8*)&ntp, sizeof(ntp_t)); }
int ICACHE_FLASH_ATTR discovery_create(DISCOVER_ENV *env) { DISCOVERY_OBJECT * handle = instance(); if (NULL == handle) { os_printf("[dis] create error \n"); return -1; } if (os_strlen(env->dev_uuid) > 0) { os_strcpy((char*)handle->dev_uuid, (char*)env->dev_uuid); } handle->port = env->port; handle->out_buf = (int8*)os_zalloc(MAX_RECV_BUF_LEN); if (NULL == handle->out_buf) { return -1; } handle->udp_conn.type = ESPCONN_UDP; handle->udp_conn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); if (NULL == handle->udp_conn.proto.udp) { return -1; } handle->udp_conn.proto.udp->local_port = BROADCASE_PORT; espconn_regist_recvcb(&handle->udp_conn, discovery_recv); espconn_create(&handle->udp_conn); os_printf("[dis] create ok \n"); return 0; }
/****************************************************************************** * FunctionName : user_devicefind_init * Description : the espconn struct parame init * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_devicefind_init(void) { ptrespconn.type = ESPCONN_UDP; ptrespconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); ptrespconn.proto.udp->local_port = 1025; espconn_regist_recvcb(&ptrespconn, user_devicefind_recv); espconn_create(&ptrespconn); }
void user_socket_init(int rxPort) { ptresp_probe.type = ESPCONN_UDP; ptresp_probe.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); ptresp_probe.proto.udp->local_port = 333; espconn_regist_recvcb(&ptresp_probe, user_socket_probe_rx); espconn_create(&ptresp_probe); ptresp_serial.type = ESPCONN_UDP; ptresp_serial.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); ptresp_serial.proto.udp->local_port = rxPort; espconn_regist_recvcb(&ptresp_serial, user_socket_serial_rx); espconn_create(&ptresp_serial); os_timer_disarm(&tx_timer); os_timer_setfn(&tx_timer, (os_timer_func_t *)rx_timer_cb, NULL); //os_timer_arm(&link_led_timer, 1000, 1); }
void ICACHE_FLASH_ATTR user_udp_init(void) { ptrespconn.type = ESPCONN_UDP; ptrespconn.proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp)); ptrespconn.proto.udp->local_port = 1025; /// ESP8266 udp port espconn_regist_recvcb(&ptrespconn, user_udp_recv); // register a udp packet receiving callback espconn_create(&ptrespconn); // create udp os_printf("Listen UDP port 1025\r\n"); }
/****************************************************************************** * FunctionName : user_check_ip * Description : check whether get ip addr or not * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_check_ip(void) { struct ip_info ipconfig; //disarm timer first os_timer_disarm(&test_timer); //get ip info of ESP8266 station wifi_get_ip_info(STATION_IF, &ipconfig); if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) { //os_printf("got ip !!! \r\n"); wifi_set_broadcast_if(STATIONAP_MODE); // send UDP broadcast from both station and soft-AP interface user_udp_espconn.type = ESPCONN_UDP; user_udp_espconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); user_udp_espconn.proto.udp->local_port = 1112; // set a available port const char udp_remote_ip[4] = {255, 255, 255, 255}; os_memcpy(user_udp_espconn.proto.udp->remote_ip, udp_remote_ip, 4); // ESP8266 udp remote IP user_udp_espconn.proto.udp->remote_port = 5556; // ESP8266 udp remote port espconn_regist_recvcb(&user_udp_espconn, user_udp_recv_cb); // register a udp packet receiving callback espconn_regist_sentcb(&user_udp_espconn, user_udp_sent_cb); // register a udp packet sent callback espconn_create(&user_udp_espconn); // create udp user_udp_send(); // send udp data os_timer_arm(&send_timer, 40, 0); } else { if ((wifi_station_get_connect_status() == STATION_WRONG_PASSWORD || wifi_station_get_connect_status() == STATION_NO_AP_FOUND || wifi_station_get_connect_status() == STATION_CONNECT_FAIL)) { //os_printf("connect fail !!! \r\n"); } else { //os_printf("still waiting...\n"); //re-arm timer to check ip os_timer_setfn(&test_timer, (os_timer_func_t *)user_check_ip, NULL); os_timer_arm(&test_timer, 500, 0); } } }
void ICACHE_FLASH_ATTR SetupMDNS() { MDNSNames[0] = strdupcaselower( &SETTINGS.DeviceName[0] ); pMDNSServer = (struct espconn *)os_zalloc(sizeof(struct espconn)); ets_memset( pMDNSServer, 0, sizeof( struct espconn ) ); pMDNSServer->type = ESPCONN_UDP; pMDNSServer->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); pMDNSServer->proto.udp->local_port = 5353; espconn_regist_recvcb(pMDNSServer, got_mdns_packet); espconn_create( pMDNSServer ); }
LOCAL void setupUDP() { esp_udp udp; udp.local_port = 25867; conn1.type = ESPCONN_UDP; conn1.state = ESPCONN_NONE; conn1.proto.udp = &udp; espconn_create(&conn1); espconn_regist_recvcb(&conn1, recvCB); os_printf("Listening for data\n"); }
/* * 硬件平台相关,创建UDP套接字,监听12476端口 */ void ICACHE_FLASH_ATTR airkiss_nff_start(void) { airkiss_udp.local_port = 12476; ptrairudpconn.type = ESPCONN_UDP; ptrairudpconn.proto.udp = &(airkiss_udp); espconn_create(&ptrairudpconn); espconn_regist_recvcb(&ptrairudpconn, wifilan_recv_callbk); os_timer_setfn(&time_serv, (os_timer_func_t *) time_callback, NULL); os_timer_arm(&time_serv, 5000, 1); //5s定时器 }
LOCAL void ICACHE_FLASH_ATTR setupTCP() { conn1.type = ESPCONN_TCP; conn1.state = ESPCONN_NONE; tcp1.local_port = 8080; conn1.proto.tcp = &tcp1; espconn_accept(&conn1); espconn_create(&conn1); espconn_regist_connectcb(&conn1, recvCB); //espconn_regist_recvcb(&conn1, recvCB); os_printf("Listening for data\n"); } // End of setupUDP
void ICACHE_FLASH_ATTR airkiss_start_discover(void) { ssdp_udp.local_port = DEFAULT_LAN_PORT; pssdpudpconn.type = ESPCONN_UDP; pssdpudpconn.proto.udp = &(ssdp_udp); espconn_regist_recvcb(&pssdpudpconn, airkiss_wifilan_recv_callbk); espconn_create(&pssdpudpconn); os_timer_disarm(&ssdp_time_serv); os_timer_setfn(&ssdp_time_serv, (os_timer_func_t *)airkiss_wifilan_time_callback, NULL); os_timer_arm(&ssdp_time_serv, 1000, 1);//1s }
/****************************************************************************** * FunctionName : user_devicefind_init * Description : the espconn struct parame init * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_devicefind_init(void) { #if ESP_DEBUG_MODE && ESP_MESH_SUPPORT user_DeviceFindRespSet(true); DF_DBG("device find string: len: %d ; %s \r\n",os_strlen(device_find_response_ok),device_find_response_ok); #endif ptrespconn.type = ESPCONN_UDP; ptrespconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); ptrespconn.proto.udp->local_port = 1025; espconn_regist_recvcb(&ptrespconn, user_devicefind_recv); espconn_create(&ptrespconn); }
static int ICACHE_FLASH_ATTR server_init_udp(uint8 ifnum) { DEBUG("enter server_init_udp"); static struct ip_info info; static struct espconn server_conn; static esp_udp server_udp_sta; int rc; if (!wifi_get_ip_info(ifnum, &info)) { ets_uart_printf("Failed to get ip info.\n"); DEBUG("exit server_init_udp"); return -1; } server_udp_sta.local_port = 1025; os_memcpy(server_udp_sta.local_ip, &(info.ip.addr), 4); server_conn.type = ESPCONN_UDP; server_conn.proto.udp = &server_udp_sta; server_conn.link_cnt = 0; server_conn.reverse = NULL; if (espconn_regist_sentcb(&server_conn, udpserver_sent_cb) != 0) { ets_uart_printf("Failed to register sent callback.\n"); DEBUG("exit server_init_udp"); return -1; } if (espconn_regist_recvcb(&server_conn, udpserver_recv_cb) != 0) { ets_uart_printf("Failed to register recv callback.\n"); DEBUG("exit server_init_udp"); return -1; } if ((rc = espconn_create(&server_conn)) != 0) { if (rc == ESPCONN_ISCONN) { ets_uart_printf("UDP server already connected.\n"); } else { ets_uart_printf("Failed to create UDP server.\n"); DEBUG("exit server_init_udp"); return -1; } } // udpserver_conn = &server_conn; ets_uart_printf("Successfully initialized UDP server.\n\n"); DEBUG("exit server_init_udp"); return 0; }
/****************************************************************************** * FunctionName : initSyslog * Description : Initialize the syslog library * Parameters : syslog_host -- the syslog host (host:port) * host: IP-Addr | hostname * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR syslog_init(char *syslog_host) { if (!*syslog_host) { syslogState = SYSLOG_HALTED; return; } char host[32], *port = &host[0]; syslog_task = register_usr_task(syslog_udp_send_event); syslogHost.min_heap_size = flashConfig.syslog_minheap; syslogHost.port = 514; syslogState = SYSLOG_WAIT; os_strncpy(host, syslog_host, 32); while (*port && *port != ':') // find port delimiter port++; if (*port) { *port++ = '\0'; syslogHost.port = atoi(port); } wifi_set_broadcast_if(STATIONAP_MODE); // send UDP broadcast from both station and soft-AP interface syslog_espconn.type = ESPCONN_UDP; syslog_espconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); syslog_espconn.proto.udp->local_port = espconn_port(); // set a available port #ifdef SYSLOG_UDP_RECV espconn_regist_recvcb(&syslog_espconn, syslog_udp_recv_cb); // register a udp packet receiving callback #endif espconn_regist_sentcb(&syslog_espconn, syslog_udp_sent_cb); // register a udp packet sent callback espconn_create(&syslog_espconn); // create udp if (UTILS_StrToIP((const char *)host, (void*)&syslogHost.addr)) { syslogState = SYSLOG_SENDING; syslog_send_udp(); } else { static struct espconn espconn_ghbn; espconn_gethostbyname(&espconn_ghbn, host, &syslogHost.addr, syslog_gethostbyname_cb); // syslog_send_udp is called by syslog_gethostbyname_cb() } #ifdef SYSLOG_UDP_RECV DBG("syslog_init: host: %s, port: %d, lport: %d, recvcb: %p, sentcb: %p, state: %d\n", host, syslogHost.port, syslog_espconn.proto.udp->local_port, syslog_udp_recv_cb, syslog_udp_sent_cb, syslogState ); #else DBG("syslog_init: host: %s, port: %d, lport: %d, rsentcb: %p, state: %d\n", host, syslogHost.port, syslog_espconn.proto.udp->local_port, syslog_udp_sent_cb, syslogState ); #endif }
static void ICACHE_FLASH_ATTR udp_init() { static struct espconn udpServer; static esp_udp udp; udpServer.type = ESPCONN_UDP; udpServer.state = ESPCONN_NONE; udpServer.proto.udp = &udp; udpServer.proto.udp->local_port = settings.udpPort; if (espconn_create(&udpServer) == 0) { espconn_regist_recvcb(&udpServer, udp_received); DEBUG_PRINT("[HAP]Started UDP server\n"); } }
void tftp_init() { static struct espconn tftpconn; static esp_udp tftpudp; tftpconn.type = ESPCONN_UDP; tftpconn.state = ESPCONN_NONE; tftpconn.proto.udp = &tftpudp; tftpudp.local_port=69; tftpconn.reverse = NULL; espconn_regist_recvcb(&tftpconn, tftp_recv); espconn_create(&tftpconn); #ifdef PLATFORM_DEBUG ets_uart_printf("TFTP init done.\r\n"); #endif }
ICACHE_FLASH_ATTR void syslog_init(const char *server, const char *hostname) { sint8 rc; syslog_hostname = hostname; syslog_espconn.type = ESPCONN_UDP; syslog_espconn.state = ESPCONN_NONE; syslog_espconn.proto.udp = &syslog_espconn_udp; syslog_espconn.proto.udp->local_port = espconn_port(); syslog_espconn.proto.udp->remote_port = 514; /* syslog */ ipaddr_aton(server, syslog_espconn.proto.udp->remote_ip); rc = espconn_create(&syslog_espconn); }
void ICACHE_FLASH_ATTR ssServerInit() { struct espconn * pSimpleServer; pSimpleServer = (struct espconn *)os_zalloc(sizeof(struct espconn)); ets_memset( pSimpleServer, 0, sizeof( struct espconn ) ); espconn_create( pSimpleServer ); pSimpleServer->type = ESPCONN_TCP; pSimpleServer->state = ESPCONN_NONE; pSimpleServer->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pSimpleServer->proto.tcp->local_port = 80; //espconn_set_opt(pSimpleServer, ESPCONN_REUSEADDR); espconn_regist_connectcb(pSimpleServer, ssConnCb); espconn_accept(pSimpleServer); espconn_regist_time(pSimpleServer, 0, 0); }
void ICACHE_FLASH_ATTR wifiConnectCb(uint8_t status) { if(status == STATION_GOT_IP){ INFO("Wifi connection established\n"); udp_server = (struct espconn *) os_zalloc(sizeof(struct espconn)); udp_server->type = ESPCONN_UDP; udp_server->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp)); udp_server->proto.udp->local_port = udp_port; espconn_regist_recvcb(udp_server, udpserver_recv); espconn_create(udp_server); system_os_post(DMX_TASK_PRIO, 0, 0); } else if(udp_server != NULL) { espconn_delete(udp_server); os_free(udp_server); udp_server = NULL; } }
void ICACHE_FLASH_ATTR init_dns() { //set softAP DHCP server router info uint8_t mode = 1; wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode); //1:station; 2:soft-AP, 3:station+soft-AP wifi_set_broadcast_if(3); // espconn_disconnect(&dnsConn); espconn_delete(&dnsConn); dnsConn.type=ESPCONN_UDP; dnsConn.state=ESPCONN_NONE; dnsUdp.local_port= 53; dnsConn.proto.udp=&dnsUdp; espconn_regist_recvcb(&dnsConn, dnsQueryReceived); int res = espconn_create(&dnsConn); NODE_DBG("DNS server init, conn=%p , status=%d", &dnsConn,res); }
void ICACHE_FLASH_ATTR tfp_open_connection(void) { // Common initialisations. ringbuffer_init(&tfp_rb, tfp_rb_buffer, TFP_RING_BUFFER_SIZE); brickd_init(); com_init(); for(uint8_t i = 0; i < TFP_MAX_CONNECTIONS; i++) { tfp_init_con(i); } /* * When mesh mode is enabled all the socket setup is done from mesh specific * callbacks. Existing TFP socket callbacks and implementation are used but as * a layer underneath the mesh layer. */ if(!configuration_current.mesh_enable) { ets_memset(&tfp_con_listen, 0, sizeof(struct espconn)); // Initialize the ESPConn espconn_create(&tfp_con_listen); tfp_con_listen.type = ESPCONN_TCP; tfp_con_listen.state = ESPCONN_NONE; // Make it a TCP connection tfp_con_listen.proto.tcp = &tfp_con_listen_tcp; tfp_con_listen.proto.tcp->local_port = configuration_current.general_port; espconn_regist_reconcb(&tfp_con_listen, tfp_reconnect_callback); espconn_regist_connectcb(&tfp_con_listen, tfp_connect_callback); // Start listening espconn_accept(&tfp_con_listen); // Set server timeout (in seconds) espconn_regist_time(&tfp_con_listen, 7200, 0); } else { logi("MSH:TFP init\n"); } }
// initialize the custom stuff that goes beyond esp-link void user_init() { // Initialize the GPIO subsystem. gpio_init(); /* ====================================== */ /* UART */ /* ====================================== */ // Initialize UART0 and UART1 /* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */ uart_init( BIT_RATE_115200, BIT_RATE_115200 ); // uart0_sendStr( "\nUART0 - USED TO PROGRAM THE MODULE\n" ); os_printf("\n===================\nUART1 - DEBUG OUPUT\n===================\n"); /* NOTE: PWM CANNOT BE USED SIMULTANEOUSLY WITH HW TIMER */ #if 0 /* ====================================== */ /* PWM */ /* ====================================== */ uint32 pwm_period = 1000; uint32 pwm_duty[PWM_CHANNEL] = {0}; uint32 io_info[][3] = { {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM}, {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM}, }; /* PIN FUNCTION INIT FOR PWM OUTPUT */ pwm_init(pwm_period, pwm_duty, PWM_CHANNEL, io_info); /* set pwm_duty cycle */ pwm_set_duty (14185, 0); pwm_set_duty (22222, 1); // todo: explain why 22222 is the highest possible value /* start PWM */ pwm_start(); // NOTE: PWM causes spikes in other GPIOs #endif /* ====================================== */ /* GPIO INTERRPUT */ /* ====================================== */ /* Set GPIO12 in GPIO mode */ PIN_FUNC_SELECT( PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12 ); /* Set GPIO12 as input */ GPIO_DIS_OUTPUT( GPIO_ID_PIN(12) ); /* Disable all GPIO interrupts */ ETS_GPIO_INTR_DISABLE(); /* Set a GPIO callback function */ ETS_GPIO_INTR_ATTACH( gpioCallback, NULL ); /* Configure the type of edge */ gpio_pin_intr_state_set( GPIO_ID_PIN(12), GPIO_PIN_INTR_ANYEDGE ); ETS_GPIO_INTR_ENABLE(); /* ====================================== */ /* SOFTWARE TIMER */ /* ====================================== */ // Set GPIO0 to output mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); //Set GPIO0 low gpio_output_set(0, RELAY_PIN, RELAY_PIN, 0); /* disarm timer */ os_timer_disarm((ETSTimer*)&some_timer); /* set callback */ os_timer_setfn((ETSTimer*)&some_timer, (os_timer_func_t *) softwareTimerCallback, NULL); /* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */ os_timer_arm((ETSTimer*)&some_timer, 10, 1); /* ====================================== */ /* OS TASK */ /* ====================================== */ /* setup OS task */ // system_os_task(user_procTask, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); /* send a message to OS task (fire task) */ // system_os_post(user_procTaskPrio, 0, 0 ); /* ====================================== */ /* HARDWARE TIMER */ /* ====================================== */ /* The hardware timer is used to indicate when a complete IR message frame should have * arrived in order to process the received data and calculate the IR command. * * It is configured in "one-shot" mode. It is started when the beginning of an * IR message frame is detected and stopped after the complete message frame has been read. * This means that the duration of the HW timer should be longer than the duration of * the longest message frame. In the NEC IR tranmission protocol all message frames have * a duration of approximately 67.5ms. */ /* load the HW TIMER */ uint32 ticks = usToTicks(70000); // 70ms RTC_REG_WRITE(FRC1_LOAD_ADDRESS, ticks); /* register callback function */ ETS_FRC_TIMER1_INTR_ATTACH( hwTimerCallback, NULL ); /* enable interrupts */ TM1_EDGE_INT_ENABLE(); ETS_FRC1_INTR_ENABLE(); /* don't start timer yet */ /* the timer is started inside the GPIO INT callback */ /* ====================================== */ /* UDP SERVER */ /* ====================================== */ /* usage: echo <data> | nc -wl -u <ip address> <port> * example: echo "foo" | nc -w1 -u 192.168.1.187 7777 */ /* allocate space for server */ pUdpServer = (struct espconn *) os_zalloc(sizeof(struct espconn)); /* clear allocated memory */ ets_memset(pUdpServer, 0, sizeof(struct espconn)); /* create the server */ espconn_create(pUdpServer); /* set the type of server */ pUdpServer->type = ESPCONN_UDP; /* allocate memory for UDP settings */ pUdpServer->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp)); /* set the port that the server will be listening to */ pUdpServer->proto.udp->local_port = 7777; /* register the callback */ espconn_regist_recvcb(pUdpServer, udpServerRxCb); /* start listening */ if (espconn_create(pUdpServer)) { while (1) { os_printf("Error creating a UDP server\n"); } } /* ====================================== */ /* WIFI */ /* ====================================== */ wifi_set_opmode(STATION_MODE); wifi_station_get_config_default(&stconf); // os_strncpy((char*) stconf.ssid, "TP-LINK_2.4GHz_FC2E51", 32); // os_strncpy((char*) stconf.password, "tonytony", 64); os_strncpy((char*) stconf.ssid, "WLAN-PUB", 32); os_strncpy((char*) stconf.password, "", 64); // os_strncpy((char*) stconf.ssid, "MAD air", 32); // os_strncpy((char*) stconf.password, "glioninlog", 64); stconf.bssid_set = 0; wifi_station_set_config(&stconf); // /* ====================================== */ // /* WS2812 LED STRIP */ // /* ====================================== */ // // /* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */ // ws2812_init(); // // /* G R B */ // uint8_t ledout[] = { // 0xff, 0x00, 0x00, //4th //// 0xff, 0x00, 0x00, //3rd //// 0x00, 0xff, 0x00, //2nd //// 0x00, 0x00, 0xff, //1st // }; // //#if 0 // os_printf("\r\nB R G: %x %x %x\r\n", ledout[0], ledout[1],ledout[2]); //#endif // // ws2812_push( ledout, sizeof( ledout ) ); /* ====================================== */ /* TCP CONNECTION */ /* ====================================== */ /* allocate space for server */ pTcpConn = (struct espconn *) os_zalloc(sizeof(struct espconn)); /* clear allocated memory */ ets_memset(pTcpConn, 0, sizeof(struct espconn)); /* set the type of connection */ pTcpConn->type = ESPCONN_TCP; /* set state to NONE */ pTcpConn->state = ESPCONN_NONE; /* allocate memory for TCP settings */ pTcpConn->proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp)); /* set the port that the connection will be listening to */ pTcpConn->proto.tcp->local_port = espconn_port(); /* set the remote port and IP address */ pTcpConn->proto.tcp->remote_port = 80; os_memcpy(pTcpConn->proto.tcp->remote_ip, server_ip_address, sizeof(server_ip_address)); /* register callbacks */ espconn_regist_connectcb(pTcpConn, tcpConnCb); espconn_regist_reconcb(pTcpConn, tcpReconnCb); /* disarm timer */ os_timer_disarm((ETSTimer*)&wifi_setup_timer); /* set callback */ os_timer_setfn((ETSTimer*)&wifi_setup_timer, (os_timer_func_t *) wifiConnectTimerCb, NULL); /* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */ os_timer_arm((ETSTimer*)&wifi_setup_timer, 5000, 1); return; }
// Lua: server:listen( port, ip, function(con) ) // Lua: socket:connect( port, ip, function(con) ) static int net_start( lua_State* L, const char* mt ) { NODE_DBG("net_start is called.\n"); struct espconn *pesp_conn = NULL; lnet_userdata *nud; unsigned port; size_t il; bool isserver = false; ip_addr_t ipaddr; const char *domain; uint8_t stack = 1; if (mt!=NULL && c_strcmp(mt, "net.server")==0) isserver = true; else if (mt!=NULL && c_strcmp(mt, "net.socket")==0) isserver = false; else { NODE_DBG("wrong metatable for net_start.\n"); return 0; } nud = (lnet_userdata *)luaL_checkudata(L, stack, mt); luaL_argcheck(L, nud, stack, "Server/Socket expected"); stack++; if(nud==NULL){ NODE_DBG("userdata is nil.\n"); return 0; } pesp_conn = nud->pesp_conn; port = luaL_checkinteger( L, stack ); stack++; if( pesp_conn->type == ESPCONN_TCP ) { if(isserver) pesp_conn->proto.tcp->local_port = port; else{ pesp_conn->proto.tcp->remote_port = port; pesp_conn->proto.tcp->local_port = espconn_port(); } NODE_DBG("TCP port is set: %d.\n", port); } else if (pesp_conn->type == ESPCONN_UDP) { if(isserver) pesp_conn->proto.udp->local_port = port; else{ pesp_conn->proto.udp->remote_port = port; pesp_conn->proto.udp->local_port = espconn_port(); } NODE_DBG("UDP port is set: %d.\n", port); } if( lua_isstring(L,stack) ) // deal with the domain string { domain = luaL_checklstring( L, stack, &il ); stack++; if (domain == NULL) { if(isserver) domain = "0.0.0.0"; else domain = "127.0.0.1"; } ipaddr.addr = ipaddr_addr(domain); if( pesp_conn->type == ESPCONN_TCP ) { if(isserver) c_memcpy(pesp_conn->proto.tcp->local_ip, &ipaddr.addr, 4); else c_memcpy(pesp_conn->proto.tcp->remote_ip, &ipaddr.addr, 4); NODE_DBG("TCP ip is set: "); NODE_DBG(IPSTR, IP2STR(&ipaddr.addr)); NODE_DBG("\n"); } else if (pesp_conn->type == ESPCONN_UDP) { if(isserver) c_memcpy(pesp_conn->proto.udp->local_ip, &ipaddr.addr, 4); else c_memcpy(pesp_conn->proto.udp->remote_ip, &ipaddr.addr, 4); NODE_DBG("UDP ip is set: "); NODE_DBG(IPSTR, IP2STR(&ipaddr.addr)); NODE_DBG("\n"); } } // call back function when a connection is obtained, tcp only if ( pesp_conn->type == ESPCONN_TCP ) { if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){ lua_pushvalue(L, stack); // copy argument (func) to the top of stack if(isserver) // for tcp server connected callback { if(tcpserver_cb_connect_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); tcpserver_cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX); } else { if(nud->cb_connect_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref); nud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX); } } } if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server lua_pushvalue(L, 1); // copy to the top of stack if(nud->self_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref); nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); } if( pesp_conn->type == ESPCONN_TCP ) { if(isserver){ // no secure server support for now espconn_regist_connectcb(pesp_conn, net_server_connected); // tcp server, SSL is not supported #ifdef CLIENT_SSL_ENABLE // if(nud->secure) // espconn_secure_accept(pesp_conn); // else #endif espconn_accept(pesp_conn); // if it's a server, no need to dns. espconn_regist_time(pesp_conn, tcp_server_timeover, 0); } else{ espconn_regist_connectcb(pesp_conn, net_socket_connected); espconn_regist_reconcb(pesp_conn, net_socket_reconnected); #ifdef CLIENT_SSL_ENABLE if(nud->secure){ if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port) espconn_secure_disconnect(pesp_conn); // espconn_secure_connect(pesp_conn); } else #endif { if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port) espconn_disconnect(pesp_conn); // espconn_connect(pesp_conn); } } } else if (pesp_conn->type == ESPCONN_UDP) { espconn_regist_recvcb(pesp_conn, net_socket_received); espconn_regist_sentcb(pesp_conn, net_socket_sent); if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port) espconn_delete(pesp_conn); if(isserver) espconn_create(pesp_conn); // if it's a server, no need to dns. } if(!isserver){ if((ipaddr.addr == IPADDR_NONE) && (c_memcmp(domain,"255.255.255.255",16) != 0)) { host_ip.addr = 0; dns_reconn_count = 0; if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, socket_dns_found)){ socket_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip. } } else { socket_connect(pesp_conn); } } return 0; }
/** * @brief Setup commad of start client. * @param id: commad id number * @param pPara: AT input param * @retval None */ void ICACHE_FLASH_ATTR at_setupCmdCipstart(uint8_t id, char *pPara) { char temp[64]; // enum espconn_type linkType; int8_t len; enum espconn_type linkType = ESPCONN_INVALID; uint32_t ip = 0; char ipTemp[128]; int32_t port; uint8_t linkID; // if(mdState != m_unlink) // { // uart0_sendStr("no ip\r\n"); // return; // } if(at_wifiMode == 1) { if(wifi_station_get_connect_status() != STATION_GOT_IP) { uart0_sendStr("no ip\r\n"); return; } } pPara++; if(at_ipMux) { linkID = atoi(pPara); pPara++; pPara = strchr(pPara, '\"'); } else { linkID = 0; } if(linkID >= at_linkMax) { uart0_sendStr("ID ERROR\r\n"); return; } len = at_dataStrCpy(temp, pPara, 6); if(len == -1) { uart0_sendStr("Link typ ERROR\r\n"); return; } if(os_strcmp(temp, "TCP") == 0) { linkType = ESPCONN_TCP; } else if(os_strcmp(temp, "UDP") == 0) { linkType = ESPCONN_UDP; } else { uart0_sendStr("Link typ ERROR\r\n"); return; } pPara += (len+3); len = at_dataStrCpy(ipTemp, pPara, 64); if(len == -1) { uart0_sendStr("IP ERROR\r\n"); return; } pPara += (len+2); if(*pPara != ',') { uart0_sendStr("ENTRY ERROR\r\n"); return; } pPara += (1); port = atoi(pPara); if(pLink[linkID].linkEn) { uart0_sendStr("ALREAY CONNECT\r\n"); return; } pLink[linkID].pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (pLink[linkID].pCon == NULL) { uart0_sendStr("CONNECT FAIL\r\n"); return; } pLink[linkID].pCon->type = linkType; pLink[linkID].pCon->state = ESPCONN_NONE; pLink[linkID].linkId = linkID; ip = ipaddr_addr(ipTemp); switch(linkType) { case ESPCONN_TCP: pLink[linkID].pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pLink[linkID].pCon->proto.tcp->local_port = espconn_port(); pLink[linkID].pCon->proto.tcp->remote_port = port; os_memcpy(pLink[linkID].pCon->proto.tcp->remote_ip, &ip, 4); pLink[linkID].pCon->reverse = &pLink[linkID]; espconn_regist_connectcb(pLink[linkID].pCon, at_tcpclient_connect_cb); espconn_regist_reconcb(pLink[linkID].pCon, at_tcpclient_recon_cb); specialAtState = FALSE; if((ip == 0xffffffff) && (os_memcmp(ipTemp,"255.255.255.255",16) != 0)) { espconn_gethostbyname(pLink[linkID].pCon, ipTemp, &host_ip, at_dns_found); } else { espconn_connect(pLink[linkID].pCon); at_linkNum++; } break; case ESPCONN_UDP: pLink[linkID].pCon->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); pLink[linkID].pCon->proto.udp->local_port = espconn_port(); pLink[linkID].pCon->proto.udp->remote_port = port; os_memcpy(pLink[linkID].pCon->proto.udp->remote_ip, &ip, 4); pLink[linkID].pCon->reverse = &pLink[linkID]; // os_printf("%d\r\n",pLink[linkID].pCon->proto.udp->local_port);/// pLink[linkID].linkId = linkID; pLink[linkID].linkEn = TRUE; pLink[linkID].teType = teClient; espconn_regist_recvcb(pLink[linkID].pCon, at_tcpclient_recv); espconn_regist_sentcb(pLink[linkID].pCon, at_tcpclient_sent_cb); if((ip == 0xffffffff) && (os_memcmp(ipTemp,"255.255.255.255",16) != 0)) { specialAtState = FALSE; espconn_gethostbyname(pLink[linkID].pCon, ipTemp, &host_ip, at_dns_found); } else { espconn_create(pLink[linkID].pCon); at_linkNum++; at_backOk; } break; default: break; } // os_sprintf(temp, "%d.%d.%d.%d:%d\r\n",///// // IP2STR(&ip), port);///// // uart0_sendStr(temp);//// // at_backOk;///// }
/** * @brief Setup commad of module as server. * @param id: commad id number * @param pPara: AT input param * @retval None */ void ICACHE_FLASH_ATTR at_setupCmdCipserver(uint8_t id, char *pPara) { BOOL serverEnTemp; int32_t port; char temp[32]; if(at_ipMux == FALSE) { at_backError; return; } pPara++; serverEnTemp = atoi(pPara); pPara++; if(serverEnTemp == 0) { if(*pPara != '\r') { at_backError; return; } } else if(serverEnTemp == 1) { if(*pPara == ',') { pPara++; port = atoi(pPara); } else { port = 333; } } else { at_backError; return; } if(serverEnTemp == serverEn) { uart0_sendStr("no change\r\n"); return; } if(serverEnTemp) { pTcpServer = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (pTcpServer == NULL) { uart0_sendStr("TcpServer Failure\r\n"); return; } pTcpServer->type = ESPCONN_TCP; pTcpServer->state = ESPCONN_NONE; pTcpServer->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pTcpServer->proto.tcp->local_port = port; espconn_regist_connectcb(pTcpServer, at_tcpserver_listen); espconn_accept(pTcpServer); espconn_regist_time(pTcpServer, server_timeover, 0); pUdpServer = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (pUdpServer == NULL) { uart0_sendStr("UdpServer Failure\r\n"); return; } pUdpServer->type = ESPCONN_UDP; pUdpServer->state = ESPCONN_NONE; pUdpServer->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); pUdpServer->proto.udp->local_port = port; pUdpServer->reverse = NULL; espconn_regist_recvcb(pUdpServer, at_udpserver_recv); espconn_create(pUdpServer); // if(pLink[0].linkEn) // { // uart0_sendStr("Link is builded\r\n"); // return; // } // pLink[0].pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); // if (pLink[0].pCon == NULL) // { // uart0_sendStr("Link buile Failure\r\n"); // return; // } // pLink[0].pCon->type = ESPCONN_TCP; // pLink[0].pCon->state = ESPCONN_NONE; // pLink[0].linkId = 0; // pLink[0].linkEn = TRUE; // // pLink[0].pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); // pLink[0].pCon->proto.tcp->local_port = port; // // pLink[0].pCon->reverse = &pLink[0]; // // espconn_regist_connectcb(pLink[0].pCon, user_test_tcpserver_listen); // espconn_accept(pLink[0].pCon); // at_linkNum++; } else { /* restart */ uart0_sendStr("we must restart\r\n"); return; } serverEn = serverEnTemp; at_backOk; }