 
 /***************************
 Used for sonar - HC-SR04
 Pekka Ritamaki 
 26.3.2016 
 file: wifi.c 
 
   Used sonar - HC-SR04 (1.5mA)  
   time 8MHz/4 =2MHz 
   2MHz /8 = 0.25MHZ =250KHz 
   3 * DS1820 temperature sensor 
   Wifi adapter ESP202  92mA  
   funktiot  wifi_start(void)   starttaa wifi 
   void wifi( char str1);    Kirjoita wifille 
   void close_wifi( void)  sulje wifi 
   ========================  
 
Resetin jälkeen tulee tämä 
================================
  ets Jan  8 2013,rst cause:4, boot mode:(3,7)

wdt reset
load 0x40100000, len 816, room 16 
tail 0
chksum 0x8d
load 0x3ffe8000, len 788, room 8 
tail 12
chksum 0xcf
ho 0 tail 12 room 4
load 0x3ffe8314, len 288, room 12 
tail 4
chksum 0xcf
csum 0xcf

2nd boot version : 1.2
  SPI Speed      : 40MHz
  SPI Mode       : QIO
  SPI Flash Size : 4Mbit
jump to run user1

**********************************/ 
#define LAINAUS 0x22 
// nämä ovat minun salasanani 
#define MYID  "ElisaKoti77"
#define PASSWORD "MML42GATZ6CA2"
#define REMOTEPORT  "4111"
#define LOCALPORT   "4112" 


// käynnistä wifi (resetoi) , aseta multiple connection  ja aseta  tunnus ja salasana ja Aseta UDP, mille tahanasa portille 
// remote portti 4111 ja vastaanottoportti 4112 
void wifi_start (void)  { 
unsigned int8 i, ok; 
    restart_wdt();
 printf("\r\n" ); 

 //delay_ms(100);  
  ready=0;
  ok=1; 
  do { 
   printf("AT+RST\r\n" );  //<CR><LF>
       RELEPOIS; 
   for (i=0; i<255; i++) {
        if (ready) 
           ok=0; 
         else   
          delay_ms(20);     
      } 
  }      
  while(ok); 
 delay_ms(3000);   
 
/*OK
ets Jan 8 2013,rst cause:4, boot mode:(3,0) etc 
*/

ready=0;
ok =1; 

do{  
printf("AT+CIPMUX=1\r\n"); // <CR><LF> //1= Multilpe connection. 0= single connection
  
 for (i=0; i<25; i++) 
   {   if(ready) 
           ok=0;
        else    
          delay_ms(20); 
   }     
}       
 while(ok); 
 
   delay_ms(500);
/*OK 
*/
  ready=0;
  
 ok =1; 
do{ 
 
printf("AT+CIPSERVER=1\r\n"); // 0= close server mode ,1=server mode open 
 for (i=0; i<25; i++)
      {
       if(ready)
          ok=0;
        else 
          delay_ms(20);  
       }   
}        
 while(ok); 
//printf("AT+CWLAP\r\n");  // tulosta käytettävissä oleva WIFI verkot 
//delay_ms(100);
  ready=0;
 
ok =1; 
do{ 
 
 
 printf("AT+CWJAP=%c%s%c,%c%s%c\r\n",LAINAUS,MYID,LAINAUS,LAINAUS,PASSWORD,LAINAUS); // <CR><LF> // Connect to Internet Use your own passwords.
 delay_ms(1000); 
 for (i=0; i<255; i++) { 
       if(ready) ok=0; 
        else  
        delay_ms(20);  
    }
}        
 while(ok); 
/* OK
AT+CIPSTART=id,"UDP","ipAddr",remotePort
AT+CIPSTART=id,"UDP","ipAddr",remotePort, localPort,mode
*/
//delay_ms(3000); 
  ready=0;
 ok=1; 

do{
//  printf("AT+CIPSTART=1,\"UDP\",\"0\",%s,%s,2\r\n",2,LOCALPORT) ;  // ,1 = single connection 
 printf("AT+CIPSTART=1,\"UDP\",\"255.255.255.255\",%s,%s,2\r\n",REMOTEPORT,LOCALPORT) ; 
 // ,1 = single connection tästä tulee dataa, 
//   2=destination peer entity of UDP is allowed to change.
//   0=UDP ei vaihda  0 - destination peer entity of UDP will not change.
 //#define REMOTEPORT  "4111"
 //#define LOCALPORT   "4112" 
  delay_ms(500); 
 for (i=0; i<255; i++) { 
      if(ready) 
         ok=0;
       else 
        delay_ms(20);  
 } 
}        
 while(ok);  
 delay_ms(200); 
ready=0;
ok =1; 

do{ 
printf("AT+CWMODE=1\r\n"); // <CR><LF> 1=Station mode, 2=AP Mode, 3=Station +AP mode 
  
for (i=0; i<25; i++)
       {
       if(ready) 
         ok =0;
         else 
        delay_ms(20);  
      }
} 
while(ok);
 
//printf("AT+CIPSTART=1,%cUDP%c,%c255,255,255,255%c,4111,4112\r\n",LAINAUS,LAINAUS,LAINAUS,LAINAUS) ; 

//delay_ms(1000); 
 
//printf("AT+CWLIF\r\n"); 
//delay_ms(2000);
// Connect number 1 channel to other computer at any IP (255.255.255.255) with UDP, Remote port 4111, Local port 4112
// Start the UDP-program on other computer.
 //delay_ms(1000); 
//OK
 
//printf("AT+CIPSEND,1,7\r\n"); // <CR><LF> ( Send to number 1 channel 10 character, maximun was about 400? )
 
 
//+IPD,1,9;remotesend // Msg remotesend came from remote station.

//printf("AT+CIPCLOSE=1\r\n"); //<CR><LF> // Close connection

//Everything went ok.

}
//============================================
// sammuta Wifi yhteys 
void wifi_close ( void) 
{
printf("AT+CIPCLOSE=1\r\n"); //<CR><LF> // Close connection
} 

//============================================
// lähetä wifi  sanoma  str1 merkkiä 
void  wifi ( char *str1 ) 
 {  
 int8 len; 
SammutaLED;  // sammuta LED 
 len = strlen(str1); 
printf("AT+CIPSEND=1,%d\r\n", len); //  Send to number 1 channel len characters, maximun is   2048  )
// kun annataan  +++ niin ohjelma lakkaa ottamasta dataa
// kun tulee +IPD,1,10:1234567890 niin vastaanotettu data on 1234567890, 10 merkkiä  
 delay_ms(100); 
printf(str1); 

}
 
