Hi Pete,
Okā¦so got the deep sleep mode workingā¦had to make a slight change to my previous sketch (w/o deepsleep) as the timer.setInterval(1000L, sensorDataSend) function was not running the getSensorData function, but instead going into deep sleep mode before that. So moved the getSensorData function code into the setup() function and disabled the timer.setInterval(1000L, sensorDataSend) . Its working fine, but dont know why the timer.setInterval(1000L, sensorDataSend) function will not work. Any pointers will help for academic interest.
Also wanted to know if the following sketch can be used within the void setup () before the Blynk.begin(auth, ssid, pass) -to set the ssid and pwd externally from a webpage, say, instead of hardcoding it into the sketch. Have used a jumper to connect the RST with D0 pins for enabling deepsleep.
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/kentaylor/WiFiManager
void setup() {
Serial.begin(115200);
Serial.println("\n Starting");
WiFi.printDiag(Serial);
Serial.println("Opening configuration portal");
WiFiManager wifiManager;
if (WiFi.SSID()!="") wifiManager.setConfigPortalTimeout(60);
if (!wifiManager.startConfigPortal("ESP8266","password"))
{
Serial.println("Not connected to WiFi but continuing anyway.");
}
else
{
Serial.println("connected to WiFi");
}
My working sketch as of nowā¦
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "Hidden"
#define BLYNK_DEVICE_NAME "Hidden"
#define BLYNK_AUTH_TOKEN "Hidden"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "HX711.h"
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Hidden";
char pass[] = "Hidden";
const char* host = "Hidden";
BlynkTimer timer;
HX711 scale;
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 4;
const int LOADCELL_SCK_PIN = 5;
//define the parameters for the IFTTT
#define HOSTIFTTT "Hidden"
#define EVENTO "Hidden"
#define IFTTTKEY "Hidden"
WiFiClient client;
boolean alreadyRun = false;
int upperLimit = 2;
float calibration_factor = -104625;
float weight;
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale();
//scale.tare(); //Reset the scale to 0
//long zero_factor = scale.read_average(); //Get a baseline reading
scale.set_scale(calibration_factor); //Adjust to this calibration factor
Serial.println("Readings:");
//timer.setInterval(1000L, sensorDataSend); //timer will run every sec
Serial.print("The weight in kg is:\t");
weight = ((scale.get_units(5))-0.42); // Assign Weight to reading
Serial.println(weight);
Blynk.virtualWrite(V3, weight); // sending weight value to Blynk app
if(weight > upperLimit && alreadyRun == false)
{
sendSMS();
alreadyRun = true;
}
else
{
Serial.println("Still running loop()");
delay(1000);
if(weight < upperLimit && alreadyRun == true)
{
alreadyRun = false;
}
}
delay(1000);
Serial.println("Going to Sleep");
Serial.println();
ESP.deepSleep(100*1000000); //deep sleep for 1.65 minutes
}
void loop()
{
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}
void sendSMS() {
Serial.println("Initiating to send SMS");
WiFi.begin(ssid, pass);
Serial.println(" ");
Serial.print("Waiting to connect to WiFi....");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("");
Serial.print("Connected to ");
Serial.print(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
if (client.connected())
{
client.stop();
}
client.flush();
if (client.connect(host,80))
{
Serial.println("Connected");
// build the HTTP request
String toSend = "GET /trigger/";
toSend += EVENTO;
toSend += "/with/key/";
toSend += IFTTTKEY;
toSend += "?value1=";
toSend += weight;
toSend += " HTTP/1.1\r\n";
toSend += "Host: ";
toSend += HOSTIFTTT;
toSend += "\r\n";
toSend += "Connection: close\r\n\r\n";
client.print(toSend);
delay(1000);
Serial.print("SMS Sent");
delay(1000);
}
client.flush();
client.stop();
}