0% found this document useful (1 vote)
63 views

Arduino Project

This code connects an Arduino to a WiFi network, reads temperature data from a sensor, converts it to Celsius and Fahrenheit, and sends the Fahrenheit temperature to a web server using HTTP POST requests every 3 seconds. It defines the WiFi network credentials, initializes the WiFi connection, and prints out the IP address. In the loop, it reads the sensor, calculates the temperatures, builds the POST data string, connects to the server, and sends the data.

Uploaded by

Study Time
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
63 views

Arduino Project

This code connects an Arduino to a WiFi network, reads temperature data from a sensor, converts it to Celsius and Fahrenheit, and sends the Fahrenheit temperature to a web server using HTTP POST requests every 3 seconds. It defines the WiFi network credentials, initializes the WiFi connection, and prints out the IP address. In the loop, it reads the sensor, calculates the temperatures, builds the POST data string, connects to the server, and sends the data.

Uploaded by

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

#include <WiFiNINA.

h>

#define sensorPin A5

char ssid[] = "test";


char pass[] = "";

int status = WL_IDLE_STATUS;

char server[] = "www.elithecomputerguy.com";

String postData;
String postVariable = "temp=";

WiFiClient client;

void setup() {

Serial.begin(9600);

while (status != WL_CONNECTED) {


Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}

Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
IPAddress gateway = WiFi.gatewayIP();
Serial.print("IP Address: ");
Serial.println(ip);
}

void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

postData = postVariable + temperatureF;

if (client.connect(server, 80)) {
client.println("POST /test/post.php HTTP/1.1");
client.println("Host: www.elithecomputerguy.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.print(postData);
}

if (client.connected()) {
client.stop();
}
Serial.println(postData);

delay(3000);
}

void sendGET() //client function to send/receive GET request data.


{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for
connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

You might also like