Post topics, source code that relate to the Arduino Platform

User avatar
By valen_tin
#41084 Hello

I am trying to make ESP8266 12E to talk to an arduino Due. For now it is a simple calculation, when I make the connection I will go to the real thing. I want to send a number from the ESP to the DUE, make a calculation on the due and return it to the ESP to show it on a web address.

this is my code on the esp:

Code: Select all#include <ESP8266WiFi.h>

const char WiFiAPPSK[] = "123";



WiFiServer server(80);

void setup()
{
 
  setupWiFi();
  server.begin();

  Serial.begin(115200);
 

 
  establishContact(); 
}



void loop()
{

  WiFiClient client = server.available();
  if (!client) {
    return;
  }


  String req = client.readStringUntil('\r');

  client.flush();

 
  int val = -1;
  int inByte = 0;

  if (req.indexOf("/calc/1") != -1)
    val = 1;
  else if (req.indexOf("/calc/5") != -1)
    val = 5;
  else if (req.indexOf("/calc/2") != -1)
    val = 2;



  if (val >= 0)
    Serial.print(val);
  client.flush();

  inByte = Serial.read();

  client.flush();


  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";

  if (val >= 0)
  {
    s += "666 multiplied by ";
    s += val;
    s += " is equal to ";
    s += inByte;
    s += "<br>"; // Go to the next line.

  }

  else
  {
    s += "Invalid Request.<br> Try /calc/1, /calc/2, or /calc/5.";
  }
  s += "</html>\n";

 
  client.print(s);
  delay(1);
//  Serial.println("Client disonnected");

  client.flush();
 
}

void setupWiFi()
{
  WiFi.mode(WIFI_AP);


  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "ESP8266 Thing " + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i = 0; i < AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);
}




void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print(1);   
    delay(300);
  }
}


and this is my code on the due:
Code: Select allint inByte = 0;
int changeByte = 666;
int returnValue = 0;

void setup() {
 
  Serial1.begin(115200);
 
  establishContact(); 
}

void loop()
{

  if (Serial1.available() > 0) {
 
    inByte = Serial1.read();
    returnValue = changeByte * inByte;
    Serial1.write(returnValue);
 
  }
}

void establishContact() {
 
    Serial1.print(1);   

 
}


when I start both with connected serial (esp.rx to due.tx1; esp.tx to due.rx1) nothing happens on the web address of the ESP. When not connected to the due, the esp works, but calculation cannot be done.

Any ideas of what the problem could be?

Thanks