writing to multiple fields of

조회 수: 2 (최근 30일)
BEHNAM MOBARAKI .
BEHNAM MOBARAKI . 2021년 5월 27일
댓글: BEHNAM MOBARAKI . 2021년 6월 1일
Hi.
I have to modify my question.
I am sending temperature data from 2 sensor installed on NODEMCU ESP8266. (I am using I2C comunication). Each sensor data is being sent to a single field of a channnel. On the serial monitor of Arduino IDE everything is fine. However, data of the fields are not the same of those in serial monitor. For installation of the these sensor on NODEMCU, I have used multiplexet TCA9548 and both sensors are defined on ports 0 and 1 ( tcaselect(0) and tcaselect(1)).
By these two lines of the codes, I am trying to send the temperatures on two fields of ThingSpeak:
ThingSpeak.writeField(myChannelNumber, 1, String(mlx1.readAmbientTempC()), apiKey);
ThingSpeak.writeField(myChannelNumber, 2, String(mlx2.readAmbientTempC()), apiKey);
I do not understand where the problem is. Kindly check the below code and let me know your idea.....
#include <SPI.h> //Load SPI comunication library
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <SoftwareSerial.h> // import the serial library
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
Adafruit_MLX90614 mlx1 = Adafruit_MLX90614();
Adafruit_MLX90614 mlx2 = Adafruit_MLX90614();
const char * apiKey = "S9GZsefweszxcQ21XP"; // Enter your Write API key from ThingSpeak
unsigned long myChannelNumber = 123456789;
const char *ssid = "iBehnam"; // replace with your wifi ssid and wpa2 key
const char *pass = "behnam555";
const char* server = "api.thingspeak.com";
#define TCAADDR0 0x70 // you need to conec nothing
WiFiClient client;
//OPENING THE FIRST MUX IN THE PORTS
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR0);
Wire.write(1 << i);
Wire.endTransmission();
}
////////////CLOSING THE FIRST MUX IN THE PORTS
void endtcaselect(){
Wire.beginTransmission(TCAADDR0);
Wire.write(0); // no channel selected
Wire.endTransmission();
}
void setup() {
// put your setup code here, to run once:
Serial.println("Adafruit MLX90614 test");
Serial.begin(115200);
ThingSpeak.begin(client);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
//*************INITIALIZING FIRST SENSOR MUX1*******************************
endtcaselect();
tcaselect(0);
mlx1.begin();
endtcaselect();
//*************INITIALIZING 2nd SENSOR MUX1*******************************
tcaselect(1);
mlx2.begin();
endtcaselect();
//*************INITIALIZING 2nd SENSOR MUX1*******************************
// tcaselect(2);
// mlx2.begin();
// endtcaselect();
}
void loop() {
// tcaselect(3);
// Send Day-of-Week
Serial.print("HEAT-SET--6 ");
if (client.connect(server,80)) // api.thingspeak.com
{
ThingSpeak.writeField(myChannelNumber, 1, String(mlx1.readAmbientTempC()), apiKey);
ThingSpeak.writeField(myChannelNumber, 2, String(mlx2.readAmbientTempC()), apiKey);
//*************INITIALIZING 2nd SENSOR MUX1*******************************
endtcaselect();
tcaselect(0);
Serial.print("Amb-1 = ");
Serial.print(mlx1.readAmbientTempC());
Serial.print("*C\tObj-1 = ");
Serial.print(mlx1.readObjectTempC());
Serial.print("*C ");
endtcaselect();
tcaselect(1);
Serial.print(" Amb-2 = ");
Serial.print(mlx2.readAmbientTempC());
Serial.print("*C Obj-2 = ");
Serial.print(mlx2.readObjectTempC());
Serial.println("*C ");
}
client.stop();
Serial.println("Waiting...");
delay(60000); // delay 1 minute
}
  댓글 수: 3
Christopher Stapels
Christopher Stapels 2021년 5월 27일
Adafruit_MLX90614 mlx1 = Adafruit_MLX90614();
Adafruit_MLX90614 mlx2 = Adafruit_MLX90614();
Adafruit_MLX90614 mlx3 = Adafruit_MLX90614();
The declarations look identical. How does your program know them apart? Are the pre addresssed?
BEHNAM MOBARAKI .
BEHNAM MOBARAKI . 2021년 5월 27일
I have modified my post. Right now is clearer. Kindly read it again and let me know your idea.
Regarding your question, As I have defined the first sensor (mlx1) on port-0 of Multiplexer (tcaselect(0)) and the second sensor (mlx2) on port-1 of Multiplexer (tcaselect(1)), they are working seperatelly. The temperatures on serial monitor of Arduino IDE are correct. The problem is that data are getting transfer to the fields of ThingSpeak wrongly.

댓글을 달려면 로그인하십시오.

채택된 답변

Vinod
Vinod 2021년 5월 27일
편집: Vinod 2021년 5월 27일
Try and log the values to the serial monitor. Do you see different values for each sensor?
Also, maybe your code needs to be like this:
endtcaselect();
tcaselect(0);
Serial.print("Amb-1 = ");
Serial.print(mlx1.readAmbientTempC());
Serial.print("*C\tObj-1 = ");
Serial.print(mlx1.readObjectTempC());
Serial.print("*C ");
ThingSpeak.setField(1, String(mlx1.readAmbientTempC()));
endtcaselect();
tcaselect(1);
Serial.print(" Amb-2 = ");
Serial.print(mlx2.readAmbientTempC());
Serial.print("*C Obj-2 = ");
Serial.print(mlx2.readObjectTempC());
Serial.println("*C ");
ThingSpeak.setField(2, String(mlx2.readAmbientTempC()));
ThingSpeak.writeFields(myChannelNumber, apiKey);
...
  댓글 수: 1
BEHNAM MOBARAKI .
BEHNAM MOBARAKI . 2021년 6월 1일
Dear Vinod,
Thank you. It is working righ tnow!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Read Data from Channel에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by