· 6 years ago · Jan 14, 2020, 06:24 PM
1//James Harris & Davion Clark
2#include <PulseSensorPlayground.h>
3
4
5
6#include "ThingSpeak.h"
7#include "WiFiEsp.h"
8#include <dht.h>
9#define DHT11_PIN 8 // where the dht11 is connected
10dht DHT;
11
12#define RX 6
13#define TX 7
14
15#define SECRET_SSID "RPiNetwork" // replace MySSID with your WiFi network name
16#define SECRET_PASS "CPSC1234567890" // replace MyPassword with your WiFi password
17
18#define SECRET_CH_ID 959944 // replace 0000000 with your channel number
19#define SECRET_WRITE_APIKEY "WSSNI8FUWFLTGKXV" // replace XYZ with your channel write API Key
20
21char ssid[] = SECRET_SSID; // your network SSID (name)
22char pass[] = SECRET_PASS; // your network password
23
24int keyIndex = 0; // your network key Index number (needed only for WEP)
25
26int PulseSensorPurplePin = 0; // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0
27int LED13 = 13; // The on-board Arduion LED
28
29
30int Signal; // holds the incoming raw data. Signal value can range from 0-1024
31int Threshold = 550;
32
33WiFiEspClient client;
34
35// Emulate esp on pins 6/7 if not present
36#ifndef HAVE_HWesp
37#include "SoftwareSerial.h"
38SoftwareSerial esp(RX, TX); // RX, TX
39#define ESP_BAUDRATE 19200
40#else
41#define ESP_BAUDRATE 115200
42#endif
43
44unsigned long myChannelNumber = SECRET_CH_ID;
45
46const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
47
48int number = 0; // Initial value you will upload to Cloud
49
50void setup() {
51 //Initialize serial and wait for port to open
52 pinMode(LED13,OUTPUT);
53 Serial.begin(115200);
54
55 // initialize serial for ESP module
56 setEspBaudRate(ESP_BAUDRATE);
57
58 while (!Serial) {
59 ; // wait for serial port to connect. Needed for Leonardo native USB port only
60 }
61
62 Serial.print("Searching for ESP8266...");
63 // initialize ESP module
64 WiFi.init(&esp);
65
66 // check for the presence of the shield
67 if (WiFi.status() == WL_NO_SHIELD) {
68 Serial.println("WiFi shield not present");
69 // don't continue
70 while (true);
71 }
72 Serial.println("found it!");
73
74 ThingSpeak.begin(client); // Initialize ThingSpeak
75}
76
77void loop() {
78
79 // Connect or reconnect to WiFi
80 if(WiFi.status() != WL_CONNECTED){
81 Serial.print("Attempting to connect to SSID: ");
82 Serial.println(SECRET_SSID);
83 while(WiFi.status() != WL_CONNECTED){
84 WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
85 Serial.print(".");
86 delay(5000);
87 }
88 Serial.println("\nConnected.");
89 }
90 Signal = analogRead(PulseSensorPurplePin); // Read the PulseSensor's value.
91 // Assign this value to the "Signal" variable.
92
93
94 if(Signal > Threshold){ // If the signal is above "550", then "turn-on" Arduino's on-Board LED.
95 digitalWrite(LED13,HIGH);
96 } else {
97 digitalWrite(LED13,LOW); // Else, the sigal must be below "550", so "turn-off" this LED.
98 }
99
100 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
101 // pieces of information in a channel. Here, we write to field 1.
102 int chk = DHT.read11(DHT11_PIN);
103 float C = DHT.temperature;
104 float H = DHT.humidity;
105 ThingSpeak.setField(1,C);
106 ThingSpeak.setField(2,H);
107 ThingSpeak.setField(3,Signal);
108 ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
109 //int x = ThingSpeak.writeField(myChannelNumber, 1, C, myWriteAPIKey);
110 //delay(15000);
111 //int y = ThingSpeak.writeField(myChannelNumber, 2, H, myWriteAPIKey);
112
113// if((x == 200)&& (y == 200)){
114// Serial.println("Channel update successful.");
115// Serial.print("Current Temperature = ");
116// Serial.println(DHT.temperature);
117// Serial.print("Current Humidity = ");
118// Serial.println(DHT.humidity);
119//
120// }
121// else{
122// Serial.println("Problem updating channel. HTTP error code " + String(x));
123// }
124
125 // change the value
126 number++;
127 if(number > 99){
128 number = 0;
129 }
130
131 delay(20000); // Wait 20 seconds to update the channel again
132}
133
134// This function attempts to set the ESP8266 baudrate.
135// Boards with additional hardware serial ports can use 115200,
136// otherwise software serial is limited to 19200.
137
138void setEspBaudRate(unsigned long baudrate){
139 long rates[6] = {115200,74880,57600,38400,19200,9600};
140
141 Serial.print("Setting ESP8266 baudrate to ");
142 Serial.print(baudrate);
143 Serial.println("...");
144
145 for(int i = 0; i < 6; i++){
146 esp.begin(rates[i]);
147 delay(100);
148 esp.print("AT+UART_DEF=");
149 esp.print(baudrate);
150 esp.print(",8,1,0,0\r\n");
151 delay(100);
152 }
153
154 esp.begin(baudrate);
155}