Advertisement
safwan092

Untitled

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