Advertisement
rozman50

Arduino code (17.2.2018)

Jan 23rd, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <DHT_U.h>
  2. #include <Ethernet.h>
  3. #include <EthernetUdp.h>
  4. #include <SPI.h>
  5. #include "Timer.h"
  6. #include <Servo.h>
  7.  
  8. byte MAC[] = { 0xDE, 0x12, 0x44, 0xAF, 0xBE, 0x56 };
  9. IPAddress IP(192, 168, 2, 100);
  10. unsigned int localPort = 5000;
  11.  
  12. char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
  13. String dataReq;
  14. int packetSize;
  15.  
  16. EthernetUDP UDP;
  17. Timer t;
  18. Timer t1;
  19.  
  20. #define DHTPIN 10
  21. #define DHTTYPE DHT22
  22. DHT dht(DHTPIN, DHTTYPE);
  23.  
  24. Servo myservo;
  25.  
  26. int i = 10;
  27.  
  28. void setup() {
  29.   Serial.begin(9600);
  30.   Ethernet.begin(MAC, IP);
  31.  
  32.   delay(1500);
  33.   UDP.begin(localPort);
  34.  
  35.   for (int i = 2; i < 14; ++i) {
  36.     pinMode(i, OUTPUT);
  37.   }
  38.  
  39.   Serial.println(Ethernet.localIP());
  40.   t.every(5000, takeTemperatureReading);
  41.  
  42.   myservo.attach(9);
  43. }
  44.  
  45. void loop() {
  46.  
  47.   packetSize = UDP.parsePacket();
  48.  
  49.   t.update();
  50.  
  51.   // float temp = dht.readTemperature();
  52.   // Serial.println(temp);
  53.   // delay(1000);
  54.   // Serial.println("GOOD");
  55.  
  56.   // UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
  57.   // UDP.print(temp);
  58.   // UDP.endPacket(); //Packet has been sent
  59.    
  60.   if (packetSize > 0) {
  61.  
  62.     // Some stuff for recieving data
  63.     UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // Reading the data request, saving it in packetBuffer of UDP max size
  64.     String dataReq(packetBuffer); // converting char array to string
  65.     Serial.println(dataReq);
  66.     String type = dataReq.substring(0, 3);
  67.     int pin = dataReq.substring(3, 5).toInt();
  68.     int state = dataReq.substring(5, 6).toInt();
  69.     if (type == "RLY") {
  70.       digitalWrite(pin, state);
  71.     }
  72.     else if (type == "LCK") {
  73.       if (state == 1) {
  74.         Serial.println("LOCKED");
  75.         myservo.write(0);
  76.       }
  77.       else if (state == 0) {
  78.         Serial.println("UNLOCKED");
  79.         myservo.write(150);
  80.       }
  81.     }
  82.  
  83.  
  84.   }
  85.  
  86.  
  87.   memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
  88. }
  89.  
  90. void takeTemperatureReading() {
  91.  
  92.   // float temp = dht.readTemperature();
  93.  
  94.   // String sendData = "TMP=" + String(temp);
  95.   String sendData = "TMP=" + String(i) + ".00";
  96.  
  97.   Serial.println("Good");
  98.   Serial.println(sendData);
  99.  
  100.   UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
  101.   UDP.print(sendData);
  102.   UDP.endPacket(); //Packet has been sent
  103.  
  104.   i++;
  105.   if(i == 98) i = 10;
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement