How to send continuous data to a server using esp8266-12e

Hello everyone,

I’m making a project with esp8266 and a mpu sensor where I record data with the mpu and send it wirelessly to a server using esp8266. I was able to create this server and send data to it, but I don’t know how to continuous send the data. It only runs once and then it stops. Any suggestion on what I should add to my code? I want to loop my read_sensor function so that it keeps reading the sensor and sending the data over.

#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server;

// MPU6050 Slave Device Address
const uint8_t MPU6050SlaveAddress = 0x68;

// Select SDA and SCL pins for I2C communication 
const uint8_t scl = 12;
const uint8_t sda = 13;

// sensitivity scale factor respective to full scale setting provided in datasheet 
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;

// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV   =  0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL    =  0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1   =  0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2   =  0x6C;
const uint8_t MPU6050_REGISTER_CONFIG       =  0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG  =  0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG =  0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN      =  0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE   =  0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H =  0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET  = 0x68;

int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;

char* ssid = "*****";
char* password = "**********";

void setup() 
  Serial.print("IP Address: ");

  Wire.begin(sda, scl);

void loop()

void read_sensor() 
  Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
  //divide each with their sensitivity scale factor
  String Ax = "Ax:" + String(AccelX/AccelScaleFactor);
  String Ay = " Ay:" + String(AccelY/AccelScaleFactor);
  String Az = " Az:" + String(AccelZ/AccelScaleFactor);
  String T = " T:" + String(Temperature/340+36.53); //temperature formula
  String Gx = " Gx:" + String(GyroX/GyroScaleFactor);
  String Gy = " Gy:" + String(GyroY/GyroScaleFactor);
  String Gz = " Gz:" + String(GyroZ/GyroScaleFactor);

  server.send(200,"text/plain", Ax + Ay + Az + Gx + Gy + Gz);

void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){

// read all 14 register
void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
  Wire.requestFrom(deviceAddress, (uint8_t)14);
  AccelX = (((int16_t)<<8) |;
  AccelY = (((int16_t)<<8) |;
  AccelZ = (((int16_t)<<8) |;
  Temperature = (((int16_t)<<8) |;
  GyroX = (((int16_t)<<8) |;
  GyroY = (((int16_t)<<8) |;
  GyroZ = (((int16_t)<<8) |;

//configure MPU6050
void MPU6050_Init(){
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
  I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00);

I'm making a project with esp8266 and a mpu sensor where I record data with the mpu and send it wirelessly to a server using esp8266.

can you clarify what you mean by "send it wirelessly to a server"?

You are not doing that at all.

it seems that you have created a web server that will wait for an HTTP connection and send upon request (for /) the reading to a web-client. if your web client is a web browser, just hit refresh and you'll get the next reading.

Alternatively create a web page that display the information and include a refresh so that the web page periodically asks for the data again.