Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #define FLASH_OFF 2000
- #define FLASH_ON 50
- #define lowpass_filter 0.01f
- #define pulseHIGH 60 // approx. 2/3 of peak height
- #define pulseLOW 30 // approx. 1/3 of peak height
- //#define DEBUG
- #ifdef DEBUG
- #define DEBUGprint(s) Serial.print(s);
- #define DEBUGprintln(s) Serial.println(s);
- #else
- #define DEBUGprint(s)
- #define DEBUGprintln(s)
- #endif
- const char* ssid = "Your WiFi"; // The SSID (name) of the Wi-Fi network you want to connect to
- const char* password = "WiFi Password"; // The password of the Wi-Fi network
- #define API "YOUR THINGSPEAK API WRITE KEY GOES HERE"
- const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
- WiFiClient client;
- void setup() {
- // initialize serial communications at 9600 bps:
- Serial.begin(9600);
- doWifi();
- }
- void gotoSleep(void)
- {
- // Do nothing, but function exists in case is useful later
- }
- void doWifi(void)
- {
- WiFi.begin(ssid, password); // Connect to the network
- Serial.println("Connecting to ");
- Serial.print(ssid); Serial.println(" ...");
- int i = 0;
- while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
- delay(200);
- i++;
- if (i>100) {Serial.println("Couldn't connect to WiFi.");gotoSleep();}
- }
- Serial.println('\n');
- Serial.println("Connection established!");
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- long pulseTimer=0, thisPulse=0, lastPost=0;
- uint8_t ledState = 0, pulseState=0, pulseCount=0;
- float baseline = 0, peaks = 0;
- int sensorValue = 0,cleanSignal=0; // value read from the pot
- while (true)
- {
- // read the analog in value:
- sensorValue = analogRead(analogInPin);
- baseline = baseline*(1-lowpass_filter) + lowpass_filter*sensorValue;
- cleanSignal = sensorValue - (int)baseline;
- if (pulseState==0 && cleanSignal>=pulseHIGH) // signal is going high
- {
- pulseState = 1;
- pulseCount++;
- DEBUGprint("Pulse #"); DEBUGprintln(pulseCount);
- thisPulse = millis();
- if (pulseCount==1) pulseTimer = thisPulse; // first pulse, starting the timer
- else if (pulseCount==11) // End of tenth pulse, stop the timer
- {
- pulseCount = 0;
- float pulsePeriod,powerkW;
- pulsePeriod = (millis()-pulseTimer)/10.0f; // average period of pulses in milliseconds
- powerkW = 3600.0f/pulsePeriod; // each pulse is one 1000th of a kW
- DEBUGprint("Average period:"); DEBUGprintln(pulsePeriod/1000.0f);
- DEBUGprint("Average power/kW:"); DEBUGprintln(powerkW);
- if (lastPost==0 || millis()-lastPost>(5*60*1000)) // If haven't posted data yet, or a certain time since last post, then post data
- {
- if (SendData(pulsePeriod/1000.0f, powerkW)) lastPost = millis(); // if successful post, reset timer
- }
- }
- }
- else if (pulseState==1 && cleanSignal<pulseLOW) // signal was high, now going low
- {
- pulseState = 0;
- if (millis()-thisPulse>100) {pulseCount = 0; DEBUGprintln("Bad pulse");}// means that a weird 'pulse' was detected maybe due to changing light condiitions. Reset counting.
- }
- #ifndef DEBUG
- Serial.print(100*pulseState); Serial.print(" ");
- Serial.print(baseline); Serial.print(" ");
- Serial.println(cleanSignal);
- #endif
- // wait 2 milliseconds before the next loop for the analog-to-digital
- // converter to settle after the last reading:
- delay(2);
- }
- }
- // SendData takes two floating variables and posts them to ThingSpeak using the write API key 'API'
- // Returns: true if successful, else false
- uint8_t SendData(float a, float b)
- {
- char buffer[10];
- String a_str, b_str;
- String url;
- dtostrf(a,0,1,buffer);
- a_str=buffer; a_str.replace(".","%2E");
- dtostrf(b,0,2,buffer);
- b_str=buffer; b_str.replace(".","%2E");
- url = "&field1=" + a_str + "&field2=" + b_str;
- if (client.connect("api.thingspeak.com", 80))
- {
- client.print(String("GET /update.blank?api_key=") + API + url + " HTTP/1.1\r\n");
- client.print("Host: api.thingspeak.com\r\n");
- client.print("Connection: close\r\n\r\n");
- unsigned long timeout = millis(); // Wait for client response, or timeout
- while (client.available() == 0)
- {
- if (millis() - timeout > 5000)
- {
- Serial.println(">>> Client Timeout !");
- client.stop();
- return 0;
- }
- } // end client not available and didn't time out
- Serial.println("Success");
- return 1;
- } //end if
- else
- {
- Serial.println("Couldn't connect to client");
- }
- return 0;
- } // end SendData
Add Comment
Please, Sign In to add comment