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

Tutorial 8

The document contains code for an Arduino project that reads light levels from a sensor and sends data to a server via Ethernet. It includes setup and loop functions to initialize the Ethernet connection and handle HTTP requests based on light levels. The code also features error handling and debugging outputs for monitoring the connection status and data transmission.

Uploaded by

nirjhormahfuj390
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tutorial 8

The document contains code for an Arduino project that reads light levels from a sensor and sends data to a server via Ethernet. It includes setup and loop functions to initialize the Ethernet connection and handle HTTP requests based on light levels. The code also features error handling and debugging outputs for monitoring the connection status and data transmission.

Uploaded by

nirjhormahfuj390
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Task:

Arduine Screenshots

PushingBox Screenshots:
Email Screenshot:
Code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {

0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED

};

// Set the static IP address

IPAddress ip(192, 168, 0, 111);

// initialize the library instance:

EthernetClient client;

char server[16] = "192.168.0.1";

char url[2000], devID[30] = "v1E76705507807C6"; //device id

#define LDRPIN A0 // define the LDR pin number

int lightlevel = 0;

boolean sendBox =false;

// last time you connected to the server, in milliseconds

unsigned long lastConnectionTime = 0;

// delay between updates, in milliseconds

long timegap = 10000UL;


void setup() {

pinMode(LDRPIN, INPUT);

// set the LDR pin on input to read from the sensor

// Ethernet.init(pin) to configure the CS pin

Ethernet.init(32);

// start serial port:

Serial.begin(57600);

Ethernet.begin(mac, ip);

Serial.print("My IP address: ");

Serial.println(Ethernet.localIP());

// give the Ethernet shield a second to initialize:

delay(1000);

void loop() {
lightlevel = 1024 - analogRead(LDRPIN); // read the light

// if there's an incoming data from the net connection,

// send it out through serial port. This is for debugging

// purposes only:

if(client.find("\r\n\r\n")) {

while (client.available()) {

char c = client.read();

Serial.write(c);

if(c == '1') {

Serial.println(" Successfully recorded");

if(lightlevel > 600) {

sendBox = true;

timegap = 1000; // send the message in 1 s

else {Serial.println(" Failed");}

}
if (millis() - lastConnectionTime > timegap) { // wait for timegap secs

if(sendBox)

httpRequest(1);

else

httpRequest(0);

// this method makes a HTTP connection to the server

void httpRequest(int todo)

Serial.println();

// close any connection before send a new request

client.stop();

// if there's a successful connection

if (client.connect(server, 80)) {

Serial.println("Connected to server");

// Make a HTTP request

memset(url, '\0', 2000);


if (todo == 0) { // send light data

sprintf(url, "GET /alacritas_direct.php?acc=ahmahfuj&call=KIT717/iot/recordtemp.php&t=%d


HTTP/1.1", lightlevel);

else if(todo == 1) { // send pushingBox message

sprintf(url, "GET /pbox_direct.php?devid=%s HTTP/1.1", devID);

sendBox = false;

timegap = 10000UL; // reset the time gap

Serial.println(url);

delay(10);

client.println(url);

client.println("Host: 192.168.0.1");

client.println("Connection: close");

client.println();

lastConnectionTime = millis();

else {

// if you couldn't make a connection

Serial.println("Connection failed");
}

You might also like