Advertisement
Ogomegbunam

Sensor readings emailing

Mar 4th, 2023
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | Software | 0 0
  1. ARDUINO CODE
  2. // include the library code:
  3. #include <SoftwareSerial.h>
  4. SoftwareSerial s(5,6);
  5.  
  6. //Temperature sensor library
  7. #include <OneWire.h>
  8. #include <DallasTemperature.h>
  9. int ONE_WIRE_BUS=4;
  10. OneWire oneWire (ONE_WIRE_BUS);
  11. DallasTemperature sensors (&oneWire);
  12. #include <LiquidCrystal.h>
  13.  
  14. // initialize the library by associating any needed LCD interface pin
  15. // with the arduino pin number it is connected to
  16. const int rs = 9, en = 8, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
  17. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  18.  
  19. //Moisture sensor declaration
  20. int moisturePin = A0; // moisture sensor
  21. int thresholdValue = 800;
  22. int relaysignal = 7; //Relay pin
  23.  
  24. String str; //String declaration
  25.  
  26. void setup() {
  27. // put your setup code here, to run once:
  28. s.begin(115200);
  29. pinMode(moisturePin,INPUT); // mositure sensor
  30. pinMode(relaysignal,OUTPUT); // Relay module control
  31.  
  32. Serial.begin(115200);
  33. sensors.begin();
  34. delay(2000);
  35. // set up the LCD's number of columns and rows:
  36. lcd.begin(16, 2);
  37. // Print a message to the LCD.
  38. //lcd.print("pH:"); //PH setup disabled
  39. lcd.print("VL:");
  40.  
  41. lcd.setCursor(6,0);
  42. lcd.print("TEMP:");
  43.  
  44. lcd.setCursor(0,1);
  45. lcd.print("MOIST:");
  46.  
  47. }
  48.  
  49. void loop() {
  50. // put your main code here, to run repeatedly:
  51.  
  52. // soil moisture sensor
  53. int sensorValue = analogRead(moisturePin);
  54. int moistureconversion = (100 -(sensorValue * 0.09766));
  55. Serial.println ("MOISTURE READING");
  56. Serial.print (sensorValue);
  57. Serial.println (moistureconversion);
  58. Serial.print("%");
  59.  
  60. //Readings float conversion
  61. float C = sensors.getTempCByIndex(0);
  62. float F = sensors.getTempFByIndex(0);
  63. float M = moistureconversion;
  64.  
  65. //Temperature readings Serial printing
  66. sensors.requestTemperatures ();
  67. Serial.println("TEMPERATURE READING");
  68. Serial.print("Celsius temperature: ");
  69.  
  70. Serial.print(sensors.getTempCByIndex(0));
  71.  
  72. Serial.print(" - Fahrenheit temperature: ");
  73.  
  74. Serial.println (sensors.getTempFByIndex(0));
  75. Serial.println (" ");
  76.  
  77. //Moisture sensor and relay statement
  78. if (sensorValue < thresholdValue){
  79. Serial.println("- Doesn't need watering");
  80. digitalWrite (relaysignal,LOW);
  81.  
  82. lcd.setCursor(3,0); //Status of valve OFF
  83. lcd.print("OFF");
  84. }
  85. else{
  86. Serial.println("- Time to water your plant");
  87. digitalWrite (relaysignal,HIGH);
  88.  
  89. lcd.setCursor(3,0); //Status of valve ON
  90. lcd.print("~ON");
  91. }Serial.println (" ");
  92.  
  93. //LCD display
  94. // set the cursor to column 0, line 1
  95. // (note: line 1 is the second row, since counting begins with 0):
  96. lcd.setCursor(11, 0);
  97. lcd.print(sensors.getTempCByIndex(0));
  98. lcd.setCursor(6,1);
  99. lcd.print(moistureconversion);
  100. //lcd.setCursor(3,0); //position for PH readings
  101. //lcd.print("---");
  102. lcd.setCursor(9,1);
  103. lcd.print("%");
  104.  
  105. str =String("coming from Ogom: ")+String("MOISTURE LEVEL: ")+String(M)+String("%")+String(" ")+String("TEMPERATURE; in Centigrade: ")+String(C)+String(" ")+String("Farenheit: ")+String(F);
  106. s.println(str);
  107. delay(1000);
  108. }
  109.  
  110.  
  111. NODEMCU CODE
  112. /*
  113. * EMailSender library for Arduino, esp8266 and esp32
  114. * Simple esp8266 Gmail with Email From name send example
  115. *
  116. * https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
  117. *
  118. */
  119.  
  120. #include "Arduino.h"
  121. #include <EMailSender.h>
  122. #include <ESP8266WiFi.h>
  123.  
  124. const char* ssid = "******";
  125. const char* password = "*******";
  126.  
  127. uint8_t connection_state = 0;
  128. uint16_t reconnect_interval = 10000;
  129.  
  130. EMailSender emailSend("sending smtp enabled mail", "smtp password", "emailfrom@gmail.com", "Soil monitoring system");
  131.  
  132. uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
  133. {
  134. static uint16_t attempt = 0;
  135. Serial.print("Connecting to ");
  136. if(nSSID) {
  137. WiFi.begin(nSSID, nPassword);
  138. Serial.println(nSSID);
  139. }
  140.  
  141. uint8_t i = 0;
  142. while(WiFi.status()!= WL_CONNECTED && i++ < 50)
  143. {
  144. delay(200);
  145. Serial.print(".");
  146. }
  147. ++attempt;
  148. Serial.println("");
  149. if(i == 51) {
  150. Serial.print("Connection: TIMEOUT on attempt: ");
  151. Serial.println(attempt);
  152. if(attempt % 2 == 0)
  153. Serial.println("Check if access point available or SSID and Password\r\n");
  154. return false;
  155. }
  156. Serial.println("Connection: ESTABLISHED");
  157. Serial.print("Got IP address: ");
  158. Serial.println(WiFi.localIP());
  159. return true;
  160. }
  161.  
  162. void Awaits()
  163. {
  164. uint32_t ts = millis();
  165. while(!connection_state)
  166. {
  167. delay(50);
  168. if(millis() > (ts + reconnect_interval) && !connection_state){
  169. connection_state = WiFiConnect();
  170. ts = millis();
  171. }
  172. }
  173. }
  174.  
  175. void setup()
  176. {
  177.  
  178. Serial.begin(115200);
  179. while (!Serial) {
  180. }
  181.  
  182. connection_state = WiFiConnect(ssid, password);
  183. if(!connection_state) // if not connected to WIFI
  184. Awaits(); // constantly trying to connect
  185. }
  186.  
  187. void loop()
  188. {
  189. if (Serial.available()){
  190. // Serial.write(Serial.read());
  191. int s=Serial.read();
  192. Serial.write(s);
  193. }
  194. int s = Serial.read();
  195. EMailSender::EMailMessage message;
  196. message.subject = "Soil monitor update";
  197. message.message = s;
  198. //"Ciao come stai<br>io bene.<br>www.mischianti.org";
  199.  
  200. EMailSender::Response resp = emailSend.send("recieving mail", message);
  201.  
  202. Serial.println("Sending status: ");
  203.  
  204. Serial.println(resp.status);
  205. Serial.println(resp.code);
  206. Serial.println(resp.desc);
  207.  
  208. delay (5000);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement