Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DHT_U.h>
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- #include <SPI.h>
- #include "Timer.h"
- #include <Servo.h>
- byte MAC[] = { 0xDE, 0x12, 0x44, 0xAF, 0xBE, 0x56 };
- IPAddress IP(192, 168, 2, 100);
- unsigned int localPort = 5000;
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
- String dataReq;
- int packetSize;
- EthernetUDP UDP;
- Timer t;
- Timer t1;
- #define DHTPIN 10
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- Servo myservo;
- int i = 10;
- void setup() {
- Serial.begin(9600);
- Ethernet.begin(MAC, IP);
- delay(1500);
- UDP.begin(localPort);
- for (int i = 2; i < 14; ++i) {
- pinMode(i, OUTPUT);
- }
- Serial.println(Ethernet.localIP());
- t.every(5000, takeTemperatureReading);
- myservo.attach(9);
- }
- void loop() {
- packetSize = UDP.parsePacket();
- t.update();
- // float temp = dht.readTemperature();
- // Serial.println(temp);
- // delay(1000);
- // Serial.println("GOOD");
- // UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
- // UDP.print(temp);
- // UDP.endPacket(); //Packet has been sent
- if (packetSize > 0) {
- // Some stuff for recieving data
- UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // Reading the data request, saving it in packetBuffer of UDP max size
- String dataReq(packetBuffer); // converting char array to string
- Serial.println(dataReq);
- String type = dataReq.substring(0, 3);
- int pin = dataReq.substring(3, 5).toInt();
- int state = dataReq.substring(5, 6).toInt();
- if (type == "RLY") {
- digitalWrite(pin, state);
- }
- else if (type == "LCK") {
- if (state == 1) {
- Serial.println("LOCKED");
- myservo.write(0);
- }
- else if (state == 0) {
- Serial.println("UNLOCKED");
- myservo.write(150);
- }
- }
- }
- memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
- }
- void takeTemperatureReading() {
- // float temp = dht.readTemperature();
- // String sendData = "TMP=" + String(temp);
- String sendData = "TMP=" + String(i) + ".00";
- Serial.println("Good");
- Serial.println(sendData);
- UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
- UDP.print(sendData);
- UDP.endPacket(); //Packet has been sent
- i++;
- if(i == 98) i = 10;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement