#include <SparkFun_ADXL345.h> // SparkFun ADXL345 Library // MQTT headers #include <SPI.h> #ifdef ARDUINO_SAMD_MKR1010 #include <WiFi101.h> #define WL_NO_MODULE WL_NO_SHIELD #else #include <WiFiNINA.h> #endif #include <ArduinoMqttClient.h> #include "config.h" WiFiSSLClient net; MqttClient mqtt(net); String accelerometerTopic = "itp/" + DEVICE_ID + "/accelerometer" int state = false; int button = 6; void setup() { Serial.begin(9600); Serial.println("Connecting WiFi"); pinMode(button, INPUT); connectWiFi(); setAxl(); reconnect(); } void loop() { mqtt.poll(); if (digitalRead(button) == HIGH && state == false) { state = true; delay(200); } else if (digitalRead(button) == HIGH && state == true) { state = false; delay(200); } if (state == true) { int x, y, z; adxl.readAccel(&x, &y, &z); mqtt.beginMessage(accelerometerTopic); Serial.println("begin message"); mqtt.print(x); mqtt.print(","); mqtt.print(y); mqtt.print(","); mqtt.print(z); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", "); Serial.println(z); Serial.println("end message"); mqtt.endMessage(); } else if (state == false) { Serial.println("waiting"); } }