Components and supplies
Ultrasonic Sensor - HC-SR04 (Generic)
Arduino UNO
Geared DC Motor, 24 V
NodeMCU ESP8266 Breakout Board
Relay (generic)
Apps and platforms
Visual Studio 2017
Arduino IDE
Project description
Code
automaic gate control
c_cpp
1/** LIBRARIES**/ 2#include <Arduino.h> 3#include <WiFi.h> 4#include 5 <WiFiMulti.h> 6#include <WiFiClientSecure.h> 7#include <WebSocketsClient.h> 8#include 9 <ArduinoJson.h> 10#include "HX710B.h" 11#include "FS.h" 12#include "SD.h" 13#include 14 <SPI.h> 15#include <NTPClient.h> 16#include <WiFiUdp.h> 17/** LIBRARIES**/ 18 19/**SD 20 CARD**/ 21uint64_t uS_TO_S_FACTOR = 1000000; 22uint64_t TIME_TO_SLEEP = 300; 23#define 24 SD_CS 5 25RTC_DATA_ATTR int sensor_data = 0; 26String Data; 27WiFiUDP ntpUDP; 28NTPClient 29 timeClient(ntpUDP); 30String Date; 31String day; 32String Time; 33/** SD CARD**/ 34 35/** 36 WEBSOCKET INITALIZATION**/ 37WiFiMulti WiFiMulti; 38WebSocketsClient webSocket; 39WebSocketsClient 40 webSocket3; 41/** WEBSOCKET INITALIZATION**/ 42 43/** PIN DECLARATION FOR PRESSURE 44 AND ULTRASONIC RESPECTIVE**/ 45const int DOUT = 22; //sensor data pin 46const 47 int SCLK = 24; //sensor clock pin 48float pressure; 49float fixed_pressure=549.7; 50HX710B 51 pressure_sensor; 52 53static int sensorVal = 0; 54 55/** PIN DECLARATION FOR 56 PRESSURE AND ULTRASONIC RESPECTIVE**/ 57const int trigPin = 5; 58const int echoPin 59 = 18; 60long duration; 61float distanceMm; 62#define SOUND_SPEED 0.034 63int 64 inputHeight=10; 65int heightTolorance=1; 66int fixedHeight=46; 67//int openingHeight; 68//int 69 userOpeningdifference; 70 71/** calculation to control the motor**/ 72int openingHeight=fixedHeight-distanceMm; 73int 74 userOpeningdifference=inputHeight-openingHeight; 75 /** calculation to control 76 the motor**/ 77/** PIN DECLARATION FOR PRESSURE AND ULTRASONIC RESPECTIVE**/ 78 79/** 80 FOR PRESSURE **/ 81//float pressure; 82float inputpressure=10; 83float pressureTolorance=1; 84float 85 fixedpressure=46; 86 87/** calculation to control the motor**/ 88int openingpressure=fixedpressure-pressure; 89int 90 userOpeningDifferencep=inputHeight-openingpressure; 91/** FOR RELAY**/ 92#define 93 CW 14 //CW is defined as pin #7// 94#define CCW 15 //CCW is defined as pin #8// 95 96//for 97 limit switch 98int Lswitch = 2; 99 100// CHANGE THIS TO ADD YOUR WIFI USERNAME/PASSWORD 101const 102 char * WIFI_SSID = "TP-Link_27ED"; 103const char * WIFI_PASS = "19202122"; 104 105 106//Initialize the JSON data we send to our websocket server 107const int capacity 108 = JSON_OBJECT_SIZE(3); 109StaticJsonDocument<capacity> doc; 110StaticJsonDocument<1024> 111 rx_doc; 112 113 /** FOR DEVICE TEMPERATURE READING**/ 114#define USE_SERIAL Serial 115 116 117 #ifdef __cplusplus 118 119extern "C" { 120 121#endif 122 123uint8_t temprature_sens_read(); 124 125#ifdef 126 __cplusplus 127 128} 129 130#endif 131 132uint8_t temprature_sens_read(); 133void 134 webSocketEvent(WStype_t type, uint8_t *payload, size_t length) 135{ 136 137 switch(type) 138 139 { 140 case WStype_DISCONNECTED: 141 USE_SERIAL.printf("[WSc] Disconnected!\ 142"); 143 144 break; 145 case WStype_CONNECTED: 146 USE_SERIAL.printf("[WSc] Connected 147 to url: %s\ 148", payload); 149 break; 150 case WStype_TEXT: 151 USE_SERIAL.printf("[WSc] 152 get text: %s\ 153", payload); 154 // deserializeJson(rx_doc,payload); 155 // 156 // String height = rx_doc["height"]; 157 // USE_SERIAL.println(rx_doc["height"]); 158 159 break; 160 case WStype_BIN: 161 USE_SERIAL.printf("[WSc] get binary length: 162 %u\ 163", length); 164 break; 165 case WStype_ERROR: 166// case WStype_FRAGMENT_TEXT_START: 167 168 case WStype_FRAGMENT_BIN_START: 169 case WStype_FRAGMENT: 170 case WStype_FRAGMENT_FIN: 171 172 case WStype_PING: 173 case WStype_PONG: 174 break; 175 } 176} 177/** FOR 178 DEVICE TEMPERATURE READING**/ 179 180void setup() 181{ 182 //Set the baud rate 183 184 USE_SERIAL.begin(115200); 185 USE_SERIAL.printf("Begin websocket client program...."); 186 187 /** FOR WIFI CONNECTION **/ 188 USE_SERIAL.println(); 189 WiFi.mode(WIFI_STA); 190 191 WiFi.begin(WIFI_SSID, WIFI_PASS); 192 Serial.print("Connecting to WiFi .."); 193 194 while (WiFi.status() != WL_CONNECTED) { 195 Serial.print('.'); 196 delay(1000); 197 198 } 199/** FOR WIFI CONNECTION **/ 200 201 Serial.println(WiFi.localIP()); 202// 203 for (uint8_t t = 4; t > 0; t--) 204// { 205// USE_SERIAL.printf("[SETUP] 206 BOOT WAIT %d...\ 207", t); 208// USE_SERIAL.flush(); 209// delay(1000); 210// 211 } 212 213// WiFiMulti.addAP(WIFI_SSID, WIFI_PASS); 214 215 // WiFi.disconnect(); 216 217 USE_SERIAL.printf("Connecting"); 218// while (WiFiMulti.run() != WL_CONNECTED) 219// 220 { 221// USE_SERIAL.printf("."); 222// delay(100); 223// } 224 USE_SERIAL.printf("Connected!"); 225 226 227 // server address, port and URL 228 webSocket.begin("192.168.0.141", 8085, 229 "/sendSensorData"); 230 webSocket3.begin("192.168.0.141", 8085, "/request"); 231 232 233 // event handler 234 webSocket.onEvent(webSocketEvent); 235 webSocket3.onEvent(webSocketEvent); 236 237 // use HTTP Basic Authorization this is optional remove if not needed 238 // 239 webSocket.setAuthorization("user", "Password"); 240 241 // try ever 5000 again 242 if connection has failed 243 webSocket.setReconnectInterval(5000); 244 webSocket3.setReconnectInterval(10000); 245 246 247 248 // // Set the resolution values 249 // analogReadResolution(RESOLUTION); 250 251 pinMode(Lswitch, INPUT); 252 253 /**SD CARD**/ 254 timeClient.begin(); 255 256 timeClient.setTimeOffset(18000); 257 258 if(!SD.begin()) { 259 Serial.println("Card 260 Mount Failed"); 261 return; 262 } 263 uint8_t cardType = SD.cardType(); 264 265 if(cardType == CARD_NONE) { 266 Serial.println("No SD card attached"); 267 268 return; 269 } 270 Serial.println("Initializing SD card..."); 271 if (!SD.begin()) 272 { 273 Serial.println("SD card initialization failed!"); 274 return; 275 276 } 277 278 File file = SD.open("/sensor_readings.txt"); 279 if(!file) { 280 281 Serial.println("File does not exist"); 282 Serial.println("Creating file..."); 283 284 writeFile(SD, "/temperature_readings.txt", "Reading Number, Date, Hour, Temperature 285 \ \ 286"); 287 } 288 else { 289 Serial.println("File exists"); 290 } 291 292 file.close(); 293 294 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); 295/** 296 sensors.begin(); 297 obtainReadings(); 298 obtain_Date_Time(); 299 data_logging();**/ 300 301 Serial.println("Sensor data logged successfully! Going to sleep"); 302 esp_deep_sleep_start(); 303 304 pressure_sensor.begin(DOUT, SCLK); 305} 306 307void presssurereading() 308{ 309 310 if (pressure_sensor.is_ready()) 311{ 312 Serial.print("mmh20: "); 313 pressure=(pressure_sensor.pascal()-fixed_pressure)*2.469; 314 315 Serial.println(pressure); 316} 317} 318 319void lmitswitch() 320{ 321 if(digitalRead(Lswitch) 322 == LOW) 323 { 324 digitalWrite(CW,LOW); //Motor runs clockwise 325 } 326 327 328 else if(digitalRead(Lswitch) == HIGH) 329 { 330 digitalWrite(CW,HIGH); //Motor 331 runs clockwise/ 332 } 333} 334 335void readDistanceValue() 336{ 337pinMode(trigPin, 338 OUTPUT); // Sets the trigPin as an Output 339pinMode(echoPin, INPUT); // Sets the 340 echoPin as an Input 341 342digitalWrite(trigPin, LOW); 343 delayMicroseconds(2); 344 345 // Sets the trigPin on HIGH state for 10 micro seconds 346 digitalWrite(trigPin, 347 HIGH); 348 delayMicroseconds(10); 349 digitalWrite(trigPin, LOW); 350 351 // 352 Reads the echoPin, returns the sound wave travel time in microseconds 353 duration 354 = pulseIn(echoPin, HIGH); 355 356 // Calculate the distance 357 distanceMm = 358 (duration * SOUND_SPEED/2)*10; 359 360 // Prints the distance in the Serial Monitor 361 362 Serial.print("Distance (Mm): "); 363 USE_SERIAL.println(distanceMm); 364 // 365 Check if value read is different then send a websocket message to the server 366 367 if (distanceMm != sensorVal) 368 { 369 sensorVal = distanceMm; 370 //send JSON 371 message in this format {"value": 100} 372 doc["Distance"] = distanceMm; 373 374 // Declare a buffer to hold the result 375 376 char output[50]; 377 serializeJson(doc, 378 output); 379 380 // send message to server when Connected 381 webSocket.sendTXT(output); 382 383 } 384 // sleep for some time before next read 385 delay(100); 386} 387 388void 389 readPressureValue() 390{ 391 392 pressure_sensor.begin(DOUT, SCLK); 393 394 if 395 (pressure_sensor.is_ready()) 396{ 397 Serial.print("mmh20: "); 398 pressure=(pressure_sensor.pascal()-fixed_pressure)*2.469; 399 400 Serial.println(pressure); 401} 402 // Check if value read is different then 403 send a websocket message to the server 404 if (pressure != sensorVal) 405 { 406 407 sensorVal = pressure; 408 409 //send JSON message in this format {"value": 410 100} 411 doc["pressure"] = pressure; 412 413 414 // Declare a buffer 415 to hold the result 416 char output[50]; 417 418 serializeJson(doc, output); 419 420 421 // send message to server when Connected 422 webSocket.sendTXT(output); 423 424 } 425 426 // sleep for some time before next read 427 delay(100); 428} 429 430void 431 readDeviceTempValue() 432{ 433 434Serial.print("Temperature: "); 435float TempDevice=(temprature_sens_read() 436 - 32) / 1.8; 437Serial.print(TempDevice); 438Serial.println(" C"); 439delay(1000); 440 441 // Check if value read is different then send a websocket message to the server 442 443 if (TempDevice != sensorVal) 444 { 445 sensorVal = TempDevice; 446 447 //send 448 JSON message in this format {"value": 100} 449 doc["TempDevice"] = TempDevice; 450 451 // Declare a buffer to hold the result 452 char output[50]; 453 454 serializeJson(doc, 455 output); 456 457 // send message to server when Connected 458 webSocket.sendTXT(output); 459 460 } 461 462 // sleep for some time before next read 463 delay(100); 464} 465// 466 This will go into loop 467void clockwiserotation() 468{ 469 if(openingHeight>inputHeight) 470{ 471 472 digitalWrite(CW,HIGH); //Motor runs clockwise/ 473} 474} 475void counterclockwiserotation() 476{ 477 478 if(openingHeight>inputHeight) 479{ 480 digitalWrite(CCW, HIGH); 481} 482} 483void 484 motorstop() 485{ 486 if(userOpeningdifference<heightTolorance) 487{ 488 digitalWrite(CW, 489 LOW); //Motor stops// 490} 491} 492void obtain_Date_Time() { 493 while(!timeClient.update()) 494 { 495 timeClient.forceUpdate(); 496 } 497 Date = timeClient.getFormattedTime(); 498 499 Serial.println(Date); 500 501 int split = Date.indexOf("T"); 502 day = Date.substring(0, 503 split); 504 Serial.println(day); 505 Time = Date.substring(split+1, Date.length()-1); 506 507 Serial.println(Time); 508} 509 510void data_logging() { 511 Data = String(sensor_data) 512 + "," + String(day) + "," + String(Time) + "," + 513 String(distanceMm) 514 + "\ \ 515"; 516 Serial.print("Save data: "); 517 Serial.println(Data); 518 519 appendFile(SD, "/temperature_readings.txt", Data.c_str()); 520} 521 522void 523 writeFile(fs::FS &fs, const char * path, const char * message) { 524 Serial.printf("Writing 525 file: %s\ 526", path); 527 528 File file = fs.open(path, FILE_WRITE); 529 if(!file) 530 { 531 Serial.println("Failed to open file for writing"); 532 return; 533 534 } 535 if(file.print(message)) { 536 Serial.println("File written"); 537 538 } else { 539 Serial.println("Write failed"); 540 } 541 file.close(); 542} 543 544void 545 appendFile(fs::FS &fs, const char * path, const char * message) { 546 Serial.printf("Appending 547 to file: %s\ 548", path); 549 550 File file = fs.open(path, FILE_APPEND); 551 if(!file) 552 { 553 Serial.println("Failed to open file for appending"); 554 return; 555 556 } 557 if(file.print(message)) { 558 Serial.println("Message appended"); 559 560 } else { 561 Serial.println("Append failed"); 562 } 563 file.close(); 564} 565void 566 loop() 567{ 568 readDistanceValue(); 569 presssurereading(); 570 readPressureValue(); 571 572 readDeviceTempValue(); 573 clockwiserotation(); 574 counterclockwiserotation(); 575 576 motorstop(); 577 lmitswitch(); 578// obtainReadings(); 579 obtain_Date_Time(); 580 581 data_logging(); 582 sensor_data++; 583 webSocket.loop(); 584 webSocket3.loop(); 585}
automaic gate control
c_cpp
1/** LIBRARIES**/ 2#include <Arduino.h> 3#include <WiFi.h> 4#include <WiFiMulti.h> 5#include <WiFiClientSecure.h> 6#include <WebSocketsClient.h> 7#include <ArduinoJson.h> 8#include "HX710B.h" 9#include "FS.h" 10#include "SD.h" 11#include <SPI.h> 12#include <NTPClient.h> 13#include <WiFiUdp.h> 14/** LIBRARIES**/ 15 16/**SD CARD**/ 17uint64_t uS_TO_S_FACTOR = 1000000; 18uint64_t TIME_TO_SLEEP = 300; 19#define SD_CS 5 20RTC_DATA_ATTR int sensor_data = 0; 21String Data; 22WiFiUDP ntpUDP; 23NTPClient timeClient(ntpUDP); 24String Date; 25String day; 26String Time; 27/** SD CARD**/ 28 29/** WEBSOCKET INITALIZATION**/ 30WiFiMulti WiFiMulti; 31WebSocketsClient webSocket; 32WebSocketsClient webSocket3; 33/** WEBSOCKET INITALIZATION**/ 34 35/** PIN DECLARATION FOR PRESSURE AND ULTRASONIC RESPECTIVE**/ 36const int DOUT = 22; //sensor data pin 37const int SCLK = 24; //sensor clock pin 38float pressure; 39float fixed_pressure=549.7; 40HX710B pressure_sensor; 41 42static int sensorVal = 0; 43 44/** PIN DECLARATION FOR PRESSURE AND ULTRASONIC RESPECTIVE**/ 45const int trigPin = 5; 46const int echoPin = 18; 47long duration; 48float distanceMm; 49#define SOUND_SPEED 0.034 50int inputHeight=10; 51int heightTolorance=1; 52int fixedHeight=46; 53//int openingHeight; 54//int userOpeningdifference; 55 56/** calculation to control the motor**/ 57int openingHeight=fixedHeight-distanceMm; 58int userOpeningdifference=inputHeight-openingHeight; 59 /** calculation to control the motor**/ 60/** PIN DECLARATION FOR PRESSURE AND ULTRASONIC RESPECTIVE**/ 61 62/** FOR PRESSURE **/ 63//float pressure; 64float inputpressure=10; 65float pressureTolorance=1; 66float fixedpressure=46; 67 68/** calculation to control the motor**/ 69int openingpressure=fixedpressure-pressure; 70int userOpeningDifferencep=inputHeight-openingpressure; 71/** FOR RELAY**/ 72#define CW 14 //CW is defined as pin #7// 73#define CCW 15 //CCW is defined as pin #8// 74 75//for limit switch 76int Lswitch = 2; 77 78// CHANGE THIS TO ADD YOUR WIFI USERNAME/PASSWORD 79const char * WIFI_SSID = "TP-Link_27ED"; 80const char * WIFI_PASS = "19202122"; 81 82//Initialize the JSON data we send to our websocket server 83const int capacity = JSON_OBJECT_SIZE(3); 84StaticJsonDocument<capacity> doc; 85StaticJsonDocument<1024> rx_doc; 86 87 /** FOR DEVICE TEMPERATURE READING**/ 88#define USE_SERIAL Serial 89 90 #ifdef __cplusplus 91 92extern "C" { 93 94#endif 95 96uint8_t temprature_sens_read(); 97 98#ifdef __cplusplus 99 100} 101 102#endif 103 104uint8_t temprature_sens_read(); 105void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) 106{ 107 108 switch(type) 109 { 110 case WStype_DISCONNECTED: 111 USE_SERIAL.printf("[WSc] Disconnected!\ 112"); 113 break; 114 case WStype_CONNECTED: 115 USE_SERIAL.printf("[WSc] Connected to url: %s\ 116", payload); 117 break; 118 case WStype_TEXT: 119 USE_SERIAL.printf("[WSc] get text: %s\ 120", payload); 121 // deserializeJson(rx_doc,payload); 122 // // String height = rx_doc["height"]; 123 // USE_SERIAL.println(rx_doc["height"]); 124 break; 125 case WStype_BIN: 126 USE_SERIAL.printf("[WSc] get binary length: %u\ 127", length); 128 break; 129 case WStype_ERROR: 130// case WStype_FRAGMENT_TEXT_START: 131 case WStype_FRAGMENT_BIN_START: 132 case WStype_FRAGMENT: 133 case WStype_FRAGMENT_FIN: 134 case WStype_PING: 135 case WStype_PONG: 136 break; 137 } 138} 139/** FOR DEVICE TEMPERATURE READING**/ 140 141void setup() 142{ 143 //Set the baud rate 144 USE_SERIAL.begin(115200); 145 USE_SERIAL.printf("Begin websocket client program...."); 146 /** FOR WIFI CONNECTION **/ 147 USE_SERIAL.println(); 148 WiFi.mode(WIFI_STA); 149 WiFi.begin(WIFI_SSID, WIFI_PASS); 150 Serial.print("Connecting to WiFi .."); 151 while (WiFi.status() != WL_CONNECTED) { 152 Serial.print('.'); 153 delay(1000); 154 } 155/** FOR WIFI CONNECTION **/ 156 157 Serial.println(WiFi.localIP()); 158// for (uint8_t t = 4; t > 0; t--) 159// { 160// USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\ 161", t); 162// USE_SERIAL.flush(); 163// delay(1000); 164// } 165 166// WiFiMulti.addAP(WIFI_SSID, WIFI_PASS); 167 168 // WiFi.disconnect(); 169 USE_SERIAL.printf("Connecting"); 170// while (WiFiMulti.run() != WL_CONNECTED) 171// { 172// USE_SERIAL.printf("."); 173// delay(100); 174// } 175 USE_SERIAL.printf("Connected!"); 176 177 // server address, port and URL 178 webSocket.begin("192.168.0.141", 8085, "/sendSensorData"); 179 webSocket3.begin("192.168.0.141", 8085, "/request"); 180 181 // event handler 182 webSocket.onEvent(webSocketEvent); 183 webSocket3.onEvent(webSocketEvent); 184 // use HTTP Basic Authorization this is optional remove if not needed 185 // webSocket.setAuthorization("user", "Password"); 186 187 // try ever 5000 again if connection has failed 188 webSocket.setReconnectInterval(5000); 189 webSocket3.setReconnectInterval(10000); 190 191 192 // // Set the resolution values 193 // analogReadResolution(RESOLUTION); 194 pinMode(Lswitch, INPUT); 195 196 /**SD CARD**/ 197 timeClient.begin(); 198 timeClient.setTimeOffset(18000); 199 200 if(!SD.begin()) { 201 Serial.println("Card Mount Failed"); 202 return; 203 } 204 uint8_t cardType = SD.cardType(); 205 if(cardType == CARD_NONE) { 206 Serial.println("No SD card attached"); 207 return; 208 } 209 Serial.println("Initializing SD card..."); 210 if (!SD.begin()) { 211 Serial.println("SD card initialization failed!"); 212 return; 213 } 214 215 File file = SD.open("/sensor_readings.txt"); 216 if(!file) { 217 Serial.println("File does not exist"); 218 Serial.println("Creating file..."); 219 writeFile(SD, "/temperature_readings.txt", "Reading Number, Date, Hour, Temperature \ \ 220"); 221 } 222 else { 223 Serial.println("File exists"); 224 } 225 file.close(); 226 227 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); 228/** sensors.begin(); 229 obtainReadings(); 230 obtain_Date_Time(); 231 data_logging();**/ 232 Serial.println("Sensor data logged successfully! Going to sleep"); 233 esp_deep_sleep_start(); 234 pressure_sensor.begin(DOUT, SCLK); 235} 236 237void presssurereading() 238{ 239 if (pressure_sensor.is_ready()) 240{ 241 Serial.print("mmh20: "); 242 pressure=(pressure_sensor.pascal()-fixed_pressure)*2.469; 243 Serial.println(pressure); 244} 245} 246 247void lmitswitch() 248{ 249 if(digitalRead(Lswitch) == LOW) 250 { 251 digitalWrite(CW,LOW); //Motor runs clockwise 252 } 253 254 else if(digitalRead(Lswitch) == HIGH) 255 { 256 digitalWrite(CW,HIGH); //Motor runs clockwise/ 257 } 258} 259 260void readDistanceValue() 261{ 262pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 263pinMode(echoPin, INPUT); // Sets the echoPin as an Input 264 265digitalWrite(trigPin, LOW); 266 delayMicroseconds(2); 267 // Sets the trigPin on HIGH state for 10 micro seconds 268 digitalWrite(trigPin, HIGH); 269 delayMicroseconds(10); 270 digitalWrite(trigPin, LOW); 271 272 // Reads the echoPin, returns the sound wave travel time in microseconds 273 duration = pulseIn(echoPin, HIGH); 274 275 // Calculate the distance 276 distanceMm = (duration * SOUND_SPEED/2)*10; 277 278 // Prints the distance in the Serial Monitor 279 Serial.print("Distance (Mm): "); 280 USE_SERIAL.println(distanceMm); 281 // Check if value read is different then send a websocket message to the server 282 if (distanceMm != sensorVal) 283 { 284 sensorVal = distanceMm; 285 //send JSON message in this format {"value": 100} 286 doc["Distance"] = distanceMm; 287 // Declare a buffer to hold the result 288 289 char output[50]; 290 serializeJson(doc, output); 291 292 // send message to server when Connected 293 webSocket.sendTXT(output); 294 } 295 // sleep for some time before next read 296 delay(100); 297} 298 299void readPressureValue() 300{ 301 302 pressure_sensor.begin(DOUT, SCLK); 303 304 if (pressure_sensor.is_ready()) 305{ 306 Serial.print("mmh20: "); 307 pressure=(pressure_sensor.pascal()-fixed_pressure)*2.469; 308 Serial.println(pressure); 309} 310 // Check if value read is different then send a websocket message to the server 311 if (pressure != sensorVal) 312 { 313 sensorVal = pressure; 314 315 //send JSON message in this format {"value": 100} 316 doc["pressure"] = pressure; 317 318 319 // Declare a buffer to hold the result 320 char output[50]; 321 322 serializeJson(doc, output); 323 324 // send message to server when Connected 325 webSocket.sendTXT(output); 326 } 327 328 // sleep for some time before next read 329 delay(100); 330} 331 332void readDeviceTempValue() 333{ 334 335Serial.print("Temperature: "); 336float TempDevice=(temprature_sens_read() - 32) / 1.8; 337Serial.print(TempDevice); 338Serial.println(" C"); 339delay(1000); 340 // Check if value read is different then send a websocket message to the server 341 if (TempDevice != sensorVal) 342 { 343 sensorVal = TempDevice; 344 345 //send JSON message in this format {"value": 100} 346 doc["TempDevice"] = TempDevice; 347 // Declare a buffer to hold the result 348 char output[50]; 349 350 serializeJson(doc, output); 351 352 // send message to server when Connected 353 webSocket.sendTXT(output); 354 } 355 356 // sleep for some time before next read 357 delay(100); 358} 359// This will go into loop 360void clockwiserotation() 361{ 362 if(openingHeight>inputHeight) 363{ 364 digitalWrite(CW,HIGH); //Motor runs clockwise/ 365} 366} 367void counterclockwiserotation() 368{ 369 if(openingHeight>inputHeight) 370{ 371 digitalWrite(CCW, HIGH); 372} 373} 374void motorstop() 375{ 376 if(userOpeningdifference<heightTolorance) 377{ 378 digitalWrite(CW, LOW); //Motor stops// 379} 380} 381void obtain_Date_Time() { 382 while(!timeClient.update()) { 383 timeClient.forceUpdate(); 384 } 385 Date = timeClient.getFormattedTime(); 386 Serial.println(Date); 387 388 int split = Date.indexOf("T"); 389 day = Date.substring(0, split); 390 Serial.println(day); 391 Time = Date.substring(split+1, Date.length()-1); 392 Serial.println(Time); 393} 394 395void data_logging() { 396 Data = String(sensor_data) + "," + String(day) + "," + String(Time) + "," + 397 String(distanceMm) + "\ \ 398"; 399 Serial.print("Save data: "); 400 Serial.println(Data); 401 appendFile(SD, "/temperature_readings.txt", Data.c_str()); 402} 403 404void writeFile(fs::FS &fs, const char * path, const char * message) { 405 Serial.printf("Writing file: %s\ 406", path); 407 408 File file = fs.open(path, FILE_WRITE); 409 if(!file) { 410 Serial.println("Failed to open file for writing"); 411 return; 412 } 413 if(file.print(message)) { 414 Serial.println("File written"); 415 } else { 416 Serial.println("Write failed"); 417 } 418 file.close(); 419} 420 421void appendFile(fs::FS &fs, const char * path, const char * message) { 422 Serial.printf("Appending to file: %s\ 423", path); 424 425 File file = fs.open(path, FILE_APPEND); 426 if(!file) { 427 Serial.println("Failed to open file for appending"); 428 return; 429 } 430 if(file.print(message)) { 431 Serial.println("Message appended"); 432 } else { 433 Serial.println("Append failed"); 434 } 435 file.close(); 436} 437void loop() 438{ 439 readDistanceValue(); 440 presssurereading(); 441 readPressureValue(); 442 readDeviceTempValue(); 443 clockwiserotation(); 444 counterclockwiserotation(); 445 motorstop(); 446 lmitswitch(); 447// obtainReadings(); 448 obtain_Date_Time(); 449 data_logging(); 450 sensor_data++; 451 webSocket.loop(); 452 webSocket3.loop(); 453}
Downloadable files
circiut digram
circiut digram
Comments
Only logged in users can leave comments
Automatic gate control for agricalture purpose | Arduino Project Hub