NodeMCU sending to PC
Posted: Tue Nov 08, 2016 3:27 pm
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?
This is just the part where I get the Data from the MPU6050:
Thanks.
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 all
void 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.