From 8dd2dbd9f276a017dc8de80521cb98df01d9764e Mon Sep 17 00:00:00 2001
From: Dery Rahman Ahaddienata <13515097@std.stei.itb.ac.id>
Date: Sun, 12 Nov 2017 14:11:09 +0700
Subject: [PATCH] Add wifi module
---
earthquake/earthquake.ino | 341 ++++++++++++++++++++++----------------
1 file changed, 196 insertions(+), 145 deletions(-)
diff --git a/earthquake/earthquake.ino b/earthquake/earthquake.ino
index be4bab1..89eb10a 100644
--- a/earthquake/earthquake.ino
+++ b/earthquake/earthquake.ino
@@ -5,40 +5,52 @@
#include "Wire.h"
#endif
-int inPin = 8; // the number of the input pin
-int outPin = 12; // the number of the output pin
+/*--------------------
+ * WiFi Configuration
+ *--------------------*/
-int state = HIGH; // the current state of the output pin
-int reading; // the current reading from the input pin
-int previous = LOW; // the previous reading from the input pin
+#define SSID "hm" //SSID
+#define PASS "abcdefgh" //Password
-// the follow variables are long's because the time, measured in miliseconds,
-// will quickly become a bigger number than can be stored in an int.
-long time = 0; // the last time the output pin was toggled
-long debounce = 200; // the debounce time, increase if the output flickers
+/*--------------------
+ * Pin const
+ *--------------------*/
-//LCD
-LiquidCrystal lcd(0, 1, 8, 9, 10, 11); /// REGISTER SELECT PIN,ENABLE PIN,D4 PIN,D5 PIN, D6 PIN, D7 PIN
+// Toggle button
+const int inPin = 8; // the number of the input pin
+const int outPin = 12; // the number of the output pin
+const int ledPin = 5; // LED connected to digital pin 5
-//SEVEN SEGMENT
-const int latchPin = 7; //to IC pin 12
-const int dataPin = 3; //to IC pin 14
-const int clockPin = 4; //to IC pin 11
+// 7 segment
+const int latchPin = 7; //to IC pin 12
+const int dataPin = 3; //to IC pin 14
+const int clockPin = 4; //to IC pin 11
const int seismicPin = A0;
+
+/*--------------------
+ * Global variable
+ * --------------------*/
+
+// Toggle button
+int state = HIGH; // the current state of the output pin
+int reading; // the current reading from the input pin
+int previous = LOW; // the previous reading from the input pin
+long time = 0; // the last time the output pin was toggled
+long debounce = 200; // the debounce time, increase if the output flickers
+
+// LCD
+// LiquidCrystal lcd(0, 1, 8, 9, 10, 11); /// REGISTER SELECT PIN,ENABLE PIN,D4 PIN,D5 PIN, D6 PIN, D7 PIN
+
+// Seven Segment
const byte digit[] = {64, 121, 36, 48, 25, 18, 2, 120, 0, 16};
byte magnitude;
int lastSeismoDetected;
int loopNumber;
-//ACCELEROMETER/GYRO
+// Gyro Sensor
MPU6050 mpu;
-#define OUTPUT_READABLE_WORLDACCEL
-#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards
-#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
-#define WAVE_TIMEOUT 100
-#define TIME_WAVE_STORE 100
-int waveDuration = 0;
-bool blinkState = false;
+#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno
+#define TIME_WAVE_STORE 100 //
// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
@@ -51,61 +63,163 @@ Quaternion q; // [w, x, y, z] quaternion container
VectorInt16 aa; // [x, y, z] accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
-VectorInt16 aaWorldPrev[TIME_WAVE_STORE]; // [x, y, z] world-frame accel sensor measurements
+VectorInt16 aaWorldPrev[TIME_WAVE_STORE];
VectorFloat gravity; // [x, y, z] gravity vector
float euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
-bool isChangeStore[TIME_WAVE_STORE];
-int periode;
-int frequency;
+bool isChangeStore[TIME_WAVE_STORE]; // Store is has a change within TIME_WAVE_STORE time before
double amplitude;
+int magnitudeC;
//INTERRUPT DETECTION ROUTINE
-volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
+volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
+
+/*--------------------
+ * Declaration
+ *--------------------*/
+
+boolean connectWiFi();
+void dmpDataReady();
+void toggleButtonInit();
+void sevenSegmentInit();
+void lcdInit();
+void gyroInit();
+void changeShift();
+bool isChange();
+double magnitudeConvert();
+void displaySevenSegment(int);
+void ledActive();
+void ledDeactive();
+void gyroOn();
+void gyroOff();
+
+void setup() {
+
+ Serial.begin(115200);
+ while(!Serial);
+ Serial.println("AT");
+
+ while(!connectWiFi());
+ Serial.println("CONNECT");
+
+ // Toggle button
+ toggleButtonInit();
+
+ // Seven Segment
+ sevenSegmentInit();
+
+ lastSeismoDetected = -10000;
+ loopNumber = 0;
+ amplitude = 0;
+
+ // LCD
+ lcdInit();
+
+ // Gyro Sensor
+ gyroInit();
+
+}
+
+void loop() {
+ loopNumber++;
+
+ // //LCD
+ // lcd.print(" circuit digest");//print name
+ // lcd.setCursor(0, 1); // set the cursor to column 0, line 2
+ // lcd.print("ic tronic");//print name
+ // delay(250);//delay of 0.75sec
+ // lcd.scrollDisplayLeft();//shifting data on LCD
+ // lcd.setCursor(0, 0);// set the cursor to column 0, line1
+
+ // int measurement = analogRead(seismicPin);
+ // float seismoMag = measurement / 2000000000.0 * 5.0;
+ // Serial.print("measurment = "); Serial.println(measurement);
+ // Serial.print("magnitude = "); Serial.println(seismoMag);
+ // if (seismoMag > 2 && loopNumber - lastSeismoDetected > 10000) {
+ // lastSeismoDetected = loopNumber;
+ // // detected
+ // // send post to http://jauhararifin.cf:8888/api/earthquakes
+ // // header : content-type = application/x-www-form-urlencoded
+ // // data = "lat=1&long=1&date=2016-12-06 PM 22:03:36.000Z&strength=" + seismoMag
+ // }
+ reading = digitalRead(inPin);
+ if (reading == HIGH && previous == LOW && millis() - time > debounce) {
+ if (state == HIGH)
+ state = LOW;
+ else
+ state = HIGH;
+ time = millis();
+ }
+ digitalWrite(outPin, state);
+ previous = reading;
+
+ if(state == HIGH){
+ gyroOn();
+ } else {
+ gyroOff();
+ }
+
+}
+
+
+/*--------------------
+ * Implementation
+ *--------------------*/
+
+boolean connectWiFi() {
+ Serial.println("AT+CWMODE=1");
+ delay(2000);
+ String cmd = "AT+CWJAP=\"";
+ cmd += SSID;
+ cmd += "\",\"";
+ cmd += PASS;
+ cmd += "\"";
+ Serial.println(cmd);
+ delay(5000);
+ if (Serial.find((char*)"OK"))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
void dmpDataReady() {
mpuInterrupt = true;
}
-void setup() {
+
+void toggleButtonInit(){
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
-
- //SEVEN SEGMENT
+}
+
+void sevenSegmentInit(){
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
magnitude = 0;
+}
- lastSeismoDetected = -10000;
- loopNumber = 0;
- periode = 0;
- frequency = 0;
- amplitude = 0;
+void lcdInit(){}
- //LCD
- lcd.begin(16, 2);
+void gyroInit(){
- //ACCELEROMETER/GYRO
+ magnitudeC = 0;
// join I2C bus (I2Cdev library doesn't do this automatically)
-#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
- Wire.begin();
- Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
-#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
- Fastwire::setup(400, true);
-#endif
- // initialize serial communication
- // (115200 chosen because it is required for Teapot Demo output, but it's
- // really up to you depending on your project)
- Serial.begin(115200);
- while (!Serial); // wait for Leonardo enumeration, others continue immediately
- // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3V or Arduino
- // Pro Mini running at 3.3V, cannot handle this baud rate reliably due to
- // the baud timing being too misaligned with processor ticks. You must use
- // 38400 or slower in these cases, or use some kind of external separate
- // crystal solution for the UART timer.
+ #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
+ Wire.begin();
+ Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
+ #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
+ Fastwire::setup(400, true);
+ #endif
+
// initialize device
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);
+
// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
@@ -113,7 +227,7 @@ void setup() {
// load and configure the DMP
Serial.println(F("Initializing DMP..."));
devStatus = mpu.dmpInitialize();
- // supply your own gyro offsets here, scaled for min sensitivity
+
mpu.setXGyroOffset(220);
mpu.setYGyroOffset(76);
mpu.setZGyroOffset(-85);
@@ -144,9 +258,6 @@ void setup() {
Serial.print(devStatus);
Serial.println(F(")"));
}
- // configure LED for output
- pinMode(LED_PIN, OUTPUT);
-
}
void changeShift() {
@@ -157,6 +268,7 @@ void changeShift() {
aaWorldPrev[TIME_WAVE_STORE-1] = aaWorld;
}
+
bool isChange(){
int offset = 200;
int i;
@@ -175,9 +287,8 @@ bool isChange(){
}
double magnitudeConvert() {
- // assume the epicentral distance is 1km
+ // assume the epicentral distance is 0.5km
return log10(amplitude)-0.83+1.73*log10(0.5);
-// return log10(amplitude)-2.48+2.76*log10(0.5);
}
void displaySevenSegment(int val){
@@ -186,47 +297,35 @@ void displaySevenSegment(int val){
digitalWrite(latchPin, HIGH);
}
-void accelerometerOn(){
- //ACCELERO/GYRO
- // if programming failed, don't try to do anything
+void ledActive(){
+ Serial.print("LED : ");
+ Serial.println(magnitudeC);
+ int fadeValue = floor(((float)magnitudeC/10)*255);
+ Serial.print("Fade Value : "); Serial.println(fadeValue);
+ analogWrite(ledPin, fadeValue);
+}
+
+void ledDeactive(){
+ analogWrite(ledPin, 0);
+}
+
+void gyroOn(){
if (!dmpReady) return;
- // wait for MPU interrupt or extra packet(s) available
- while (!mpuInterrupt && fifoCount < packetSize) {
- // other program behavior stuff here
- // .
- // .
- // .
- // if you are really paranoid you can frequently test in between other
- // stuff to see if mpuInterrupt is true, and if so, "break;" from the
- // while() loop to immediately process the MPU data
- // .
- // .
- // .
- }
- // reset interrupt flag and get INT_STATUS byte
+ while (!mpuInterrupt && fifoCount < packetSize); // wait for MPU interrupt or extra packet(s) available
+
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
- // get current FIFO count
fifoCount = mpu.getFIFOCount();
- // check for overflow (this should never happen unless our code is too inefficient)
- if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
- // reset so we can continue cleanly
+ if ((mpuIntStatus & 0x10) || fifoCount == 1024) { // check for overflow
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));
- // otherwise, check for DMP data ready interrupt (this should happen frequently)
} else if (mpuIntStatus & 0x02) {
- // wait for correct available data length, should be a VERY short wait
- while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
-
- // read a packet from FIFO
- mpu.getFIFOBytes(fifoBuffer, packetSize);
+ while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount(); // wait for correct available data length, should be a VERY short wait
- // track FIFO count here in case there is > 1 packet available
- // (this lets us immediately read more without waiting for an interrupt)
+ mpu.getFIFOBytes(fifoBuffer, packetSize); // read a packet from FIFO
fifoCount -= packetSize;
- // display initial world-frame acceleration, adjusted to remove gravity
- // and rotated based on known orientation from quaternion
+ // display initial world-frame acceleration, adjusted to remove gravity and rotated based on known orientation from quaternion
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetAccel(&aa, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
@@ -242,69 +341,21 @@ void accelerometerOn(){
// blink LED to indicate activity
changeShift();
bool check = isChange();
- digitalWrite(LED_PIN, check);
if(check){
- int magnitudeC = (int) magnitudeConvert();
+ magnitudeC = (int) magnitudeConvert();
Serial.println(amplitude);
Serial.println(magnitudeC);
displaySevenSegment(magnitudeC%10);
+ ledActive();
} else {
+ ledDeactive();
amplitude = 0;
displaySevenSegment(0);
}
}
}
-void accelerometerOff(){
+void gyroOff(){
displaySevenSegment(0);
- digitalWrite(LED_PIN, LOW);
-}
-
-void loop() {
- loopNumber++;
-
- // //SEVEN SEGMENT
- // digitalWrite(latchPin, LOW);
- // shiftOut(dataPin, clockPin, MSBFIRST, digit[9]);
- // digitalWrite(latchPin, HIGH);
- // magnitude = (magnitude+1)%10;
- // delay(250);
-
- // //LCD
- // lcd.print(" circuit digest");//print name
- // lcd.setCursor(0, 1); // set the cursor to column 0, line 2
- // lcd.print("ic tronic");//print name
- // delay(250);//delay of 0.75sec
- // lcd.scrollDisplayLeft();//shifting data on LCD
- // lcd.setCursor(0, 0);// set the cursor to column 0, line1
-
- // int measurement = analogRead(seismicPin);
- // float seismoMag = measurement / 2000000000.0 * 5.0;
- // Serial.print("measurment = "); Serial.println(measurement);
- // Serial.print("magnitude = "); Serial.println(seismoMag);
- // if (seismoMag > 2 && loopNumber - lastSeismoDetected > 10000) {
- // lastSeismoDetected = loopNumber;
- // // detected
- // // send post to http://jauhararifin.cf:8888/api/earthquakes
- // // header : content-type = application/x-www-form-urlencoded
- // // data = "lat=1&long=1&date=2016-12-06 PM 22:03:36.000Z&strength=" + seismoMag
- // }
- reading = digitalRead(inPin);
- if (reading == HIGH && previous == LOW && millis() - time > debounce) {
- if (state == HIGH)
- state = LOW;
- else
- state = HIGH;
-
- time = millis();
- }
- digitalWrite(outPin, state);
- previous = reading;
-
- if(state == HIGH){
- accelerometerOn();
- } else {
- accelerometerOff();
- }
-
+ ledDeactive();
}
--
GitLab