Arduino Uno R3+W5100+LM35, Temperature not display in Thingspeak... | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Arduino Uno R3+W5100+LM35, Temperature not display in Thingspeak...
No permission to create posts
July 14, 2017
6:34 am
Avatar
dtecres
Malaysia
New Member
Members
Forum Posts: 4
Member Since:
July 14, 2017
sp_UserOfflineSmall Offline

Dear All,

I'm trying to use the W5100 Ethernet Shield with Arduino Uno R3 to send the temperature read from LM35 to Thingspeak. 

Unfortunately, nothing happened after an hour (interval is 5 minutes). I can't find any wrong in the code and I don't know what went wrong. Frown 

I'm attaching the code as below:

#include <SPI.h>
#include <Ethernet.h>
#include "ThingSpeak.h"

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

//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

EthernetClient client;

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

void setup()
{
// Serial connection
Serial.begin(9600);

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

// Connect to local network and internet
Ethernet.begin(W5100_MacAddress);
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();
Serial.println("Connect to Thingspeak Server...");
Serial.println();
}
else
{
Serial.println();
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
if(ThingSpeak.writeField(Channel_Number, Channel_Field, LM35_Temp_Value, Write_API_Key))
{
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();
}

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

}

July 14, 2017
11:41 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 209
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

What response are you getting from the server?

try 

int response =thingSpeak.writeFireld(...;

serial.println("Response from server "+ String(response));

and see what the response says.

July 14, 2017
10:03 pm
Avatar
dtecres
Malaysia
New Member
Members
Forum Posts: 4
Member Since:
July 14, 2017
sp_UserOfflineSmall Offline

Hi Cstapels,

I get the response as below:

Response from server -302

Thanks. Smile

July 17, 2017
5:06 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 209
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

302 is a redirection, I don't thing thingSpeak would provide that to you directly.  A wild guess is that your network is redirecting the request away from ThingSpeak.  Are you able to update the channel from a browser or GET using POSTMAN connected on the same network?  You can use the syntax below, which is shown here in the documentation.

https://api.thingspeak.com/update?api_key=<your Write API KEY>&field1=1010 

 

Are you able to update the channel this way?

July 18, 2017
9:50 am
Avatar
dtecres
Malaysia
New Member
Members
Forum Posts: 4
Member Since:
July 14, 2017
sp_UserOfflineSmall Offline

Hi cstapels,

I was able to update as method provided.

July 21, 2017
3:33 am
Avatar
dtecres
Malaysia
New Member
Members
Forum Posts: 4
Member Since:
July 14, 2017
sp_UserOfflineSmall Offline

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..."));
}

July 21, 2017
4:39 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 209
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Consider using ThingTweet and a react to generate a tweet when your channel value reaches a particular value.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: dennis55
23 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

josephrj11, dennis55, jillhp4, tabathawg69, MarinaMa, Susandom

Administrators: Hans: 387, lee: 457