Transmit data over WiFi modem
ESP_8266.cpp@0:0b46638559b3, 2019-06-13 (annotated)
- Committer:
- docent
- Date:
- Thu Jun 13 13:49:26 2019 +0000
- Revision:
- 0:0b46638559b3
To my students 13.06.2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
docent | 0:0b46638559b3 | 1 | #include "mbed.h" |
docent | 0:0b46638559b3 | 2 | #include "ESP_8266.h" |
docent | 0:0b46638559b3 | 3 | //#define DEBUG |
docent | 0:0b46638559b3 | 4 | |
docent | 0:0b46638559b3 | 5 | char tr_data[256]; |
docent | 0:0b46638559b3 | 6 | Serial *pwifi; |
docent | 0:0b46638559b3 | 7 | DigitalOut *pCH_PD; |
docent | 0:0b46638559b3 | 8 | DigitalOut *pled; |
docent | 0:0b46638559b3 | 9 | |
docent | 0:0b46638559b3 | 10 | //------------ |
docent | 0:0b46638559b3 | 11 | Timeout ift; |
docent | 0:0b46638559b3 | 12 | RxWiFi_t rx_wifi; |
docent | 0:0b46638559b3 | 13 | |
docent | 0:0b46638559b3 | 14 | //----------- |
docent | 0:0b46638559b3 | 15 | void ift_isr() |
docent | 0:0b46638559b3 | 16 | { |
docent | 0:0b46638559b3 | 17 | rx_wifi.event=1; |
docent | 0:0b46638559b3 | 18 | } |
docent | 0:0b46638559b3 | 19 | |
docent | 0:0b46638559b3 | 20 | //--------------------- |
docent | 0:0b46638559b3 | 21 | void RxWiFi_interrupt() |
docent | 0:0b46638559b3 | 22 | { |
docent | 0:0b46638559b3 | 23 | while(pwifi->readable()) { |
docent | 0:0b46638559b3 | 24 | rx_wifi.buffer[rx_wifi.in] = pwifi->getc(); |
docent | 0:0b46638559b3 | 25 | ift.attach(ift_isr, 0.01); |
docent | 0:0b46638559b3 | 26 | if(rx_wifi.event==0 && rx_wifi.in<(RXWIFI_BUFFER_SIZE-1) ) |
docent | 0:0b46638559b3 | 27 | rx_wifi.in = (rx_wifi.in + 1); |
docent | 0:0b46638559b3 | 28 | } |
docent | 0:0b46638559b3 | 29 | } |
docent | 0:0b46638559b3 | 30 | //---------------------------------------------------- |
docent | 0:0b46638559b3 | 31 | void IniWiFi(Serial *pw,DigitalOut *pcs,DigitalOut *pl) |
docent | 0:0b46638559b3 | 32 | { |
docent | 0:0b46638559b3 | 33 | pCH_PD=pcs; |
docent | 0:0b46638559b3 | 34 | pled=pl; |
docent | 0:0b46638559b3 | 35 | pwifi=pw; |
docent | 0:0b46638559b3 | 36 | |
docent | 0:0b46638559b3 | 37 | *pCH_PD=0; |
docent | 0:0b46638559b3 | 38 | pwifi->baud(115200); |
docent | 0:0b46638559b3 | 39 | pwifi->attach(&RxWiFi_interrupt, Serial::RxIrq); |
docent | 0:0b46638559b3 | 40 | *pCH_PD=1; |
docent | 0:0b46638559b3 | 41 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 42 | rx_wifi.cipdinfo=0; |
docent | 0:0b46638559b3 | 43 | while(rx_wifi.event==0) { |
docent | 0:0b46638559b3 | 44 | wait_ms(25); |
docent | 0:0b46638559b3 | 45 | *pled=!*pled; |
docent | 0:0b46638559b3 | 46 | } |
docent | 0:0b46638559b3 | 47 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 48 | rx_wifi.in=0; |
docent | 0:0b46638559b3 | 49 | rx_wifi.app=0; |
docent | 0:0b46638559b3 | 50 | } |
docent | 0:0b46638559b3 | 51 | |
docent | 0:0b46638559b3 | 52 | //------------------------------------ |
docent | 0:0b46638559b3 | 53 | void WriteWiFi(uint8_t *sbuf, int len) |
docent | 0:0b46638559b3 | 54 | { |
docent | 0:0b46638559b3 | 55 | if(pwifi->writeable()) { |
docent | 0:0b46638559b3 | 56 | for (int i=0; i<len; i++) |
docent | 0:0b46638559b3 | 57 | pwifi->putc(sbuf[i]); |
docent | 0:0b46638559b3 | 58 | } |
docent | 0:0b46638559b3 | 59 | } |
docent | 0:0b46638559b3 | 60 | |
docent | 0:0b46638559b3 | 61 | //---------------------------------- |
docent | 0:0b46638559b3 | 62 | void SendToWiFi_(char *data,int len) |
docent | 0:0b46638559b3 | 63 | { |
docent | 0:0b46638559b3 | 64 | pwifi->printf("AT+CIPSEND=%d\r\n",len); |
docent | 0:0b46638559b3 | 65 | while(rx_wifi.event==0) wait_us(10); |
docent | 0:0b46638559b3 | 66 | rx_wifi.in =0; |
docent | 0:0b46638559b3 | 67 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 68 | WriteWiFi((uint8_t *)data,len); |
docent | 0:0b46638559b3 | 69 | } |
docent | 0:0b46638559b3 | 70 | |
docent | 0:0b46638559b3 | 71 | //----------------------- |
docent | 0:0b46638559b3 | 72 | void WiFiDataProcessing() |
docent | 0:0b46638559b3 | 73 | { |
docent | 0:0b46638559b3 | 74 | char *bptr,*aptr,*pptr; |
docent | 0:0b46638559b3 | 75 | char data[256]; |
docent | 0:0b46638559b3 | 76 | |
docent | 0:0b46638559b3 | 77 | if(rx_wifi.event){ |
docent | 0:0b46638559b3 | 78 | rx_wifi.app=0; |
docent | 0:0b46638559b3 | 79 | rx_wifi.buffer[rx_wifi.in]=0; |
docent | 0:0b46638559b3 | 80 | bptr=strstr(rx_wifi.buffer,"+IPD"); |
docent | 0:0b46638559b3 | 81 | //-----------recive UDP data-------------------------- |
docent | 0:0b46638559b3 | 82 | if(bptr!=NULL) { |
docent | 0:0b46638559b3 | 83 | bptr=strchr(rx_wifi.buffer,','); |
docent | 0:0b46638559b3 | 84 | bptr++; |
docent | 0:0b46638559b3 | 85 | rx_wifi.len=strtol(bptr,&aptr,10); |
docent | 0:0b46638559b3 | 86 | bptr=strchr(aptr,':'); |
docent | 0:0b46638559b3 | 87 | pptr=strchr(aptr,','); |
docent | 0:0b46638559b3 | 88 | if((bptr>pptr)&&(bptr!=NULL)&&(pptr!=NULL)) { |
docent | 0:0b46638559b3 | 89 | aptr++; |
docent | 0:0b46638559b3 | 90 | pptr=strchr(aptr,','); |
docent | 0:0b46638559b3 | 91 | *pptr=0; |
docent | 0:0b46638559b3 | 92 | pptr++; |
docent | 0:0b46638559b3 | 93 | *bptr=0; |
docent | 0:0b46638559b3 | 94 | } else |
docent | 0:0b46638559b3 | 95 | bptr=NULL; |
docent | 0:0b46638559b3 | 96 | if (bptr!=NULL) { |
docent | 0:0b46638559b3 | 97 | bptr++; |
docent | 0:0b46638559b3 | 98 | for (int i=0; i<rx_wifi.len; i++) |
docent | 0:0b46638559b3 | 99 | data[i]=bptr[i]; |
docent | 0:0b46638559b3 | 100 | rx_wifi.answer=wifi_executeCallback(data);//<------action------ |
docent | 0:0b46638559b3 | 101 | #ifdef DEBUG |
docent | 0:0b46638559b3 | 102 | pc_printfCallback("wifi_executeCallback"); |
docent | 0:0b46638559b3 | 103 | #endif |
docent | 0:0b46638559b3 | 104 | rx_wifi.app=1; |
docent | 0:0b46638559b3 | 105 | rx_wifi.ipd=0; |
docent | 0:0b46638559b3 | 106 | if(strcmp(rx_wifi.IPaddr,aptr)!=0) { |
docent | 0:0b46638559b3 | 107 | sprintf(rx_wifi.IPaddr,"%s",aptr); |
docent | 0:0b46638559b3 | 108 | sprintf(rx_wifi.UDPport,"%s",pptr); |
docent | 0:0b46638559b3 | 109 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 110 | rx_wifi.in =0; |
docent | 0:0b46638559b3 | 111 | pwifi->printf("AT+CIPCLOSE\r\n"); |
docent | 0:0b46638559b3 | 112 | while(rx_wifi.event==0) wait_us(10); |
docent | 0:0b46638559b3 | 113 | rx_wifi.buffer[rx_wifi.in]=0; |
docent | 0:0b46638559b3 | 114 | #ifdef DEBUG |
docent | 0:0b46638559b3 | 115 | pc_printfCallback(rx_wifi.buffer); |
docent | 0:0b46638559b3 | 116 | #endif |
docent | 0:0b46638559b3 | 117 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 118 | rx_wifi.in =0; |
docent | 0:0b46638559b3 | 119 | pwifi->printf("AT+CIPSTART=\"UDP\",\"%s\",%s,1112,2\r\n",rx_wifi.IPaddr,rx_wifi.UDPport); |
docent | 0:0b46638559b3 | 120 | rx_wifi.ipd=1; |
docent | 0:0b46638559b3 | 121 | |
docent | 0:0b46638559b3 | 122 | } else if(rx_wifi.answer==0) { |
docent | 0:0b46638559b3 | 123 | /*//---------------------information feedback ----------------- |
docent | 0:0b46638559b3 | 124 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 125 | rx_wifi.in =0; |
docent | 0:0b46638559b3 | 126 | SendToWiFi_(data,rx_wifi.len); |
docent | 0:0b46638559b3 | 127 | */ |
docent | 0:0b46638559b3 | 128 | } |
docent | 0:0b46638559b3 | 129 | } |
docent | 0:0b46638559b3 | 130 | } else { |
docent | 0:0b46638559b3 | 131 | //-----------------------------recive Modem answer------------------------------------------- |
docent | 0:0b46638559b3 | 132 | #ifdef DEBUG |
docent | 0:0b46638559b3 | 133 | if(rx_wifi.buffer[0]<128) |
docent | 0:0b46638559b3 | 134 | pc_printfCallback(rx_wifi.buffer); |
docent | 0:0b46638559b3 | 135 | #else |
docent | 0:0b46638559b3 | 136 | if(rx_wifi.buffer[0]<128){ |
docent | 0:0b46638559b3 | 137 | char *endptr=strstr(rx_wifi.buffer,"AT+CIFSR"); |
docent | 0:0b46638559b3 | 138 | if(endptr!=NULL) |
docent | 0:0b46638559b3 | 139 | pc_printfCallback(rx_wifi.buffer);//pc.printf("%s",rx_wifi_buffer); |
docent | 0:0b46638559b3 | 140 | } |
docent | 0:0b46638559b3 | 141 | #endif |
docent | 0:0b46638559b3 | 142 | if(strstr(rx_wifi.buffer,"WIFI GOT IP")!=NULL) { |
docent | 0:0b46638559b3 | 143 | pwifi->printf("AT+CIPSTART=\"UDP\",\"192.168.255.255\",1112,1112,2\r\n"); |
docent | 0:0b46638559b3 | 144 | } else if(strstr(rx_wifi.buffer,"CONNECT")!=NULL) { |
docent | 0:0b46638559b3 | 145 | if (rx_wifi.cipdinfo==0) { |
docent | 0:0b46638559b3 | 146 | rx_wifi.cipdinfo=1; |
docent | 0:0b46638559b3 | 147 | pwifi->printf("AT+CIPDINFO=1\r\n"); |
docent | 0:0b46638559b3 | 148 | } else if(rx_wifi.ipd) { |
docent | 0:0b46638559b3 | 149 | rx_wifi.ipd=0; |
docent | 0:0b46638559b3 | 150 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 151 | rx_wifi.in =0; |
docent | 0:0b46638559b3 | 152 | if(data[1]==' ') |
docent | 0:0b46638559b3 | 153 | SendToWiFi_(data,rx_wifi.len); |
docent | 0:0b46638559b3 | 154 | } else |
docent | 0:0b46638559b3 | 155 | pwifi->printf("AT+CIFSR\r\n"); |
docent | 0:0b46638559b3 | 156 | } |
docent | 0:0b46638559b3 | 157 | } |
docent | 0:0b46638559b3 | 158 | rx_wifi.in =0; |
docent | 0:0b46638559b3 | 159 | rx_wifi.event=0; |
docent | 0:0b46638559b3 | 160 | } |
docent | 0:0b46638559b3 | 161 | } |
docent | 0:0b46638559b3 | 162 | |
docent | 0:0b46638559b3 | 163 | //-------------------- |
docent | 0:0b46638559b3 | 164 | void SendResponseWiFi() |
docent | 0:0b46638559b3 | 165 | { |
docent | 0:0b46638559b3 | 166 | if(rx_wifi.app){ |
docent | 0:0b46638559b3 | 167 | if (rx_wifi.answer&&rx_wifi.ipd==0){ |
docent | 0:0b46638559b3 | 168 | ResponseWiFiCallback(tr_data,rx_wifi.answer); |
docent | 0:0b46638559b3 | 169 | if(rx_wifi.answer) |
docent | 0:0b46638559b3 | 170 | SendToWiFi_(tr_data,strlen(tr_data)); |
docent | 0:0b46638559b3 | 171 | } |
docent | 0:0b46638559b3 | 172 | rx_wifi.answer=0; |
docent | 0:0b46638559b3 | 173 | rx_wifi.app=0; |
docent | 0:0b46638559b3 | 174 | } |
docent | 0:0b46638559b3 | 175 | } |
docent | 0:0b46638559b3 | 176 |