Advertisement
safwan092

Untitled

Dec 13th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.13 KB | None | 0 0
  1. #include "DHT.h"
  2. #include <Wire.h>
  3. #include <WiFi.h>
  4. #include <HTTPClient.h>
  5. #include <TinyGPSPlus.h>
  6. #include <Adafruit_INA219.h>
  7. #include "DFRobot_VEML7700.h"
  8.  
  9. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  10. // Variables To Send To Google Sheets
  11. //--------------------------------------------------------------------------------
  12. String dht22Sensor_Temp_Value = "";
  13. String dht22Sensor_Hum_Value = "";
  14. String dustSensor_Value = "";
  15. String gpsModem_Lat_Value = "";
  16. String gpsModem_Lon_Value = "";
  17. String gpsModem_Alt_Value = "";
  18. String luxSensor_Value = "";
  19. String ina219Sensor_Voltage_Value = "";
  20. String ina219Sensor_Current_Value = "";
  21. String ina219Sensor_Power_Value = "";
  22. String windSpeedSensor_Value = "";
  23.  
  24. const char * ssid = "deema";
  25. const char * password = "deema1234";
  26. String GOOGLE_SCRIPT_ID = "AKfycbz9To4wZ7uxCKOBePFnlfdxYs5zvvyWHuRebSzWNOljgb6fGAul-aNL-IRDO8qkx2ry";
  27.  
  28. unsigned long dataMillis = 0;
  29. unsigned long sendInterval = 10000; // 10000 [milliSecond] = 10 [Seconds]
  30. //--------------------------------------------------------------------------------
  31. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  32. // Temperature and Humidity Sensor Connections with ESP32
  33. // [DHT22 Sensor]
  34. //--------------------------------------------------------------------------------
  35. // // DHT22 [+] ----> ESP32 [5V]
  36. #define DHTPIN 5 // DHT22 [out] ----> ESP32 [D5]
  37. // // DHT22 [-] ----> ESP32 [GND]
  38. //--------------------------------------------------------------------------------
  39. // Dust Sensor Connections with ESP32
  40. // [Sharp Optical Dust Sensor GP2Y1014AU0F]
  41. //--------------------------------------------------------------------------------
  42. #define DustPin 34 // Dust [BLACK] ----> ESP32 [D34]
  43. #define ledPower 15 // Dust [WHITE] ----> ESP32 [D15]
  44. // // Dust [RED] ----> ESP32 [5V]
  45. // // Dust [BLUE] ----> N/C
  46. // // Dust [YELLOW]----> ESP32 [GND]
  47. //--------------------------------------------------------------------------------
  48. // GPS Modem Connections with ESP32
  49. // [Neo-6M GPS Modem]
  50. //--------------------------------------------------------------------------------
  51. // // GPS [VCC] ----> ESP32 [3.3V]
  52. // // GPS [RX] ----> ESP32 [TX2]
  53. // // GPS [TX] ----> ESP32 [RX2]
  54. // // GPS [GND] ----> ESP32 [GND]
  55. //--------------------------------------------------------------------------------
  56. // LUX Sensor Connections with ESP32
  57. // [VEML 7700 LUX Sensor]
  58. //--------------------------------------------------------------------------------
  59. // // LUX [VIN] ----> ESP32 [3.3V]
  60. // // LUX [3Vo] ----> N/C
  61. // // LUX [GND] ----> ESP32 [GND]
  62. // // LUX [SCL] ----> ESP32 [D22]
  63. // // LUX [SDA] ----> ESP32 [D21]
  64. //--------------------------------------------------------------------------------
  65. // Power Voltage/Current Sensor Connections with ESP32
  66. // [INA219 DC Current Monitor Sensor]
  67. //--------------------------------------------------------------------------------
  68. // // INA219 [VCC] ----> ESP32 [3.3V]
  69. // // INA219 [GND] ----> ESP32 [GND]
  70. // // INA219 [SCL] ----> ESP32 [D22]
  71. // // INA219 [SDA] ----> ESP32 [D21]
  72. //--------------------------------------------------------------------------------
  73. // Wind Speed Sensor Connections with ESP32
  74. // [PR-3000-FSJT-N01 Wind Speed Sensor]
  75. //--------------------------------------------------------------------------------
  76. // // Wind [GND] ----> ESP32 [GND]
  77. #define RXD2 2 // Wind [RXD] ----> ESP32 [D2]
  78. #define TXD2 4 // Wind [TXD] ----> ESP32 [D4]
  79. // // Wind [VCC] ----> ESP32 [VIN]
  80. //--------------------------------------------------------------------------------
  81. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  82. // Temperature and Humidity Sensor Variables
  83. // [DHT22 Sensor]
  84. //--------------------------------------------------------------------------------
  85. #define DHTTYPE DHT22
  86. DHT dht(DHTPIN, DHTTYPE);
  87. //--------------------------------------------------------------------------------
  88. // Dust Sensor Variables
  89. // [Sharp Optical Dust Sensor GP2Y1014AU0F]
  90. //--------------------------------------------------------------------------------
  91. int samplingTime = 280;
  92. int deltaTime = 40;
  93. int sleepTime = 9680;
  94. float voMeasured = 0;
  95. float calcVoltage = 0;
  96. float dustDensity = 0;
  97. //--------------------------------------------------------------------------------
  98. // GPS Modem Variables
  99. // [Neo-6M GPS Modem]
  100. //--------------------------------------------------------------------------------
  101. TinyGPSPlus gps;
  102. String gpsLat, gpsLon, gpsAlt;
  103. //--------------------------------------------------------------------------------
  104. // LUX Sensor Variables
  105. // [VEML 7700 LUX Sensor]
  106. //--------------------------------------------------------------------------------
  107. DFRobot_VEML7700 LUX_Sensor_VEML7700;
  108. float lux;
  109. //--------------------------------------------------------------------------------
  110. // Power Voltage/Current Sensor Variables
  111. // [INA219 DC Current Monitor Sensor]
  112. //--------------------------------------------------------------------------------
  113. Adafruit_INA219 ina219;
  114. float shuntvoltage = 0;
  115. float busvoltage = 0;
  116. float current_mA = 0;
  117. float loadvoltage = 0;
  118. float power_mW = 0;
  119. //--------------------------------------------------------------------------------
  120. // Wind Speed Sensor Variables
  121. // [PR-3000-FSJT-N01 Wind Speed Sensor]
  122. //--------------------------------------------------------------------------------
  123. byte ByteArray[250];
  124. int ByteData[20];
  125. //--------------------------------------------------------------------------------
  126. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  127. void setup() {
  128. Serial.begin(9600);
  129. dht22Sensor_Setup();
  130. dustSensor_Setup();
  131. gps_Setup();
  132. luxSensor_Setup();
  133. ina219_Setup();
  134. windSpeedSensor_Setup();
  135. WiFi_Setup();
  136. }//end of Setup
  137.  
  138. void loop() {
  139. read_dht22Sensor();
  140. read_dustSensor();
  141. read_gpsModem();
  142. read_luxSensor();
  143. read_ina219Sensor();
  144. read_windSpeedSensor();
  145. //-------------------------------------------
  146. if (millis() - dataMillis > sendInterval) {
  147. dataMillis = millis();
  148. Send_Data_To_Google_Sheets();
  149. }
  150. //-------------------------------------------
  151. }//end of LOOP
  152.  
  153. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  154. // Temperature and Humidity Sensor Functions
  155. // [DHT22 Sensor]
  156. //--------------------------------------------------------------------------------
  157. void dht22Sensor_Setup() {
  158. dht.begin();
  159. }
  160. void read_dht22Sensor() {
  161. float h = dht.readHumidity();
  162. float t = dht.readTemperature();
  163. float f = dht.readTemperature(true);
  164. if (isnan(h) || isnan(t) || isnan(f)) {
  165. Serial.println(F("Failed to read from DHT sensor!"));
  166. return;
  167. }
  168. float hif = dht.computeHeatIndex(f, h);
  169. float hic = dht.computeHeatIndex(t, h, false);
  170. Serial.print(F("Humidity: "));
  171. Serial.print(h);
  172. Serial.print(F("% Temperature: "));
  173. Serial.print(t);
  174. Serial.print(F("°C "));
  175. Serial.print(f);
  176. Serial.print(F("°F Heat index: "));
  177. Serial.print(hic);
  178. Serial.print(F("°C "));
  179. Serial.print(hif);
  180. Serial.println(F("°F"));
  181. dht22Sensor_Temp_Value = String(t);
  182. dht22Sensor_Hum_Value = String(h);
  183.  
  184. }
  185. //--------------------------------------------------------------------------------
  186. // Dust Sensor Functions
  187. // [Sharp Optical Dust Sensor GP2Y1014AU0F]
  188. //--------------------------------------------------------------------------------
  189. void dustSensor_Setup() {
  190. pinMode(ledPower, OUTPUT);
  191. }
  192. void read_dustSensor() {
  193. digitalWrite(ledPower, LOW); // power on the LED
  194. delayMicroseconds(samplingTime);
  195. voMeasured = analogRead(DustPin); // read the dust value
  196. delayMicroseconds(deltaTime);
  197. digitalWrite(ledPower, HIGH); // turn the LED off
  198. delayMicroseconds(sleepTime);
  199. // 0 - 5V mapped to 0 - 1023 integer values
  200. // recover voltage
  201. calcVoltage = voMeasured * (5.0 / 1024.0);
  202. // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  203. // Chris Nafis (c) 2012
  204. dustDensity = 170 * calcVoltage - 0.1;
  205. Serial.println(dustDensity); // unit: ug/m3
  206.  
  207. dustSensor_Value = String(dustDensity);
  208.  
  209. }
  210. //--------------------------------------------------------------------------------
  211. // GPS Modem Functions
  212. // [Neo-6M GPS Modem]
  213. //--------------------------------------------------------------------------------
  214. void gps_Setup() {
  215. Serial.begin(115200);
  216. Serial2.begin(9600);//Serial Channel for GPS with ESP32 [TX2+RX2]
  217.  
  218. }
  219. void read_gpsModem() {
  220. if (Serial2.available() > 0) {
  221. if (gps.encode(Serial2.read())) {
  222. ///////////////////////////////////////////
  223. if (gps.location.isValid()) {
  224. float gpslat_float = gps.location.lat();
  225. gpsLat = String(gpslat_float, 6);
  226. Serial.print(F("- latitude: "));
  227. Serial.println(gpsLat);
  228. float gpslon_float = gps.location.lng();
  229. gpsLon = String(gpslon_float, 6);
  230. Serial.print(F("- longitude: "));
  231. Serial.println(gpsLon);
  232. } else {
  233. Serial.println(F("- location: INVALID"));
  234. gpsLat = "0.00";
  235. gpsLon = "0.00";
  236. }
  237. Serial.print(F("- speed: "));
  238. if (gps.altitude.isValid()) {
  239. float altMeters = gps.altitude.meters();
  240. gpsAlt = String(altMeters, 2);
  241. Serial.print(gpsAlt);
  242. Serial.println(F(" Meter"));
  243. } else {
  244. Serial.println(F("INVALID"));
  245. gpsAlt = "0.00";
  246. }
  247. Serial.println();
  248.  
  249. gpsModem_Lat_Value = String(gpsLat);
  250. gpsModem_Lon_Value = String(gpsLon);
  251. gpsModem_Alt_Value = String(gpsAlt);
  252.  
  253. ///////////////////////////////////////////
  254. }//end encoding
  255. }
  256. if (millis() > 5000 && gps.charsProcessed() < 10)
  257. Serial.println(F("No GPS data received: check wiring"));
  258. }
  259. //--------------------------------------------------------------------------------
  260. // LUX Sensor Functions
  261. // [VEML 7700 LUX Sensor]
  262. //--------------------------------------------------------------------------------
  263. void luxSensor_Setup() {
  264. LUX_Sensor_VEML7700.begin();
  265. }
  266. void read_luxSensor() {
  267. LUX_Sensor_VEML7700.getALSLux(lux);
  268. Serial.print("Lux:");
  269. Serial.print(lux);
  270. Serial.println(" lx");
  271.  
  272. luxSensor_Value = String(lux);
  273.  
  274. }
  275. //--------------------------------------------------------------------------------
  276. // Power Voltage/Current Sensor Functions
  277. // [INA219 DC Current Monitor Sensor]
  278. //--------------------------------------------------------------------------------
  279. void ina219_Setup() {
  280. // Initialize the INA219.
  281. // By default the initialization will use the largest range (32V, 2A).
  282. if (! ina219.begin()) {
  283. Serial.println("Failed to find INA219 chip");
  284. while (1) {
  285. delay(10);
  286. }
  287. }
  288. }
  289. void read_ina219Sensor() {
  290. shuntvoltage = ina219.getShuntVoltage_mV();
  291. busvoltage = ina219.getBusVoltage_V();
  292. current_mA = ina219.getCurrent_mA();
  293. current_mA = current_mA / 1000.00;// convert to Amp
  294. power_mW = ina219.getPower_mW();
  295. power_mW = power_mW / 1000.00;// convert to Watt
  296. loadvoltage = busvoltage + (shuntvoltage / 1000);
  297.  
  298. Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
  299. Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  300. Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
  301. Serial.print("Current: "); Serial.print(current_mA); Serial.println(" A");
  302. Serial.print("Power: "); Serial.print(power_mW); Serial.println(" W");
  303. Serial.println("");
  304.  
  305. ina219Sensor_Voltage_Value = String(loadvoltage);
  306. ina219Sensor_Current_Value = String(current_mA);
  307. ina219Sensor_Power_Value = String(power_mW);
  308.  
  309. }
  310. //--------------------------------------------------------------------------------
  311. // Wind Speed Sensor Functions
  312. // [PR-3000-FSJT-N01 Wind Speed Sensor]
  313. //--------------------------------------------------------------------------------
  314. void windSpeedSensor_Setup() {
  315. Serial1.begin(4800, SERIAL_8N1, RXD2, TXD2);
  316. }
  317. void read_windSpeedSensor() {
  318. delay(200);
  319. byte msgfs[] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B };
  320. int i;
  321. int len = 8;
  322. for (i = 0; i < len; i++) {
  323. Serial1.write(msgfs[i]);
  324. }
  325. len = 0;
  326. int a = 0;
  327. while (Serial1.available()) {
  328. ByteArray[a] = Serial1.read();
  329. a++;
  330. }
  331. int b = 0;
  332. String registros;
  333. for (b = 0; b < a; b++) {
  334. registros = String(ByteArray[b], HEX);
  335. }
  336. ByteData[0] = ByteArray[3] * 256 + ByteArray[4];
  337. float winds;
  338. winds = ByteData[0] * 0.1;
  339.  
  340. Serial.print("Wind Speed = ");
  341. Serial.print(winds);
  342. Serial.println(" m/s");
  343. windSpeedSensor_Value = String(winds);
  344. //delay(50);//200
  345. }
  346. //--------------------------------------------------------------------------------
  347. void WiFi_Setup() {
  348. WiFi.mode(WIFI_STA);
  349. WiFi.begin(ssid, password);
  350. Serial.print("Connecting to Wi-Fi");
  351. while (WiFi.status() != WL_CONNECTED) {
  352. delay(500);
  353. Serial.print(".");
  354. }
  355. Serial.println("OK");
  356. }
  357.  
  358. /*
  359.  
  360. dht22Sensor_Temp_Value
  361. dht22Sensor_Hum_Value
  362. dustSensor_Value
  363. gpsModem_Lat_Value
  364. gpsModem_Lon_Value
  365. gpsModem_Alt_Value
  366. luxSensor_Value
  367. ina219Sensor_Voltage_Value
  368. ina219Sensor_Current_Value
  369. ina219Sensor_Power_Value
  370. windSpeedSensor_Value
  371.  
  372. */
  373. void Send_Data_To_Google_Sheets() {
  374. String param;
  375. param = "dataA=" + dht22Sensor_Temp_Value;
  376. param += "&dataB=" + dht22Sensor_Hum_Value;
  377. param += "&dataC=" + dustSensor_Value;
  378. param += "&dataD=" + gpsModem_Lat_Value;
  379. param += "&dataE=" + gpsModem_Lon_Value;
  380. param += "&dataF=" + gpsModem_Alt_Value;
  381. param += "&dataG=" + luxSensor_Value;
  382. param += "&dataH=" + ina219Sensor_Voltage_Value;
  383. param += "&dataJ=" + ina219Sensor_Current_Value;
  384. param += "&dataK=" + ina219Sensor_Power_Value;
  385. param += "&dataL=" + windSpeedSensor_Value;
  386. write_to_google_sheet(param);
  387. }
  388.  
  389. void write_to_google_sheet(String params) {
  390. HTTPClient http;
  391. String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?" + params;
  392. //Serial.print(url);
  393. Serial.println("Postring GPS data to Google Sheet");
  394. //---------------------------------------------------------------------
  395. //starts posting data to google sheet
  396. http.begin(url.c_str());
  397. http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  398. int httpCode = http.GET();
  399. Serial.print("HTTP Status Code: ");
  400. Serial.println(httpCode);
  401. //---------------------------------------------------------------------
  402. //getting response from google sheet
  403. String payload;
  404. if (httpCode > 0) {
  405. payload = http.getString();
  406. Serial.println("Payload: " + payload);
  407. }
  408. //---------------------------------------------------------------------
  409. http.end();
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement