Advertisement
Guest User

Arduino Mega

a guest
Apr 13th, 2020
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1.  
  2.  
  3. #include <Adafruit_Sensor.h>
  4. #include <DHT.h>
  5. #include <DHT_U.h>
  6.  
  7.  
  8. String s = "";
  9. String readInput;
  10.  
  11. //Temp Sensor
  12. #define DHT11_PIN 31
  13. DHT dht(DHT11_PIN, DHT11);
  14. int curTemp = 68;
  15. int curHumidity = 30;
  16.  
  17. void setup() {
  18.  
  19.   //Serial3 (NOTICE THE "3") is where/how the Mega & ESP will communicate, and it's important to run it slow, e.g. 9600
  20.   Serial3.begin(9600);
  21.  
  22.   //this is just normal serial output (e.g. Serial Monitor)
  23.   Serial.begin(115200);
  24.   Serial.println("Mega Ready to rock!  Starting loop:");
  25. }
  26.  
  27. String readString;
  28. int cnt = 0;
  29. int testValueToESP = 100;
  30.  
  31. int loopCnt_SensorsReport = 0;
  32. int loopCntDelay_SensorsReport = 120000;
  33.  
  34. void loop() {
  35.  
  36.   //this listens for INCOMING message from the ESP.  If an incoming msg exists, it compiles it into a string
  37.   char incomingByte;
  38.   while (Serial3.available() > 0)
  39.   {
  40.     delay(10); //if the data came
  41.     incomingByte = Serial3.read(); //read byte
  42.     readString += incomingByte;
  43.   }
  44.  
  45.   //if there was a new message, we act on it.  In this case we are just turning on/off the built-in LED.
  46.   //couple things to note here that really threw me for a loop:
  47.   //  notice the .equals comparison method, I found this to be necessary
  48.   //  notice the 1\r\n and 0\r\n values I'm comparing.  It wasn't until I realized the \r\n (carriage return
  49.   //         & new line where included) that I got the comparisons to work properly
  50.   if (readString != "") {
  51.     Serial.print("received this: ");
  52.     Serial.println(readString);
  53.  
  54.     if (readString.equals("1\r\n")) {
  55.       Serial.println("turning on BUILTIN LED");
  56.       digitalWrite(LED_BUILTIN, HIGH);
  57.     } else if (readString.equals("0\r\n")) {
  58.       Serial.println("turning off BUILTIN LED");
  59.       digitalWrite(LED_BUILTIN, LOW);
  60.     } else {
  61.       Serial.print("unhandled event: ");
  62.       Serial.println(readString);
  63.     }
  64.   }
  65.  
  66.   //clear the incoming message so we don't act on it again
  67.   readString = "";
  68.  
  69.   //every ~10k milliseconds, let's sent a # to the ESP (this was just a simple communications test)
  70.   if (cnt <= 10000) {
  71.     cnt++;
  72.     delay(1);
  73.   } else {
  74.     cnt = 0;
  75.     testValueToESP++;
  76.     Serial3.print("t: ");
  77.     Serial3.println(testValueToESP);
  78.   }
  79.  
  80.   //at a regular interval, let's read the temp & humidity sensor, serialize the info into JSON, and send it to the ESP so it
  81.   //can do something interesting with the sensor data, like log it to a database.
  82.   loopCnt_SensorsReport++;
  83.   if (loopCnt_SensorsReport > loopCntDelay_SensorsReport)
  84.   {
  85.     Serial.println("loopCntDelay_SensorsReport hit!");
  86.     curTemp = dht.readTemperature(true);
  87.     curHumidity = dht.readHumidity();
  88.  
  89.     loopCnt_SensorsReport = 0; //reset
  90.  
  91.     //construct some JSON
  92.     String sensorRpt = "{ 'Type': 'SensorReport', 'Temp': " + String(curTemp) + ", 'Humdity': " + String(curHumidity) + "}";
  93.  
  94.     //send the JSON to the ESP
  95.     Serial3.println(sensorRpt);
  96.    
  97.   }
  98.  
  99.   //this was for my personal testing to slow things down, but obviously it's currently at "0"
  100.   delay(0);
  101. }
  102.  
  103. //end of Mega sketch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement