Johnson Quthelindazheng
Created February 17, 2019 © GPL3+

Awake

Making sure you're absolutely awake!

Advanced8 hours8
Awake

Things used in this project

Story

Read more

Code

NodeMCU Code

C/C++
How to set up a basic web server
include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

#include <WiFiUdp.h>

#include <SoftwareSerial.h>
#define Arduino_RX 4 //pin D2 (GPIO4) on NodeMCU
#define Arduino_TX 5 //pin D1 (GPIO5) on NodeMCU
//set up serial object for communication with arduino
SoftwareSerial ArduinoSerial(Arduino_RX, Arduino_TX);


const char* ssid = "Hello_IoT";
const char* password = "12345678";

// Create an instance of the server
// specify the port to listen on as an argumentii
WiFiServer server(80);
boolean connectWifi();

void setup() {
  Serial.begin(19200);
  delay(10);
  ArduinoSerial.begin(9600);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  

  //Setup WIFI mode, ssid, password
  WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint 
  WiFi.softAP("Hello_IoT", "12345678"); // Provide the (SSID, password); 
  //Setup http server
  server.begin(); // Start the HTTP Server
  IPAddress HTTPS_ServerIP= WiFi.softAPIP(); // Obtain the IP of the Server 
  Serial.print("Server IP is: "); // Print the IP to the monitor window 
  Serial.println(HTTPS_ServerIP);
  
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  client.setTimeout(100); 
      String req = client.readString();

    Serial.println("Message from client start...)");
       Serial.println(req);
   Serial.println("...Message from client end)");       

  
  // Match the request
  int val;
  if (req.indexOf("/ZERO") != -1) 
    val = 0;
  else if (req.indexOf("/ONE") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }


  client.flush();

   // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.print("received");
 
  if(val == 1) {
    client.print("One");
    ArduinoSerial.print(1);
  } else {
    client.print("Five");
    ArduinoSerial.print(5);
  }
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

Arduino code

C/C++
Responsible for the mobile app, button interface, and general functionality
//For app
#include <SoftwareSerial.h>
#define ESP8266_RX 9 //digital pin 9
#define ESP8266_TX 10 //digital pin 10
//set up serial object to communicate with NodeMCU
SoftwareSerial ESPserial(ESP8266_RX, ESP8266_TX);

//For button control
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
int dormant_signal = 2;
int d_increase_signal = 3;

//For functionality
const int trigPin = 7, echoPin = 8, speakerPin = 3;                   //trigPin is the trigger (output), the other is echo (input)
const int criticalDist = 30;                                          //comments
const int toneMin = 120, toneMax = 1500;
const int modePin = 4;                                                //high mode = use button interface
const int incrementSleepCycles = 5;
int userDelay, buttonSleepCycles, mode;                               //delay for alarm to go off

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
  Serial.println("Initialized.");
  ESPserial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(mode, INPUT);
  lcd.begin(16,2);
  lcd.setRGB(245,176,65);
  lcd.print("~ AWAKE ~");
}

void loop() {
  mode = digitalRead(modePin);

  if(mode == LOW){ //if mode low, use wifi
    userDelay = getDelay();
    delay(userDelay);
  }
  else{ //if mode high, use button
    buttonSleepCycles = getButtonDelay();
    displayTime(buttonSleepCycles);
    delay(buttonSleepCycles*1000);
  }
  
  alarm(true);

  while(1){
    if(confirm()){
      alarm(false);
      break;
    }
  }

  lcd.clear();
}

int getButtonDelay(){
  buttonSleepCycles = 0;

  displayMessage("Press button to");
  displayMessage2("increment time");
  
  while(1){
    Serial.println(buttonSleepCycles);
    if(digitalRead(incrementSleepCycles) == HIGH){
        buttonSleepCycles++;
        displayTime(buttonSleepCycles);
        delay(250);
        break;
    }
  }

  unsigned long timeStart = millis();
  unsigned long timeEnd = millis();

  while(abs(timeEnd - timeStart) < 2000){
    timeEnd = millis();
    if(digitalRead(incrementSleepCycles) == HIGH){
      buttonSleepCycles++;
      displayTime(buttonSleepCycles);
      delay(250);
      timeStart = millis();
      timeEnd = millis();
    }
    if(buttonSleepCycles == 8){
      buttonSleepCycles = 0;
      displayTime(buttonSleepCycles);
      delay(250);
    }
  }

  return buttonSleepCycles;
}

void displayTime(int sleepCycles){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.setRGB(255, 0, 0);
  lcd.print("WAKE IN:");
  lcd.setCursor(9,0);
  lcd.print(sleepCycles*1.5);
  lcd.setCursor(0,1);
  lcd.print("HOURS");
}

void displayMessage(String text){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.setRGB(255, 0, 0);
  lcd.print(text);
}

void displayMessage2(String text){
  lcd.setCursor(0,1);
  lcd.setRGB(255,0,0);
  lcd.print(text);
}

int getDelay(){
  displayMessage("WAITING FOR DATA");
  
  while(1){
    Serial.println("Waiting for data");
    
    if (ESPserial.available()){
      //set hello to the original single-digit integer that was sent by the NodeMCU
      int hello = ESPserial.read()-48; 
      Serial.print(hello); //for debugging
//    return hello*1000;
      if(hello == 1){
        return 5*1000;
      }
      if(hello == 5){
       return 10*1000;
      }
    }
  }
}

boolean confirm() {
  int counter = 0;      //Counts how many times

  Serial.println("checking 10 times");
  
  for (int a = 0; a < 10; a++) {
    Serial.print("Distance (cm) = ");
    Serial.println(getDist());
    if (getDist() < criticalDist) {
      Serial.println(counter);
      counter++;
    }
  }

  if (counter >= 8) {
    Serial.println(counter);
    return true;
  }
  return false;
}

void alarm(boolean setting){
  if(setting){
    Serial.println("WAKEUP!");
    tone(speakerPin, (toneMin+toneMax)/2);
  }
  else{
    noTone(speakerPin);
  }
}

float getDist() {
  //First turns off trigger to ensure a clean signal. Sends high to trigPin for 10e-6s. Turns off trigger
  //Records the length of the pulse in e-6s; the length of the pulse represents how long it took for the signal to reach the ultrasonic sensor again
  //pulseIn() --> times how long a pulse lasts (either HIGH or LOW, you specify) at a specified pin
  //Performs a calculation for distance in cm and returns the distance
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  float duration = pulseIn(echoPin, HIGH);
  float distanceCm = ((343 * duration) / 10000) / 2;

  return distanceCm;
}

Awake

Github

Credits

Johnson Qu

Johnson Qu

1 project • 0 followers
thelindazheng

thelindazheng

0 projects • 0 followers
Thanks to Loovee.

Comments