My first serious Arduino project: Ethernet Relay Controller – Day 2

So, today is my second day getting my hands seriously dirty while molesting my Arduinos.

Following up on Day 1, I decided to try transferring reading and data to make sure that I can work with UDP packet transfer and Arduinos.

I don’t have a real sketch of how I connected the sensor. But I used this sensor to read humidity and temperature data.

Here’s a video showing results:

I connected the red wire to +5 pin, black wire to GND pin, and the yellow wire to Digital Pin 2.

Before jumping to code, I’ll have to explain some difficulties I’ve stumbled upon (Not really difficulties, just few hiccups).

First, mac address. In older Ethernet Shields, you can assign your desired mac address. But current ones won’t work properly unless you assign the mac address written on the board (At least thats what I think. Didn’t work with your funny 0xDEADBEEFFEED mac address until I assigned the actual one).

Another thing is reading the data as float and having to send them as char\string along with the message. I had to include stdlib.h and use dtostrf().

Now let me explain dtostrf() because I struggled to understand it a bit:

dtostrf() is actually this:

dtostrf(FLOAT_VALUE,MINIMUM,ACCURACY,BUFFER);

FLOAT_VALUE is where the float value you want to read from.
MINIMUM is basically the minimum digits you want after the decimal point.
ACCURACY is how many digits you want it the string (or char[]) to be.
BUFFER is where you want to store the resulted string.

Now, for example, you want XX.XX to be the final result of dtostrf() and you have the float value Y, you do this:
char buf[5];
float value = 21.14521;
dtostrf(value,1,2,&buf[0]);

&buf[0] because we want to insert the result from the beginning of that char array. You can do &buf[1], but it’ll skip the first char.

 

Now, CODING TIEM!!

#include
#include "DHT.h"
#include
#include
#include
#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
EthernetUDP Udp;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x49, 0x0D };
IPAddress server(192, 168, 1, 101);

unsigned int localPort = 8888;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

void setup() {
Ethernet.begin(mac);
Serial.begin(9600);

Udp.begin(localPort);
dht.begin();
}

void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
int packetSize = Udp.parsePacket();
Ethernet.maintain();

if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
}
else
{
char strh[6];
char strt[6];
dtostrf(h,1,2,&strh[0]);
dtostrf(t,1,2,&strt[0]);
Udp.beginPacket(server, Udp.remotePort());
Udp.write("Humidity: ");
Udp.write(strh);
Udp.write("\tTemperature: ");
Udp.write(strt);
Udp.endPacket();
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
Serial.print("Humidity: ");
Serial.print(strh);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(strt);
Serial.println(" *C");
}
}

One thought on “My first serious Arduino project: Ethernet Relay Controller – Day 2

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.