Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Cppnetworkingtest

a code for cpp networking using arduino

Uploaded by

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

Cppnetworkingtest

a code for cpp networking using arduino

Uploaded by

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

#include <Arduino.

h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <Arduino_JSON.h>
#include "SPIFFS.h"

EthernetServer server(80);

void setup() {
// Start the Ethernet connection.
Ethernet.begin(macAddress, ipAddress, gatewayAddress, subnetMask);

// Start the web server.


server.begin();
}

void loop() {
// Wait for a client to connect.
EthernetClient client = server.available();

// If a client is connected, read their request.


if (client) {
String request = client.readStringUntil('\r');

// If the request is to open the desired webpage, send the appropriate


response.
if (request.startsWith("GET /openWebpage HTTP/1.1")) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

// Send the HTML page.


client.println("<html>");
client.println("<head>");
client.println("<title>Webpage Opener</title>");
client.println("</head>");
client.println("<body>");
client.println("<p><a href='https://www.google.com'>Open Google</a></p>");
client.println("</body>");
client.println("</html>");
}

// Close the connection.


client.stop();
}
}

// Json Variable to Hold Sensor Readings


JSONVar readings;

// Timer variables
unsigned long lastTime = 0;
unsigned long lastTimeAcc = 0;
unsigned long accelerometerDelay = 10;
// Create a sensor object
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

float ax;
float ay;
float az;
float aax;
float aay;
float aaz;
float Sumx;
float Sumy;
float Sumz;

// Init ADXL345
void initaccel(){
if (!accel.begin()) {
Serial.println("Failed to find ADXL345 chip");
while (1) {
delay(10);
}
}
Serial.println("ADXL Found!");
}

void initSPIFFS() {
if (!SPIFFS.begin()) {
Serial.println("An error has occurred while mounting SPIFFS");
}
Serial.println("SPIFFS mounted successfully");
}

server.begin();
accel.setRange(ADXL345_RANGE_4_G);

delay(10000);
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);

Sumx = 0.00; //Initialize/reset


Sumy = 0.00;
Sumz = 0.00;
//Take 1000 readings, find min, max, and average. This loop takes about 100ms.
for (float i = 0; i < 1000; i++)
{
ax = event.acceleration.x;

Sumx = Sumx + ax;//Sum for averaging

}
for (float i = 0; i < 1000; i++)
{
ay = event.acceleration.y;
Sumy = Sumy + ay;//Sum for averaging
}

for (float i = 0; i < 1000; i++)


{
az = event.acceleration.z;
Sumz = Sumz + az;//Sum for averaging

}
aax = (Sumx/1000.00);
aay = (Sumy/1000.00);
aaz = (Sumz/1000.00);

String getAccReadings() {
sensors_event_t event;
accel.getEvent(&event);
// Get current acceleration values
ax = event.acceleration.x;
ay = event.acceleration.y;
az = event.acceleration.z;

readings["ax"] = String(ax);
readings["ay"] = String(ay);
readings["az"] = String(az);

float cax = ax-aax;


float cay = ay-aay;
float caz = az-aaz;

readings["cax"] = String(cax);
readings["cay"] = String(cay);
readings["caz"] = String(caz);

String accString = JSON.stringify (readings);


return accString;
}

String cgetAccReadings() {
sensors_event_t event;
accel.getEvent(&event);
ax = event.acceleration.x;
ay = event.acceleration.y;
az = event.acceleration.z;
float cax = ax-aax;
float cay = ay-aay;
float caz = az-aaz;

readings["cax"] = String(cax);
readings["cay"] = String(cay);
readings["caz"] = String(caz);

String accString = JSON.stringify (readings);


return accString;
}

void loop() {
if ((millis() - lastTimeAcc) > accelerometerDelay) {
// Send Events to the Web Server with the Sensor Readings
events.send(getAccReadings().c_str(),"accelerometer_readings",millis());
events.send(cgetAccReadings().c_str(),"caccelerometer_readings",millis());
lastTimeAcc = millis();
}
}

You might also like