Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
main.cpp 4.02 KiB
/*
Web client

This sketch connects to a website through a MKR NB 1500 board. Specifically,
this example downloads the URL "http://example.org/" and
prints it to the Serial monitor.

Circuit:
- MKR NB 1500 board
- Antenna
- SIM card with a data plan

created 8 Mar 2012
by Tom Igoe
*/

// libraries
#include "ArduinoLowPower.h"
#include <ArduinoJson.h>
#include <MKRNB.h>
#include <string>

// initialize the library instance
GPRS gprs;
NB nbAccess;

void attachToNetwork();

std::string getResponse(NBClient &client);
std::string getResponseBody(const std::string &response);
std::string getResponse(std::string server, std::string path, int port)
{
	NBClient client;
	// if you get a connection, report back via serial:
	if (client.connect(server.c_str(), port)) {
		Serial.println("connected to server");
		// Make a HTTP request:
		client.print("GET ");
		client.print(path.c_str());
		client.println(" HTTP/1.1");
		client.print("Host: ");
		client.println(server.c_str());
		client.println("Connection: close");
		client.println();
	} else {
		// if you didn't get a connection to the server:
		Serial.println("connection failed");
	}

	std::string response = getResponse(client);

	std::string token = getResponseBody(response);
	Serial.println(token.c_str());
	client.stop();
	return token;
}
std::string getResponseBody(const std::string &response)
{
	std::string delimiter = "\r\n\r\n";
	std::string token = response.substr(response.find(delimiter)).replace(0, delimiter.length(), "");
	return token;
}

std::string getResponse(NBClient &client)
{
	String status = client.readStringUntil('\r');
	Serial.println(status);

	std::string response;
	while (client.connected()) {
		if (client.available()) {
			char c = client.read();
			response += c;
		}
	}
	return response;
}

std::string postRequest(std::string server, std::string path, int port, std::string body)
{
	NBClient client;
	// if you get a connection, report back via serial:
	if (client.connect(server.c_str(), port)) {
		Serial.println("connected to server");
		// Make a HTTP request:
		client.print("POST ");
		client.print(path.c_str());
		client.println(" HTTP/1.1");
		client.print("Host: ");
		client.println(server.c_str());
		client.println("Connection: close");
		client.println("Content-Type: application/json");
		client.print("Content-Length: ");
		client.println(body.length());
		client.println();
		client.println(body.c_str());
	} else {
		// if you didn't get a connection to the server:
		Serial.println("connection failed");
	}

	std::string response = getResponse(client);

	std::string token = getResponseBody(response);

	Serial.println(token.c_str());
	client.stop();
	return token;
}

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

void attachToNetwork()
{
	Serial.println("Starting Arduino web client.");
	// connection state
	boolean connected = false;

	// After starting the modem with NB.begin()
	// attach to the GPRS network with the APN, login and password
	while (!connected) {
		if ((nbAccess.begin("", "m2m.public.at") == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) {
			connected = true;
		} else {
			Serial.println("Not connected");
			exit(1);
			delay(1000);
		}
	}
}

void loop()
{
	attachToNetwork();

	Serial.println("connecting...");

	auto response = getResponse("date.jsontest.com", "/", 80);
	//	convert response to json object
	DynamicJsonDocument doc(1024);
	auto error = deserializeJson(doc, response);
	if (error) {
		Serial.print(F("deserializeJson() failed: "));
		Serial.println(error.f_str());
		return;
	}
	const char *date = doc["date"];
	long long milliseconds_since_epoch = doc["milliseconds_since_epoch"].as<long long>();
	const char *time = doc["time"];
	Serial.println(date);
	Serial.println(milliseconds_since_epoch);
	Serial.println(time);
	nbAccess.shutdown();

	// TODO: WAKE UP FROM WATCHDOG (PIN)
	LowPower.deepSleep(10000);

	// TODO: SAVE TO EEPROM OR SD CARD VIA SPI
	// TODO: REFACTOR CODE
	// TODO: TEST CODE

}