Dear all,
I am trying to read some data from 2 sensors (T/RH with DHT22, Oxygen % with ME3-O2) through NodeMCU and send them to ThingSpeak on the Cloud.
I wrote a succesful code (thanks to great users help) in this other thread (Just for reference).
I am trying to read from the only analog input I have on the NodeMCU and send it to the Cloud.
Here is the code:
// IoT project for T/RH (DHT22) and Oxygen content (ME3-O2) with NodeMCU
// Libraries
#include <DHT.h> // Lib for the T/RH sensor
#include <ESP8266WiFi.h> // Lib for the Wi-Fi
#include <WiFiClient.h> // Lib for the Wi-Fi
#include <ThingSpeak.h> // Lib for the cloud https://thingspeak.com/
// // Define the DHT22 sensor variables:
#define DHTPIN D5 // Signal pin for the DHT22 sensor
#define DHTTYPE DHT22 // Define the type of sensor
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object
uint8_t t, h; // Temperature and relative humidity variables
// Define the ME3-O2 sensor variables:
float sensorValue = analogRead(A0); // Read the sensor data from the analog pin
float sensorVoltage = (sensorValue / 1024) * 5.0; // Adapt the output with a fraction of 0-5V input from Arduino Voltage. In V.
float SensorVoltageADJ = sensorVoltage / 344 * 1000; // I am getting rid of the OPAmp. We can use this in the characteristic curve provided with the sensor. In mV.
float oxyCont = SensorVoltageADJ * 20.95 / 5.90; // Calibration with reference to standard condition (linear interpolation)
// Define the Wi-Fi and Cloud variables:
const char* ssid = "MyNetwork"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "MyPassword"; // The password of the Wi-Fi network
WiFiClient client; // Create a client that can connect to a specified internet IP address and port
unsigned long myChannelNumber = MyChannelNumber; // ThingSpeak channel number
const char * myWriteAPIKey = "MYAPIkey"; // ThingSpeak API write key
//============================================
void setup()
{
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
// Connect to Wi-Fi network
Serial.println('\n');
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) // Wait for the Wi-Fi to connect
{
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
Serial.println('\n');
ThingSpeak.begin(client); // Connect to the Cloud (
}
//============================================
void loop()
{
static boolean data_state = false;
t = dht.readTemperature(); // Get the values of the temperature
h = dht.readHumidity(); // Get the values of the relative humidity
Serial.print("Temperature:");
Serial.print(t);
Serial.println("C");
Serial.print("Relative Humidity:");
Serial.print(h);
Serial.println("%");
Serial.println('\n');
Serial.print(oxyCont);
Serial.println("%");
Serial.println('\n');
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1 and then 2.
if ( data_state )
{
ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey); // Send Temperature to the Cloud
data_state = false;
}
else
{
ThingSpeak.writeField(myChannelNumber, 2, h, myWriteAPIKey); // Send Relative Humidity to the Cloud
data_state = true;
}
ThingSpeak.writeField(myChannelNumber, 3, oxyCont, myWriteAPIKey); // Send Temperature to the Cloud
delay(30000); // IMPORTANT - ThingSpeak will only accept updates every 15 seconds (FREE LICENCE)
}
I have got few questions for the community, please:
-
The code is now compiling, but perhaps may kindly someone troubleshoot it or give me a warning if you notice something that is not-appropriate please?
-
May I get the values of T/RH with some decimal digits using "float" instead of uint8_t?
-
In the loop I am trying to write on channel field 1, then IF it is written, I will write in channel field 2 and then IF it is written in 1 and 2 I will write in 3. then start from the beginning. I mean, I would like to be sure that before writing in a field, I already wrote the data in the previous one.
Is this a good idea or I could just put something like:
ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey); // Send Temperature to the Cloud
ThingSpeak.writeField(myChannelNumber, 2, h, myWriteAPIKey); // Send Relative Humidity to the Cloud
ThingSpeak.writeField(myChannelNumber, 3, oxyCont, myWriteAPIKey); // Send O2% to the Cloud
if is not too efficient (or may create issues long term...) I can I implement that condition of writing in a channel only if the available information related to the previous field have been written in the previous field?
I appreciate there are lot of questions but I am working on this for few hours and I put them alltogether now ![]()
Thanks a lot guys.
M.
