writing to multiple fields of

8 views (last 30 days)
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 Comments
BEHNAM MOBARAKI .
BEHNAM MOBARAKI . on 27 May 2021
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.

Sign in to comment.

Accepted Answer

Vinod
Vinod on 27 May 2021
Edited: Vinod on 27 May 2021
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 Comment
BEHNAM MOBARAKI .
BEHNAM MOBARAKI . on 1 Jun 2021
Dear Vinod,
Thank you. It is working righ tnow!

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

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

Start Hunting!