How to send data from qt to arduino by i2c
-
I wrote the code,
MainWindow.CPP
void MainWindow::settemp() { Pi2c arduino(7); QSqlQuery q; QString Data; q.prepare("select * from Temp where sno=1"); q.exec(); while(q.next()) { Data=q.value(1).toString(); } QString code=Data; QByteArray string = code.toLatin1(); char * strdata = string.data(); arduino.i2cWrite(strdata , 30); }
Retrieves data from database.
Initially i set Data = 25;Arduino.code
#include "Adafruit_Thermal.h" #include "SoftwareSerial.h" #include <Wire.h> #define TX_PIN 4 // Arduino transmit YELLOW WIRE labeled RX on printer #define RX_PIN 3 // Arduino receive GREEN WIRE labeled TX on printer SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor float data; int Incu_ThermistorPin = A1; int Read_ThermistorPin = A0; int Vo; float R1 = 10000; float logR2, R2, IT, RT, I_Tc, R_Tc, I_Tf, R_Tf; float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07; void setup() { Wire.begin(0x07); //Set Arduino up as an I2C slave at address 0x07 Wire.onRequest(requestEvent); //Prepare to send data Wire.onReceive(receiveEvent); //Prepare to recieve data } void loop() { Wire.begin(0x07); Vo = analogRead(Incu_ThermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); logR2 = log(R2); IT = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); I_Tc = IT - 273.15; I_Tf = (I_Tc * 9.0) / 5.0 + 32.0; if (I_Tc < data) { analogWrite(6, 250); } else { analogWrite(6, 0); } delay(100); Vo = analogRead(Read_ThermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); logR2 = log(R2); RT = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); R_Tc = RT - 273.15; R_Tf = (R_Tc * 9.0) / 5.0 + 32.0; if (R_Tc < data) { analogWrite(5, 250); } else { analogWrite(5, 0); } delay(100); } void requestEvent() { String s1 = String(R_Tc, 1); String s2 = String(I_Tc, 1); String all = s1 + " " + s2; char buf[30]; all.toCharArray(buf, 30); Wire.write(buf, 30); } void receiveEvent(int numBytes) { char rc[30] = ""; int count = 0; while (Wire.available()) { char c = Wire.read(); rc[count] = c; count++; } data = atof(rc); }
How to get the temperature value I input in qt in arduino,
where i'm doing wrong ..Thanks in advance..
-
I wrote the code,
MainWindow.CPP
void MainWindow::settemp() { Pi2c arduino(7); QSqlQuery q; QString Data; q.prepare("select * from Temp where sno=1"); q.exec(); while(q.next()) { Data=q.value(1).toString(); } QString code=Data; QByteArray string = code.toLatin1(); char * strdata = string.data(); arduino.i2cWrite(strdata , 30); }
Retrieves data from database.
Initially i set Data = 25;Arduino.code
#include "Adafruit_Thermal.h" #include "SoftwareSerial.h" #include <Wire.h> #define TX_PIN 4 // Arduino transmit YELLOW WIRE labeled RX on printer #define RX_PIN 3 // Arduino receive GREEN WIRE labeled TX on printer SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor float data; int Incu_ThermistorPin = A1; int Read_ThermistorPin = A0; int Vo; float R1 = 10000; float logR2, R2, IT, RT, I_Tc, R_Tc, I_Tf, R_Tf; float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07; void setup() { Wire.begin(0x07); //Set Arduino up as an I2C slave at address 0x07 Wire.onRequest(requestEvent); //Prepare to send data Wire.onReceive(receiveEvent); //Prepare to recieve data } void loop() { Wire.begin(0x07); Vo = analogRead(Incu_ThermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); logR2 = log(R2); IT = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); I_Tc = IT - 273.15; I_Tf = (I_Tc * 9.0) / 5.0 + 32.0; if (I_Tc < data) { analogWrite(6, 250); } else { analogWrite(6, 0); } delay(100); Vo = analogRead(Read_ThermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); logR2 = log(R2); RT = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); R_Tc = RT - 273.15; R_Tf = (R_Tc * 9.0) / 5.0 + 32.0; if (R_Tc < data) { analogWrite(5, 250); } else { analogWrite(5, 0); } delay(100); } void requestEvent() { String s1 = String(R_Tc, 1); String s2 = String(I_Tc, 1); String all = s1 + " " + s2; char buf[30]; all.toCharArray(buf, 30); Wire.write(buf, 30); } void receiveEvent(int numBytes) { char rc[30] = ""; int count = 0; while (Wire.available()) { char c = Wire.read(); rc[count] = c; count++; } data = atof(rc); }
How to get the temperature value I input in qt in arduino,
where i'm doing wrong ..Thanks in advance..
@Ramkumar-Mohan said in How to send data from qt to arduino by i2c:
arduino.i2cWrite(strdata , 30);
What is "arduino" here?
I don't really see a relation to Qt. -
I wrote the code,
MainWindow.CPP
void MainWindow::settemp() { Pi2c arduino(7); QSqlQuery q; QString Data; q.prepare("select * from Temp where sno=1"); q.exec(); while(q.next()) { Data=q.value(1).toString(); } QString code=Data; QByteArray string = code.toLatin1(); char * strdata = string.data(); arduino.i2cWrite(strdata , 30); }
Retrieves data from database.
Initially i set Data = 25;Arduino.code
#include "Adafruit_Thermal.h" #include "SoftwareSerial.h" #include <Wire.h> #define TX_PIN 4 // Arduino transmit YELLOW WIRE labeled RX on printer #define RX_PIN 3 // Arduino receive GREEN WIRE labeled TX on printer SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor float data; int Incu_ThermistorPin = A1; int Read_ThermistorPin = A0; int Vo; float R1 = 10000; float logR2, R2, IT, RT, I_Tc, R_Tc, I_Tf, R_Tf; float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07; void setup() { Wire.begin(0x07); //Set Arduino up as an I2C slave at address 0x07 Wire.onRequest(requestEvent); //Prepare to send data Wire.onReceive(receiveEvent); //Prepare to recieve data } void loop() { Wire.begin(0x07); Vo = analogRead(Incu_ThermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); logR2 = log(R2); IT = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); I_Tc = IT - 273.15; I_Tf = (I_Tc * 9.0) / 5.0 + 32.0; if (I_Tc < data) { analogWrite(6, 250); } else { analogWrite(6, 0); } delay(100); Vo = analogRead(Read_ThermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); logR2 = log(R2); RT = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); R_Tc = RT - 273.15; R_Tf = (R_Tc * 9.0) / 5.0 + 32.0; if (R_Tc < data) { analogWrite(5, 250); } else { analogWrite(5, 0); } delay(100); } void requestEvent() { String s1 = String(R_Tc, 1); String s2 = String(I_Tc, 1); String all = s1 + " " + s2; char buf[30]; all.toCharArray(buf, 30); Wire.write(buf, 30); } void receiveEvent(int numBytes) { char rc[30] = ""; int count = 0; while (Wire.available()) { char c = Wire.read(); rc[count] = c; count++; } data = atof(rc); }
How to get the temperature value I input in qt in arduino,
where i'm doing wrong ..Thanks in advance..
@Ramkumar-Mohan
As @jsulm says I don't see anything much about Qt in your code. We don't know how your arduino thing works, that's up to you/its documentation. Nor do you give any indication of what is "going wrong" with what you have --- we are not mind readers! Surely the first thing to do is see whether the query returns whatever you expect versus whether your "parsing" of that is at issue?I will just comment: unless the "Wire" sends a terminating
\0
on its data yourreceiveEvents()
call todata = atof(rc)
may "overflow" when trying to convert to a float there. Also if that function takes anint numBytes
parameter I wonder if you're supposed to use that in the code....Also is
requestEvent()
supposed to send 30 characters if the built string is shorter than that?Only you know these things....