Python Forum
assign the variable to every value received serial from Arduino
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assign the variable to every value received serial from Arduino
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for the reply,
Any kind of useful example which help?
Reply
#4
Working with lists

show your code so far if you want more specific help
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,027 Aug-24-2023, 07:56 AM
Last Post: gowb0w
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,084 Mar-13-2023, 04:02 PM
Last Post: deanhystad
Question How to understand the received bytes of ser.read? pf2022 3 1,979 Mar-24-2022, 11:37 AM
Last Post: pf2022
  serial connection to Arduino Jack9 4 2,480 Oct-22-2021, 10:18 AM
Last Post: Jack9
  Attribute Error received not understood (Please Help) crocolicious 5 2,689 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,365 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  Serial loopback with Arduino doesn't work ThomasS 3 2,787 Sep-19-2020, 12:47 PM
Last Post: deanhystad
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,268 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,361 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  Invalid JSON payload received. Unknown name “”: Root element must be a message." hellraiser 4 13,265 Aug-18-2019, 03:41 PM
Last Post: hellraiser

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020