How to ultrasonic sensor reading into database

Discussion on how to get started with various type of IoT projects

How to ultrasonic sensor reading into database

Postby amirulhafiz157 » Wed Jul 08, 2020 12:35 pm

Hello, I need some help

I'm using Arduino r3 with arduino wifi shield and ultrasonic sensor
I want the ultrasonic sensor reading to be insert into database, i'm using xampp

This is my project code:
CODE: SELECT_ALL_CODE
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <CytronWiFiServer.h>
#include <SoftwareSerial.h>
#include <NewPing.h> // This library allows us to search (or "ping") for the nearest object.
 
#define TRIGGER_PIN  11  // Digital Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     10  // Digital Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. Trigger pin sends out
                                                    // the sound wave and the echo pin recieves it when it bounces back.
int thresh [6] = {40,30,20,10,1,-1}; // Create an array for thresholds. Each number in the array represents a distance.

const char *ssid = "NoMoreUniFi";
const char *pass = "rumahkub39";
IPAddress ip(192, 168, 1 ,242);
ESP8266Server server(80);

const char htmlHeader[] = "HTTP/1.1 200 OK\r\n"
                        "Content-Type: text/html\r\n"
                        "Connection: close\r\n\r\n"
                        "<!DOCTYPE HTML>\r\n"
                        "<html>\r\n";
                         
void setup() {
 
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  if(!wifi.begin(2, 3))
  {
    Serial.println(F("Error talking to shield"));
    while(1);
  }
  Serial.println(wifi.firmwareVersion());
  Serial.print(F("Mode: "));Serial.println(wifi.getMode());// 1- station mode, 2- softap mode, 3- both
  // Uncomment these 2 lines if you are using static IP Address
  // Serial.println(F("Setup wifi config"));
  // wifi.config(ip);
  Serial.println(F("Start wifi connection"));
  if(!wifi.connectAP(ssid, pass))
  {
    Serial.println(F("Error connecting to WiFi"));
    while(1);
  }
  Serial.print(F("Connected to "));Serial.println(wifi.SSID());
  Serial.println(F("IP address: "));
  Serial.println(wifi.localIP());
  wifi.updateStatus();
  Serial.println(wifi.status()); //2- wifi connected with ip, 3- got connection with servers or clients, 4- disconnect with clients or servers, 5- no wifi
  espblink(100);
  server.begin();
 
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec) so that there's time to respond.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  int distance = uS / US_ROUNDTRIP_CM; // Convert microseconds into distance in centimeters.
  Serial.print(distance); // Print distance.
  Serial.print("cm");
// if statement determines how full the bin is. If distance is between thresh[0], or 24cm, and thresh[1], or 18cm,
// the bin is only 25% full.
  if(distance<thresh[0]&&distance>=thresh[1]){
    Serial.print(", 0% Full"); 
    }
  else if(distance<thresh[1]&&distance>=thresh[2]){
    Serial.print(", 25% Full");
  }   
  else if(distance<thresh[2]&&distance>=thresh[3]){
    Serial.print(", 50% Full");
  }
  else if(distance<thresh[3]&&distance>=thresh[4]){
    Serial.print(", 75% Full");
  }
  else{
    Serial.print(", Empty"); // Empty will print if you take the sensor out of the bin or the sensor is not getting a reading
  }
  Serial.println();
  delay(1000);
  serverTest();
}
void espblink(int time)
{
  for(int i = 0;i<12;i++)
  {
    wifi.digitalWrite(2,wifi.digitalRead(2)^1);
    delay(time);
  }
}
void serverTest()
{
  ESP8266Client client = server.available();
 
  if(client.available()>0)
  {
    String req = client.readStringUntil('\r');
    // First line of HTTP request looks like "GET /path HTTP/1.1"
    // Retrieve the "/path" part by finding the spaces
    int addr_start = req.indexOf(' ');
    int addr_end = req.indexOf(' ', addr_start + 1);
    if (addr_start == -1 || addr_end == -1) {
      Serial.print(F("Invalid request: "));
      Serial.println(req);
      return;
    }
    req = req.substring(addr_start + 1, addr_end);
    Serial.print(F("Request: "));
    Serial.println(req);
    client.flush();
   
    if(req.equals("/"))
    {
      IPAddress ip = wifi.localIP();
      String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
      client.print(htmlHeader);
      String htmlBody = "Hello from ESP8266 at ";
      htmlBody += ipStr;
      htmlBody += "</html>\r\n\r\n";
      client.print(htmlBody);
    }
   
    else if(req.equals("/analog"))
    {
      client.print(htmlHeader);
      String htmlBody="";
      for (int a = 0; a < 6; a++)
      {
        htmlBody += "A";
        htmlBody += String(a);
        htmlBody += ": ";
        htmlBody += String(analogRead(a));
        htmlBody += "<br>\r\n";
      }
      htmlBody += "\r\n</html>\r\n";
      client.print(htmlBody);
    }
   
    else if(req.equals("/gpio2"))
    {
      wifi.digitalWrite(2, wifi.digitalRead(2)^1);
      client.print(htmlHeader);
      String htmlBody="GPIO2 is now ";
      htmlBody += wifi.digitalRead(2)==HIGH?"HIGH":"LOW";
      htmlBody += "</html>\r\n";
      client.print(htmlBody);
    }
   
    else if(req.equals("/info"))
    {
      String toSend = wifi.firmwareVersion();
      toSend.replace("\r\n","<br>");
      client.print(htmlHeader);
      client.print(toSend);
      client.print("</html>\r\n");
    }

    else
      client.print("HTTP/1.1 404 Not Found\r\n\r\n");
   
    client.stop();
 
  }
}


So it's possible for reading in serial monitor being transfer into database and being view in the webserver?
Thank you
amirulhafiz157
Greenhorn
 
Posts: 2
Joined: Wed Jul 08, 2020 12:20 pm

Return to Getting Started - IoT

Who is online

Users browsing this forum: No registered users and 14 guests