Chat here is you are connecting ESP-xx type modules to existing AVR based Arduino

Moderator: igrr

User avatar
By kumouni
#79008 Hi,
I am in a desperate need for help. I posted this on Arduino forum too but here might be more appropriate.

What I want to do:
I have two ESP8266s, two Arduino UNOs, one ADXL335 Accelerometer.
1) I want to get 3 readings (x, y, z) from the accelerometer (which is connected to Arduino),
2) send the data via the client ESP8266 to the server ESP8266 via wifi (local network and not though the Internet),
3)Print the data of the 3 readings on the Serial Monitor on my computer, which is connected to an Arduino (which is connected to the server ESP8266, which is getting the data wirelessly from the client ESP8266).

I used this ( https://robotzero.one/sending-data-esp8266-to-esp8266/ ) tutorial to do this project.

The problem I'm having:
1) The 2nd and the 3rd readings (for y and z axis) are not changing on the server side's Serial Monitor. It's completely static (e.g. printing out 100-200-ish number all the time). The 1st reading (input of the x-axis of the accelerometer) is being communicated to the server successfully, and I can see it changing on the Serial Monitor.

2) Having said this, the changing of the 1st number (reading of x) being printed out on the Serial Monitor is extremely slow, and sometimes gets stuck on the same value for a long time even I tilt on the x-axis (which I want it to change the numbers immediately). It worked fine when I experimented with just one analog input (e.g. potentiometer, using just the x-input of the accelerometer...).


My code:
I have 3 codes below - I'm putting everything here because I'm such a noob and am not sure what's causing the issue.
However, I'm guessing - what's causing the issue could be to do with

1) How I'm structuring the URI - the bit around "String url = "/data/";" on the client side
2) How I'm using server.hasArg on the server side
3) How I'm using server.arg on the server side
4) 'HTTP GET' requests on the client side

But this is just my guess, and I don't know how to fix it.
How can I solve these issues? :'(

I would really appreciate your help!!


Server / Access Point ESP8266 Code:
Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char *ssid = "ESP";
ESP8266WebServer server(80);

void handleSentVar() {
  if (server.hasArg("sensor_reading")) {

    Serial.print(server.arg("sensor_reading")); //THIS CHANGES EVEN IT'S SUPER SLOW
    Serial.print(",");
    Serial.print(server.arg("sensor_reading1"));  //THIS DOESN'T CHANGE
    Serial.print(",");
    Serial.println(server.arg("sensor_reading2"));  //THIS DOESN'T CHANGE

    server.send(200); //200 = OK

    delay(1);
  }
}

void setup() {
  delay(1000);
  Serial.begin(9600);
  Serial.println();
  Serial.print("Configuring access point...");

  WiFi.softAP(ssid);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/data/", HTTP_GET, handleSentVar);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}



Client / Station Arduino Code:
Code: Select all#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2,3); // RX | TX

const int ap1 = A5;
const int ap2 = A4;
const int ap3 = A3;
const int numReadings = 30;
int inByte = 0;         // incoming serial byte

int readingsX[numReadings];     
int readingsY[numReadings];     
int readingsZ[numReadings];     

int readIndexX = 0;             
int readIndexY = 0;             
int readIndexZ = 0;             
int totalX = 0;                 
int totalY = 0;                 
int totalZ = 0;                 
int averageX = 0;               
int averageY = 0;               
int averageZ = 0;               
 
void setup() {
  Serial.begin(9600); //This must always be 9600
  ESPserial.begin(9600);  //This must match the baud rate of the Serial.begin for ESP8266

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readingsX[thisReading] = 0;
    readingsY[thisReading] = 0;
    readingsZ[thisReading] = 0;
  }
}

void loop() {

//  X------------------------------
   // subtract the last reading:
  totalX = totalX - readingsX[readIndexX];
 
  // read from the sensor:
  int calx = 347; //CALIBRATE X
  int x = analogRead(ap1)-calx;
  x = abs(x);
  int xMapped = map(x,0,50,0,50);

  readingsX[readIndexX] = xMapped;
 
  // add the reading to the total:
  totalX = totalX + readingsX[readIndexX];
  // advance to the next position in the array:
  readIndexX = readIndexX + 1;

  // if we're at the end of the array...
  if (readIndexX >= numReadings) {
    // ...wrap around to the beginning:
    readIndexX = 0;
  }

  // calculate the average:
  averageX = totalX / numReadings;
  // send it to the computer as ASCII digits
  ESPserial.write(averageX);
  ESPserial.write(",");

  delay(1);        // delay in between reads for stability

... SAME SET OF CODES FOR Y AND Z...
 
}//------End of Loop-----




Client / Station ESP8266 Code (for ESP8266):

Code: Select all#include <ESP8266WiFi.h>
const char *ssid = "ESP";

int outputValue = 0;        // value sent to server
int outputValue1 = 0;        // value sent to server
int outputValue2 = 0;        // value sent to server

byte data;
byte data1;
byte data2;

void setup() {
  Serial.begin(9600);
  delay(1000);
  /* Explicitly set the ESP8266 to be a WiFi-client */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid);

}

void loop() {
  char inData[15];
  char outData[3];
  byte index = 0;

  if(Serial.available()){  //When there is data from Arduino available...
   
     char aChar = Serial.read();
     if(aChar == '\n') {
        // End of record detected. Time to parse
           char *p = inData; //making p point to the string "x,y,z"

           char *str;        // declaring str
           int counter = 0; //initialise the counter
           
           while ((str = strtok_r(p, ",", &p))) {// delimiter is the comma. NULL is the terminator
                 outData[counter] = *str; //use the counter as an index to add each value to the array. data[0]=x, data[1]=y...
                 counter++; //increment the counter

              p = NULL;
           }
           data = outData[0];
           data1 = outData[1];
           data2 = outData[2];
           
          index = 0;
          inData[index] = NULL;
         
     } else {
        inData[index] = aChar;  //Store current character. inData[current index] = current character
        index++;  //move onto next index untill \n
        inData[index] = '\0'; // Keep the string NULL terminated
     }  //---end of parsing---

  }

  outputValue = data; //int = byte
  outputValue1 = data1;
  outputValue2 = data2;

  char intToPrint[5];
  itoa(outputValue, intToPrint, 10);
  char intToPrint1[5];
  itoa(outputValue1, intToPrint1, 10);
  char intToPrint2[5];
  itoa(outputValue2, intToPrint2, 10);
  //^integer to string conversion. itoa(int val, Char s, int redix/base).
  //buffer (intToPrint) must have sufficient storage.


  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const char * host = "192.168.4.1";
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    return;
  }

 
  // We now create a URI for the request
  String url = "/data/";
  url += "?sensor_reading=";  //send key-value pairs by adding "?", followed by 'key=value'
  url += intToPrint;
  url += "&sensor_reading1="; 
  url += intToPrint1; //THIS DOESN'T CHANGE ON SERVER'S SERIAL MONITOR
  url += "&sensor_reading2=";
  url += intToPrint2; //THIS DOESN'T CHANGE ON SERVER'S SERIAL MONITOR

  // This will send the GET request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
               
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  delay(1);
}//------End of Loop-----
User avatar
By QuickFix
#79020 Why are you using two Arduino's and two ESP's? :?

Just leave out the Arduino's from the equation and connect the accelerometer directly to the server ESP.
Then, connect the client ESP to your PC and you're done.

Of course you'll need to modify your code and choose a different accelerometer (since the ADXL335 is analog), but there are loads of cheap I2C sensors available.
User avatar
By kumouni
#79216
QuickFix wrote:Why are you using two Arduino's and two ESP's? :?

Just leave out the Arduino's from the equation and connect the accelerometer directly to the server ESP.
Then, connect the client ESP to your PC and you're done.

Of course you'll need to modify your code and choose a different accelerometer (since the ADXL335 is analog), but there are loads of cheap I2C sensors available.


Hi QuickFix,
Thanks so much for your reply - and sorry for late update!
You are right, that might be easier.
The reasons why I'm using 2 Arduinos are:
1) because I was (am) a super beginner at Arduino / coding in C in general and since I started with Arduino, it just naturally lead to the method.
2) I have never tried I2C communication before and I don't know where to start. :cry:
3) I'm using Arduino Uno to program ESP8266 - I don't have the USB-to-ESP-01 programmer, and I'd already bought 20 of ADXL335s....

I can buy USB-to-ESP-01 programmer. However, it would be great if you can suggest me how to keep the ADXL335 in this project, since I bought many of them and it would be the best if I didn't have to let it go to waste.
Is there any way I can make this work?