Compare commits

...

3 Commits

2 changed files with 141 additions and 134 deletions

View File

@ -12,9 +12,3 @@
platform = espressif32
board = upesy_wroom
framework = arduino
lib_deps = adafruit/Adafruit MPU6050@^2.2.6
; How to change bit-per-second rate for your project:
; Add right under [env:upesy_wroom]:
; "monitor_speed = x" (without the "")
; x == baudrate of selected choice, bsp: 115200, 8000, 300

View File

@ -1,141 +1,154 @@
//--------------------------------------------------------------------------------------------------------------------------//
// Setting up the Adafruit_MPU6050 library for the accelerator chip
// Setting up an Access Point hosted by the ESP32
//--------------------------------------------------------------------------------------------------------------------------//
// Check the other guide in the .docx as to how to INSTALL the necessary library //
// What is the difference between an Access Point and using the ESP as a client? //
// Well, there's a big difference as to how either work, and what is needed. As an Access Point, you can create your own little "network" hosted
// over the small WLAN module on the developing board. That let's you access a website on the ESP for example, without
// having to go over any of your internet provider's servers since it's purely local.
// When hosting an Access Point, you CANNOT log into another Wlan network. Clients (for example your phone) can log into the one on the ESP though.
// If you want to use an async Webserver, that works better with the use of a router inbetween, and has more advantages all in all.
// If no internet connection is required and it's for little displayment, a local Access Point should be enough.
// Must be included for most things! //
#include <Arduino.h> // Only include once of course.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Arduino.h>
// Create instance of the sensor library //
Adafruit_MPU6050 mpu;
// Load Wifi library //
#include <WiFi.h>
// Set your network credentials (you are entirely free to choose anything) //
const char* _ssid = "ESP32-Access-Point"; // for example
const char* _password = "1234IamAPassword"; // that's a bad example, but you get the gist
// Set up instance and the right port (default: 80) //
WiFiServer server(80);
// String to save the http adress in //
String _http = "";
// State variable for the example following in the loop() function later //
String _output_state = "off";
void setup()
{
Serial.begin(115200); // For Serial communication, check if the right bit-per-second rate is selected!
// If you want to change said bit-per-second, look in the .ini file for information.
// How do you know the one selected does not work / there's something wrong in the .ini file?
// Check the terminal, if that is the case everything printed is Unicode gibberish.
Serial.println("Setting up access point...");
WiFi.softAP(_ssid, _password);
IPAddress _IP = WiFi.softAPIP(); // You can see what is hosted on the ESP over this IP address.
Serial.print("AP IP Address: ");
Serial.println(_IP);
while(!Serial) delay(10); // Wait until the Serial communication is set up and initialized.
Serial.println("Starting...");
if(!mpu.begin()) // Exception for the case of the mpu not starting up
{
// If this is the case, check the wiring and whether or not the right pins are connected.
// Another issue that could lead to this is too little or too much current going through the chip.
Serial.println("Failed to find MPU6050 chip.");
while(1)
{
delay(10);
server.begin(); // starts WiFi server.
}
}
Serial.println("MPU6050 has been found.");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G); // Set to the range of your choice for the accelerator values
// depending on the range you want your read in values to vary in.
Serial.println("Accelerator range set to: ");
switch(mpu.getAccelerometerRange()) // Check setting and print for insight.
{
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange())
{
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth())
{
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
delay(100);
// What is the difference between "void setup()" and "void loop()"? Why put all this in here? //
// The setup fuction is only initialized once, and though that's true for the entire lifetime of the programm,
// the started services continue to run in here on this threat, allowing the loop to be executed simultaniously.
// Mind, and that's IMPORTANT!!, that you should refer from using "while(1)" in this function as much as possible.
// (I'm aware there is one in here in this project, but only because it is handled correctly and carefully.)
}
// Put your variables here for testing, otherwise I'd recommend putting it in a class //
float _x_accel = 0; // x acceleration
float _y_accel = 0; // y acceleration
float _calc = 0; // for calculations
void loop()
{
// Save vals if you want to send them: //
sensors_event_t _a;
sensors_event_t _g;
sensors_event_t _temp; // Used as placeholder for now in the next function, even if you don't need temperature values.
// Listen for any incoming clients, you can also create a thread extra for this if little processing power is needed //
WiFiClient _client = server.available();
// If that is the case, make sure to keep the service running. It should not be stopped or interrupted at all if the programm is still running,
// I'd advice to use an exeption / interrupt that is listening in on whether or not a new client or http request has been received.
// Be careful with that in general if you try it.
mpu.getEvent(&_a, &_g, &_temp); // Reads in your values.
_x_accel = _a.acceleration.x; // acceleration in x.
_y_accel = _a.acceleration.y; // acceleration in y.
// Basic example for use //
_calc = _y_accel * 12.5; // Calculate postion if you want to.
if(_calc > 100) // reduce values if you only want values from (-100) - 100 for example
if(_client)
{
_calc = 100;
}
else if(_calc < (-100))
String _current_line = "";
while(_client.connected()) // As long as the client is connected...
{
_calc = (-100);
if(_client.available()) // Checks whether or not there is bites to read from.
{
char _c = _client.read(); // reads a single bite.
_http += _c; // assembling the full http request one by one.
if(_c != 10) // Listen in for LF (new line / line feat) since that is
// a signal for the end of the request.
// (Side note: Using '\n' here should technicall work here, but can create some errors.)
// (Use 10 (ASCII) instead to avoid any misleading errors.)
{
// Send a response here, since every request needs a response.
// If something went wrong, be sure to send an error message instead of nothing.
// Replying: //
if(_current_line.length() == 0)
{
// HTTP headers always start a certain way. So be sure to keep the following format.
_client.println("HTTP/1.1 200 OK");
_client.println("Content-type:text/html");
_client.println("Connection: close");
_client.println();
// And there you go, a correct response that a client can work with!
// Now you can technically compute whatever changes the client might have requested,
// whether that was a press of a button or a press on a link. Any action needs to be responded to,
// or else one might end up in the void of white like a blank page, or nothing happens.
// The following is an example of how you would process requests with a html button.
// NOTE! This is html code written inside a C/C++ code. It's far from optional, and SPIFFS is therefor recommended if a
// seperate html & css file is prefered. You can read about that in the tutorial branch to the Async Server.
// For now, this is purely for demonstration.
// Handling the change of the example button //
if (_http.indexOf("GET Button on") >= 0)
{
Serial.println("Button turned on");
_output_state = "on";
// digitalWrite(PIN, HIGH); // == how you would pull a Pin up when the button activates.
}
else if (_http.indexOf("GET Button off") >= 0)
{
Serial.println("Button turned off");
_output_state = "off";
// digitalWrite(PIN, LOW); // again for example.
}
// And done! //
// Mind that everything in this loop repeats itself over and over, only stopping until the programm fails to execute or //
// the current is cut. If you'd like to restart everything, press the little button titled "RST" or "BOOT" (if the first does nothing) on the ESP. //
// Displaying the site //
_client.println("<!DOCTYPE html><html>");
_client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
_client.println("<link rel=\"icon\" href=\"data:,\">");
// Set up a button //
_client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
_client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
_client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
_client.println("</style></head>"); // closes the style tag.
if (_output_state=="off")
{
_client.println("<p><a href=\"Button on\"><button class=\"button\">ON</button></a></p>");
}
else
{
_client.println("<p><a href=\"Button off\"><button class=\"button button2\">OFF</button></a></p>");
}
// End the HTTP response with a LF as well //
_client.println();
break;
}
else // received a LF, clear the current line for input.
{
_current_line = "";
}
} // end of if()
else if(_c != 13) // Handles the exeption for if you got anything but a carriage return.
// NOTE! Technically, '\r' should also work, but like '\n', it can cause issues.
// (Once more use 13 (ASCII) instead)
{
_current_line += _c;
}
} // end of if()
} // end of while()
// Clear the request variable for later use //
_http = "";
// Close the connection to the client //
_client.stop();
Serial.println("Client disconnected.");
Serial.println();
// For every time the loop repeats, a new client and a new http request is being processed.
}
}
// As said, the example is a tedious an impractical way to host reactive html code.
// Yet, it's enough for simple and fast displaying of values over a local WiFi Webserver.
// Still, since it needs an entire thread to run, it's far from the ideal way.