//#define Version "160524"
// remplacé baniere animée par texte fixe .. page web plus disponible
#define Version "160523"
// rajout test favico pour rafraichir la page en synchro
//#define Version "160521"
// PB si usage timer1 pour la duree de boucle
// s'embale à fond ??? passe au travers du test timer1 N
// => SUPER GROSSE BETISE : while(Flag_Timer1=0); au lieu de ==
// voir Session_echange_Wifi3_log_160521.txt
//#define With_Timer1
// Use of "Library_ESP8266_3.h" au leu de "Library_ESP8266_4.h"
// rev 21 mai 2016
// rajout count Nb d'echanges PIC-WIFI3
// rajout explications dans le code State Machine
// trouvé bg sur test no change qui ne marchait pas
// rev 16/05/2016
// Use "Library_ESP8266_3.h" au leu de "Library_ESP8266_3.h"
// toujours un pb sur Config ...ESP8266 AT+CWMODE=3 reponse ??
// meme en rajoutant test sur no change
// modif marquee
// modi pour placement automatique des parametres couleurs via recherche diese #
#define Dossier "C:\\_MikroC\_MesProjets_MikroC\_18F87J50_ESP8266_Server"
#define Projet "ESP8266_server_1605.mcppi"
#define Source "18F87J50_ESP8266_server" // etc
//C:\_MikroC\_MesProjets_MikroC\_WIFI3_ESP8266\_ESP8266_server
// 29 mars 2016
// ESP8266_server_160329.c
// adaptation à WIFI3-click et 18F87J50
// modif affectation pin Power et reset wifi
// blocage à Config ...ESP8266 .. state machine
#include "Library_ESP8266_4.h" // #include "Library_ESP8266_2.h"
//-- commandes terminal VT220
#define CLS 12 // effacement de page sur Terminal VBRAY
#define CR 13
#define VT 10
#define LF 10
#define TAB 9
#define Bell 7
#define Byte unsigned char
#define LCD_ADR 0x74 // LCD 2119 2x16 car OK avec alim 3,3V
#define UART_Wr_Ptr UART2_Write
#define UART_Rd_Ptr UART2_Read
//ac:PIC18F87J50_Pinoutjpg
//ac:wifi3_click_18F87J50_inter_link
//ac:Wifi3_Clikcer2_18F87J50_Test_hardware
//ac:Pic18F87J50_datasheet
// LEDs on PIC18F87J50 Clicker2
sbit LD1 at LATD4_bit;
sbit LD2 at LATE4_bit;
sbit LD1_Direction at TRISD4_bit;
sbit LD2_Direction at TRISE4_bit;
// BPs on PIC18F87J50 Clicker2
sbit BP1 at RD7_bit;
sbit BP2 at RH3_bit;
sbit BP1_Direction at TRISD7_bit;
sbit BP2_Direction at TRISH3_bit;
sbit Scope_Bit at LATH5_bit; // pin pour chrono à l'oscillo
sbit Scope_Bit_Direction at LATH5_bit;
// module connections Wifi3-click mikrobus #1
sbit CH_PD at LATA2_bit; // resert pin
sbit CH_PD_Direction at LATA2_bit;
sbit GPIO15 at LATE1_bit;
sbit GPIO15_Direction at LATE1_bit;
#define MAXLEN1 256
unsigned char buffer1[MAXLEN1];
unsigned int i,j,k,l,m;
int m0,m1;
unsigned int Index1=0;
unsigned int i1=0;
unsigned int UART1_DataReady=0;
unsigned char c1=0;
float f1,f2;
char TEXTE[128];
char *txt;
char *px;
char CRam1[64];
unsigned char addr;
unsigned char volt[6];
unsigned int Count;
int t;
unsigned char tmp,c2;
int UART2_DataReady;
unsigned int i2,Index2;
unsigned char cx;
unsigned int Pointeurs_Dieses[6];
char Run_Once=0;
unsigned int WebPageLen=0;
int Pointeur_Volt=0;
int Pointeur_Count=0;
char Compte[6];
volatile int Flag_Timer0;
volatile unsigned int Cpt0;
volatile int Flag_Timer1;
volatile unsigned int Cpt1;
unsigned char *SSID_AP = "ESP8266";
unsigned char *password_AP = "ESP12345";
unsigned char *IP_address="192.168.4.1";// choice IP acording IP for your router
const char chanel = 4;
const char enc = 4;
char ESP_ID =0;
unsigned int Do_ReFresch=0;
const char esp8266_rec_max=16;
// this array store GET /?+data then 6 for GET /? and 10 for max.data //
unsigned char esp8266_received_data[esp8266_rec_max+1];
char interrupt_detected=0;
//****************************************************//
// default html page
//
\
//\
| \
\
*/
//
const char indexPage1[] =
"\
\
\
\
test ESP8266 serveur\
\
\
Test Wifi3 avec clicker2 PIC18F87J50
\
\
\
";
void strConstRamCpy(char *dest, const char *source);
void LCD_CLS(void);
void LCD_Clear(void); // <-- PB >> au lieu de space.. because LCD avec ROM G
int LCD2119_Init(void);
void LCD_RAZ_Ligne(int L);
void LCD_Write_Chr_At(int Ligne,int Col,char c1) ;
void LCD_Write_Text_At(unsigned int Ligne,unsigned int col,char * t1);
void LCD_Write_CText_At(unsigned int Ligne,unsigned int col,const char * t1);
void LCD_Write_CText(const char * t1);
void Cree_Euro(void) ;
void Cree_8_cars_CGRAM(void) ;
void LCD_Cde(unsigned char Cde);
void LCD_Gauche(void);
void LCD_Droite(void);
unsigned char check_device(unsigned char dev_address); // necessite liaison RS232 UART1
void UART1_Write_CText(const char *txt1);
void CRLF1(void) ;
void RAZ_UART1(void);
void Search_Pointeurs(void);
void InitTimer0(void);
void Init_Timer1(unsigned int Max);
#include "LCD2119.inc"
// Lecture charactere de UART2 via usage de machine d'etat (state machine)
void interrupt() iv 0x0008 ics ICS_AUTO
{
if (RC2IF_bit == 1) // uart2 rx interrupt request
{
Scope_Bit=1 ; // RH5
esp8266_read_message(); // decortique le message
// à 19200 bds espace temporel max < = 1920 bytes/1000mS soit 0.52mS / byte
// soit 520 µS
// Fosc=48Mhz => 1 cycle 12MHz soit 0.083µS => 520/0.0.083= 3285 cycles
// taille occupee = 1114 bytes
Scope_Bit=0; // RH5
}
else
{
if (TMR0IF_bit==1)
{ // timer0 à 1 sec at 48MHz
TMR0H = 0x48;
TMR0L = 0xE5;
Cpt0++;
if (Cpt0>8)
{
Flag_Timer0=1;
TMR0ON_bit=0;
TMR0IE_bit=0;
}
TMR0IF_bit = 0;
}
}
#ifdef With_Timer1
if ((TMR1IF_bit==1) &&( TMR1IE_bit==1))
{
TMR1IF_bit = 0;
// precaler =1/8 Fosc=48Mhz 25mS
TMR1H = 0x6D;
TMR1L = 0x84;
Cpt1--;
// TXREG1='+';
if (Cpt1==0) // 400x25mS => 10 sec
{
Flag_Timer1=1 ;
TMR1ON_bit=0;
TMR0IE_bit=0;
}
TMR1IF_bit = 0;
}
#endif
}
//Timer0
//Prescaler 1:256; TMR0 Preload = 18661; Actual Interrupt Time : 1
void InitTimer0(void)
{
T0CON = 0x87;
TMR0H = 0x48;
TMR0L = 0xE5;
Flag_Timer0=0;
TMR0IE_bit=1;
Cpt0=0;
}
#ifdef With_Timer1
//Timer1
//Prescaler 1:8; TMR1 Preload = 28036; Actual Interrupt Time :25 ms at 48MHZ
void Init_Timer1(unsigned int Max)
{
T1CON = 0x00;
RD16_bit=1;
T1CKPS1_bit=1; // prescaler=1/8
T1CKPS0_bit=1;
TMR1CS_bit=0 ; //Internal clock (FOSC/4)
TMR1H = 0x6D; // 28036;
TMR1L = 0x84;
Cpt1=Max;
Flag_Timer1=0;
RCON.IPEN=1;
TMR1IP_bit=1;
TMR1IF_bit = 0;
TMR1IE_bit = 1;
TMR1ON_bit=1;
}
#endif
void Float2Ascii (float x, unsigned char *str,char precision)
{
/* converts a floating point number to an ascii string */
/* x is stored into str, which should be at least 30 chars long */
int ie, i, k, ndig;
double y;
ndig = ( precision<=0) ? 7 : (precision > 22 ? 23 : precision+1);
ie = 0;
/* if x negative, write minus and reverse */
if ( x < 0)
{
*str++ = '-';
x = -x;
}
/* put x in range 1 <= x < 10 */
if (x > 0.0) while (x < 1.0)
{
x *= 10.0; // a la place de =*
ie--;
}
while (x >= 10.0)
{
x = x/10.0;
ie++;
}
// in f format, number of digits is related to size
ndig += ie; // a la place de =+
//round. x is between 1 and 10 and ndig will be printed to
// right of decimal point so rounding is ...
for (y = i = 1; i < ndig; i++)
y = y/10.;
x += y/2.;
if (x >= 10.0) {x = 1.0; ie++;}
if (ie<0)
{
*str++ = '0'; *str++ = '.';
if (ndig < 0) ie = ie-ndig;
for (i = -1; i > ie; i--) *str++ = '0';
}
for (i=0; i < ndig; i++)
{
k = x;
*str++ = k + '0';
if (i == ie ) *str++ = '.';
x -= (y=k);
x *= 10.0;
}
*str = '\0';
}
void Search_Pointeurs(void)
{
k=sizeof(indexPage1); // en ROM
UART1_Write_CText("Long page1 en ROM= ");
WordToStr(k,txt);
UART1_Write_Text(txt);UART1_Write(TAB);
k=sizeof(indexPage2); // en RAM
UART1_Write_CText("Long page2 en RAM= ");
WordToStr(k,txt);
UART1_Write_Text(txt);
CRLF1();
CRLF1();
UART1_Write_CText("\r\nRecherche @ et #(dieses) char by char \r\nindexpage2 AVANT mise à jour:\r\n");
j=0;
k=strlen(indexPage2);
for (i=0;i5)
{
UART1_Write_CText("\r\nErreur pointeur dieses !\r\n");
while(1);
}
}
UART1_Write(cx);
}
// resultat des recherches
CRLF1();
for (j=0;j<4;j++)
{
UART1_Write_CText("\r\nPointeur dieses #");
UART1_Write(j+48);
UART1_Write(TAB);
WordToStr(Pointeurs_Dieses[j],txt);
UART1_Write_Text(txt);
}
UART1_Write_CText("\r\nPointeur Volt : ");
WordToStr(Pointeur_Volt,txt);
UART1_Write_Text(txt);
UART1_Write_CText("\r\nPointeur Count : ");
WordToStr(Pointeur_Count,txt);
UART1_Write_Text(txt);
CRLF1();
CRLF1();
}
void ESP8266_UserTCP()
{
int j;
// Reception de commandes
//GET /?dev2=d2off
if(memcmp(esp8266_received_data+11, "D4on", 4)==0) // do we have ON command
LD1=1; //LATD.F4 = 1; set PORTD bit4
else
if(memcmp(esp8266_received_data+11, "D4off", 5)==0) // do we have OFF command
LD1=0; //LATD.F4 = 0 clear PORTD bit4
if(!memcmp(esp8266_received_data+11, "E4on", 4)) // do we have ON command
LD2=1; //LATE.F4 = 1 set PORTE bit4
else
if(!memcmp(esp8266_received_data+11, "E4off", 5)) // do we have OFF command
LD2=0; //LATE.F4 = 0 clear PORTE bit4
else
if(!memcmp(esp8266_received_data+11, " / ", 4))
Do_ReFresch=1;
// envoi de la valeur ANALogique
// memcpy(indexPage2+495,volt, 4);
memcpy(indexPage2+Pointeur_Volt,volt, 4);
// envoi de la valeur entiere 16 bits Count
//The output string has fixed width of 6 characters including null character at the end
memcpy(indexPage2+Pointeur_Count,Compte, 5);
UART1_Write_CText("Reception data from pageWeb:\r\n");
UART1_Write_Text( esp8266_received_data);
CRLF1();
// ecritures d'etats
if (LD1)
{
memcpy(indexPage2+Pointeurs_Dieses[0], "#00FF00", 6); //// Vert ON
memcpy(indexPage2+Pointeurs_Dieses[1], "#FF0000", 6); // rouge OFF
}
else
{
memcpy(indexPage2+Pointeurs_Dieses[0], "#FF0000", 6); // Rouge ON
memcpy(indexPage2+Pointeurs_Dieses[1], "#00FF00", 6); // vert OFF
}
if (LD2) //PORTE.F4)
{
memcpy(indexPage2+Pointeurs_Dieses[2], "#00FF00", 6); // Vert ON
memcpy(indexPage2+Pointeurs_Dieses[3], "#FF0000", 6); // rouge OFF
}
else
{
memcpy(indexPage2+Pointeurs_Dieses[2], "#FF0000", 6); // rouge OFF
memcpy(indexPage2+Pointeurs_Dieses[3], "#00FF00", 6); // Vert ON
}
if(Run_Once==0)
{
CRLF1();
UART1_Write_CText("\r\nListe indexpage1:\r\n");
k=sizeof(indexPage1);
for (i=0;i< k;i++)
{
cx=indexPage1[i];
UART1_Write(cx);
}
CRLF1();
CRLF1();
CRLF1();
UART1_Write_CText("\r\nListe indexpage2 APRES mise à jour:\r\n");
for (i=0;i< strlen(indexPage2);i++) UART1_Write(indexPage2[i]);
CRLF1();
CRLF1();
k=sizeof(indexPage1);
UART1_Write_CText("Long page1 en ROM= ");
WordToStr(k,txt);
UART1_Write_Text(txt);UART1_Write(TAB);
m=sizeof(indexPage2); // en RAM
UART1_Write_CText("Long page2 en RAM= ");
WordToStr(m,txt);
UART1_Write_Text(txt);
CRLF1();
WebPageLen=k+m;
Run_Once=1;
}
Send_ESP8266_data(indexPage1,indexPage2,WebPageLen); // 1309 = length for indexPage1 + lenght for indexPage2
}
// --- Copie le texte depuis ROM vers RAM
void strConstRamCpy(char *dest, const char *source)
{
while(*source) *dest++ = *source++ ;
*dest = 0 ; // terminateur
}
void UART1_Write_CText(const char *txt1)
{
while (*txt1)
UART1_Write(*txt1++);
}
void CRLF1()
{
UART1_Write(CR);
UART1_Write(LF);
}
void RAZ_UART1()
{ RC1IE_bit=0 ;
for (Index1=0;Index1 10secc at 48MHz
#else
InitTimer0(); // 1x9 = 9 sec at 48MHz
#endif
LCD_CLS();
if(esp_DataReady_received)
{
// GET /?dev1=D4on
LCD_Write_CText_At(1,1,"ID=");
UART1_Write_CText("\r\n ID=");
UART1_Write(esp8266_id); CRLF1();
LCD_Write_Chr_At(1,4,esp8266_id+48);
LCD_Write_CText_At(2,1,"DATA:");
// si on recoit
// +IPD,2,435:GET /?led1=D4on HTTP/1.1
// :GET /?led1=D4on HTTP/1.1
// isolation de la partie utile via la machine d'etat (voir lib)
// explication du -6
//GET /?Led1=D4on H
//GET /?Led1=D4on 0
// 654321 (-6)
// D4on 0
for (j=0;j 3,22265625=3300/1024;
t = (unsigned int) f1; // only decimal part
volt[0]=((t/1000)%10)+48;
volt[1]='.' ;
volt[2]=((t/100)%10) +48;
volt[3]=((t/10)%10) +48;
//volt[3]=(t%10) +48;
volt[4]=0;
sprintf(txt,"EA1 = %s Volts",volt);
UART1_Write_Text(txt); CRLF1();
LCD_Write_Text_At(2,1,txt);
Count++;
WordToStr(Count,Compte) ;
UART1_Write_CText("Counts = ");
UART1_Write_Text(Compte); CRLF1();
#ifdef With_Timer1
while(Flag_Timer1==0); // 7sec
#else
while(Flag_Timer0==0); // 9 sec
#endif
}
}
// voir Session_echange_Wifi3_log_160521.txt