Quantcast
Channel: ThingSpeak IoT Community - Forum: Arduino
Viewing all articles
Browse latest Browse all 172

dtecres on Arduino Uno R3+W5100+LM35, Temperature not display in Thingspeak...

$
0
0

Hi cstapels,

I've found the cause of the data not able to update to Thingspeak. This is due to model DNS is not set to open DNS, after set to open DNS it works fine now.

However, it turned to not working back when I had added send email function when temperature over the threshold. I can't figure what went wrong and the response number returned after pushed the data to Thingspeak same as before added the send email function. 

Please help...

Thanks.

Best Regards,

Kinble Gray

 

#include <SPI.h>
#include <Ethernet.h>
#include <utility/w5100.h>
#include "ThingSpeak.h"

// W5100 Network Setup
byte W5100_MacAddress[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
const IPAddress No_Connet_IP = IPAddress(0, 0, 0, 0);

//Thingspeak Setup
unsigned long Channel_Number = 302633; // Channel ID
const char * Write_API_Key = "VAMPTX0YTHJ17WJ5"; // Write API Key
int Channel_Field = 1; // Field where Temperature data upload to

// LM35 sensor input to analog
#define LM35_Temp_Sensor A0

// SMTP2GO eMail Setup
char smtp_server[] = "smtpcorp.com";
int smtp_port = 2525;

EthernetClient client;

float LM35_Temp_Value = 0; // Initial Temperature value
float Preset_Temp_Value = 37.00; // Preset Temperature
int Interval = 300; // Interval read the temperature every 5 mins

void setup()
{
Serial.begin(9600);
delay(200);
while (!Serial)
{
delay(200);
}
Serial.println("Arduino Uno R3 was detected...");

// Disable SD Card SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);

Ethernet.begin(W5100_MacAddress);
byte MACvalue[6];

// Detecting W5100 Ethernet Shield present
W5100.getMACAddress(MACvalue);
for (byte i = 0; i < 6; i++)
{
if (MACvalue[i] != W5100_MacAddress[i])
{
Serial.println(F("W5100 ethernet shield is not detected!!!"));
while (true)
{
// Waiting user to restart the module...
}
}
}
Serial.println();
Serial.println(F("W5100 ethernet shield was detected..."));

// Check the W5100 Ethernet Connection to DHCP server
IPAddress W5100_IPAddress = Ethernet.localIP();
if (W5100_IPAddress[0] == No_Connet_IP[0] || W5100_IPAddress[1] == No_Connet_IP[1] || W5100_IPAddress[2] == No_Connet_IP[2] || W5100_IPAddress[3] == No_Connet_IP[3])
{
Serial.println(F("W5100 failed to connect to DHCP server!!!"));
while (true)
{
// Waiting user to restart the module...
}
}
else
{
Serial.println();
Serial.println(F("W5100 is connect to DHCP server..."));
Serial.println();
Serial.print("W5100 IP Address: ");
Serial.println(Ethernet.localIP());
Serial.print("W5100 Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("W5100 Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.println();
}

// Connect to Thingspeak server
if (ThingSpeak.begin(client))
{
Serial.println("Connect to Thingspeak Server!!!");
Serial.println();
}
else
{
Serial.println("Failed to connect to Thingspeak Server!!!");
Serial.println();
}
}

void loop()
{
LM35_Temp_Value = analogRead(LM35_Temp_Sensor)* 0.0048828125 * 100;
Serial.print("Current temperature: ");
Serial.print(LM35_Temp_Value);
Serial.println("°C");
delay(100);

// Write the temperature to Thingspeak Server
int response = ThingSpeak.writeField(Channel_Number, Channel_Field, LM35_Temp_Value, Write_API_Key);
Serial.println("Response from server: "+ String(response));
if(response >=0)
{
Serial.println();
Serial.println("Uploading data to Thingspeak Server...");
Serial.println();
}
else
{
Serial.println();
Serial.println("Failed to upload data to Thingspeak Server!!!");
Serial.println();
}
client.stop();

// Send alert email in temperature over the threshold
if (LM35_Temp_Value >= Preset_Temp_Value)
{
Serial.println("Temperature over threshold!!!");
Serial.println("Sending over temperature alert email...");
delay(100);
if(sendEmail())
{
Serial.println(F("Over temperature alert email sent successfully..."));
}
else
{
Serial.println(F("Over temperature alert email sent failed!!!"));
}
}

// The temperature will read every 5 mins
for (int i=0; i <= Interval; i++)
{
delay(1000); // 1 second delay
}

}

byte sendEmail()
{
byte thisByte = 0;
byte respCode;

Serial.println(F("Connecting to SMTP Server..."));
if(client.connect(smtp_server,smtp_port) == 1)
{
Serial.println(F("W5100 is connect to SMTP Server..."));
}
else
{
Serial.println(F("W5100 failed to connect to SMTP Server!!!"));
return 0;
}
if(!eRcv()) return 0;

Serial.println(F("Sending hello..."));
// Replace 1.2.3.4 with your Arduino's ip
//client.println("EHLO 1.2.3.4");
client.println("EHLO W5100_IPAddress");
if(!eRcv()) return 0;

Serial.println(F("Sending auth login..."));
client.println("auth login");
if(!eRcv()) return 0;

Serial.println(F("Sending UserID..."));
// Change to your base64 encoded user
client.println("User ID");
if(!eRcv()) return 0;

Serial.println(F("Sending Password..."));
// change to your base64 encoded password
client.println("Password");
if(!eRcv()) return 0;

// change to your email address (sender)
Serial.println(F("Sending From..."));
client.println("MAIL From: <ektan@unigen.com>");
if(!eRcv()) return 0;

// change to recipient address
Serial.println(F("Sending To..."));
client.println("RCPT To: <ektan@unigen.com>");
if(!eRcv()) return 0;

Serial.println(F("Sending DATA..."));
client.println("DATA");
if(!eRcv()) return 0;

Serial.println(F("Sending email..."));

// change to recipient address
client.println("To: EK Tan <ektan@unigen.com>");

// change to your address
client.println("From: Eekhay Tan <ektan@unigen.com>");
client.println("Subject: Over Temperature Email Alert!!!
");
client.print("Temperature: ");
client.print(LM35_Temp_Value);
client.println("°C");
client.println("Current temperature is over the threshold!!!");
client.println("Please take immediate action now!!!");
client.println(".");
if(!eRcv()) return 0;

Serial.println(F("Sending QUIT..."));
client.println("QUIT");
if(!eRcv()) return 0;

client.stop();

Serial.println(F("SMTP Server is disconnected..."));

return 1;
}

byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;

while(!client.available()) {
delay(1);
loopCount++;

// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("
eRcv Timeout!!!"));
return 0;
}
}

respCode = client.peek();
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}

if(respCode >= '4')
{
efail();
return 0;
}

return 1;
}

void efail()
{
byte thisByte = 0;
int loopCount = 0;

client.println(F("QUIT"));

while(!client.available())
{
delay(1);
loopCount++;

// if nothing received for 10 seconds, timeout
if(loopCount > 10000)
{
client.stop();
Serial.println(F("
eFail Timeout!!!"));
return;
}
}

while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}

client.stop();

Serial.println(F("SMTP Server is disconnected..."));
}


Viewing all articles
Browse latest Browse all 172

Trending Articles