#include const int slavePIN = 4; // set up the speed, data order and data mode SPISettings settingsSPI(1000000, MSBFIRST, SPI_MODE3); const int VOLTAGE_SCALE = 675; //680 const float CURRENT_SCALE = 31.5; //250 const int RMS_VOLTAGE_ADDRESS = 0x2C; const int RMS_CURRENT_ADDRESS = 0x3F; const int STATUS_BITS = 0x1A; // Include the ESP8266 WiFi library. (Works a lot like the // Arduino WiFi library.) #include // Include the SparkFun Phant library. #include ////////////////////// // WiFi Definitions // ////////////////////// const char WiFiSSID[] = "****SSID****"; const char WiFiPSK[] = "****PASS****"; ///////////////////// // Pin Definitions // ///////////////////// const int LED_PIN = 2; // Thing's onboard, green LED //////////////// // Phant Keys // //////////////// const char PhantHost[] = "data.sparkfun.com"; const char PublicKey[] = "2J8dEzMvpGhoAXg3zy6E"; const char PrivateKey[] = "GPpYDNwBjgFn1aEvprM8"; ///////////////// // Post Timing // ///////////////// const unsigned long postRate = 10000; unsigned long lastPost = 0; #include "RunningMedian.h" const int sampleCount = 11; RunningMedian voltageSamples = RunningMedian(sampleCount); RunningMedian currentSamples = RunningMedian(sampleCount); void setup() { Serial.begin(115200); // pinMode(DIGITAL_PIN, INPUT_PULLUP); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); connectWiFi(); Serial.begin(115200); // set the Slave Select Pins as outputs: pinMode (slavePIN, OUTPUT); digitalWrite (slavePIN, HIGH); // initialize SPI: SPI.begin(); } void loop() { if (lastPost + postRate <= millis()) { if (postToPhant()) lastPost = millis(); else delay(100); } } int32_t readSPI(uint8_t address) { uint8_t val0, val1, val2; int32_t result; // read three bytes from device A SPI.beginTransaction(settingsSPI); digitalWrite (slavePIN, LOW); // reading only, so data sent does not matter SPI.transfer(0x01); SPI.transfer(address << 2); val0 = SPI.transfer(0); val1 = SPI.transfer(0); val2 = SPI.transfer(0); digitalWrite (slavePIN, HIGH); SPI.endTransaction(); result = (val0 << 8) | val1; result = (result << 8) | val2; return result; } void connectWiFi() { byte ledStatus = LOW; // Set WiFi mode to station (as opposed to AP or AP_STA) WiFi.mode(WIFI_STA); // WiFI.begin([ssid], [passkey]) initiates a WiFI connection // to the stated [ssid], using the [passkey] as a WPA, WPA2, // or WEP passphrase. WiFi.begin(WiFiSSID, WiFiPSK); // Use the WiFi.status() function to check if the ESP8266 // is connected to a WiFi network. while (WiFi.status() != WL_CONNECTED) { // Blink the LED digitalWrite(LED_PIN, ledStatus); // Write LED high/low ledStatus = (ledStatus == HIGH) ? LOW : HIGH; // Delays allow the ESP8266 to perform critical tasks // defined outside of the sketch. These tasks include // setting up, and maintaining, a WiFi connection. delay(100); // Potentially infinite loops are generally dangerous. // Add delays -- allowing the processor to perform other // tasks -- wherever possible. } } int postToPhant() { // LED turns on when we enter, it'll go off when we // successfully post. digitalWrite(LED_PIN, HIGH); // Declare an object from the Phant library - phant Phant phant(PhantHost, PublicKey, PrivateKey); for (int i = 1; i <= sampleCount; i++) { while (1) { // Wait for DRDY if (bitRead(readSPI(STATUS_BITS), 23)) break; } voltageSamples.add((readSPI(RMS_VOLTAGE_ADDRESS)/pow(2, 23)) * VOLTAGE_SCALE); currentSamples.add((readSPI(RMS_CURRENT_ADDRESS)/pow(2, 23)) * CURRENT_SCALE); delay(20); } phant.add("voltage", voltageSamples.getMedian()); phant.add("current", currentSamples.getMedian()); // Now connect to data.sparkfun.com, and post our data: WiFiClient client; const int httpPort = 80; if (!client.connect(PhantHost, httpPort)) { // If we fail to connect, return 0. return 0; } // If we successfully connected, print our Phant post: client.print(phant.post()); // Read all the lines of the reply from server and print them to Serial while (client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); // Trying to avoid using serial } // Before we exit, turn the LED off. digitalWrite(LED_PIN, LOW); return 1; // Return success }