|
|
View previous topic :: View next topic |
Author |
Message |
hsogukpinar
Joined: 20 Sep 2021 Posts: 8
|
esp8266 |
Posted: Tue May 09, 2023 5:01 am |
|
|
Dear Friends,
I am planning to use esp8266 module with PIC18F4550 by CCS C but i can't find library file of esp8266. If you have, please share with me.
Best |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Tue May 09, 2023 5:18 am |
|
|
First thing. Think again. You really need to be using the LF4550, not the
standard 4550 chip, and building your board to use 3.3v. Otherwise you
will need level translation hardware.
I suspect you will say something like 'I have a board for the 5v Arduino'.
Problem is that the Arduino has logic inputs that accept TTL (2.4v Vih)
signals. The PIC does not. The serial RX pin on the 4550, requires the
input to go up to 4v before it'll be seen as high. So you need to look at
adding level translation hardware if you carry on and use the 4550.
There is a CCS library here:
[url]
https://github.com/Picproje/ESP8266-Wifi-Modul-ornek-Uygulama/tree/master/CCS%20C
[/url]
However it does need rewriting to really work properly (not handling
INT_RDA, is a way that anyone would recommend). |
|
|
hsogukpinar
Joined: 20 Sep 2021 Posts: 8
|
esp8266 |
Posted: Tue May 09, 2023 2:00 pm |
|
|
Actually I haven't found a comprehensive esp8266 library yet. It was written for a special trial purpose only in the specified link and it does not work, if there is a ready library, could you please share it? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed May 10, 2023 1:01 am |
|
|
I have to say, just write it to do what you want.
This is the whole point about programming. |
|
|
hsogukpinar
Joined: 20 Sep 2021 Posts: 8
|
esp8266 library |
Posted: Wed May 10, 2023 7:20 am |
|
|
Code: | /**
* C library for the ESP8266 WiFi module with a PIC microcontroller
* Copyright (C) 2015 Camil Staps <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*****************************************************************************
*
* File: esp8266.c
* Author: Camil Staps <[email protected]>
* Website: http://github.com/camilstaps/ESP8266_PIC
* Version: 0.1
*
* See: esp8266.h
*
* C library for interfacing the ESP8266 Wi-Fi transceiver module with a PIC18
* microcontroller. Should be used with the XC8 compiler.
-> Modified and adapted by Circuitdigest.com
-> Author: Aswinth Raj <[email protected]>
-> Website: circuitdigest.com
*/
unsigned char TXSTA,RCSTA,SPBRG,INTCON,PIR1,PIE1,TXREG,PORTC,TRISC;
int count;
//****** define sfr address *****//
#byte TXSTA = 0x98
#byte RCSTA = 0x18
#byte SPBRG = 0x99
#byte PIE1 = 0x8c
#byte INTCON= 0x8b
#byte TXREG = 0x19
#byte RCREG = 0x1A
#byte PIR1 = 0x0c
#byte PORTC = 0x07
#byte TRISC = 0x87
#bit TXIF = PIR1.4
#bit TRMT =TXSTA.1
#word U1STA = 0x0222
//#bit OERR = U1STA.1
#bit OERR=RCSTA.1
#bit FERR=RCSTA.2
#bit CREN=RCSTA.4
#byte PIR1=0xC
#bit RCIF=PIR1.5
void Initialize_ESP8266(void)
{
PORTC = 0x00; //0b 0000 0000 //Clear Port C
TRISC = 0xFF; //0b 1111 1111 //Enable USART Transmitter
TXSTA = 0x24; //0b 0010 0100 //Transmit Enable //SYNC = 0, Asynchronous Mode
RCSTA = 0x90; //0b 1001 0000 //Receive Enable //Serial Port Enable
SPBRG = 0x40; //0b 0100 0000 //SPBRG = 64, Baudrate = 19.231KBAUD, High Baud Rate
INTCON = 0x00; //0b 0000 0000 //Disable Global and Peripheral Interrupt
PIR1 = 0x10; //0b 0001 0000 //USART transmit/Receive Interrupt Flag bit
PIE1 = 0x00; //0b 0000 0000; //Individual enable bits for the peripheral interrupts.
count = 0;
}
//________UART module Initialized__________//
//**Function to send one byte of date to UART**//
void _esp8266_putch(char bt)
{
while(!TXIF); // hold the program till TX buffer is free
TXREG = bt; //Load the transmitter buffer with the received value
}
//_____________End of function________________//
//**Function to get one byte of date from UART**//
char _esp8266_getch()
{
if(OERR) // check for Error
{
CREN = 0; //If error -> Reset
CREN = 1; //If error -> Reset
}
while(!RCIF); // hold the program till RX buffer is free
return RCREG; //receive the value and send it to main function
}
//_____________End of function________________//
//**Function to convert string to byte**//
void ESP8266_send_string(char* st_pt)
{
while(*st_pt) //if there is a char
_esp8266_putch(*st_pt++); //process it as a byte data
}
//___________End of function______________//
/**
* Check if the module is started
*
* This sends the `AT` command to the ESP and waits until it gets a response.
*
* @return true if the module is started, false if something went wrong
*/
bit esp8266_isStarted(void) {
_esp8266_print("AT\r\n");
return (_esp8266_waitResponse() == ESP8266_OK);
}
/**
* Restart the module
*
* This sends the `AT+RST` command to the ESP and waits until there is a
* response.
*
* @return true iff the module restarted properly
*/
bit esp8266_restart(void) {
_esp8266_print("AT+RST\r\n");
if (_esp8266_waitResponse() != ESP8266_OK) {
return false;
}
return (_esp8266_waitResponse() == ESP8266_READY);
}
/**
* Enable / disable command echoing.
*
* Enabling this is useful for debugging: one could sniff the TX line from the
* ESP8266 with his computer and thus receive both commands and responses.
*
* This sends the ATE command to the ESP module.
*
* @param echo whether to enable command echoing or not
*/
void esp8266_echoCmds(bool echo) {
_esp8266_print("ATE");
if (echo) {
_esp8266_putch('1');
} else {
_esp8266_putch('0');
}
_esp8266_print("\r\n");
_esp8266_waitFor("OK");
}
/**
* Set the WiFi mode.
*
* ESP8266_STATION : Station mode
* ESP8266_SOFTAP : Access point mode
*
* This sends the AT+CWMODE command to the ESP module.
*
* @param mode an ORed bitmask of ESP8266_STATION and ESP8266_SOFTAP
*/
void esp8266_mode(unsigned char mode) {
_esp8266_print("AT+CWMODE=");
_esp8266_putch(mode + '0');
_esp8266_print("\r\n");
_esp8266_waitResponse();
}
/**
* Connect to an access point.
*
* This sends the AT+CWJAP command to the ESP module.
*
* @param ssid The SSID to connect to
* @param pass The password of the network
* @return an ESP status code, normally either ESP8266_OK or ESP8266_FAIL
*/
unsigned char esp8266_connect(unsigned char* ssid, unsigned char* pass) {
_esp8266_print("AT+CWJAP=\"");
_esp8266_print(ssid);
_esp8266_print("\",\"");
_esp8266_print(pass);
_esp8266_print("\"\r\n");
return _esp8266_waitResponse();
}
/**
* Disconnect from the access point.
*
* This sends the AT+CWQAP command to the ESP module.
*/
void esp8266_disconnect(void) {
_esp8266_print("AT+CWQAP\r\n");
_esp8266_waitFor("OK");
}
/**
* Store the current local IPv4 address.
*
* This sends the AT+CIFSR command to the ESP module.
*
* The result will not be stored as a string but byte by byte. For example, for
* the IP 192.168.0.1, the value of store_in will be: {0xc0, 0xa8, 0x00, 0x01}.
*
* @param store_in a pointer to an array of the type unsigned char[4]; this
* array will be filled with the local IP.
*/
void esp8266_ip(unsigned char* store_in) {
_esp8266_print("AT+CIFSR\r\n");
unsigned char received;
do {
received = _esp8266_getch();
} while (received < '0' || received > '9');
for (unsigned char i = 0; i < 4; i++) {
store_in[i] = 0;
do {
store_in[i] = 10 * store_in[i] + received - '0';
received = _esp8266_getch();
} while (received >= '0' && received <= '9');
received = _esp8266_getch();
}
_esp8266_waitFor("OK");
}
/**
* Open a TCP or UDP connection.
*
* This sends the AT+CIPSTART command to the ESP module.
*
* @param protocol Either ESP8266_TCP or ESP8266_UDP
* @param ip The IP or hostname to connect to; as a string
* @param port The port to connect to
*
* @return true iff the connection is opened after this.
*/
bit esp8266_start(unsigned char protocol, char* ip, unsigned char port) {
_esp8266_print("AT+CIPSTART=\"");
if (protocol == ESP8266_TCP) {
_esp8266_print("TCP");
} else {
_esp8266_print("UDP");
}
_esp8266_print("\",\"");
_esp8266_print(ip);
_esp8266_print("\",");
unsigned char port_str[5] = "\0\0\0\0";
sprintf(port_str, "%u", port);
_esp8266_print(port_str);
_esp8266_print("\r\n");
if (_esp8266_waitResponse() != ESP8266_OK) {
return 0;
}
if (_esp8266_waitResponse() != ESP8266_LINKED) {
return 0;
}
return 1;
}
// Send data (AT+CIPSEND)
/**
* Send data over a connection.
*
* This sends the AT+CIPSEND command to the ESP module.
*
* @param data The data to send
*
* @return true iff the data was sent correctly.
*/
bit esp8266_send(unsigned char* data) {
unsigned char length_str[6] = "\0\0\0\0\0";
sprintf(length_str, "%u", strlen(data));
_esp8266_print("AT+CIPSEND=");
_esp8266_print(length_str);
_esp8266_print("\r\n");
while (_esp8266_getch() != '>');
_esp8266_print(data);
if (_esp8266_waitResponse() == ESP8266_OK) {
return 1;
}
return 0;
}
/**
* Read a string of data that is sent to the ESP8266.
*
* This waits for a +IPD line from the module. If more bytes than the maximum
* are received, the remaining bytes will be discarded.
*
* @param store_in a pointer to a character array to store the data in
* @param max_length maximum amount of bytes to read in
* @param discard_headers if set to true, we will skip until the first \r\n\r\n,
* for HTTP this means skipping the headers.
*/
void esp8266_receive(unsigned char* store_in, uint16_t max_length, bool discard_headers) {
_esp8266_waitFor("+IPD,");
uint16_t length = 0;
unsigned char received = _esp8266_getch();
do {
length = length * 10 + received - '0';
received = _esp8266_getch();
} while (received >= '0' && received <= '9');
if (discard_headers) {
length -= _esp8266_waitFor("\r\n\r\n");
}
if (length < max_length) {
max_length = length;
}
/*sprintf(store_in, "%u,%u:%c%c", length, max_length, _esp8266_getch(), _esp8266_getch());
return;*/
uint16_t i;
for (i = 0; i < max_length; i++) {
store_in[i] = _esp8266_getch();
}
store_in[i] = 0;
for (; i < length; i++) {
_esp8266_getch();
}
_esp8266_waitFor("OK");
}
/**
* Output a string to the ESP module.
*
* This is a function for internal use only.
*
* @param ptr A pointer to the string to send.
*/
void _esp8266_print(unsigned const char *ptr) {
while (*ptr != 0) {
_esp8266_putch(*ptr++);
}
}
/**
* Wait until we found a string on the input.
*
* Careful: this will read everything until that string (even if it's never
* found). You may lose important data.
*
* @param string
*
* @return the number of characters read
*/
inline uint16_t _esp8266_waitFor(unsigned char *string) {
unsigned char so_far = 0;
unsigned char received;
uint16_t counter = 0;
do {
received = _esp8266_getch();
counter++;
if (received == string[so_far]) {
so_far++;
} else {
so_far = 0;
}
} while (string[so_far] != 0);
return counter;
}
/**
* Wait until we received the ESP is done and sends its response.
*
* This is a function for internal use only.
*
* Currently the following responses are implemented:
* * OK
* * ready
* * FAIL
* * no change
* * Linked
* * Unlink
*
* Not implemented yet:
* * DNS fail (or something like that)
*
* @return a constant from esp8266.h describing the status response.
*/
inline unsigned char _esp8266_waitResponse(void) {
unsigned char so_far[6] = {0,0,0,0,0,0};
unsigned const char lengths[6] = {2,5,4,9,6,6};
unsigned const char* strings[6] = {"OK", "ready", "FAIL", "no change", "Linked", "Unlink"};
unsigned const char responses[6] = {ESP8266_OK, ESP8266_READY, ESP8266_FAIL, ESP8266_NOCHANGE, ESP8266_LINKED, ESP8266_UNLINK};
unsigned char received;
unsigned char response;
bool continue_loop = true;
while (continue_loop) {
received = _esp8266_getch();
for (unsigned char i = 0; i < 6; i++) {
if (strings[i][so_far[i]] == received) {
so_far[i]++;
if (so_far[i] == lengths[i]) {
response = responses[i];
continue_loop = false;
}
} else {
so_far[i] = 0;
}
}
}
return response;
} |
|
|
|
hsogukpinar
Joined: 20 Sep 2021 Posts: 8
|
esp8266 |
Posted: Wed May 10, 2023 7:22 am |
|
|
Dear friends,
I tried to modified MPLAB library to CCS C compiler but there is a mistake could you you review and suggest something? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu May 11, 2023 1:34 am |
|
|
The key big problem in what you post, is that it is for a specific processor.
Not your processor.
For example
#byte TXSTA = 0x98
This is the address for this register on a PIC16, not a PIC18.
The same applies to every other register selection in this code.
The neat thing is that CCS can generate the right value for you:
#byte TXSTA =getenv("SFR:TXSTA")
Which will automatically fill in the correct address on any processor.
This needs to be done for every register selection here.
You need to include stdint.h before loading this or the integer sizes
won't be supported as posted. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|