Skip to content

Instantly share code, notes, and snippets.

@rewida17
Last active December 8, 2019 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rewida17/447fedf7351e6a98c9e33e75b04d2498 to your computer and use it in GitHub Desktop.
Save rewida17/447fedf7351e6a98c9e33e75b04d2498 to your computer and use it in GitHub Desktop.
Get data from MPU and print in JSON on site #arduino
/*
Web Server + MPU9250
*/
#include <SPI.h>
#include <UIPEthernet.h>
#include <MPU9250_asukiaaa.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>
String AX = String("\"aX\":");
String AY = String("\"aY\":");
String AZ = String("\"aZ\":");
String GX = String("\"gX\":");
String GY = String("\"gY\":");
String GZ = String("\"gZ\":");
//MPU
MPU9250_asukiaaa mySensor;
float aX, aY, aZ, aSqrt, gX, gY, gZ, mDirection, mX, mY, mZ;
//MPU
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 101, 23, 100);
EthernetServer server(80); //port 80
void setup() {
Ethernet.init(10); //CS connected to ?? Pin 10
Ethernet.begin(mac, ip); //STATIC
// Ethernet.begin(mac); //DHCP
server.begin();
//MPU
mySensor.beginAccel(); //Enable Accel
mySensor.beginGyro(); //Enable Gyro
// mySensor.beginMag(); //-||-
//MPU
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
//client.println(F("Content-Type: text/html"));
//client.println(F("Content-Type: /plain"));
client.println(F("Content-Type:application/json"));
client.println(F("Connection: close")); // the connection will be closed after completion of the response
client.println(); //TIO
// send a standard http response header
if (mySensor.gyroUpdate() == 0) {
gX = mySensor.gyroX();
gY = mySensor.gyroY();
gZ = mySensor.gyroZ();
// client.println("gyroX: " + String(gX));
// client.println("gyroY: " + String(gY));
// client.println("gyroZ: " + String(gZ));
} // GYRO
if (mySensor.accelUpdate() == 0) {
aX = mySensor.accelX();
aY = mySensor.accelY();
aZ = mySensor.accelZ();
// aSqrt = mySensor.accelSqrt();
//JSON PRINTING !!!! ON TOP MORE :)
client.println( "[{" +\
String(AX)+String(aX) +","+ \
String(AY)+String(aY) +","+ \
String(AZ)+String(aZ) +","+ \
String(GX)+String(gX) +","+ \
String(GY)+String(gY) +","+ \
String(GZ)+String(gZ) +","+ \
"\"Time\":"+ String(millis())+\
"}]" );
//[{ "x": 43.4627,"y": -3.79636, "z": "a"}]
//client.println("accelY: " + String(aY));
//client.println("accelZ: " + String(aZ));
//client.println("accelSqrt: " + String(aSqrt));
} //ACCEL
//if (mySensor.magUpdate() == 0) {
// mX = mySensor.magX();
// mY = mySensor.magY();
// mZ = mySensor.magZ();
// mDirection = mySensor.magHorizDirection();
// client.println("magX: " + String(mX));
// client.println("maxY: " + String(mY));
// client.println("magZ: " + String(mZ));
// client.println("horizontal direction: " + String(mDirection));
// } //MAGNETO
// client.println(String(millis()) + "ms");
}
break;
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
//Ethernet.maintain(); //DHCP RENEW
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment