I am having problems with the accelerometer readings not being correct after sending the json packets. It works the first time around, but after that the readings freeze. I am somewhat new to this, but thought I had it figured out. Is this a timing issue or too much in the loop?
#include <SPI.h>
#include <WiFiNINA.h>
#include <avr/dtostrf.h>
#include <Arduino_LSM6DS3.h>
#include <Wire.h>
#include <MadgwickAHRS.h>
/********************************
* Constants and objects
*******************************/
#define DEVICE_LABEL "Test-Sensor-1"
#define TOKEN "****************************************"
char const * VARIABLE_LABEL_1 = "x1tilt";
char const * VARIABLE_LABEL_2 = "x2tilt";
char const * VARIABLE_LABEL_3 = "x3tilt";
char const * VARIABLE_LABEL_4 = "x4tilt";
char const * VARIABLE_LABEL_5 = "y1tilt";
char const * VARIABLE_LABEL_6 = "y2tilt";
char const * VARIABLE_LABEL_7 = "y3tilt";
char const * VARIABLE_LABEL_8 = "y4tilt";
char const * VARIABLE_LABEL_9 = "aRoll";
char const * VARIABLE_LABEL_10 = "aPitch";
// initialize a Madgwick filter:
Madgwick filter;
// sensor's sample rate is fixed at 104 Hz:
const float sensorRate = 104.00;
// values for orientation:
float roll = 0.0;
float pitch = 0.0;
float heading = 0.0;
const unsigned long event1Interval = 60000;
unsigned long previousTime = 0;
char const *SERVER="things.ubidots.com";
//Replace the above line if you are an Educational user char const *SERVER="things.ubidots.com";
const int HTTPPORT= 443;
char const *AGENT="Test Sensor 1";
char const *HTTP_VERSION = " HTTP/1.1\r\n";
char const *VERSION ="1.0";
char const *PATH= "/api/v1.6/devices/";
char const * SSID_NAME = "****"; // Put here your SSID name
char const * SSID_PASS = "****"; // Put here your password
int status = WL_IDLE_STATUS;
WiFiSSLClient client;
/********************************
* Auxiliar Functions
*******************************/
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void getResponseServer() {
Serial.println(F("\nUbidots' Server response:\n"));
while (client.available()) {
char c = client.read();
Serial.print(c); // Uncomment this line to visualize the response on the Serial Monitor
}
}
void waitServer() {
int timeout = 0;
while (!client.available() && timeout < 5000) {
timeout++;
delay(1);
if (timeout >= 5000) {
Serial.println(F("Error, max timeout reached"));
break;
}
}
}
void sendData(char* payload) {
int contentLength = strlen(payload);
/* Connecting the client */
if (client.connect(SERVER, HTTPPORT)) {
Serial.println("connected to server");
client.print(F("POST "));
client.print(PATH);
client.print(DEVICE_LABEL);
client.print(F("/"));
client.print(HTTP_VERSION);
client.print(F("Host: "));
client.print(SERVER);
client.print(F("\r\n"));
client.print(F("User-Agent: "));
client.print(AGENT);
client.print(F("\r\n"));
client.print(F("X-Auth-Token: "));
client.print(TOKEN);
client.print(F("\r\n"));
client.print(F("Connection: close\r\n"));
client.print(F("Content-Type: application/json\r\n"));
client.print(F("Content-Length: "));
client.print(contentLength);
client.print(F("\r\n\r\n"));
client.print(payload);
client.print(F("\r\n"));
Serial.print(F("POST "));
Serial.print(PATH);
Serial.print(DEVICE_LABEL);
Serial.print(F("/"));
Serial.print(HTTP_VERSION);
Serial.print(F("Host: "));
Serial.print(SERVER);
Serial.print(F("\r\n"));
Serial.print(F("User-Agent: "));
Serial.print(AGENT);
Serial.print(F("\r\n"));
Serial.print(F("X-Auth-Token: "));
Serial.print(TOKEN);
Serial.print(F("\r\n"));
Serial.print(F("Connection: close\r\n"));
Serial.print(F("Content-Type: application/json\r\n"));
Serial.print(F("Content-Length: "));
Serial.print(contentLength);
Serial.print(F("\r\n\r\n"));
Serial.print(payload);
Serial.print(F("\r\n"));
waitServer();
getResponseServer();
}
/* Disconnecting the client */
client.stop();
}
/********************************
* Main Functions
*******************************/
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SSID_NAME);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(SSID_NAME, SSID_PASS);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWiFiStatus();
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
// attempt to start the IMU:
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU");
// stop here if you can't access the IMU:
while (true);
}
// start the filter to run at the sample rate:
filter.begin(sensorRate);
}
void loop(){
// values for acceleration and rotation:
float xAcc, yAcc, zAcc;
float xGyro, yGyro, zGyro;
// check if the IMU is ready to read:
if (IMU.accelerationAvailable() &&
IMU.gyroscopeAvailable()) {
// read accelerometer and gyrometer:
IMU.readAcceleration(xAcc, yAcc, zAcc);
IMU.readGyroscope(xGyro, yGyro, zGyro);
// update the filter, which computes orientation:
filter.updateIMU(xGyro, yGyro, zGyro, xAcc, yAcc, zAcc);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
// print the filter's results:
Serial.print(pitch);
Serial.print(",");
Serial.println(roll);
}
unsigned long currentTime = millis();
if ( currentTime - previousTime >= event1Interval ) {
char payload[200];
char str_val_1[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value = digitalRead(2);
dtostrf(value, 4, 2, str_val_1);
sprintf(payload, "%s","");
sprintf(payload, "{\"");
sprintf(payload, "%s%s\":%s", payload, VARIABLE_LABEL_1, str_val_1);
sprintf(payload, "%s}", payload);
char payload2[200];
char str_val_2[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value2 = digitalRead(3);
dtostrf(value2, 4, 2, str_val_2);
sprintf(payload2, "%s","");
sprintf(payload2, "{\"");
sprintf(payload2, "%s%s\":%s", payload2, VARIABLE_LABEL_2, str_val_2);
sprintf(payload2, "%s}", payload2);
char payload3[200];
char str_val_3[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value3 = digitalRead(4);
dtostrf(value3, 4, 2, str_val_3);
sprintf(payload3, "%s","");
sprintf(payload3, "{\"");
sprintf(payload3, "%s%s\":%s", payload3, VARIABLE_LABEL_3, str_val_3);
sprintf(payload3, "%s}", payload3);
char payload4[200];
char str_val_4[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value4 = digitalRead(5);
dtostrf(value4, 4, 2, str_val_4);
sprintf(payload4, "%s","");
sprintf(payload4, "{\"");
sprintf(payload4, "%s%s\":%s", payload4, VARIABLE_LABEL_4, str_val_4);
sprintf(payload4, "%s}", payload4);
char payload5[200];
char str_val_5[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value5 = digitalRead(6);
dtostrf(value5, 4, 2, str_val_5);
sprintf(payload5, "%s","");
sprintf(payload5, "{\"");
sprintf(payload5, "%s%s\":%s", payload5, VARIABLE_LABEL_5, str_val_5);
sprintf(payload5, "%s}", payload5);
char payload6[200];
char str_val_6[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value6 = digitalRead(7);
dtostrf(value6, 4, 2, str_val_6);
sprintf(payload6, "%s","");
sprintf(payload6, "{\"");
sprintf(payload6, "%s%s\":%s", payload6, VARIABLE_LABEL_6, str_val_6);
sprintf(payload6, "%s}", payload6);
char payload7[200];
char str_val_7[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value7 = digitalRead(8);
dtostrf(value7, 4, 2, str_val_7);
sprintf(payload7, "%s","");
sprintf(payload7, "{\"");
sprintf(payload7, "%s%s\":%s", payload7, VARIABLE_LABEL_7, str_val_7);
sprintf(payload7, "%s}", payload7);
char payload8[200];
char str_val_8[30];
/*4 is the total lenght of number,maximun number accepted is 99.99*/
int value8 = digitalRead(9);
dtostrf(value8, 4, 2, str_val_8);
sprintf(payload8, "%s","");
sprintf(payload8, "{\"");
sprintf(payload8, "%s%s\":%s", payload8, VARIABLE_LABEL_8, str_val_8);
sprintf(payload8, "%s}", payload8);
//Send the payload to Ubidots
sendData(payload);
sendData(payload2);
sendData(payload3);
sendData(payload4);
sendData(payload5);
sendData(payload6);
sendData(payload7);
sendData(payload8);
previousTime = currentTime;
}
}