Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
Internet of Things Project - Lucy Hemingway 3CB105
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lucy.hemingway
Internet of Things Project - Lucy Hemingway 3CB105
Commits
ec12e7ce
Commit
ec12e7ce
authored
Jan 16, 2022
by
lucy.hemingway
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
142 additions
and
0 deletions
+142
-0
Code for IoT Device
Code for IoT Device
+142
-0
No files found.
Code for IoT Device
0 → 100644
View file @
ec12e7ce
//pins used for sensors
#define trigPin 12
#define echoPin 13
#define fsrpin A1
#include <SPI.h>
#include <WiFiNINA.h>
#include <ThingSpeak.h>
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth (0,1);
int redLed = 2; // Set green LED to pin 3, red to pin 2
int fsrreading = 0;
int bluetoothinput;
// wifi connection
// with reference to The Internet of Things 3CB105 Week 6 Lecture "Arduino Uno WiFi connection"
const char ssid[] = "TALKTALKE2531E";
const char pass[] = "WB8WH3H7";
const unsigned long channel_id = 1592248;
const char write_api_key[] = "VN2B96JBUY6HUPX7";
WiFiClient client;
void setup() {
int status = WL_IDLE_STATUS;
Bluetooth.begin (9600);
Bluetooth.println("Attempting to connect.");
status = WiFi.begin(ssid,pass);
if (status != WL_CONNECTED) {
Bluetooth.println("Not connected, try again.");
while (true);
}
else{
Bluetooth.print("Connected to ");
Bluetooth.println(ssid);
Bluetooth.print("with IP address: ");
Bluetooth.println(WiFi.localIP());
ThingSpeak.begin(client);
}
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLed, OUTPUT);
Bluetooth.println ("Bluetooth connected, please press 0 to turn off, or press 1 to get last reading: ");
Bluetooth.println("Distance and Force readings from your device: ");
}
void loop() {
delay(500);
//bluetooth connection - with reference to https://gist.github.com/ashinjohn/9fcc9362dc4787ae08c030c2e7401f4c - accessed on 11/12/2021
if (Bluetooth.available()){
bluetoothinput = Bluetooth.read();
if (bluetoothinput == '0'){
Bluetooth.println("Device turned off, please reset device to start again");
exit(0);
}
}
// programmed Ultrsonic sensor with reference to - https://create.arduino.cc/projecthub/MisterBotBreak/how-to-use-an-ultrasonic-sensor-181cee
// code to turn on and off the sound and work out the distance by multiplying by 0.034 and dividing by 2
delay(100);
int duration, distance = 0, i;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Trig pin sends a ping
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Echo receives the ping
//distance = (duration / 2) / 29.1;
distance = duration * 0.034 / 2;
Bluetooth.println("");
Bluetooth.println("Distance = " + String(distance) ); //print the distance
if (distance > 10){
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
Bluetooth.println("Sit back!");
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
}
//FSR code with reference to - https://www.makerguides.com/fsr-arduino-tutorial/
fsrreading = analogRead(fsrpin); //read pin
Bluetooth.println("Force Reading = " + String(fsrreading)); //print the force
// show different wording depending on the force read to compare different levels
// the wording has now beeen shortened so not much is shown on the tera term unless important
// readings starting at 90 as there is a slight amount of pressure felt by the fsr with no touch.
if (fsrreading <= 90) {
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
Bluetooth.println("Not being touched! Sort out your posture");
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
}
Bluetooth.println("Posting " + String(fsrreading) + " to ThingSpeak"); // declare posting pressure to thingspeak
ThingSpeak.setField(2, String(fsrreading));
ThingSpeak.writeFields(channel_id,write_api_key); //post to thingspeak
// If sensor detects object further than 8 cm or a force less than 200 then the LED will turn on
// to show that the posture is not good
if (distance >= 10 || fsrreading <= 90) {
digitalWrite(redLed, HIGH); // Turn on red LED
delay(500);
}
// else
else {
digitalWrite(redLed, LOW); // Turn off red LED
}
delay(2000); //delay before collecting the next set of data
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment