Created
July 1, 2016 05:08
-
-
Save woodif/395e9b110be6e396d2bd5dc35b249a72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESPert.h> | |
#include <ESP8266WiFi.h> | |
#include "CMMCInterval.h" | |
void CurrentPrint(); | |
CMMCInterval myInterval1(1, CurrentPrint); | |
float out = 0; | |
double sum = 0; | |
unsigned long count = 0; | |
float avg = 0 ; | |
float amp = 0; | |
float watt = 0; | |
float watt_h = 0; | |
extern "C" { | |
#include "ets_sys.h" | |
#include "os_type.h" | |
#include "osapi.h" | |
#include "mem.h" | |
#include "user_interface.h" | |
#include "cont.h" | |
} | |
ESPert espert; | |
#define DEBUG | |
#define DEBUG_PRINTER Serial | |
#ifdef DEBUG | |
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); } | |
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); } | |
#else | |
#define DEBUG_PRINT(...) {} | |
#define DEBUG_PRINTLN(...) {} | |
#endif | |
const char* ssid = "@ESPertAP_001"; | |
const char* password = "espertap"; | |
const int sleepTimeS = 300; | |
void connectWifi(); | |
void reconnectWifiIfLinkDown(); | |
void uploadThingsSpeak(float adc, float amp, float watt_h, float powersum); | |
void setup() { | |
Serial.begin(115200); | |
pinMode(A0, INPUT); | |
unsigned long rtc_time = system_get_rtc_time(); | |
unsigned long clock_time = system_get_time(); | |
rst_info* info = system_get_rst_info(); | |
//rst_info* info = ESP.getResetInfoPtr(); | |
int reason = info->reason; | |
int cause = info->exccause; | |
connectWifi(); | |
} | |
void loop() { | |
myInterval1.Update(); | |
sum += analogRead(A0); | |
count ++; | |
uploadThingsSpeak(avg, amp, watt, watt_h); | |
//ESP.deepSleep(sleepTimeS * 6000000); | |
reconnectWifiIfLinkDown(); | |
} | |
void reconnectWifiIfLinkDown() { | |
if (WiFi.status() != WL_CONNECTED) { | |
DEBUG_PRINTLN("WIFI DISCONNECTED"); | |
connectWifi(); | |
} | |
} | |
void connectWifi() { | |
DEBUG_PRINTLN(); | |
DEBUG_PRINTLN(); | |
DEBUG_PRINT("Connecting to "); | |
DEBUG_PRINTLN(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
DEBUG_PRINT("."); | |
} | |
DEBUG_PRINTLN(""); | |
DEBUG_PRINTLN("WiFi connected"); | |
DEBUG_PRINTLN("IP address: "); | |
DEBUG_PRINTLN(WiFi.localIP()); | |
} | |
void uploadThingsSpeak(float avg, float amp, float watt, float watt_h) { | |
espert.led.on(); | |
static const char* host = "api.thingspeak.com"; | |
static const char* apiKey = "WIJM5KZNNK0YWW43"; | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
DEBUG_PRINTLN("connection failed"); | |
return; | |
} | |
// We now create a URI for the request | |
String url = "/update/"; | |
// url += streamId; | |
url += "?key="; | |
url += apiKey; | |
url += "&field1="; | |
url += avg; | |
url += "&field2="; | |
url += amp; | |
url += "&field3="; | |
url += watt; | |
url += "&field4="; | |
url += watt_h; | |
DEBUG_PRINT("Requesting URL: "); | |
DEBUG_PRINTLN(url); | |
// This will send the request to the server | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(1000); | |
espert.led.off(); | |
} | |
void CurrentPrint() | |
{ | |
avg = sum / (double)count; | |
count = 0; | |
sum = 0; | |
////////////////////////////////// | |
amp = abs ((avg * 0.010051480051480051) - 0.09195624195624198); | |
////////////////////////////////// | |
watt = amp * 220; | |
////////////////////////////////// | |
watt_h = watt / 36000; | |
//Serial.println(avg); | |
//Serial.println(amp); | |
//Serial.println(watt); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment