Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Nicklars
#57845 Hi Guys,

I am trying to send Sensor Data from the NodeMCU to my PC. I am constantly reading ACC and Gyro Data from a MPU6050. I want to send this to a PC and analyse it there.
I was doing this with a standalone ESP8266 and a Arduino Uno and got it work perfectly, but I wanted to make the whole package smaller so I decided to use a NodeMCU.

So I think I have managed to read the Sensor Data, now i want to send it to a PC.

I am taking the "WiFi Access Point" from the Examples, but I can't get this to work.
Do I need to use " server.handleClient();" at the beginning of the loop?
Do I need to do it everytime I want to send something?
What does " server.handleClient();" even do? (my C++ skills are limited)
Whats the first parameter at the server.send? (does it have to be 200 or 404)

Can anybody help me, please?

Code: Select all * Copyright (c) 2015, Majenko Technologies
 * All rights reserved.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>

long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;

long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;


/* Set these to your desired credentials. */
const char *ssid = "ESP";
const char *password = "thereisnospoon";
String tmp = "";

ESP8266WebServer server(80);

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {
   server.send(200, "text/html", "<h1>You are connected</h1>");
}

void setup() {
   delay(1000);
   Serial.begin(115200);
   Serial.println();
   Serial.print("Configuring access point...");
   /* You can remove the password parameter if you want the AP to be open. */
   WiFi.softAP(ssid);

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

  //MPU
  Wire.begin(4, 5);
  setupMPU();
}

void loop() {
  server.handleClient();
  delay(100);
  recordAccelRegisters();
  recordGyroRegisters();
  tmp = String(gForceX) + " " + String(gForceY) + " " + String(gForceZ) + " " + String(rotX) + " " + String(rotY) + " " + String(rotZ) + "\r\n";
  Serial.print(tmp);
  server.send(200, "text/plain", tmp);
}


This is just the part where I get the Data from the MPU6050:
Code: Select allvoid setupMPU(){
  Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
  Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
  Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
  Wire.endTransmission(); 
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
  Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
  Wire.endTransmission();
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
  Wire.write(0b00000000); //Setting the accel to +/- 2g
  Wire.endTransmission();
}

void recordAccelRegisters() {
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x3B); //Starting register for Accel Readings
  Wire.endTransmission();
  Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
  while(Wire.available() < 6);
  accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  processAccelData();
}

void processAccelData(){
  gForceX = accelX / 16384.0;
  gForceY = accelY / 16384.0;
  gForceZ = accelZ / 16384.0;
}

void recordGyroRegisters() {
  Wire.beginTransmission(0b1101000); //I2C address of the MPU
  Wire.write(0x43); //Starting register for Gyro Readings
  Wire.endTransmission();
  Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
  while(Wire.available() < 6);
  gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  processGyroData();
}

void processGyroData() {
  rotX = gyroX / 131.0;
  rotY = gyroY / 131.0;
  rotZ = gyroZ / 131.0;
}


Thanks.
User avatar
By mrburnette
#57900
I am constantly reading ACC and Gyro Data from a MPU6050. I want to send this to a PC and analyse it there.


What is the "need" for WiFi?

1) Your NodeMCU can converse over the USB to serial port. You can use a term-program on PC to capture the serial data.

2) You could create a soft-AP, run a client, use UDP and use Processing to run a UDP listenerm too.

Examples:
1: https://www.hackster.io/rayburne/esp8266-turn-off-wifi-reduce-current-big-time-1df8ae

2: https://www.hackster.io/rayburne/tardis-time-esp8266-ap-webserver-gps-6b5d2a


Ray