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