Thursday, April 7, 2016

Arduino Serial Communication with ESP8266

Here's the wiring.
Here's the wiring diagram....
Using AltSerial ensured that I was not getting a whole lot of gibberish in Serial Com. I also think the RX and TX pins on the ESP8266 are 5V tolerant. Been using them for a direct connection Arduino UNO with no problem. I will add a 10K Ohm resistor on the TX pin from arduino to see if it makes a difference to stability of the connection. ESP8266 chips are total flakes when it comes to a stable connection. Use the ESP-201s instead that have extra pins as well.
1. Program the ESP8266 to be an HTTP webserver. I used the sample that was provided with ESP8266 library on GITHUB
/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO2 low,
 *    http://server_ip/gpio/1 will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be 
 *  printed to Serial when the module is connected.
 */

#include 

const char* ssid = "i_am_mumbhai-2.4";
const char* password = "!ceCr3am";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  //Serial.begin(9600);
  delay(10);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("wait\r\n");
  }
  Serial.print("connect\r\n");
  
  // Start the server
  server.begin();
  Serial.println("started\r\n");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  //Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  //Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1) {
    Serial.println(0);
    val = 0;
  } else if (req.indexOf("/gpio/1") != -1) {
    Serial.println(1);
    val = 1;
  } else if (req.indexOf("/gpio/2") != -1) {
    Serial.println(2);
    val = 2;
  } else if (req.indexOf("/gpio/3") != -1) {
    Serial.println(3);
    val = 3;
  } else {
    Serial.println(req);
    client.stop();
    return;
  }

  Serial.println(req);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\n\r\nGPIO is now  " + String(val) + "\r\n\r\n\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}


2. Program the arduino to talk to the ESP8266 over serial and parse the incoming string to read the pin number and intended value.
#include 

//#include 

//SoftwareSerial esp8266(11,10);

AltSoftSerial esp8266;


char* pch;
void setup() {
  // put your setup code here, to run once:
  pinMode(7, OUTPUT);
  Serial.begin(115200);
  esp8266.begin(115200);
  Serial.println("start");
  esp8266.println("esp start");
  
}


void loop() {
  
  
  /* 2nd attempt - works better */
  String content = "";
  char character;
  delay(500);
  //
  while(esp8266.available()) {
    cli();
    character = esp8266.read();
    content.concat(character);
    sei();    
  }
    
  if (content != "" && (content.indexOf("/state/on")>0 || content.indexOf("/state/off")>0)) {
    
    Serial.println(content);
    Serial.println("============");

    /* String manip 2nd attempt */
    int start = content.indexOf("/gpio/");
    int end_1 = content.lastIndexOf("/off");
    int end_2 = content.lastIndexOf("/on");
    
    String interest = (end_1>0) ? (content.substring(start,end_1))+"/off" : (content.substring(start,end_2))+"/on";
    int pinValue = (end_1>0) ? HIGH : LOW;
    Serial.println("interest:" + interest);
    String pin = interest.substring(6,7);
    Serial.println("pin:" + pin);

    digitalWrite(pin.toInt(), pinValue);
  }
  
  
}