Tuesday, 17 December 2024

Tim's ESP8266-01 linker

 

This is my ESP8266-01 linker.

It is an application I wrote to communicate with the ESP8266-01 via Wi-Fi from the PC.
It makes the Tim's ESP8266-01 linker act like a go-between UART and Wi-Fi.





I hope the video explains it all.
It can be downloaded from here: Tim's ESP8266-01 linker.zip

Once the ESP8266-01 is programmed, you should be able to connect it to any Microcontroller that has UART and communicate to the Microcontroller via Wi-Fi.

The Arduino code for the ESP8266-01.

/*
 File: WiFi_UART_with_RTS_CTS.ino (Place in folder with the same name)

  Using an ESP8266-01 to act as Wi-Fi / UART with Hardware handshaking.
   The hardware handshaking to be done using pins 0 and 2.
   Ther is #define HARD_HANDSHAKING
   This turns on/off hardware handshaking.

 By Tim Jackson.1960
  Give creadit where due.

 In the code you will need to enter you’re:
  "Your local network name"
  "Your local network password"
*/

#include <esp8266wifi.h>

/*
 Hard Handshaking Pins.
*/
#define RTS_PIN 0 /* GPIO0 for Request To Send (RTS) */
#define CTS_PIN 2 /* GPIO2 for Clear To Send (CTS) */

//#define HARD_HANDSHAKING /* Uncomment to enable hardware handshaking */

/*
 Wi-Fi
*/
//#define FIXED_IP /* Uncomment this if you know how to set a fixed IP address. */
/*
 The name of your local network.
 The Password.

 These are required using Station mode (STA).
*/
const char* LOCAL_SSID_STA = "Your local network name"; /* Your local network name. */
const char* LOCAL_PASSWORD_STA = "Your local network password"; /* Your local network password. */
/*
	ESP8266-01 Identification
*/
const char* HOST_NAME = "Tims ESP8266_01_154"; /* The name for the module seen on the network. */
/*
 The following IPAdresses are for use with fixed IP address mode.
 You will need to know how to reserve IP addresses on your router to use these.

 With regards IP addresses gateway and dns.
 Point these to the address of your router.
 This project is for local networks and don't need to be registered on a global server.

 Normally your router/modem will be 192.168.0.1
*/
IPAddress ip(192, 168, 0, 154);     /* Device IP address. */
IPAddress subnet(255, 255, 255, 0); /* Subnet mask (Don't change) */
IPAddress gateway(192, 168, 0, 1);  /* Gateway (router IP) */
IPAddress dns(192, 168, 0, 1);      /* DNS server (router IP) */

WiFiServer server(80); /* Set up a server on port 80 */
WiFiClient client;

String WiFi_Buffer = "";          /* Buffer to hold received Wi-Fi data */
bool WiFi_DataReceived = false;   /* Flag to indicate Wi-Fi data received */
String Serial_Buffer = "";        /* Buffer to hold received Serial data */
bool Serial_DataReceived = false; /* Flag to indicate Serial data received */

void setup() {
 /*
  Initialize UART
 
   The default Baud when using Arduino devices is 9600.
   Unless you know what you are connecting to.
   Check your connected device's UART speed.
 
 */
 Serial.begin(9600);

#if defined(HARD_HANDSHAKING)
 pinMode(RTS_PIN, OUTPUT);	/* Set RTS as output */
 pinMode(CTS_PIN, INPUT);	/* Set CTS as input */
#endif /* HARD_HANDSHAKING */
 
 /*
  Connect to Wi-Fi
  */
 WiFi.hostname(HOST_NAME);
#if defined(FIXED_IP)
 WiFi.config(ip, gateway, subnet, dns);
#endif /* FIXED_IP */
 WiFi.begin(LOCAL_SSID_STA, LOCAL_PASSWORD_STA);
 while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
 }
 
 /*
  Print the IP address to the Serial Monitor.
  Address will be allocated by local server.
 
 Serial.println("WiFi Connected! IP address: ");
 Serial.println(WiFi.localIP());
 Serial.print("ESP8266 IP Address: ");
 Serial.println(WiFi.localIP());
 
 /* Print the MAC address */
 Serial.print("MAC Address: ");
 Serial.println(WiFi.macAddress());

 Serial.print("Hostname: ");
 Serial.println(WiFi.hostname());

 server.begin();	/* Start the server */
}
 
void loop() {
 client = server.available(); /* Check for incoming clients */
 
 if (client) {
  while (client.connected()) {
   if (client.available()) {
    while (client.available()) {
     WiFi_Buffer += (char)client.read(); /* Read all available data from the client */
    }
    client.flush();           /* Clear the client buffer */
    //client.println("ok");   /* Debug */
    WiFi_DataReceived = true; /* Set the flag */
    delay(1);                 /* Slight delay */
   }

#if defined(HARD_HANDSHAKING)
   /*
    Set Ready To Send (RTS) if WiFi Data Received.
   */
   if (WiFi_DataReceived) {
    digitalWrite(RTS_PIN, LOW); /* Set RTS low to indicate Ready To Send */
   }
   else {
    digitalWrite(RTS_PIN, HIGH); /* Set RTS high otherwise */
   }
 
   /*
    Wait for attached device to be ready to receive.
     Clear To Send (CTS)
   */
   while (digitalRead(CTS_PIN)) {
   /* Waiting for CTS_PIN to go LOW. */
   }
 
   /*
    Send WiFi_Buffer, when CTS_PIN is LOW
     Clear To Send (CTS)
   */
   if (!digitalRead(CTS_PIN)) {
    if (WiFi_DataReceived) {
     Serial.print(WiFi_Buffer); /* Print received Wi-Fi data */
     WiFi_Buffer = "";          /* Clear the buffer after processing */
     WiFi_DataReceived = false; /* Reset the flag after processing the data */
    }
   }
 
   if (Serial.available()) {
    while (Serial.available()) {
     Serial_Buffer += (char)Serial.read(); /* Read all available data from the Serial */
    }
    Serial_DataReceived = true;	/* Set Serial data received flag */
    digitalWrite(RTS_PIN, LOW);	/* Set RTS low to indicate Ready To Send */
   }
 
   /*
    Send Serial data over Wi-Fi if received.
   */
   if (Serial_DataReceived) {
    client.print(Serial_Buffer); /* Send Serial data to Wi-Fi client */
    Serial_Buffer = "";          /* Clear the Serial buffer after processing */
    Serial_DataReceived = false; /* Reset the Serial data received flag */
    digitalWrite(RTS_PIN, HIGH); /* Set RTS high after sending */
   }
#else
    if (WiFi_DataReceived) {
     Serial.print(WiFi_Buffer);	/* Print received Wi-Fi data */
     WiFi_Buffer = "";		/* Clear the buffer after processing */
     WiFi_DataReceived = false;	/* Reset the flag after processing the data */
    }
 
    if (Serial.available()) {
     while (Serial.available()) {
      Serial_Buffer += (char)Serial.read(); /* Read all available data from the Serial */
     }
     Serial_DataReceived = true; /* Set Serial data received flag */
    }
 
    /*
     Send Serial data over Wi-Fi if received.
    */
     if (Serial_DataReceived) {
      client.print(Serial_Buffer); /* Send Serial data to Wi-Fi client */
      Serial_Buffer = "";          /* Clear the Serial buffer after processing */
      Serial_DataReceived = false; /* Reset the Serial data received flag */
    }

#endif /* HARD_HANDSHAKING */
   }
  }
}

No comments:

Post a Comment