I am working on a gesture control car project and I find everyone on the internet using RF modules or Bluetooth modules to do it, I had two NodeMCU lying around so I thought why not use them and bring a change. But I am finding difficulty in sending the accelerometer values from one Nodemcu to another, For connecting the two Nodemcu I took help from here
So instead of a string (which they use), I tried sending an array of two variables, but I fail to send/receive it from the transmitter to the receiver. I have spent a lot of time on it, but I think I need help.
I now know ( just this morning ) that Nodemcu has only one analog pin, I have ordered for a 74HC4051 IC. So I haven't changed the code for that, so please bear that in mind. Also, I have used the xAxis only now just to test if the data is being sent, but it is not.
If there is any other easier way of sending the two variable values, please do suggest.
This is the actual project, I asked the author for help, but no reply yet.
I have given below the edited code that I have now
Transmitter code
#include <SPI.h>
#include <ESP8266WiFi.h>
int xAxis = A0;
int yAxis = D2;
int data[1];
int x;
int ledPin = D8;
char ssid[] = "********"; // SSID
char pass[] = "********"; // password I removed them while posting the question
WiFiServer server(80);
IPAddress ip(192, 168,1, 80); // IP address of the server
IPAddress gateway(192,168,1,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup() {
Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
server.begin(); // starts the server
Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // some parameters from the network
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());
Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
pinMode(ledPin, OUTPUT);
}
void loop () {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
client.flush();
Serial.println("Data send");
x= analogRead(xAxis);
data[0]= analogRead(xAxis);
client.println(data[x,yAxis]);
Serial.println(x);
Serial.println(data[0]); //This appears on the screen only if i have the line data[0]= analogRead(xAxis);
digitalWrite(ledPin, HIGH);
}
client.stop(); // tarminates the connection with the client
}delay(2000);
}
Receiver Code
#include <SPI.h>
#include <ESP8266WiFi.h>
int ENA = D1;
int ENB = D2;
int MotorA1 = D3;
int MotorA2 = D4;
int MotorB1 = D5;
int MotorB2 = D6;
int data[1];
int xAxis;
int yAxis;
int ledPin = D8;
char ssid[] = "*****";
char pass[] = "*****";
unsigned long askTimer = 0;
IPAddress server(192,168,1,80); // the fix IP address of the server
WiFiClient client;
void setup() {
Serial.begin(115200); // only for debug
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
/* Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // Network parameters
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());*/
pinMode(ledPin, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(MotorA1, OUTPUT);
pinMode(MotorA2, OUTPUT);
pinMode(MotorB1, OUTPUT);
pinMode(MotorB2, OUTPUT);
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
void loop () {
client.connect(server, 80); // Connection to the server
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
data[xAxis,yAxis] = client.read(); // receives the answer from the sever
Serial.println(" Received from Server ");
client.flush();
digitalWrite(ledPin, HIGH);
Serial.println(xAxis);
if(yAxis > 400) {
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else if(yAxis < 320) {
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
} else if(xAxis < 320){
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else if(xAxis > 400){
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else {
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
delay(2000); // client will trigger the communication after two seconds
}