Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ZOOM RECORDING : https://psu.zoom.us/rec/share/3dFYE7PXtT5JS5X3-ULtcIkYBp6meaa80SRK_PULnRozXKrivk44EH1D91u5EL_C
- * Adapt code so that every time you simulate a button press on Arduino, it reads the values from its onboard accelerometer
- * Send these parameters to an IFTTT maker webhook
- * Event name "accel_readings"
- * Webhook should trigger an email service
- *
- */
- #include <SPI.h>
- #include <WiFiNINA.h>
- #include <Arduino_LSM6DS3.h>
- #include "arduino_secrets.h"
- const int BUTTON_PIN = 2; // digital pin D2 (near ground pin)
- char ssid[] = SECRET_SSID; // your network SSID (name)
- char pass[] = SECRET_PSW;// your network PASSWORD ()
- String apiKey = SECRET_APIKEY;
- int press_counter = 0;
- int status = WL_IDLE_STATUS;
- char server_name[] = "maker.ifttt.com"; // first 3 parts of URL
- WiFiClient client; // client object
- void setup() {
- //Initialize serial and wait for port to open:
- Serial.begin(9600);
- pinMode(BUTTON_PIN, INPUT_PULLUP); // when nothing connected, pulls up to 3.3V - very noisy without it
- // attempt to connect to Wifi network:
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to SSID: ");
- Serial.println(ssid);
- //use the line below if your network is protected by wpa password
- status = WiFi.begin(ssid, pass);
- // wait 5 seconds for connection:
- delay(5000);
- Serial.println(status);
- }
- Serial.println("Connected to WiFi!");
- // attempt to connect accelerometer
- if (!IMU.begin()) {
- Serial.println("Failed to initialize IMU!");
- while (1);
- }
- else {
- Serial.println("Connected to Accelerometer!");
- }
- }
- void loop() {
- /*if(digitalRead(BUTTON_PIN)==LOW){ // checks to see if button pin is low (ground)
- press_counter++; // increments
- Serial.println("Button pressed!");
- String additional_info = "?value1=";
- additional_info += press_counter;
- sendIFTTT("button_pressed", additional_info);
- */
- // declare x, y, z axis variables
- float x, y, z;
- if (IMU.accelerationAvailable()) {
- IMU.readAcceleration(x, y, z); // read values for float variables
- if(digitalRead(BUTTON_PIN)==LOW) {
- delay(1000); // delay loop by 1 second
- Serial.println("Button pressed!");
- //if button simulated send values for x y z
- String x_axis = "?value1=";
- x_axis += x;
- String y_axis = "?value2=";
- y_axis += y;
- String z_axis = "?value3=";
- z_axis += z;
- sendIFTTT("accel_readings", x_axis, y_axis, z_axis);
- }
- }
- }
- // String x_axis, String y_axis, String z_axis
- // x_axis + y_axis + z_axis
- void sendIFTTT(String accel_readings, String x_axis, String y_axis, String z_axis) { // passing in two parameters
- Serial.println("Starting connection to server...");
- // if you get a connection, report back via serial:
- if (client.connect(server_name, 80)) { // 80 = standard http port
- Serial.println("Connected to server");
- // Make a HTTP GET request:
- client.print(String("GET ") + "/trigger/" + accel_readings + "/with/key/" + apiKey + x_axis + y_axis + z_axis); // this string much match http string from webhook application Documentation
- client.println(" HTTP/1.1"); // type of http request
- client.println("Host: maker.ifttt.com");
- client.println("Connection: close");
- client.println(); //It is incredibly important to have the blank println statement after
- //close request
- delay(500);
- client.flush();
- client.stop();
- }
- // if unable to connect
- else {
- Serial.println("unable to connect");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement