This article shows how to develop a interface software that read 2 values from a Microcontroller, through serial port, and show the 2 values in a Website. The process is next:
1. The Microcontroller sends 2 values through serial port, every 2 seconds (in this example).
First value must have the following format #0000
Second value must have the following format $0000
2. The interface software PC_serial_web.c reads the serial port COM1.
3. The interface software PC_serial_web.cgenerate the website pic_serial_web.htm
4. The website pic_serie_web.htm shows the 2 values and auto refresh every 2 seconds.
Micro PIC –>( Serial Port)–> PC (PC_serial_web.c)–> Website (pic_serial_web.htm)
The fact of to see values of a Microcontroller in a website it’s quite useful for many applications (counters, sensors, etc).
Find an example code with the header file in the code tab!!
Interface software PC_serial_web.c
Fig. 1: Screenshot of PC Serial Web on Windows
The code is developed in C, and it´s a Windows version.
In this case we have connected to a PIC 16F628A but the interface software “PC_serial_web.c”is totally compatible with all kind of Microcontrollers.
Fig. 2: Screenshot PIC Serial Website
Project Source Code
###
**********PIC_serial_web**********
//// PIC_serial_web.c (For PC windows)// PIC_serial_web.c reads by the serial port COM1 2 values// and shows in the web page generated pic_serial_web.html//// Microcontroller PIC send by serial port 2 values// First Value must have the following format #0000// Second Value must have the following format $0000// Comunications parameters are// 9600,8,1bit stop, without parity//// Micro PIC -->( Serial Port)--> PC (PC_serial_web.c)--> Web (pic_serial_web.html)#include <windows.h>#include <stdio.h>#include <stdlib.h>char texto_recibido[MAX_PATH];char *valor;char valor1[100];char valor2[100];//generation website functionint web(){FILE *fp;fp = fopen("pic_serial_web.htm", "w"); /* Open file for write*/fprintf(fp, "<html>n");fprintf(fp, "<br>n");fprintf(fp, "<title>PIC Serial Website</title> <br>n");fprintf(fp, "<body>n");fprintf(fp, "Micro PIC -->( serial port)--> PC (PC_serial_web.c)--> Web (pic_serial_web.html) <br>n");fprintf(fp, "<br>n");fprintf(fp, "It must be executed the file PIC_serial_web.exe <br>n");fprintf(fp, "<br>n");fprintf(fp, "PIC send 2 values through serial port <br>n");fprintf(fp, "<br>n");fprintf(fp, "Comunications parameters are 9600,8,1bit stop, without parity <br>n");fprintf(fp, "<br>n");fprintf(fp, "First Value must have the following format #0000 <br>n");fprintf(fp, "<br>n");fprintf(fp, "Second Value must have the following format $0000 <br>n");fprintf(fp, "<br>n");fprintf(fp, "Value 1 receipt by serial port COM1 = ");fprintf(fp, "<b>%s</b>n",valor1);fprintf(fp, "<br>n");fprintf(fp, "Value 2 receipt by serial port COM1 = ");fprintf(fp, "<b>%s</b>n",valor2);fprintf(fp, "<br>n");fprintf(fp, "<br>n");fprintf(fp, "<br>n");fprintf(fp, "<br>n");fprintf(fp, "Developed by LUIS MORQUECHO<br>n");fprintf(fp, "<A HREF=http://galeon.com/luismorquecho>http://galeon.com/luismorquecho</A>n");fprintf(fp, "</body>n");// Every 2 second it's refreshedfprintf(fp, "<META HTTP-EQUIV=REFRESH CONTENT=2> n");fprintf(fp, "</html>n");fclose(fp); /* close the file */}//int main(int argc, char *argv[])int main(){int cnt = 0;// Declare variables and structures for serial portHANDLE hSerial;DCB dcbSerialParams = {0};COMMTIMEOUTS timeouts = {0};DWORD dwBytesWritten = 0;char dev_name[MAX_PATH];char text_to_send[MAX_PATH];printf("*************** Driver serial Web for Microcontroller *****************nn");printf("PIC_serial_web.c reads by the serial port COM1 2 values n");printf("and shows in the web page generated pic_serial_web.html n");printf("Microcontroller PIC send by serial port 2 values n");printf("First Value must have the following format #0000 n");printf("Second Value must have the following format $0000 n");printf("Every 2 seconds are showed the values in the website nn");printf("Micro PIC -->( Serial Port)--> PC (PC_serial_web.c)--> Web (pic_serial_web.html)nn");printf("http://galeon.com/luismorquecho nn");printf("Exit CTRL C n");//Loop every 2 seconds reads serial port COM1 and generates websitewhile(1){//*********** Here chanfe serial port *************strcpy(dev_name, "COM1");//************ Introduce text to send (nothing in this case) ********//strcpy(text_to_send,"#1234 $9876");// Open the specified serial port (first command line argument)//fprintf(stderr, "Opening serial port %s...", dev_name);hSerial = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, 0,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);if (hSerial==INVALID_HANDLE_VALUE){fprintf(stderr, "Error serial portn");return 1;}//fprintf(stderr, "OKn");// Set device parameters (9600 baud, 1 start bit,// 1 stop bit, no parity)dcbSerialParams.DCBlength = sizeof(dcbSerialParams);if (GetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error getting device staten");CloseHandle(hSerial);return 1;}dcbSerialParams.BaudRate = CBR_9600;dcbSerialParams.ByteSize = 8;dcbSerialParams.StopBits = ONESTOPBIT;dcbSerialParams.Parity = NOPARITY;if(SetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error setting device parametersn");CloseHandle(hSerial);return 1;}// Set COM port timeout settingstimeouts.ReadIntervalTimeout = 50;timeouts.ReadTotalTimeoutConstant = 50;timeouts.ReadTotalTimeoutMultiplier = 10;timeouts.WriteTotalTimeoutConstant = 50;timeouts.WriteTotalTimeoutMultiplier = 10;if(SetCommTimeouts(hSerial, &timeouts) == 0){fprintf(stderr, "Error setting timeoutsn");CloseHandle(hSerial);return 1;}//Here we write in the serial port (Not used in this case)//fprintf(stderr, "Sending text: %sn", text_to_send);//if(!WriteFile(hSerial, text_to_send, strlen(text_to_send),//&dwBytesWritten, NULL))//{//fprintf(stderr, "Error writing text to %sn", dev_name);//}//else//{//fprintf(stderr, "%d bytes written to %sn",//dwBytesWritten, dev_name);//}//Maximum number of characters to receiveint num_caracteres;num_caracteres = 100;//Here we reads Serial PortReadFile(hSerial, texto_recibido, num_caracteres, &dwBytesWritten, NULL);//fprintf(stderr, "readed text %sn", texto_recibido);// Close serial port//fprintf(stderr, "Closing serial port...");if (CloseHandle(hSerial) == 0){fprintf(stderr, "Errorn", dev_name);return 1;}//fprintf(stderr, "OKn");// Read value 1 that have format #valor and filter itvalor = strstr(texto_recibido,"#");if (valor != NULL){// Remove the # caracter and separate from the value 2if (valor[1] != '$'){valor1[0] = valor[1];}if (valor[2] != '$'){valor1[1] = valor[2];}if (valor[3] != '$'){valor1[2] = valor[3];}if (valor[4] != '$'){valor1[3] = valor[4];}}else{valor1 [0]= '?';}// Read value 2 that have format $valor and filter itvalor= strstr(texto_recibido,"$");if (valor != NULL){// Remove the $ caracter and separate from the value 1if (valor[1] != '#'){valor2[0] = valor[1];}if (valor[2] != '#'){valor2[1] = valor[2];}if (valor[3] != '#'){valor2[2] = valor[3];}if (valor[4] != '#'){valor2[3] = valor[4];}}else{valor2 [0]= '?';}// Generate websiteweb();// Run once website to showif (cnt == 0){system("PIC_serial_web.htm");cnt =1;}// wait 2 secondsSleep(2000);}// exit normallyreturn 0;}*********Header File**********// HEADER FILE#ifndef ICD2_DEBUG#pragma chip PIC16F628A, core 14, code 2048, ram 32 : 0x14F#else#pragma chip PIC16F628A, core 14, code 0x700, ram 32 : 0x14F// last 256 locations are reserved for debugging//RESERVED RAM LOCATIONSchar reservedICD2 @ 0x70; // reserved RAM for ICD2#pragma stackLevels 7 // reserve one level for debugging#endif#pragma ramdef 0x70 : 0x7F mapped_into_all_banks#define INT_lgen_style#define INT_rambank 0#pragma config_def 0x1118/* Predefined:char W;char INDF, TMR0, PCL, STATUS, FSR, PORTA, PORTB;char OPTION, TRISA, TRISB;char PCLATH, INTCON;bit PS0, PS1, PS2, PSA, T0SE, T0CS, INTEDG, RBPU_;bit Carry, DC, Zero_, PD, TO, RP0, RP1, IRP;bit RBIF, INTF, T0IF, RBIE, INTE, T0IE, GIE;bit PA0, PA1; // PCLATH*/#pragma char PIR1 @ 12#pragma char TMR1L @ 14#pragma char TMR1H @ 15#pragma char T1CON @ 16#pragma char TMR2 @ 17#pragma char T2CON @ 18#pragma char CCPR1L @ 21#pragma char CCPR1H @ 22#pragma char CCP1CON @ 23#pragma char RCSTA @ 24#pragma char TXREG @ 25#pragma char RCREG @ 26#pragma char CMCON @ 31#pragma char PIE1 @ 0x8C#pragma char PCON @ 0x8E#pragma char PR2 @ 0x92#pragma char TXSTA @ 0x98#pragma char SPBRG @ 0x99#pragma char EEDATA @ 0x9A#pragma char EEADR @ 0x9B#pragma char EECON1 @ 0x9C#pragma char EECON2 @ 0x9D#pragma char VRCON @ 0x9F#pragma bit RA0 @ 5.0#pragma bit RA1 @ 5.1#pragma bit RA2 @ 5.2#pragma bit RA3 @ 5.3#pragma bit RA4 @ 5.4#pragma bit RA5 @ 5.5#pragma bit RA6 @ 5.6#pragma bit RA7 @ 5.7#pragma bit RB0 @ 6.0#pragma bit RB1 @ 6.1#pragma bit RB2 @ 6.2#pragma bit RB3 @ 6.3#pragma bit RB4 @ 6.4#pragma bit RB5 @ 6.5#pragma bit RB6 @ 6.6#pragma bit RB7 @ 6.7#pragma bit PEIE @ 11.6 mapped_into_all_banks#pragma bit TMR1IF @ 12.0#pragma bit TMR2IF @ 12.1#pragma bit CCP1IF @ 12.2#pragma bit TXIF @ 12.4#pragma bit RCIF @ 12.5#pragma bit CMIF @ 12.6#pragma bit EEIF @ 12.7#pragma bit TMR1ON @ 16.0#pragma bit TMR1CS @ 16.1#pragma bit T1SYNC_ @ 16.2#pragma bit T1OSCEN @ 16.3#pragma bit T1CKPS0 @ 16.4#pragma bit T1CKPS1 @ 16.5#pragma bit T2CKPS0 @ 18.0#pragma bit T2CKPS1 @ 18.1#pragma bit TMR2ON @ 18.2#pragma bit TOUTPS0 @ 18.3#pragma bit TOUTPS1 @ 18.4#pragma bit TOUTPS2 @ 18.5#pragma bit TOUTPS3 @ 18.6#pragma bit CCP1M0 @ 23.0#pragma bit CCP1M1 @ 23.1#pragma bit CCP1M2 @ 23.2#pragma bit CCP1M3 @ 23.3#pragma bit CCP1Y @ 23.4#pragma bit CCP1X @ 23.5#pragma bit RX9D @ 24.0#pragma bit OERR @ 24.1#pragma bit FERR @ 24.2#pragma bit ADDEN @ 24.3#pragma bit ADEN @ 24.3#pragma bit CREN @ 24.4#pragma bit SREN @ 24.5#pragma bit RX9 @ 24.6#pragma bit SPEN @ 24.7#pragma bit CM0 @ 31.0#pragma bit CM1 @ 31.1#pragma bit CM2 @ 31.2#pragma bit CIS @ 31.3#pragma bit C1INV @ 31.4#pragma bit C2INV @ 31.5#pragma bit C1OUT @ 31.6#pragma bit C2OUT @ 31.7#pragma bit TRISA0 @ 0x85.0#pragma bit TRISA1 @ 0x85.1#pragma bit TRISA2 @ 0x85.2#pragma bit TRISA3 @ 0x85.3#pragma bit TRISA4 @ 0x85.4#pragma bit TRISA5 @ 0x85.5#pragma bit TRISA6 @ 0x85.6#pragma bit TRISA7 @ 0x85.7#pragma bit TRISB0 @ 0x86.0#pragma bit TRISB1 @ 0x86.1#pragma bit TRISB2 @ 0x86.2#pragma bit TRISB3 @ 0x86.3#pragma bit TRISB4 @ 0x86.4#pragma bit TRISB5 @ 0x86.5#pragma bit TRISB6 @ 0x86.6#pragma bit TRISB7 @ 0x86.7#pragma bit TMR1IE @ 0x8C.0#pragma bit TMR2IE @ 0x8C.1#pragma bit CCP1IE @ 0x8C.2#pragma bit TXIE @ 0x8C.4#pragma bit RCIE @ 0x8C.5#pragma bit CMIE @ 0x8C.6#pragma bit EEIE @ 0x8C.7#pragma bit BOR_ @ 0x8E.0#pragma bit POR_ @ 0x8E.1#pragma bit OSCF @ 0x8E.3#pragma bit TX9D @ 0x98.0#pragma bit TRMT @ 0x98.1#pragma bit BRGH @ 0x98.2#pragma bit SYNC @ 0x98.4#pragma bit TXEN @ 0x98.5#pragma bit TX9 @ 0x98.6#pragma bit CSRC @ 0x98.7#pragma bit RD @ 0x9C.0#pragma bit WR @ 0x9C.1#pragma bit WREN @ 0x9C.2#pragma bit WRERR @ 0x9C.3#pragma bit VR0 @ 0x9F.0#pragma bit VR1 @ 0x9F.1#pragma bit VR2 @ 0x9F.2#pragma bit VR3 @ 0x9F.3#pragma bit VRR @ 0x9F.5#pragma bit VROE @ 0x9F.6#pragma bit VREN @ 0x9F.7###
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.