· 5 years ago · Oct 10, 2020, 10:34 AM
1#include <ESP8266WiFi.h>
2#include <WiFiClient.h>
3
4#define FLASH_OFF 2000
5#define FLASH_ON 50
6#define lowpass_filter 0.01f
7#define pulseHIGH 60 // approx. 2/3 of peak height
8#define pulseLOW 30 // approx. 1/3 of peak height
9
10//#define DEBUG
11
12#ifdef DEBUG
13#define DEBUGprint(s) Serial.print(s);
14#define DEBUGprintln(s) Serial.println(s);
15#else
16#define DEBUGprint(s)
17#define DEBUGprintln(s)
18#endif
19
20const char* ssid = "Your WiFi"; // The SSID (name) of the Wi-Fi network you want to connect to
21const char* password = "WiFi Password"; // The password of the Wi-Fi network
22#define API "YOUR THINGSPEAK API WRITE KEY GOES HERE"
23
24const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
25
26WiFiClient client;
27
28void setup() {
29 // initialize serial communications at 9600 bps:
30 Serial.begin(9600);
31 doWifi();
32}
33
34void gotoSleep(void)
35{
36 // Do nothing, but function exists in case is useful later
37}
38
39void doWifi(void)
40{
41 WiFi.begin(ssid, password); // Connect to the network
42 Serial.println("Connecting to ");
43 Serial.print(ssid); Serial.println(" ...");
44
45 int i = 0;
46 while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
47 delay(200);
48 i++;
49 if (i>100) {Serial.println("Couldn't connect to WiFi.");gotoSleep();}
50 }
51
52 Serial.println('\n');
53 Serial.println("Connection established!");
54 Serial.print("IP address:\t");
55 Serial.println(WiFi.localIP());
56}
57
58void loop() {
59long pulseTimer=0, thisPulse=0, lastPost=0;
60uint8_t ledState = 0, pulseState=0, pulseCount=0;
61float baseline = 0, peaks = 0;
62int sensorValue = 0,cleanSignal=0; // value read from the pot
63
64 while (true)
65 {
66 // read the analog in value:
67 sensorValue = analogRead(analogInPin);
68
69 baseline = baseline*(1-lowpass_filter) + lowpass_filter*sensorValue;
70 cleanSignal = sensorValue - (int)baseline;
71
72 if (pulseState==0 && cleanSignal>=pulseHIGH) // signal is going high
73 {
74 pulseState = 1;
75 pulseCount++;
76
77 DEBUGprint("Pulse #"); DEBUGprintln(pulseCount);
78 thisPulse = millis();
79 if (pulseCount==1) pulseTimer = thisPulse; // first pulse, starting the timer
80 else if (pulseCount==11) // End of tenth pulse, stop the timer
81 {
82 pulseCount = 0;
83 float pulsePeriod,powerkW;
84 pulsePeriod = (millis()-pulseTimer)/10.0f; // average period of pulses in milliseconds
85 powerkW = 3600.0f/pulsePeriod; // each pulse is one 1000th of a kW
86
87 DEBUGprint("Average period:"); DEBUGprintln(pulsePeriod/1000.0f);
88 DEBUGprint("Average power/kW:"); DEBUGprintln(powerkW);
89
90 if (lastPost==0 || millis()-lastPost>(5*60*1000)) // If haven't posted data yet, or a certain time since last post, then post data
91 {
92 if (SendData(pulsePeriod/1000.0f, powerkW)) lastPost = millis(); // if successful post, reset timer
93 }
94 }
95 }
96 else if (pulseState==1 && cleanSignal<pulseLOW) // signal was high, now going low
97 {
98 pulseState = 0;
99 if (millis()-thisPulse>100) {pulseCount = 0; DEBUGprintln("Bad pulse");}// means that a weird 'pulse' was detected maybe due to changing light condiitions. Reset counting.
100 }
101
102#ifndef DEBUG
103 Serial.print(100*pulseState); Serial.print(" ");
104 Serial.print(baseline); Serial.print(" ");
105 Serial.println(cleanSignal);
106
107#endif
108 // wait 2 milliseconds before the next loop for the analog-to-digital
109 // converter to settle after the last reading:
110
111 delay(2);
112 }
113}
114
115// SendData takes two floating variables and posts them to ThingSpeak using the write API key 'API'
116// Returns: true if successful, else false
117uint8_t SendData(float a, float b)
118{
119 char buffer[10];
120 String a_str, b_str;
121 String url;
122
123 dtostrf(a,0,1,buffer);
124 a_str=buffer; a_str.replace(".","%2E");
125
126 dtostrf(b,0,2,buffer);
127 b_str=buffer; b_str.replace(".","%2E");
128 url = "&field1=" + a_str + "&field2=" + b_str;
129
130 if (client.connect("api.thingspeak.com", 80))
131 {
132 client.print(String("GET /update.blank?api_key=") + API + url + " HTTP/1.1\r\n");
133 client.print("Host: api.thingspeak.com\r\n");
134 client.print("Connection: close\r\n\r\n");
135
136 unsigned long timeout = millis(); // Wait for client response, or timeout
137
138 while (client.available() == 0)
139 {
140 if (millis() - timeout > 5000)
141 {
142 Serial.println(">>> Client Timeout !");
143 client.stop();
144 return 0;
145 }
146 } // end client not available and didn't time out
147 Serial.println("Success");
148 return 1;
149 } //end if
150 else
151 {
152 Serial.println("Couldn't connect to client");
153 }
154 return 0;
155} // end SendData