User Tools

Site Tools


amc2022:groupz:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

amc2022:groupz:start [2022/09/11 23:17] – [Table] ziad001amc2022:groupz:start [2023/01/05 14:38] (current) – external edit 127.0.0.1
Line 210: Line 210:
 <code c++> <code c++>
  
-/********************************** 
  * AMC Group Z   * AMC Group Z 
  * Monitoring Drought Effects  * Monitoring Drought Effects
- */+ 
  
-#include <WiFi.h> +#include <WiFi.h> //1 
-#include <PubSubClient.h> +#include <PubSubClient.h> //2 
-#include <EEPROM.h> +#include <EEPROM.h> //3 
-#include <Adafruit_MLX90614.h> +#include <Adafruit_MLX90614.h> //4 
-#include <DFRobot_DHT11.h> +#include <DFRobot_DHT11.h> //5 
-DFRobot_DHT11 DHT;+DFRobot_DHT11 DHT; //6
  
-#define AOUT_PIN 36 +#define AOUT_PIN 36 //7 
-#define DHT11_PIN 14 +#define DHT11_PIN 14 //8 
-#define EEPROM_BYTES 2 +#define EEPROM_BYTES 2 //9 
-//#define RestartPin 27 +//#define RestartPin 27 //10 
-#define uS_TO_S_FACTOR 1000000 +#define uS_TO_S_FACTOR 1000000 //10 
-#define TIME_TO_SLEEP  882+#define TIME_TO_SLEEP  882 //11
  
-RTC_DATA_ATTR int bootCount = 0;+RTC_DATA_ATTR int bootCount = 0; //12
  
-Adafruit_MLX90614 mlx = Adafruit_MLX90614();+Adafruit_MLX90614 mlx = Adafruit_MLX90614(); //13
  
-int dry = 2677; +int dry = 2677; //14 
-int wet = 977;+int wet = 977; //15
  
 // WiFi // WiFi
-const char *ssid = "Vodafone-D285"; +const char *ssid = "Vodafone-D285"; //16 
-const char *password = "JgDHNp4JrEejmGgA";+const char *password = "JgDHNp4JrEejmGgA"; //17
  
 // MQTT Broker // MQTT Broker
-const char *mqtt_broker = "broker.emqx.io"; +const char *mqtt_broker = "broker.emqx.io"; //18 
-const char *topic_1 = "soil_moisture_value_group_z"; +const char *topic_1 = "soil_moisture_value_group_z"; //19 
-const char *topic_2 = "temperature_group_z"; +const char *topic_2 = "temperature_group_z"; //20 
-const char *topic_3 = "air_humidity_group_z"; +const char *topic_3 = "air_humidity_group_z"; //21 
-const char *topic_4 = "temperature2_group_z"; +const char *topic_4 = "temperature2_group_z"; //22 
-const char *topic_5 = "ground_temperature_group_z"; +const char *topic_5 = "ground_temperature_group_z"; //23
-//const char *topic_down = "amc2022/groupz/down"; +
-const int mqtt_port = 1883;+
  
-WiFiClient espClient; +const int mqtt_port = 1883//24
-PubSubClient client(espClient);+
  
-void callback(char *topic, byte *payload, unsigned int length) { +WiFiClient espClient; //25 
- Serial.print("Message arrived in topic: "); +PubSubClient client(espClient); //26 
- Serial.println(topic);+ 
 +void callback(char *topic, byte *payload, unsigned int length) { //27 
 + Serial.print("Message arrived in topic: ");  
 + Serial.println(topic); 
  Serial.print("Message:");  Serial.print("Message:");
- for (int i = 0; i < length; i++) {+ for (int i = 0; i < length; i++) { 
      Serial.print((char) payload[i]);      Serial.print((char) payload[i]);
  }  }
Line 265: Line 264:
 } }
  
-void print_wakeup_reason(){+void print_wakeup_reason(){ //28
   esp_sleep_wakeup_cause_t wakeup_reason;   esp_sleep_wakeup_cause_t wakeup_reason;
  
Line 276: Line 275:
 } }
      
-void setup() {+void setup() { //29
  
   EEPROM.begin(EEPROM_BYTES);   EEPROM.begin(EEPROM_BYTES);
-  Serial.begin(115200); +  Serial.begin(115200); //30 
-  while (!Serial);+  while (!Serial); //31
  
-  if (!mlx.begin()) {+  if (!mlx.begin()) { //32
     Serial.println("Error connecting to MLX sensor. Check wiring.");     Serial.println("Error connecting to MLX sensor. Check wiring.");
     while (1);     while (1);
   };   };
-  delay(1000);+  delay(1000); //33
      
-  ++bootCount;+  ++bootCount; //34
   Serial.println("Boot number: " + String(bootCount));   Serial.println("Boot number: " + String(bootCount));
-  print_wakeup_reason();+  print_wakeup_reason();//35
  
-  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);+  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //36
   Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +   Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
   " Seconds");   " Seconds");
  
-  Serial.println("Transmitting data.");+  Serial.println("Transmitting data."); //37
   delay(1000);   delay(1000);
-  WiFi.begin(ssid, password); +  WiFi.begin(ssid, password); //38 
-  while (WiFi.status() != WL_CONNECTED) { +  while (WiFi.status() != WL_CONNECTED) { //39 
-    delay(1000); +    delay(1000);  
-    Serial.println("Connecting to WiFi.."); +    Serial.println("Connecting to WiFi.."); //40 
-    Serial.println("Connected to the WiFi network");+    Serial.println("Connected to the WiFi network"); 
      
   //connecting to a mqtt broker   //connecting to a mqtt broker
-  client.setServer(mqtt_broker, mqtt_port);+  client.setServer(mqtt_broker, mqtt_port); //41
   client.setCallback(callback);   client.setCallback(callback);
      
-  while (!client.connected()) {+  while (!client.connected()) {  //42
     String client_id = "esp32-client-";     String client_id = "esp32-client-";
     client_id += String(WiFi.macAddress());     client_id += String(WiFi.macAddress());
     Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());     Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
          
-if (client.connect(client_id.c_str())) {+if (client.connect(client_id.c_str())) {  //43
     Serial.println("Public emqx mqtt broker connected");     Serial.println("Public emqx mqtt broker connected");
     }      } 
-    else {+    else {  //44
     Serial.print("failed with state ");     Serial.print("failed with state ");
     Serial.print(client.state());     Serial.print(client.state());
Line 325: Line 324:
 } }
  
-void loop() {+void loop() {  //45
  
-  char soil_moisture_value[3];+  char soil_moisture_value[3];  //46
   char air_temperature[3];   char air_temperature[3];
   char air_humidity[3];   char air_humidity[3];
Line 333: Line 332:
   char ground_temperature[3];   char ground_temperature[3];
      
-  int value = analogRead(AOUT_PIN); +  int value = analogRead(AOUT_PIN);   //47 
-  int percHumidity = map(value, dry, wet, 0, 100);+  int percHumidity = map(value, dry, wet, 0, 100);  //48
  
   delay(2000);   delay(2000);
  
-  if ((percHumidity > -5) && (percHumidity < 105)) {  +  if ((percHumidity > -5) && (percHumidity < 105)) { //49 
-     Serial.print("Soil Moisture Value: ");+     Serial.print("Soil Moisture Value: ");  //50
      Serial.println(percHumidity);      Serial.println(percHumidity);
  
-  dtostrf(percHumidity,1,0, soil_moisture_value); +  dtostrf(percHumidity,1,0, soil_moisture_value);  //51  
-  client.publish(topic_1, soil_moisture_value);+  client.publish(topic_1, soil_moisture_value);   //52
   }   }
   delay(2000);   delay(2000);
Line 383: Line 382:
   delay(1000);   delay(1000);
      
-  Serial.flush(); +  Serial.flush();  //53
   esp_deep_sleep_start();   esp_deep_sleep_start();
   Serial.println("This will never be printed");   Serial.println("This will never be printed");
Line 594: Line 593:
  
 Powering ESP32 directly with a 9v battery is not recommended [29]. Therefore we need to use a voltage regulator to supply the ESP32 with 5 volts only. Using the Pololu voltage regulator and a voltmeter, we regulated the voltage to 5.39 Volts, as shown in the picture below, so the voltage that was reaching the ESP32 was 5.39 Volts. Powering ESP32 directly with a 9v battery is not recommended [29]. Therefore we need to use a voltage regulator to supply the ESP32 with 5 volts only. Using the Pololu voltage regulator and a voltmeter, we regulated the voltage to 5.39 Volts, as shown in the picture below, so the voltage that was reaching the ESP32 was 5.39 Volts.
-^{{ :amc2022:groupz:battery-pololu-voltage.jpg?nolink&400 |}}^ +^ {{ :amc2022:groupz:battery-pololu-voltage.jpg?400&nolink }}                                                           
-|//**Figure 14**// Voltage values reaching the ESP32 when connected with a voltage regulator|+| //**Figure 14**// Voltage values reaching the ESP32 when connected with a voltage regulator, ''Photo by Ziad Helou''  |
  
  
amc2022/groupz/start.1662931046.txt.gz · Last modified: 2023/01/05 14:38 (external edit)