You are on page 1of 3

#include <Arduino.

h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include "DHT.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include "I2Cdev.h"
#include "MPU6050.h"

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation


// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
long loop_timer;
int lcd_loop_counter;
float angle_pitch, angle_roll;
int angle_pitch_buffer, angle_roll_buffer;
boolean set_gyro_angles;
float angle_roll_acc, angle_pitch_acc;
float angle_pitch_output, angle_roll_output;
const int MPU = 0x68;
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
//SPIClass* hspi = NULL;
float AccX, AccY, AccZ;
float GyroX, GyroY, GyroZ;
float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
float roll, pitch, yaw;
float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
float elapsedTime, currentTime, previousTime;
int c = 0;
float acc_total;
#define csn 15
#define mosi 13 //hspi
#define miso 12
#define clk 14
#define ce 1
float giatri[5] = { 1, 2, 3, 0, 0 }; // gia tri cua cam bien //
CE, CSN
const uint64_t address = 0xF0F0F0F0E1LL;
const char* ssid = "Redmi Note 11"; //replace with your SSID
const char* password = "12345678"; //replace with your password
AsyncWebServer server(80);
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
RF24 radio(1, 15); // CE, CSN
void imu_cal2() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
float cvt_ax = (float)ax / 16000.0;
float cvt_ay = (float)ay / 16000.0;
float cvt_az = (float)az / 16000.0;
cvt_ax = cvt_ax * 10;
cvt_ay = cvt_ay * 10;
cvt_az = cvt_az * 10;
acc_total = sqrt(pow(cvt_ax, 2) + pow(cvt_ay, 2) + pow(cvt_az, 2));
giatri[0] = acc_total;
giatri[3] = cvt_ax;
giatri[4] = cvt_ay;
}

bool connect_wifi = 1;
float acc_total_first;
void acc_setup() {
/*
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x6B); //Send the requested starting register
Wire.write(0x00); //Set the requested starting register
Wire.endTransmission(); //End the transmission
//Configure the accelerometer (+/-8g)
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x1C); //Send the requested starting register
Wire.write(0x10); //Set the requested starting register
Wire.endTransmission();
*/
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
accelgyro.initialize();
}
void setup(void) {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
uint16_t count_delay = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
count_delay++;
if (count_delay >= 30) {
connect_wifi = 0;
dht.begin();
acc_setup();
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
return;
}
}
Serial.println("");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
request->send(200, "text/plain", "Hello! This is a simple text message sent
from ESP8266. Microcontrollerslab");
});

AsyncElegantOTA.begin(&server); // Start ElegantOTA


server.begin();
Serial.println("HTTP server started!");
// dht.begin();
dht.begin();
acc_setup();
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
delay(20);
}

void loop(void) {
if (connect_wifi == 1) AsyncElegantOTA.loop();
static uint8_t state = 0;
static unsigned long time_ = millis();
imu_cal2();
if (millis() - time_ > 50) {
time_ = millis();

float humi = dht.readHumidity();


float temp = dht.readTemperature();
if (isnan(humi) || isnan(temp)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.println(temp); // in ra mh nhiet do
Serial.println(humi); // in ra mh do am
/// temp = 2 hum =1; angle_x=0;angle_y=3 angle_z=4;
giatri[1] = humi;
giatri[2] = temp;
}
static unsigned long time_2 = millis();
if (millis() - time_2 > 200) {
radio.write(giatri, sizeof(giatri));
time_2 = millis();
}
}

You might also like