Hi everyone!
I'm new to the Arduino world and i have bought a MKR NB 1500 board and an Adafruit TMP117 sensor, i only want to take measurements of the temperature every 20 second and send it to my database.
I know that the board is working because i can connect it to IOT cloud and i receive the temperature.
Now my problem is that i want the data to be sent to my database instead.
I have tried with this code but it doesn't work, can anyone help me how to solve this problem?
I have tried with and Spark fun ESP 32 Thing Plus C card so i know that my PHP code works and i receive the data in the database but i cant make it work with the BN 15000 board.
This is my code:
#include <MKRNB.h>
#include <ArduinoHttpClient.h>
#include <Wire.h>
#include <Adafruit_TMP117.h>
#define APN "online.telia.se" // Access Point Name provided by your cellular network provider
#define SERVER_ADDRESS "example.se"
#define SERVER_PORT 443
#define RESOURCE_PATH "https://example.se/Data/Temperature.php"
// Initialize the TMP117 sensor
Adafruit_TMP117 tmp117;
// Set up the NB client
NB nbAccess;
NBSSLClient client;
// Function to send temperature data to the server
void sendTemperature(float temp) {
HttpClient http(client, SERVER_ADDRESS, SERVER_PORT);
// Construct the GET request URL with the temperature value
String url = RESOURCE_PATH;
url += "?temperature=";
url += String(temp);
// Print the complete URL
Serial.print("Sending data to: ");
Serial.println(url);
// Send the GET request
http.get(url);
int statusCode = http.responseStatusCode();
// Check for a successful request
if (statusCode == 200) {
Serial.println("HTTP request successful");
} else {
Serial.print("HTTP request failed with status code ");
Serial.println(statusCode);
}
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // Wait for serial port to connect
}
// Start NB connection
Serial.println("Starting NB connection...");
nbAccess.begin(APN);
while (!nbAccess.ready()) {
Serial.println("Failed to start NB connection! Trying again...");
delay(5000);
}
Serial.println("NB connection established!");
// Initialize TMP117 sensor
if (!tmp117.begin()) {
Serial.println("TMP117 sensor not detected. Please check wiring.");
while (1);
}
}
void loop() {
// Read temperature
sensors_event_t temp;
tmp117.getEvent(&temp);
float temperature = temp.temperature;
// Send temperature to server
sendTemperature(temperature);
// Wait for 20 seconds
delay(20000);
}
This is the output in the serial monitor:
Starting NB connection...
NB connection established!
Sending data to: https://example.se/Data/Temperature.php?temperature=19.05
HTTP request failed with status code -2