Advertisement
safwan092

Untitled

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