Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_Sensor.h>
- #include <DHT.h>
- #include <DHT_U.h>
- String s = "";
- String readInput;
- //Temp Sensor
- #define DHT11_PIN 31
- DHT dht(DHT11_PIN, DHT11);
- int curTemp = 68;
- int curHumidity = 30;
- void setup() {
- //Serial3 (NOTICE THE "3") is where/how the Mega & ESP will communicate, and it's important to run it slow, e.g. 9600
- Serial3.begin(9600);
- //this is just normal serial output (e.g. Serial Monitor)
- Serial.begin(115200);
- Serial.println("Mega Ready to rock! Starting loop:");
- }
- String readString;
- int cnt = 0;
- int testValueToESP = 100;
- int loopCnt_SensorsReport = 0;
- int loopCntDelay_SensorsReport = 120000;
- void loop() {
- //this listens for INCOMING message from the ESP. If an incoming msg exists, it compiles it into a string
- char incomingByte;
- while (Serial3.available() > 0)
- {
- delay(10); //if the data came
- incomingByte = Serial3.read(); //read byte
- readString += incomingByte;
- }
- //if there was a new message, we act on it. In this case we are just turning on/off the built-in LED.
- //couple things to note here that really threw me for a loop:
- // notice the .equals comparison method, I found this to be necessary
- // notice the 1\r\n and 0\r\n values I'm comparing. It wasn't until I realized the \r\n (carriage return
- // & new line where included) that I got the comparisons to work properly
- if (readString != "") {
- Serial.print("received this: ");
- Serial.println(readString);
- if (readString.equals("1\r\n")) {
- Serial.println("turning on BUILTIN LED");
- digitalWrite(LED_BUILTIN, HIGH);
- } else if (readString.equals("0\r\n")) {
- Serial.println("turning off BUILTIN LED");
- digitalWrite(LED_BUILTIN, LOW);
- } else {
- Serial.print("unhandled event: ");
- Serial.println(readString);
- }
- }
- //clear the incoming message so we don't act on it again
- readString = "";
- //every ~10k milliseconds, let's sent a # to the ESP (this was just a simple communications test)
- if (cnt <= 10000) {
- cnt++;
- delay(1);
- } else {
- cnt = 0;
- testValueToESP++;
- Serial3.print("t: ");
- Serial3.println(testValueToESP);
- }
- //at a regular interval, let's read the temp & humidity sensor, serialize the info into JSON, and send it to the ESP so it
- //can do something interesting with the sensor data, like log it to a database.
- loopCnt_SensorsReport++;
- if (loopCnt_SensorsReport > loopCntDelay_SensorsReport)
- {
- Serial.println("loopCntDelay_SensorsReport hit!");
- curTemp = dht.readTemperature(true);
- curHumidity = dht.readHumidity();
- loopCnt_SensorsReport = 0; //reset
- //construct some JSON
- String sensorRpt = "{ 'Type': 'SensorReport', 'Temp': " + String(curTemp) + ", 'Humdity': " + String(curHumidity) + "}";
- //send the JSON to the ESP
- Serial3.println(sensorRpt);
- }
- //this was for my personal testing to slow things down, but obviously it's currently at "0"
- delay(0);
- }
- //end of Mega sketch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement