Sending data to server

Hi,

I have a system that sends data to Google sheets with Arduino WIFI Rev 2 using PushingBox.
I'm trying to send data to the server and can't. I tried to do this before and I succeeded, I don't know what happened now that it doesn't work.
I am attaching the program and the output I get.
I would appreciate help if anyone knows.
Thanks


#include <SoftwareSerial.h>
#include <WiFiNINA.h>
#include <SPI.h>


const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "v2DF8E5DAEB0D87E"; //device ID on Pushingbox for our Scenario

const char* MY_SSID = "Assaflrr";
const char* MY_PWD =  "0542409999";

int humidityData=0;
int celData = 0;
int fehrData = 0;
int hifData = 0;
int hicData = 0;

WiFiClient client;  

int status = WL_IDLE_STATUS;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // 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 SSID: ");
    Serial.println(MY_SSID);
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(MY_SSID, MY_PWD);

    // wait 10 seconds for connection:
    delay(10000);
  }
  
  Serial.println("Connected to wifi");
  printWifiStatus();
  
}

void loop() {

   // Wait between measurements.
  delay(10000);

  humidityData = 1;
  celData = 2;
  fehrData = 3;
  hifData = 4;
  hicData = 5;

  Serial.print("Humidity: ");
  Serial.print(humidityData);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(celData);
  Serial.print(" *C ");
  Serial.print(fehrData);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hicData);
  Serial.print(" *C ");
  Serial.print(hifData);
  Serial.println(" *F\n");

Serial.println("\nSending Data to Server..."); 
  // if you get a connection, report back via serial:

    //API service using WiFi Client through PushingBox then relayed to Google
    if (client.connect(WEBSITE, 80))
      { 
        Serial.println("\nserver");
         client.print("GET /pushingbox?devid=" + devid
       + "&humidityData=" + (String) humidityData
       + "&celData="      + (String) celData
       + "&fehrData="     + (String) fehrData
       + "&hicData="      + (String) hicData
       + "&hifData="      + (String) hifData
         );

      // HTTP 1.1 provides a persistent connection, allowing batched requests
      // or pipelined to an output buffer
      client.println(" HTTP/1.1"); 
      client.print("Host: ");
      client.println(WEBSITE);
      client.println();
      Serial.println("\nData Sent"); 
      }
}


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 the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Your Arduino cannot connect to the host api.pushingbox.com on port 80. Maybe your router doesn't have Internet connectivity, maybe some firewall isn't allowing that, maybe a routing problem, we don't know.

Thanks, do you have any idea how to fix this?

You are asking this way:

I'm 25 years old. What is the date DD.MM. when I was born?

It is not possible to answer the day-number and the month-number from this poor information "I'm 25 years old"

You have to be much more specific what was the situation when it was working?

What code?
What router?
What Ip-adress?
was used when it was working

what has changed since it was working?

Hi, the truth was that everything was the same. Same code, same router, same network
I didn't change anything
Now that I did another test it sent twice and then stopped sending

post the complete sketch using this method

I did itright after my question
I will quote it again

#include <SoftwareSerial.h>
#include <WiFiNINA.h>
#include <SPI.h>


const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "v2DF8E5DAEB0D87E"; //device ID on Pushingbox for our Scenario

const char* MY_SSID = "Assaflrr";
const char* MY_PWD =  "0542409999";

int humidityData=0;
int celData = 0;
int fehrData = 0;
int hifData = 0;
int hicData = 0;

WiFiClient client;  

int status = WL_IDLE_STATUS;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // 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 SSID: ");
    Serial.println(MY_SSID);
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(MY_SSID, MY_PWD);

    // wait 10 seconds for connection:
    delay(10000);
  }
  
  Serial.println("Connected to wifi");
  printWifiStatus();
  
}

void loop() {

   // Wait between measurements.
  delay(10000);

  humidityData = 1;
  celData = 2;
  fehrData = 3;
  hifData = 4;
  hicData = 5;

  Serial.print("Humidity: ");
  Serial.print(humidityData);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(celData);
  Serial.print(" *C ");
  Serial.print(fehrData);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hicData);
  Serial.print(" *C ");
  Serial.print(hifData);
  Serial.println(" *F\n");

Serial.println("\nSending Data to Server..."); 
  // if you get a connection, report back via serial:

    //API service using WiFi Client through PushingBox then relayed to Google
    if (client.connect(WEBSITE, 80))
      { 
        Serial.println("\nserver");
         client.print("GET /pushingbox?devid=" + devid
       + "&humidityData=" + (String) humidityData
       + "&celData="      + (String) celData
       + "&fehrData="     + (String) fehrData
       + "&hicData="      + (String) hicData
       + "&hifData="      + (String) hifData
         );

      // HTTP 1.1 provides a persistent connection, allowing batched requests
      // or pipelined to an output buffer
      client.println(" HTTP/1.1"); 
      client.print("Host: ");
      client.println(WEBSITE);
      client.println();
      Serial.println("\nData Sent"); 
      }
}


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 the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

try this code-version
it uses the function yield()
and non-blocking timing.
As I don't have your hardware I can compile / test it myself.
It might be that you still get compiler-errors

#include <SoftwareSerial.h>
#include <WiFiNINA.h>
#include <SPI.h>


const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "v2DF8E5DAEB0D87E"; //device ID on Pushingbox for our Scenario

const char* MY_SSID = "Assaflrr";
const char* MY_PWD =  "0542409999";

int humidityData = 0;
int celData = 0;
int fehrData = 0;
int hifData = 0;
int hicData = 0;

WiFiClient client;

int status = WL_IDLE_STATUS;

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long myTimer = 0;
unsigned long my10SecTimer = 0;


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(MY_SSID);
  my10SecTimer = millis();
  myTimer = millis();
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(MY_SSID, MY_PWD);
    // wait 10 seconds for connection:
    if ( TimePeriodIsOver(myTimer, 500) ) {
      Serial.print(".");
    }
    delay(100);
    yield(); // process what should be processed in the background
    if ( TimePeriodIsOver(my10SecTimer, 10000) ) {
      break; // after ten seconds
    }
  }
  Serial.println("Connected to wifi");
  printWifiStatus();
}

void loop() {
  // Wait between measurements.
  //delay(10000);
  yield(); // process what should be processed in the background
  if ( TimePeriodIsOver(my10SecTimer, 10000) ) { // wait non-blocking
    humidityData = 1;
    celData = 2;
    fehrData = 3;
    hifData = 4;
    hicData = 5;
    Serial.print("Humidity: ");
    Serial.print(humidityData);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(celData);
    Serial.print(" *C ");
    Serial.print(fehrData);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hicData);
    Serial.print(" *C ");
    Serial.print(hifData);
    Serial.println(" *F\n");
    Serial.println("\nSending Data to Server...");
    // if you get a connection, report back via serial:
    //API service using WiFi Client through PushingBox then relayed to Google
    if (client.connect(WEBSITE, 80))
    {
      Serial.println("\nserver");
      client.print("GET /pushingbox?devid=" + devid
                   + "&humidityData=" + (String) humidityData
                   + "&celData="      + (String) celData
                   + "&fehrData="     + (String) fehrData
                   + "&hicData="      + (String) hicData
                   + "&hifData="      + (String) hifData
                  );
      // HTTP 1.1 provides a persistent connection, allowing batched requests
      // or pipelined to an output buffer
      client.println(" HTTP/1.1");
      client.print("Host: ");
      client.println(WEBSITE);
      client.println();
      Serial.println("\nData Sent");
    }
  }
}


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 the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

thank you for helping. i still get the same output

So "same output" means

?

For narrowing down the problem you should add more serial output to get a more detailed picture at which line of code the code-execution seems to stop.

the demo-code shown here

has an else-condition

  else { 
    if(DEBUG){Serial.println("connection failed");} 
  } 

You removed this else or the code you used as te template did not have it.
It is now 5 days with your problem still not solved.
Don't know how much time you spend on this project.

Anyway by careful analysing through serial debug-output
or
by a small stepped modification of the demo-code

  • small modification, then test
  • small modification, then test
  • small modification, then test
    ....
    would have lead to a running project at least with a high probalility.
    But it seems to be a lesson each beginner must have felt as a pain in his own body that
    trying to change too many things at the same time costs more time than it saves.

best regards Stefan

What happens if you try to connect to a different website?

What happens if you try the same GET request to the pushingbox API from your computer?

:This is the project I based it on

https://create.arduino.cc/projecthub/detox/send-mkr1000-data-to-google-sheets-1175ca

here is the original code:

//-----------------------------------------------
//This sketch is combined from Adafruit DHT sensor and tdicola for dht.h library
// https://learn.adafruit.com/dht/overview
// https://gist.github.com/teos0009/acad7d1e54b97f4b2a88
//other Authors Arduino and associated Google Script:
//Aditya Riska Putra
//Ahmed Reza Rafsanzani
//Ryan Eko Saputro
//See Also:
//http://jarkomdityaz.appspot.com/
//
//ELINS UGM
//
//Modified for Hackster.io project for the MKR1000 
//by Stephen Borsay(Portland, OR, USA)
//Since Arduino can't https, we need to use Pushingbox API (uses http)to run 
//the Google Script (uses https). Alternatly use Ivan's SecureWifi encryption


#include <WiFi101.h>
#include "DHT.h"

#define DHTPIN 5    // what pin we're connected to, pin1 is 5th pin from end

// Uncomment whatever DHT sensor type you're using!
#define DHTTYPE DHT11  // DHT 11
//#define DHTTYPE DHT21  // DHT 21
//#define DHTTYPE DHT22  // DHT 22

DHT dht(DHTPIN,DHTTYPE);

const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "YOUR_DEVICEID"; //device ID on Pushingbox for our Scenario

const char* MY_SSID = "YOUR SSID";
const char* MY_PWD =  "YOUR WiFi PASSWORD";


int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // 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 SSID: ");
    Serial.println(MY_SSID);
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(MY_SSID, MY_PWD);

    // wait 10 seconds for connection:
    delay(10000);
  }
  
  Serial.println("Connected to wifi");
  printWifiStatus();
  
}

void loop() {

   // Wait between measurements.
  delay(10000);

  //prefer to use float, but package size or float conversion isnt working
  //will revise in future with a string fuction or float conversion function

  int humidityData = dht.readHumidity();
  // Read temperature as Celsius (the default)
  int celData = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  int fehrData = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidityData) || isnan(celData) || isnan(fehrData))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  int hifData = dht.computeHeatIndex(fehrData, humidityData);
  // Compute heat index in Celsius (isFahreheit = false)
  int hicData = dht.computeHeatIndex(celData, humidityData, false);

  Serial.print("Humidity: ");
  Serial.print(humidityData);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(celData);
  Serial.print(" *C ");
  Serial.print(fehrData);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hicData);
  Serial.print(" *C ");
  Serial.print(hifData);
  Serial.println(" *F\n");

Serial.println("\nSending Data to Server..."); 
  // if you get a connection, report back via serial:
WiFiClient client;  //Instantiate WiFi object, can scope from here or Globally

    //API service using WiFi Client through PushingBox then relayed to Google
    if (client.connect(WEBSITE, 80))
      { 
         client.print("GET /pushingbox?devid=" + devid
       + "&humidityData=" + (String) humidityData
       + "&celData="      + (String) celData
       + "&fehrData="     + (String) fehrData
       + "&hicData="      + (String) hicData
       + "&hifData="      + (String) hifData
         );

      // HTTP 1.1 provides a persistent connection, allowing batched requests
      // or pipelined to an output buffer
      client.println(" HTTP/1.1"); 
      client.print("Host: ");
      client.println(WEBSITE);
      client.println("User-Agent: MKR1000/1.0");
      //for MKR1000, unlike esp8266, do not close connection
      client.println();
      Serial.println("\nData Sent"); 
      }
}


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 the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}


I will thank you again for your willingness to help me.
My project is a system for generating energy from the sun and the wind.
That's why I have wind sensors and LEDs that are used like solar radiation sensors.
I omitted the parts of the sensor in the original plan because I don't use such a sensor.
In my project I will use the data from my sensors but right now just to check what doesn't work for me, I took the original program and put fixed values for the variables of the sensors.
The situation is quite strange because the project already worked for me with all my sensors and I even did tests for a long time and the program sent data without stopping.
And simply because I'm very busy at work so I took a little break and when I came back to work on it it didn't work well.
I didn't change the network or the program, so I don't understand what could go wrong.
The output I get in my tests varies from time to time - sometimes it sends twice and then stops, sometimes it sends once and stops, sometimes it doesn't send at all

: For example I just did a test right now and the information was sent 10 times and then stopped

14:33:37.807 -> Attempting to connect to SSID: Assaflrr
14:33:49.832 -> Connected to wifi
14:33:49.879 -> SSID: Assaflrr
14:33:49.879 -> IP Address: 10.0.0.20
14:33:49.925 -> signal strength (RSSI):-57 dBm
14:33:59.916 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:33:59.962 -> 
14:33:59.962 -> 
14:33:59.962 -> Sending Data to Server...
14:34:00.055 -> 
14:34:00.055 -> Data Sent
14:34:10.058 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:34:10.104 -> 
14:34:10.151 -> 
14:34:10.151 -> Sending Data to Server...
14:34:10.197 -> 
14:34:10.197 -> Data Sent
14:34:20.211 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:34:20.258 -> 
14:34:20.258 -> 
14:34:20.258 -> Sending Data to Server...
14:34:20.304 -> 
14:34:20.304 -> Data Sent
14:34:30.352 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:34:30.399 -> 
14:34:30.399 -> 
14:34:30.399 -> Sending Data to Server...
14:34:30.445 -> 
14:34:30.445 -> Data Sent
14:34:40.458 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:34:40.550 -> 
14:34:40.550 -> 
14:34:40.550 -> Sending Data to Server...
14:34:40.689 -> 
14:34:40.689 -> Data Sent
14:34:50.707 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:34:50.821 -> 
14:34:50.821 -> 
14:34:50.821 -> Sending Data to Server...
14:34:50.821 -> 
14:34:50.821 -> Data Sent
14:35:00.872 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:35:00.919 -> 
14:35:00.919 -> 
14:35:00.919 -> Sending Data to Server...
14:35:00.960 -> 
14:35:00.960 -> Data Sent
14:35:10.980 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:35:11.073 -> 
14:35:11.073 -> 
14:35:11.073 -> Sending Data to Server...
14:35:11.120 -> 
14:35:11.120 -> Data Sent
14:35:21.119 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:35:21.211 -> 
14:35:21.211 -> 
14:35:21.211 -> Sending Data to Server...
14:35:21.257 -> 
14:35:21.257 -> Data Sent
14:35:31.262 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:35:31.309 -> 
14:35:31.309 -> 
14:35:31.355 -> Sending Data to Server...
14:35:31.401 -> 
14:35:31.401 -> Data Sent
14:35:41.381 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:35:41.475 -> 
14:35:41.475 -> 
14:35:41.475 -> Sending Data to Server...
14:36:01.476 -> Humidity: 1 %	Temperature: 2 *C 3 *F	Heat index: 5 *C 4 *F
14:36:01.523 -> 
14:36:01.523 -> 
14:36:01.523 -> Sending Data to Server...

I will try to go back to the original code as you said and change each time a little

i didnt try to connect to a different website, i will try it today
and the GET request is working from my computer

thank you:)

staying in the stage of thinking "I did work why does it not work anymore?!" keeps you away from solving the problem.

After some repeats with always unsatisfying results you should start analysing.
In two ways:

  1. adding more serial debug-output to your actual code

  2. testing if the problem occurs in the same way if you use the original code.

I ask you explicitly:
Do you really want to add more debug-output to your code?

You did:
original Wifi-library

#include <WiFi101.h>

your wifi-library

#include <WiFiNINA.h>

original code

      client.println(" HTTP/1.1"); 
      client.print("Host: ");
      client.println(WEBSITE);
      client.println("User-Agent: MKR1000/1.0");  // sending user-agent info
      //for MKR1000, unlike esp8266, do not close connection
      client.println();

your code

    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(WEBSITE);
    // sending user-agent info is MISSING
    client.println();

I'm not experienced with internetconncetions. I'm not experienced with:
TCP/IP
synchron/asynchron websockets
GET / POST
clients / servers
etc. etc.
I have just some experience with analysing "strange" problems. My personal experience:

assumings are good to create a priority-list in which order you should check everything.

assuming: "this detail doesn't matter. I can skip analysing it"
Delays and distracts from solving the problem

I don't know if the things mentioned above are causing the problems. You should simply checking it.

best regards Stefan

That eliminates some reasons of the list I posted above. It cannot be the firewall.
I guess that you simply have a bad WiFi network, so the Arduino looses the connection sometimes. As you don't have code that reconnects to the WiFi network if the connection got lost it cannot send the data from that moment on.

inside loop
adding printing the RSSI-value

adding printing WiFi-connection-status

reading in the documenation about the client-object what functions does the client-object offer to print client-connection status which is something different than WiFi-connection status.

maybe there are even call-back-functions that can be configured.
Callback means if a certain event is happening like

  • connecting to server
  • server dis-connecting etc.
    the callbac-function could print.

did you test longer intervals than 10 seconds?
did you test shorter intervals than 10 seconds?

When you tested on your PC did you do send just one or two requests or did you do it in a regular manner?

You could modify your code to assign each and every character into strings or arrays of char and then you are able to print the exact same bytesequence to the serial monitor that you are using in the client.print()-calls
this would enable to show what you are really sending.