0% found this document useful (0 votes)
34 views

LDRTest2 Ino

The document contains code for an ESP8266 microcontroller to connect to WiFi, read light sensor values from three positions, and upload the sensor data to a MySQL database hosted online. It defines functions for reading the light sensor, calculating lux values, connecting to WiFi, and making HTTP requests to upload the data. The main loop reads the sensors sequentially, calculates lux and raw analog values, and calls the upload function to send the data to the remote database.

Uploaded by

Ellima Chu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

LDRTest2 Ino

The document contains code for an ESP8266 microcontroller to connect to WiFi, read light sensor values from three positions, and upload the sensor data to a MySQL database hosted online. It defines functions for reading the light sensor, calculating lux values, connecting to WiFi, and making HTTP requests to upload the data. The main loop reads the sensors sequentially, calculates lux and raw analog values, and calls the upload function to send the data to the remote database.

Uploaded by

Ellima Chu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <ESP8266WiFi.

h>
#include <ESP8266HTTPClient.h>

// Update HOST URL here


// Enter HOST URL without "http:// " and "/" at the end of URL
#define HOST "bokchoy.cloud"

// WIFI SSID here


#define WIFI_SSID "Bautista Wifi"
// WIFI password here
#define WIFI_PASSWORD "qGhqMuPD"

//int sensorPin = A0;


// const int sM1 = 14; // Digital 5 Bottom
// const int sM2 = 12; // Digital 6 Middle
// const int sM3 = 13; // Digital 7 Top
int sldr[3] = {14, 12, 13};

// Do not forget, pick from 1-9


int sensorCode = ;

// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;

// Define the GPIO pin for the relay


const int relayPin = D1;

int aRead;

void setup() {
// Connect to the internet and get the current time
configTime(0, 0, "pool.ntp.org");
setTime(0);

// Set the relay pin as an output


pinMode(relayPin, OUTPUT);

Serial.begin(115200);
Serial.println("Communication Started \n\n");
delay(1000);

// initialize built in led on the board


pinMode(LED_BUILTIN, OUTPUT);

WiFi.mode(WIFI_STA);
//try to connect with wifi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
//print local IP address
Serial.println(WiFi.localIP());

delay(30);

for (int i = 0; i < 3; i++) {


pinMode(sldr[i], OUTPUT);
digitalWrite(sldr[i], LOW);
}
}

int aDelay = 500; // Analog Delay


void loop() {

String pos[3];
float lux[3];
float aval[3];
for (int i = 0; i < 3; i++) {
String position = (i==0) ? "Bottom" : (i==1) ? "Middle" : "Top";
pos[i] = position;
digitalWrite(sldr[i], HIGH);
delay(aDelay);
Serial.print(position + ": ");
lux[i] = read();
Serial.print(lux[i]);
Serial.print(" Lux - ");
aval[i] = aRead;
Serial.println(aval[i]);
digitalWrite(sldr[i], LOW);
delay(aDelay);

// Get the current time


int hour = hour();
int minute = minute();

// Turn on the relay at 6 pm


if (hour == 18 && minute == 0) {
digitalWrite(relayPin, HIGH);
}

// Turn off the relay at 10 pm


if (hour == 22 && minute == 0) {
digitalWrite(relayPin, LOW);
}
// Delay for one minute before checking the time again
delay(60000);

uploadToDB(sensorCode, pos, lux, aval);

Serial.println("");
delay(5000);
}

//Upload to Database
bool uploadToDB(int ldrno, String pos[3], float lux[3], float aval[3]) {
// http object of clas HTTPClient
HTTPClient http;

String postData = "AccessKey=USER0&SensorCode=SYS" + String(ldrno) + "&" +


pos[0] + "=" + String(lux[0]) + "&" +
pos[1] + "=" + String(lux[1]) + "&" +
pos[2] + "=" + String(lux[2]) + "&" +
"Field1=" + String(aval[0]) + "&" +
"Field2=" + String(aval[1]) + "&" +
"Field3=" + String(aval[2]);

// Connect to host where MySQL databse is hosted


http.begin("http://logic.bokchoy.cloud/db/ldr/arduino_write_ldr" + String(ldrno) + ".php");
//Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");

// Send POST request to php file and store server response code in variable named httpCode
int httpCode = http.POST(postData);

// if connection eatablished then do this


if (httpCode == 200) {
Serial.println("Values uploaded successfully.");
Serial.println(httpCode);
// Get html webpage output and store it in a string
String webpage = http.getString();
Serial.println(webpage + "\n");
}

// if failed to connect then return and restart

else {
Serial.println(httpCode);
Serial.println("Failed to upload values. \n");
http.end();
}

delay(aDelay);
digitalWrite(LED_BUILTIN, LOW);
delay(aDelay);
digitalWrite(LED_BUILTIN, HIGH);
}

// Test Read to Eliminate Error Reads


float read() {
float lux;
for (int i = 0; i < 2; i++) {
aRead = analogRead(A0);
lux = getLux(aRead);
if (isdigit(lux))
return lux;
delay(100);
}
return lux;
}

// Get the LDR Lux value


float getLux(int analogValue) {
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy