So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By bukaida
#84291 I was going through this tutorial ESP8266 and MPU6050 with python and configured my ESP8266 and MPU 6050. The .ino file once uploaded could connect with my home WiFi router after separating it from PC and connecting it to a power bank. Their example python program worked fine.
I got the following example code for accepting and displaying the output of MPU6050 on serial monitor by connecting the combined unit via USB using Arduino IDE. The complete sketch is

Code: Select all#include <Wire.h>

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

// Select SDA and SCL pins for I2C communication
const uint8_t scl = D6;
const uint8_t sda = D7;

// 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;

void setup() {
  Wire.begin(sda, scl);

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

  Serial.print("Ax: "); Serial.print(Ax);
  Serial.print(" Ay: "); Serial.print(Ay);
  Serial.print(" Az: "); Serial.print(Az);
  Serial.print(" T: "); Serial.print(T);
  Serial.print(" Gx: "); Serial.print(Gx);
  Serial.print(" Gy: "); Serial.print(Gy);
  Serial.print(" Gz: "); Serial.println(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);

However this is completely Arduino IDE dependent and requires the module to be attached via USB cable, with the PC.
A wiFi connectivity as obtained from the tutorial is given below. Similar approach is required for my project.
Now the example python reader code is
Code: Select allimport urllib.request
url = ""  # MY ESP's url, ex: https://192.168.102/ (Esp serial prints it when connected to wifi)

def get_data():
   global data

   n = urllib.request.urlopen(url).read() # get the raw html data in bytes (sends request and warn our esp8266)
   n = n.decode("utf-8") # convert raw html bytes format to string :3
   data = n
#   data = n.split()          #<optional> split datas we got. (if you programmed it to send more than one value) It splits them into seperate list elements.
#   data = list(map(int, data)) #<optional> turn datas to integers, now all list elements are integers.

# Example usage
while True:
   print("Your data(s) which we received from arduino: "+data)
   input("To test it again press enter.")

And the working sketch for the above python code is
Code: Select all/* It simply increases and returns index var everytime a python req came */

#include "ESP_MICRO.h"

int indeks = 0;

void setup(){
  start("WiFi SSID","WiFi Password");  // Wifi details connect to

void loop(){
  waitUntilNewReq();  //Waits until a new request from python come

  /* increases index when a new request came*/
  indeks += 1;

  returnThisInt(indeks); //Returns the data to python

Now how can I make the combined wired sketch code to return the values as a string [JSON would be best] like the second method. The micro library file "ESP_MICRO.h" is attached and also can be downloaded from the Tutorial link provided at the beginning. Kindly help.
You do not have the required permissions to view the files attached to this post.