safwan092

Untitled

Jan 2nd, 2024
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. /*
  2. • Wifi Hotspot No Hardcoded
  3. */
  4. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  5. #include <otadrive_esp.h>
  6. #include <WiFi.h>
  7. #include "WiFiClientSecure.h"
  8. #include "Adafruit_MQTT.h"
  9. #include "Adafruit_MQTT_Client.h"
  10. #include <OneWire.h>
  11. #include <DallasTemperature.h>
  12.  
  13.  
  14.  
  15. #define code_version "[email protected]"
  16.  
  17. /************************* WiFi Access Point *********************************/
  18.  
  19. //#define WLAN_SSID "OBESEGAS"
  20. //#define WLAN_PASS "12345@ABCDE"
  21.  
  22. /************************* Adafruit.io Setup *********************************/
  23.  
  24. #define AIO_SERVER "io.adafruit.com"
  25.  
  26. // Using port 8883 for MQTTS
  27. #define AIO_SERVERPORT 8883
  28.  
  29. // Adafruit IO Account Configuration
  30. // (to obtain these values, visit https://io.adafruit.com and click on Active Key)
  31. #define AIO_USERNAME "GJeddah12233"
  32. #define AIO_KEY "aio_oqRs03If7NMF8uSFRQ35jCnev3tX"
  33.  
  34. /************ Global State (you don't need to change this!) ******************/
  35.  
  36. // WiFiFlientSecure for SSL/TLS support
  37. WiFiClientSecure clientmqtt;
  38.  
  39. // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
  40. Adafruit_MQTT_Client mqtt(&clientmqtt, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
  41.  
  42. // io.adafruit.com root CA
  43. const char* adafruitio_root_ca = \
  44. "-----BEGIN CERTIFICATE-----\n"
  45. "MIIEjTCCA3WgAwIBAgIQDQd4KhM/xvmlcpbhMf/ReTANBgkqhkiG9w0BAQsFADBh\n"
  46. "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
  47. "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQ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hbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n"
  63. "HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n"
  64. "AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n"
  65. "c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n"
  66. "ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n"
  67. "jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n"
  68. "TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n"
  69. "7h4SeM6Y8l/7MBRpPCz6l8Y=\n"
  70. "-----END CERTIFICATE-----\n";
  71.  
  72. /****************************** Feeds ***************************************/
  73.  
  74. Adafruit_MQTT_Publish Pressure_Publish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Pressure");
  75. Adafruit_MQTT_Publish Temperature_Inside_Publish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature_Inside");
  76. Adafruit_MQTT_Publish Temperature_Outside_Publish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature_Outside");
  77. Adafruit_MQTT_Publish MQ135_CO2_Publish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/MQ135_CO2");
  78. Adafruit_MQTT_Publish MQ4_Methane_Publish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/MQ4_Methane");
  79.  
  80. /************************* I/O Pin Definitions *****************************/
  81.  
  82. #define Pressure_pin 32
  83. #define MQ135_pin 34 // CO2 Gas Sensor
  84. #define MQ4_pin 35 // Methane Gas Sensor
  85. #define ONE_WIRE_BUS 4 // Temperature Sensors
  86.  
  87. /********************** Global Variables and Objects ***********************/
  88.  
  89. unsigned long previousMillis1 = 0;
  90. unsigned long previousMillis2 = 0;
  91. float pressureZero = 409.5; //analog reading of pressure transducer at (0 psi)
  92. float pressureMax = 3685.5; //analog reading of pressure transducer at (100 psi)
  93. float pressureValue = 0;
  94. float Temp_Inside = 0;
  95. float Temp_Outside = 0;
  96. float Pressure_RAW_Reading = 0;
  97. int MQ135_gas_RAW_Reading_CO2 = 0;
  98. int MQ4_gas_RAW_Reading_Methane = 0;
  99. OneWire oneWire(ONE_WIRE_BUS);
  100. DallasTemperature sensors(&oneWire);
  101. DeviceAddress insideThermometer = { 0x28, 0xB5, 0x27, 0x94, 0x97, 0x04, 0x03, 0x3F };//[Tank/Inside]
  102. DeviceAddress outsideThermometer = { 0x28, 0x30, 0x16, 0x94, 0x97, 0x0A, 0x03, 0xAB };//[Box/Outside]
  103.  
  104. /*************************** Sketch Code ************************************/
  105.  
  106. void setup() {
  107.  
  108. Serial.begin(115200);
  109. Initialize_Sensors();
  110. Connect_To_WiFi();
  111. Connect_To_WiFi_And_MQTT_Server();
  112. OTADRIVE.setInfo("397b2ca6-458a-438e-8f24-29506888c9ce", code_version);
  113. Serial.print("Code Version: ");
  114. Serial.println(code_version);
  115.  
  116. }
  117.  
  118. void loop() {
  119. ReConnect_To_WiFi();
  120. ota();
  121. MQTT_connect();
  122. Read_Sensor_Data();
  123. if (millis() - previousMillis1 >= 1000) {
  124. previousMillis1 = millis();
  125. Show_Sensor_Data_on_Serial_Monitor();
  126. Serial.println("Show Data Every 1 Second");
  127. Serial.println("-----------------------------");
  128. }
  129. if (millis() - previousMillis2 >= 12000) {
  130. previousMillis2 = millis();
  131. Publish_Sensor_Data();
  132. Serial.println("*************************************************");
  133. Serial.println("Data Published to IOT Cloud Every 12 Seconds");
  134. Serial.println("*************************************************");
  135. }
  136. }
  137.  
  138. /*************************** Functions ************************************/
  139.  
  140. void Initialize_Sensors() {
  141. pinMode(Pressure_pin, INPUT);
  142. pinMode(MQ135_pin, INPUT);
  143. pinMode(MQ4_pin, INPUT);
  144. sensors.begin();
  145. sensors.setResolution(insideThermometer, 9);
  146. sensors.setResolution(outsideThermometer, 9);
  147.  
  148. }
  149.  
  150. float GetTemperature(DeviceAddress deviceAddress) {
  151. float tempC = sensors.getTempC(deviceAddress);
  152. return tempC;
  153. }
  154.  
  155. void Read_Sensor_Data() {
  156. sensors.requestTemperatures();
  157. pressureValue = analogRead(Pressure_pin);
  158. pressureValue = map(pressureValue, pressureZero, pressureMax, 0, 100); // 0 psi to 100 psi
  159. Temp_Inside = sensors.getTempC(insideThermometer);//GetTemperature(insideThermometer); // -10°C to +85°C
  160. Temp_Outside = sensors.getTempC(outsideThermometer);//GetTemperature(outsideThermometer); // -10°C to +85°C
  161. MQ135_gas_RAW_Reading_CO2 = analogRead(MQ135_pin); // 0 to 4096
  162. MQ4_gas_RAW_Reading_Methane = analogRead(MQ4_pin); // 0 to 4096
  163.  
  164. }
  165.  
  166. void Show_Sensor_Data_on_Serial_Monitor() {
  167.  
  168. Serial.print("Pressure: ");
  169. Serial.print(pressureValue, 1);
  170. Serial.println(" [psi]");
  171. Serial.print("Temperature[Tank/Inside]: ");
  172. Serial.print(Temp_Inside);
  173. Serial.println(" [°C]");
  174. Serial.print("Temperature[Box/Outside]: ");
  175. Serial.print(Temp_Outside);
  176. Serial.println(" [°C]");
  177. Serial.print("Gas CO2: ");
  178. Serial.print(MQ135_gas_RAW_Reading_CO2);
  179. Serial.println(" [VoltageLevel]");
  180. Serial.print("Gas Methane: ");
  181. Serial.print(MQ4_gas_RAW_Reading_Methane);
  182. Serial.println(" [VoltageLevel]");
  183. Serial.println("-----------------------------");
  184.  
  185. }
  186.  
  187. void Publish_Sensor_Data() {
  188.  
  189. MQTT_connect();
  190.  
  191. Pressure_Publish.publish(pressureValue);
  192. Temperature_Inside_Publish.publish(Temp_Inside);
  193. Temperature_Outside_Publish.publish(Temp_Outside);
  194. MQ135_CO2_Publish.publish(MQ135_gas_RAW_Reading_CO2);
  195. MQ4_Methane_Publish.publish(MQ4_gas_RAW_Reading_Methane);
  196.  
  197. }
  198.  
  199. void MQTT_connect() {
  200. int8_t ret;
  201.  
  202. // Stop if already connected.
  203. if (mqtt.connected()) {
  204. return;
  205. }
  206.  
  207. Serial.print("Connecting to MQTT... ");
  208.  
  209. uint8_t retries = 2500;
  210. while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  211. Serial.println(mqtt.connectErrorString(ret));
  212. Serial.println("Retrying MQTT connection in 5 seconds...");
  213. mqtt.disconnect();
  214. delay(5000); // wait 5 seconds
  215. retries--;
  216. //if (retries == 0) {
  217. // basically die and wait for WDT to reset me
  218. //while (1);
  219. //}
  220. }
  221.  
  222. Serial.println("MQTT Connected!");
  223. }
  224.  
  225. void Connect_To_WiFi_And_MQTT_Server() {
  226. delay(10);
  227. Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));
  228. /*
  229. // Connect to WiFi access point.
  230. Serial.println();
  231. Serial.println();
  232. Serial.print("Connecting to ");
  233. Serial.println(WLAN_SSID);
  234. delay(1000);
  235. WiFi.begin(WLAN_SSID, WLAN_PASS);
  236. delay(2000);
  237. while (WiFi.status() != WL_CONNECTED) {
  238. delay(500);
  239. Serial.print(".");
  240. }
  241. Serial.println();
  242. Serial.println("WiFi connected");
  243. Serial.println("IP address: ");
  244. Serial.println(WiFi.localIP());
  245. */
  246. // Set Adafruit IO's root CA
  247. clientmqtt.setCACert(adafruitio_root_ca);
  248. }
  249.  
  250. void ota()
  251. {
  252. if (OTADRIVE.timeTick(30))
  253. {
  254. OTADRIVE.updateFirmware();
  255. }
  256. }
  257.  
  258.  
  259. void ReConnect_To_WiFi() {
  260. while (WiFi.status() != WL_CONNECTED) {
  261. //delay(500);
  262. Serial.print(".");
  263. Connect_To_WiFi();
  264. }
  265. }
  266.  
  267. void Connect_To_WiFi() {
  268. WiFiManager wm;
  269. bool res;
  270. res = wm.autoConnect("Composting IOT Project [Setup WiFi]"); // password protected ap
  271.  
  272. if (!res) {
  273. Serial.println("Failed to Connect!!");
  274. // ESP.restart();
  275. }
  276. else {
  277. //if you get here you have connected to the WiFi
  278. Serial.println("Connected...");
  279. }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment