Python Forum

Full Version: assign the variable to every value received serial from Arduino
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am continuously getting the output of the temperature sensor from Arduino is like

28.92
64.26
93.77
and I want to assign the variable to these values.
Need suggestion about how can I be able to assign the variable to these values?

Will be very helpful.
First of all, it's the other way around - you assign values to variables, not variables to values.
Second, because it's a constant stream of data, it's better to use some sort of data structure - e.g. list to store data into memory in a variable.
You may want to store data simultaneously in a database or write to file
Thanks for the reply,
Any kind of useful example which help?
Working with lists

show your code so far if you want more specific help
Hi, Mentioned below is my "Embedded C code" for temperature sensor connected to Arduino
#include<Wire.h>

// SHT25 I2C address is 0x40(64)
#define Addr 0x40

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);
  delay(300);
}

void loop()
{
  unsigned int data[2];
  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Send humidity measurement command, NO HOLD master
  Wire.write(0xF5);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data
  // humidity msb, humidity lsb
  if(Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();

    // Convert the data
    float humidity = (((data[0] * 256.0 + data[1]) * 125.0) / 65536.0) - 6;

    // Output data to Serial Monitor
    Serial.println(humidity);

  }

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Send temperature measurement command, NO HOLD master
  Wire.write(0xF3);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data
  // temp msb, temp lsb
  if(Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();

    // Convert the data
    float cTemp = (((data[0] * 256.0 + data[1]) * 175.72) / 65536.0) - 46.85;
    float fTemp = (cTemp * 1.8) + 32;

    // Output data to Serial Monitor
    Serial.println(cTemp);
    Serial.println(fTemp);
  }
  delay(300);
}
And This is My python code which I am using in my Laptop to receive the serial data:

import serial

ard = serial.Serial('COM4', 9600);
while True:
    k = ard.readline().decode('ascii') 
    print(k)


and my output is continuously mentioned as

20.79


69.42


90.01