clear
clc
disp('*****Data Acquisition process ACTIVATED*****')
delete(instrfindall);
HebiKeyboard;
echoudp('off');
[y, Fs] = audioread('D:\MATLAB PHD Studies\Arduino and Matlab\NiallHoran.mp3');
player = audioplayer(y, Fs);
kb = HebiKeyboard();
i=1;
j=1;
while (1)
start = tic;
while(i<=6)
start1=tic;
echoudp('on',5354);
UDP_obj = udp('192.168.1.2','localport',9091,'remoteport',5354,'timeout',15,'DatagramTerminateMode','on');
UDP_obj.InputBufferSize=200;
UDP_obj.OutputBufferSize=40;
fopen(UDP_obj);
fwrite(UDP_obj,'Ready to recieve');
UDP_out1=fread(UDP_obj);
UDP_out2=char(UDP_out1)';
out=str2num(UDP_out2)'
fclose(UDP_obj);
delete(UDP_obj);
clear UDP_obj;
echoudp('off');
tin1(i)=out(1,1);
tin2(i)=out(2,1);
tout(i)=out(3,1);
hin1(i)=out(4,1);
hin2(i)=out(5,1);
hout(i)=out(6,1);
Ir1(i)=out(7,1);
Ir2(i)=out(8,1);
Ir3(i)=out(9,1);
Ir4(i)=out(10,1);
Wv(i)=out(11,1);
Rain(i)=out(12,1);
if isnan(tin1(i)) || isnan(tin2(i)) || isnan(tout(i))
k=i-1;
if k==0
k=6;
end
tin1(i)=tin1(k);
tin2(i)=tin2(k);
tout(i)=tout(k);
hin1(i)=hin1(k);
hin2(i)=hin2(k);
hout(i)=hout(k);
end
Tt=0;
while (Tt<10.00)
Tt=toc(start1);
state = read(kb);
if state.SHIFT
stop(player)
disp('*****Rain Alarm sound has stopped*****')
end
end
i=i+1
end
i=6;
Tin1(j)=tin1(i);
Tin2(j)=tin2(i);
Tout(j)=tout(i);
Hin1(j)=hin1(i);
Hin2(j)=hin2(i);
Hout(j)=hout(i);
IrrMoy(j)=((sum(Ir1)/6)+(sum(Ir2)/6)+(sum(Ir3)/6)+(sum(Ir4)/6))/4;
WvMoy(j)=sum(Wv)/6;
fid1=fopen('Greenhouse Variables (DATABASE).txt','a');
if j==1
header='Date Time Tin1 Tin2 Tout Hin1 Hin2 Hout Irr Wv Rain';
fprintf(fid1,'%s\r\n',header);
end
D=datetime('now')
DateString = datestr(now,'dd/mm/yyyy HH:MM:SS ');
par=[tin1(i) tin2(i) tout(i) hin1(i) hin2(i) hout(i) IrrMoy(j) WvMoy(j) Rain(i)]';
myformat='%3.2f %3.2f %3.2f %3.2f %3.2f %3.2f %4.2f %3.2f %3.2f\n';
fprintf(fid1,[DateString myformat],par);
fclose(fid1);
if Rain(i)>100
play(player)
end
j=j+1;
i=1;
toc(start)
end
//NodeMCU code
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5); //RX TX
#include <ArduinoJson.h>
//Server initializations :::
const char* ssid = "Rida";
const char* password = "ridagues";
WiFiUDP Udp;
unsigned int localUdpPort = 5354; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[240]; // a reply string to send back
WiFiServer server(localUdpPort);
void setup() {
ESP.wdtDisable();
ESP.wdtEnable(WDTO_8S);
// time1=millis(); // set time as now
// time2=millis();
//Router Mode:::
Serial.begin(115200);
s.begin(115200);
while (!Serial) continue;
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
////Access Point Mode:::
// Serial.begin(115200);
// s.begin(115200);
// while (!Serial) continue;
//
// Serial.print("Configuring access point...");
// WiFi.disconnect();
// delay(100);
// WiFi.mode(WIFI_STA);
// // You can remove the password parameter if you want the AP to be open.
// WiFi.softAP(ssid, password);
//
// IPAddress myIP = WiFi.softAPIP();
// Serial.print("AP IP address: ");
// Serial.println(myIP);
//
//// Start the server
// server.begin();
// Serial.println("Server started");
//// Print the IP address
// Serial.println(WiFi.localIP());
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop() {
ESP.wdtFeed();
yield();
//Waiting for incoming UDP packet is done by the following code:
int packetSize = Udp.parsePacket();
if (packetSize)
{
ESP.wdtFeed();
yield();
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
ESP.wdtFeed();
yield();
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
//send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacket);
Udp.endPacket();
}
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(s);
if (root == JsonObject::invalid())
{
ESP.wdtFeed();
yield();
return;
}
//root.prettyPrintTo(Serial);
Serial.println("");
float data1=root["data1"];
Serial.print(data1);
Serial.print(" ");
float data2=root["data2"];
Serial.print(data2);
Serial.print(" ");
float data3=root["data3"];
Serial.print(data3);
Serial.print(" ");
float data4=root["data4"];
Serial.print(data4);
Serial.print(" ");
float data5=root["data5"];
Serial.print(data5);
Serial.print(" ");
float data6=root["data6"];
Serial.print(data6);
Serial.print(" ");
float data7=root["data7"];
Serial.print(data7);
Serial.print(" ");
float data8=root["data8"];
Serial.print(data8);
Serial.print(" ");
float data9=root["data9"];
Serial.print(data9);
Serial.print(" ");
float data10=root["data10"];
Serial.print(data10);
Serial.print(" ");
float data11=root["data11"];
Serial.print(data11);
Serial.print(" ");
float data12=root["data12"];
Serial.println(data12);
//convert float values to char String:::
snprintf(replyPacket, sizeof(replyPacket), "%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f", data1,data2,data3,data4,data5,data6,data7,data8,data9,data10,data11,data12);
ESP.wdtFeed();
yield();
delay(1000);
}