Push Notifications to Android

I am trying to make a project where exceeding a certain weight will cause a notification to be sent on my phone. I have an esp8266 and load cell connected to by arduino uno. This is the code I am using:

#include <UnoWiFiDevEd.h>

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>

#include <HX711.h>

String deviceId = "*********"; <------ With the ID
const char
logServer = “api.pushingbox.com”;

const char* ssid = “Sarkaar_Wireless”;
const char* password = “************”; <------ With the password

char message;

HX711 loadcell;

void setup()
{

Serial.begin(115200);
//sendNotification(“Hello World from ESP8266!”);

//sendNotification(String message);

{
Serial.println("- connecting to Home Router SID: " + String(ssid));

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED); {
delay(500);
Serial.print(".");
}

Serial.println();
Serial.println("- succesfully connected");
Serial.println("- starting client");

WiFiClient client;

Serial.println("- connecting to pushing server: " + String(logServer));
if (client.connect(logServer, 80)) {
Serial.println("- succesfully connected");

String postStr = “devid=”;
postStr += String(deviceId);
postStr += “&message_parameter=”;
postStr += String(message);
postStr += “\r\n\r\n”;

Serial.println("- sending data…");

client.print(“POST /pushingbox HTTP/1.1\n”);
client.print(“Host: api.pushingbox.com\n”);
client.print(“Connection: close\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);
client.print(postStr.length());
client.print(”\n\n”);
client.print(postStr);
}

client.stop();
Serial.println("- stopping the client");
Serial.begin(9600);
pinMode(A1, INPUT);
pinMode(A0, OUTPUT);

//HX711 loadcell;

loadcell.set_scale();
loadcell.tare();
loadcell.get_units(10);
Serial.println(loadcell.get_units(10));
}}

void loop()

{
HX711 loadcell;

const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

const long LOADCELL_OFFSET = 50682624;
const long LOADCELL_DIVIDER = 5895655;

loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
loadcell.set_scale(LOADCELL_DIVIDER);
loadcell.set_offset(LOADCELL_OFFSET);

Serial.print("Weight: ");
Serial.println(loadcell.get_units(10), 2);
}

However whenever I run the code it nothing is printed after “connecting to Home Router SID: Sarkaar_Wireless”

What am I doing wrong?

What am I doing wrong?

If you can't even connect to your home network, why did you add all the code to (incorrectly) get data from the HX711?

    String postStr = "devid=";
    postStr += String(deviceId);
    postStr += "&message_parameter=";
    postStr += String(message);
    postStr += "\r\n\r\n";

A one character message, when the one character is a NULL. Useless.

Well I thought the connecting to the network code would work that why I added the rest but why exactly am I not able to connect to the network. What's wrong with my code there.

Garima_Mishra123:
Well I thought the connecting to the network code would work that why I added the rest but why exactly am I not able to connect to the network. What's wrong with my code there.

Strange assumption, when you haven't a clue why you can't connect.

  WiFi.begin(ssid, password);
    /* Start Wifi connection for OPEN networks
     *
     * param ssid: Pointer to the SSID string.
     */
    int begin(char* ssid);

    /* Start Wifi connection with WEP encryption.
     * Configure a key into the device. The key type (WEP-40, WEP-104)
     * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104).
     *
     * param ssid: Pointer to the SSID string.
     * param key_idx: The key index to set. Valid values are 0-3.
     * param key: Key input buffer.
     */
    int begin(char* ssid, uint8_t key_idx, const char* key);

    /* Start Wifi connection with passphrase
     * the most secure supported mode will be automatically selected
     *
     * param ssid: Pointer to the SSID string.
     * param passphrase: Passphrase. Valid characters in a passphrase
     *        must be between ASCII 32-126 (decimal).
     */
    int begin(char* ssid, const char *passphrase);

All the overloads of the begin() method return a value. A clue lurks there, for sure.

Not sure if I know what you mean

Garima_Mishra123:
This is the code I am using:

#include <UnoWiFiDevEd.h>

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>

What is going on there? It looks like you’re just randomly adding libraries, with no attempt to understand what they do.

The UnoWiFiDevEd library is for the Arduino Uno WiFi (Not the Uno WiFi Rev2), which does use the ESP8266, but in a fairly uncommon configuration. The WiFi library is for the retired Arduino WiFi Shield, which is completely different hardware from the ESP8266 and so makes absolutely no sense to use in a project with an ESP8266 connected to an Uno.

How do you have the ESP8266 connected to the Uno?

What firmware is installed on your ESP8266?

I’ve attached the ESP8266 to arduino connection. Also I’m using the esp8266-01 so I’m using the default firmware it comes out the box with.

Garima_Mishra123’s picture:
bbaa2666d0fa1d616512ef25196ce1d74afd8298.jpg

The first problem is that you have the ESP8266 connected to your Uno RX-RX, TX-TX. The correct connections are RX-TX, TX-RX. The reason is that RX stands for “receive” and TX stands for “transmit”. You might find some tutorials that tell you to make the RX-RX, TX-TX connections with the Uno. That is because this is the correct connection to make if you’re only using your Uno as a USB to TTL adapter to allow you to communicate directly between Serial Monitor on your computer and the ESP8266. But that’s not what you’re doing here. You’re trying to communicate between the sketch running on your Uno and the ESP8266. For that usage, you must have the RX-TX, TX-RX connections.

The next problem is that you’re attempting to power the ESP8266 from the 3V3 pin on the Uno. The ESP8266 requires more current than that pin can supply during WiFi communication. It might seem to work at first when powered this way, but it won’t work reliably. Any tutorial you find that recommends powering the ESP8266 this way is written by someone who doesn’t know what they’re talking about and you should disregard all information provided by that person.

When connecting an ESP8266 to an Uno, you have two choices:

  • Connect to pins 0 and 1 (as you currently have it connected): This is tricky because those pins are also used by the Uno to communicate with your computer. That means you may need to disconnect the ESP8266 from the Uno to upload sketches. It also means that using Serial.print() to send debug output to your computer may interfere with the communication with the ESP8266.
  • Connect to any pins other than 0 and 1 and use the SoftwareSerial library to create a software serial port on those pins: The problem with this is that the default baud rate of the AT firmware that your ESP-01 came with is 115200 baud, but the SoftwareSerial library doesn’t work reliably at that speed. So you need to use the appropriate AT command to change the baud rate of the AT firmware to a lower speed.

I recommend this library:
https://github.com/bportaluri/WiFiEsp
If you are sticking with using pins 0 and 1, there is a problem with that library. The author off the library left its debug output on, which is printed to Serial. In my experience, that makes the library not work when used with an ESP8266 also connected to the Serial pins. You will need to turn off the debug output by modifying the library like this:
https://github.com/bportaluri/WiFiEsp/pull/46/files
If you’re using other pins for the ESP8266 connection, that’s not necessary and you might actually find the debug output useful at first for troubleshooting. After you get things working, you might still like to turn off the library’s debug output, since it wastes memory and clutters up the serial output.

If you use the WiFiEsp library, you should start by running the example sketch you find at File > Examples > WiFiEsp > WebServerLed after installing the library to make sure everything is working before adding in a lot of extra complexity with your pushingbox/HX711 stuff.

I have changed the connection so I’m using pins 6 and 7 as RX and TX and am now using the 5V pin on the Uno to power the ESP. I’ve installed the WifiEsp library and as suggested tried the WebServerLed example but after adding my wifi name and password to the code which is:

#include “WiFiEsp.h”

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include “SoftwareSerial.h”
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid = “Sarkaar_Wireless”; // your network SSID (name)
char pass = “Garima3007$”; // your network password
int status = WL_IDLE_STATUS;

int ledStatus = LOW;

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
Serial.begin(115200); // initialize serial for debugging
Serial1.begin(9600); // initialize serial for ESP module
WiFi.init(&Serial1); // initialize ESP module

// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue
while (true);
}

// attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}

Serial.println(“You’re connected to the network”);
printWifiStatus();

// start the web server on port 80
server.begin();
}

void loop()
{
WiFiEspClient client = server.available(); // listen for incoming clients

if (client) { // if you get a client,
Serial.println(“New client”); // print a message out the serial port
buf.init(); // initialize the circular buffer
while (client.connected()) { // loop while the client’s connected
if (client.available()) { // if there’s bytes to read from the client,
char c = client.read(); // read a byte, then
buf.push(c); // push it to the ring buffer

// printing the stream to the serial monitor will slow down
// the receiving of data from the ESP filling the serial buffer
//Serial.write(c);

// you got two newline characters in a row
// that’s the end of the HTTP request, so send a response
if (buf.endsWith("\r\n\r\n")) {
sendHttpResponse(client);
break;
}

// Check to see if the client request was “GET /H” or “GET /L”:
if (buf.endsWith(“GET /H”)) {
Serial.println(“Turn led ON”);
ledStatus = HIGH;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
}
else if (buf.endsWith(“GET /L”)) {
Serial.println(“Turn led OFF”);
ledStatus = LOW;
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}
}

// close the connection
client.stop();
Serial.println(“Client disconnected”);
}
}

void sendHttpResponse(WiFiEspClient client)
{
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what’s coming, then a blank line:
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-type:text/html”);
client.println();

// the content of the HTTP response follows the header:
client.print("The LED is “);
client.print(ledStatus);
client.println(”
“);
client.println(”
");

client.println(“Click <a href=”/H">here turn the LED on
“);
client.println(“Click <a href=”/L”>here turn the LED off
");

// The HTTP response ends with another blank line:
client.println();
}

void printWifiStatus()
{
// print the SSID of the network you’re attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print where to go in the browser
Serial.println();
Serial.print(“To see this page in action, open a browser to http://”);
Serial.println(ip);
Serial.println();

I get this in my serial monitor:

[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present

Any suggestions as to why the exapmle isnt working?

Any suggestions as to why the exapmle isnt working?

Yep. You can't have a ring buffer that size.

So what can I do instead?

Garima_Mishra123:
So what can I do instead?

You can FINALLY go read the stickies, and learn to post your code correctly. Or just go away. Your choice.

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "Sarkaar_Wireless";            // your network SSID (name)
char pass[] = "Garima3007$";        // your network password
int status = WL_IDLE_STATUS;

int ledStatus = LOW;

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);	// initialize digital pin LED_BUILTIN as an output.
  Serial.begin(115200);   // initialize serial for debugging
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();
}


void loop()
{
  WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        //Serial.write(c);
        
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn led ON");
          ledStatus = HIGH;
          digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        }
        else if (buf.endsWith("GET /L")) {
          Serial.println("Turn led OFF");
          ledStatus = LOW;
          digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        }
      }
    }
    
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}


void sendHttpResponse(WiFiEspClient client)
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  
  // the content of the HTTP response follows the header:
  client.print("The LED is ");
  client.print(ledStatus);
  client.println("
");
  client.println("
");
  
  client.println("Click <a href=\"/H\">here</a> turn the LED on
");
  client.println("Click <a href=\"/L\">here</a> turn the LED off
");
  
  // The HTTP response ends with another blank line:
  client.println();
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
}
[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present

Please provide an updated description of how you have the ESP-01 wired to the Uno.

It is the same as the attachment given before but the wires in the 0 and 1 pins moved to 6 and 7 with the red wire in the diagram moved from the 3.3V pin to the 5V pin.

Garima_Mishra123:
It is the same as the attachment given before but the wires in the 0 and 1 pins moved to 6 and 7

Then you still have the incorrect RX-RX, TX-TX connection.

Garima_Mishra123:
with the red wire in the diagram moved from the 3.3V pin to the 5V pin.

Then you probably destroyed the ESP8266. It can be powered at a maximum of 3.3 V.

I swapped the two wires around so the connection is RX to TX and moved back to the 3.3V power supply.

Is it working now?

Still showing up with the same thing in the serial monitor. I’ve got the hx711 load cell connected like shown in the attachment if that makes any difference.