Smart agri
Dependencies: MQTT WIZnetInterface mbed-src
Fork of MQTTw7500 by
main.cpp@13:a75b321700d6, 2018-07-28 (annotated)
- Committer:
- Albinarackal
- Date:
- Sat Jul 28 04:33:27 2018 +0000
- Revision:
- 13:a75b321700d6
- Parent:
- 12:4f0489448955
Smart Agri
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Albinarackal | 13:a75b321700d6 | 1 | /* SMART AGRICULTURE PROJECT Applicatios |
Albinarackal | 13:a75b321700d6 | 2 | # 3 phase detection |
Albinarackal | 13:a75b321700d6 | 3 | # soil moisture detection |
Albinarackal | 13:a75b321700d6 | 4 | # rfid access to enter the field (gate open and close) |
Albinarackal | 13:a75b321700d6 | 5 | # fensing security for entire field |
Albinarackal | 13:a75b321700d6 | 6 | # security alarm for unauthorised access (for RFID and Fensing) |
Albinarackal | 13:a75b321700d6 | 7 | # Motor for water supply |
Albinarackal | 13:a75b321700d6 | 8 | */ |
justinkim | 8:f837e0d255e8 | 9 | #include "mbed.h" |
bangbh | 9:21574e058141 | 10 | #include "MQTTEthernet.h" |
bangbh | 9:21574e058141 | 11 | #include "MQTTClient.h" |
justinkim | 8:f837e0d255e8 | 12 | #define ECHO_SERVER_PORT 7 |
Albinarackal | 13:a75b321700d6 | 13 | Serial uart(D1,D0); |
Albinarackal | 13:a75b321700d6 | 14 | // wiznet wiz750SR pin configuration |
Albinarackal | 13:a75b321700d6 | 15 | AnalogIn soilSensor(PC_8); |
Albinarackal | 13:a75b321700d6 | 16 | DigitalIn p1(PC_13); |
Albinarackal | 13:a75b321700d6 | 17 | DigitalIn p2(PC_12); |
Albinarackal | 13:a75b321700d6 | 18 | DigitalIn p3(PC_9); |
Albinarackal | 13:a75b321700d6 | 19 | float soil; |
Albinarackal | 13:a75b321700d6 | 20 | char *msg; |
Albinarackal | 13:a75b321700d6 | 21 | char uart_msg; |
Albinarackal | 13:a75b321700d6 | 22 | char buffer[15]; |
Albinarackal | 13:a75b321700d6 | 23 | char *rf_on = "on"; |
Albinarackal | 13:a75b321700d6 | 24 | char *rf_off = "off"; |
Albinarackal | 13:a75b321700d6 | 25 | char *mot_on = "onmotor"; |
Albinarackal | 13:a75b321700d6 | 26 | char *mot_off = "offmotor"; |
Albinarackal | 13:a75b321700d6 | 27 | |
Albinarackal | 13:a75b321700d6 | 28 | // call back function |
bangbh | 9:21574e058141 | 29 | void messageArrived(MQTT::MessageData& md) |
justinkim | 8:f837e0d255e8 | 30 | { |
Albinarackal | 13:a75b321700d6 | 31 | strcpy(buffer,""); |
bangbh | 9:21574e058141 | 32 | MQTT::Message &message = md.message; |
bangbh | 9:21574e058141 | 33 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
bangbh | 9:21574e058141 | 34 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
Albinarackal | 13:a75b321700d6 | 35 | sprintf(buffer,"%.*s",message.payloadlen,(char*)message.payload); |
Albinarackal | 13:a75b321700d6 | 36 | printf("buffer value is %s\n",buffer); |
Albinarackal | 13:a75b321700d6 | 37 | |
bangbh | 9:21574e058141 | 38 | } |
bangbh | 9:21574e058141 | 39 | |
Albinarackal | 13:a75b321700d6 | 40 | |
Albinarackal | 13:a75b321700d6 | 41 | void baud(int baudrate) |
Albinarackal | 13:a75b321700d6 | 42 | { |
bangbh | 9:21574e058141 | 43 | Serial s(USBTX, USBRX); |
bangbh | 9:21574e058141 | 44 | s.baud(baudrate); |
bangbh | 9:21574e058141 | 45 | } |
bangbh | 9:21574e058141 | 46 | |
bangbh | 9:21574e058141 | 47 | int main (void) |
bangbh | 9:21574e058141 | 48 | { |
Albinarackal | 13:a75b321700d6 | 49 | |
Albinarackal | 13:a75b321700d6 | 50 | baud(9600); |
Albinarackal | 13:a75b321700d6 | 51 | char *topic1 = "rfiddata"; |
Albinarackal | 13:a75b321700d6 | 52 | char *topic2 = "threephasebutton"; |
justinkim | 8:f837e0d255e8 | 53 | printf("Wait a second...\r\n"); |
Albinarackal | 13:a75b321700d6 | 54 | |
bangbh | 9:21574e058141 | 55 | MQTTEthernet ipstack = MQTTEthernet(); |
Albinarackal | 13:a75b321700d6 | 56 | |
bangbh | 9:21574e058141 | 57 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
bangbh | 9:21574e058141 | 58 | |
Albinarackal | 13:a75b321700d6 | 59 | // MQTT broker address and port no. |
Albinarackal | 13:a75b321700d6 | 60 | char* hostname = "iot.eclipse.org"; |
bangbh | 9:21574e058141 | 61 | int port = 1883; |
Albinarackal | 13:a75b321700d6 | 62 | |
bangbh | 9:21574e058141 | 63 | int rc = ipstack.connect(hostname, port); |
Albinarackal | 13:a75b321700d6 | 64 | if (rc == 0) |
bangbh | 9:21574e058141 | 65 | printf("rc from TCP connect is %d\n", rc); |
Albinarackal | 13:a75b321700d6 | 66 | |
Albinarackal | 13:a75b321700d6 | 67 | |
Albinarackal | 13:a75b321700d6 | 68 | |
Albinarackal | 13:a75b321700d6 | 69 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
bangbh | 9:21574e058141 | 70 | data.MQTTVersion = 3; |
jamesabruce | 12:4f0489448955 | 71 | data.clientID.cstring = "parents"; |
bangbh | 9:21574e058141 | 72 | |
Albinarackal | 13:a75b321700d6 | 73 | if ((rc = client.connect(data)) == 0) |
bangbh | 9:21574e058141 | 74 | printf("rc from MQTT connect is %d\n", rc); |
Albinarackal | 13:a75b321700d6 | 75 | |
Albinarackal | 13:a75b321700d6 | 76 | if ((rc = client.subscribe(topic1, MQTT::QOS1, messageArrived)) == 0) |
Albinarackal | 13:a75b321700d6 | 77 | printf("rc from MQTT subscribe toppic is rfiddata %d\n", rc); |
Albinarackal | 13:a75b321700d6 | 78 | |
Albinarackal | 13:a75b321700d6 | 79 | |
Albinarackal | 13:a75b321700d6 | 80 | if ((rc = client.subscribe(topic2, MQTT::QOS1, messageArrived)) == 0) |
Albinarackal | 13:a75b321700d6 | 81 | printf("rc from MQTT subscribe toppic is threephasebutton %d\n", rc); |
Albinarackal | 13:a75b321700d6 | 82 | |
bangbh | 9:21574e058141 | 83 | MQTT::Message message; |
Albinarackal | 13:a75b321700d6 | 84 | |
Albinarackal | 13:a75b321700d6 | 85 | while (true) { |
Albinarackal | 13:a75b321700d6 | 86 | |
Albinarackal | 13:a75b321700d6 | 87 | // reading the soil moisture content from soil moisture sensor |
Albinarackal | 13:a75b321700d6 | 88 | soil = soilSensor.read_u16(); |
Albinarackal | 13:a75b321700d6 | 89 | printf(" soil val is : %f\n",soil); |
Albinarackal | 13:a75b321700d6 | 90 | |
Albinarackal | 13:a75b321700d6 | 91 | // Data reading from arduino via UART |
Albinarackal | 13:a75b321700d6 | 92 | if (uart.readable()) { |
Albinarackal | 13:a75b321700d6 | 93 | printf("uart readable\n"); |
Albinarackal | 13:a75b321700d6 | 94 | uart_msg = uart.getc(); |
Albinarackal | 13:a75b321700d6 | 95 | |
Albinarackal | 13:a75b321700d6 | 96 | if(uart_msg == 's') { // Authorised access data of RFID |
Albinarackal | 13:a75b321700d6 | 97 | msg = "Authorized Access"; |
Albinarackal | 13:a75b321700d6 | 98 | printf(" msg : %s\n", msg); |
Albinarackal | 13:a75b321700d6 | 99 | } |
Albinarackal | 13:a75b321700d6 | 100 | |
Albinarackal | 13:a75b321700d6 | 101 | if(uart_msg == 'n' || uart_msg =='f') { // unauthorised access data of RFID or Fensing |
Albinarackal | 13:a75b321700d6 | 102 | msg = "Unauthorized Access"; |
Albinarackal | 13:a75b321700d6 | 103 | printf(" msg : %s\n", msg); |
Albinarackal | 13:a75b321700d6 | 104 | |
Albinarackal | 13:a75b321700d6 | 105 | } |
Albinarackal | 13:a75b321700d6 | 106 | message.qos = MQTT::QOS0; |
Albinarackal | 13:a75b321700d6 | 107 | message.retained = false; |
Albinarackal | 13:a75b321700d6 | 108 | message.dup = false; |
Albinarackal | 13:a75b321700d6 | 109 | message.payload = (void*)msg; |
Albinarackal | 13:a75b321700d6 | 110 | message.payloadlen = strlen(msg); |
Albinarackal | 13:a75b321700d6 | 111 | rc = client.publish("rfid", message); |
Albinarackal | 13:a75b321700d6 | 112 | strcpy(msg,""); |
Albinarackal | 13:a75b321700d6 | 113 | |
Albinarackal | 13:a75b321700d6 | 114 | |
Albinarackal | 13:a75b321700d6 | 115 | } |
Albinarackal | 13:a75b321700d6 | 116 | // rfid command from mqtt "on" |
Albinarackal | 13:a75b321700d6 | 117 | if(strcmp(buffer, rf_on)==0 ) { |
Albinarackal | 13:a75b321700d6 | 118 | uart.putc('a'); //sending the data to arduino via UART |
Albinarackal | 13:a75b321700d6 | 119 | printf("rfid on data\n"); |
Albinarackal | 13:a75b321700d6 | 120 | strcpy(buffer,""); |
Albinarackal | 13:a75b321700d6 | 121 | |
justinkim | 8:f837e0d255e8 | 122 | } |
Albinarackal | 13:a75b321700d6 | 123 | // rfid command from mqtt "off" |
Albinarackal | 13:a75b321700d6 | 124 | if(strcmp(buffer,rf_off)==0) { |
Albinarackal | 13:a75b321700d6 | 125 | uart.putc('b'); //sending the data to arduino via UART |
Albinarackal | 13:a75b321700d6 | 126 | printf("rfid off data\n"); |
Albinarackal | 13:a75b321700d6 | 127 | strcpy(buffer,""); |
Albinarackal | 13:a75b321700d6 | 128 | |
Albinarackal | 13:a75b321700d6 | 129 | } |
Albinarackal | 13:a75b321700d6 | 130 | if(strcmp(buffer,mot_on)==0) { |
Albinarackal | 13:a75b321700d6 | 131 | uart.putc('c'); //sending the data to arduino via UART |
Albinarackal | 13:a75b321700d6 | 132 | printf("motor on data\n"); |
Albinarackal | 13:a75b321700d6 | 133 | strcpy(buffer,""); |
Albinarackal | 13:a75b321700d6 | 134 | |
Albinarackal | 13:a75b321700d6 | 135 | } |
Albinarackal | 13:a75b321700d6 | 136 | |
Albinarackal | 13:a75b321700d6 | 137 | if(strcmp(buffer,mot_off)==0) { |
Albinarackal | 13:a75b321700d6 | 138 | uart.putc('d'); //sending the data to arduino via UART |
Albinarackal | 13:a75b321700d6 | 139 | printf("motor off data\n"); |
Albinarackal | 13:a75b321700d6 | 140 | strcpy(buffer,""); |
Albinarackal | 13:a75b321700d6 | 141 | } |
Albinarackal | 13:a75b321700d6 | 142 | |
Albinarackal | 13:a75b321700d6 | 143 | // soil moisture and 3 phase current checking |
Albinarackal | 13:a75b321700d6 | 144 | if(soil<250) { |
Albinarackal | 13:a75b321700d6 | 145 | if((p1==1)&&(p2==1)&&(p3==1)) { //three phase checking |
Albinarackal | 13:a75b321700d6 | 146 | printf("3 phase detected\n"); |
Albinarackal | 13:a75b321700d6 | 147 | msg = "Low Moisture and 3 phase detected"; |
Albinarackal | 13:a75b321700d6 | 148 | } else { |
Albinarackal | 13:a75b321700d6 | 149 | // printf("3 phase not detected\n"); |
Albinarackal | 13:a75b321700d6 | 150 | msg = "Low Moisture but 3 phase not detected"; |
Albinarackal | 13:a75b321700d6 | 151 | } |
Albinarackal | 13:a75b321700d6 | 152 | |
Albinarackal | 13:a75b321700d6 | 153 | |
Albinarackal | 13:a75b321700d6 | 154 | message.qos = MQTT::QOS0; |
Albinarackal | 13:a75b321700d6 | 155 | message.retained = false; |
Albinarackal | 13:a75b321700d6 | 156 | message.dup = false; |
Albinarackal | 13:a75b321700d6 | 157 | message.payload = (void*)msg; |
Albinarackal | 13:a75b321700d6 | 158 | message.payloadlen = strlen(msg); |
Albinarackal | 13:a75b321700d6 | 159 | rc = client.publish("threephase", message); |
Albinarackal | 13:a75b321700d6 | 160 | strcpy(msg,""); |
Albinarackal | 13:a75b321700d6 | 161 | } |
Albinarackal | 13:a75b321700d6 | 162 | client.yield(500); |
justinkim | 8:f837e0d255e8 | 163 | } |
justinkim | 8:f837e0d255e8 | 164 | } |
justinkim | 8:f837e0d255e8 | 165 |