Buonasera, Vorrei un aiuto per capire come mai il codice non fa ciò che voglio. Più In particolare in questo esempio uso un' arduino portenta h7 che funge da server con un imu bno055 attaccato.
Sostanzialmente il problema dovrebbe essere banale. Infatti lo stesso codice, l ho creato con comunicazione bluetooth: usando un'app come client, premendo il tasto per rendere lo stato readacc=1, riesco a leggere in realtime come variano i dati che voglio leggere (fin quando non premo il tasto che rendo readacc=0). Se uso la comunicazione wifi la stessa logica non vale. Infatti premo il tasto per rendere lo stato readacc=1 e solo una riga viene restituita come output. Non capisco se sto sbagliando qualcosa nella lettura dello stato oppure altro.
#include <WiFi.h>
#include "arduino_secrets.h"
#define Serial _UART_USB_
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#define BNO055_SAMPLERATE_DELAY_MS (100)
// Check I2C device address and correct line below (by default address is 0x29 or 0x28)
// id, address
Adafruit_BNO055 bno = Adafruit_BNO055(-1, 0x28);
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
int read_acc=0;
WiFiServer server(80);
void setup() {
// put your setup code here, to run once:
_UART_USB_.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if(!bno.begin())
{
/* There was a problem detecting the BNO055 ... check your connections */
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
while(1);
}
delay(1000);
bno.setExtCrystalUse(true);
_UART_USB_.println("Access Point Web Server");
pinMode(LEDR,OUTPUT);
pinMode(LEDG,OUTPUT);
pinMode(LEDB,OUTPUT);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LEDR, HIGH);
// by default the local IP address of will be 192.168.3.1
// you can override it with the following:
// WiFi.config(IPAddress(10, 0, 0, 1));
if(strlen(pass) < 8){
_UART_USB_.println("Creating access point failed");
_UART_USB_.println("The Wi-Fi password must be at least 8 characters long");
// don't continue
while(true);
}
// print the network name (SSID);
_UART_USB_.print("Creating access point named: ");
_UART_USB_.println(ssid);
//Create the Access point
status = WiFi.beginAP(ssid,pass);
if(status != WL_AP_LISTENING){
_UART_USB_.println("Creating access point failed");
// don't continue
while (true);
}
// wait 10 seconds for connection:
delay(10000);
// start the web server on port 80
server.begin();
// you're connected now, so print out the status
printWiFiStatus();
}
void loop() {
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if (status == WL_AP_CONNECTED) {
// a device has connected to the AP
_UART_USB_.println("Device connected to AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
_UART_USB_.println("Device disconnected from AP");
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /L")) {
digitalWrite(LEDR, LOW);
client.println("The led is high ");
_UART_USB_.print("PROVALOW: ");
_UART_USB_.println(currentLine);
readacc=1;
if (readacc==1){
imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
/* Display the floating point data */
_UART_USB_.print("X: ");
_UART_USB_.print(euler.x());
_UART_USB_.print(" Y: ");
_UART_USB_.print(euler.y());
_UART_USB_.print(" Z: ");
_UART_USB_.print(euler.z());
_UART_USB_.print("\t\t");
}
}
if (currentLine.endsWith("GET /H")) {
readacc=0;
digitalWrite(LEDR, HIGH);
client.println("The led is low ");// GET /Lr turns the Red LED off
_UART_USB_.print("PROVALOW: ");
_UART_USB_.println(currentLine);
}
}
}
}
// close the connection:
// client.stop();
// Serial.println("client disconnected");
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
_UART_USB_.print("SSID: ");
_UART_USB_.println(WiFi.SSID());
// print your Wi-Fi shield's IP address:
IPAddress ip = WiFi.localIP();
_UART_USB_.print("IP Address: ");
_UART_USB_.println(ip);
// print where to go in a browser:
// Serial.print("To see this page in action, open a browser to http://");
// Serial.println(ip);
}