Go Down

Topic: MKR1000 does not publish to the clouds (Read 136 times) previous topic - next topic

Pimpanelli

Dear Arduino friends,

I have not been successful in publishing to the cloud with my MKR1000, neither to the Arduino Cloud nor to the Artik (via MQTT and HTTP request).

Did anyone of you succeed in doing so and would be kind enough to help me?

Below is my sketch to publish to Artik
The message I receive is

loop
making post request
sending data 40.0
status code  : -3
response:
wait a bit



// LCD Libray and instance
#include <LiquidCrystal.h>
LiquidCrystal lcd(5, 4, 3, 2, 1, 0);

// DHT22 Library and instance
#include "DHT.h"
#define DHTPIN 7
// Pin 6 is the LED Pin and draws power
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

//COMM
#include <ArduinoJson.h>
#include <ArduinoHttpClient.h>
#include <WiFi101.h>

char buf[200]; // buffer to store the JSON to be sent to the ARTIK cloud

// ARTIK Cloud REST endpoint
char server[] = "api.artik.cloud"; 
int port = 443; // We're using HTTPS
// Device ID tokens
String deviceToken = "xxx";
String deviceId = "xxx";
// Your wifi network
char ssid[] = "xxx";     
char pass[] = "xxx";

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, server, port);
int status = WL_IDLE_STATUS;

// variables
float temp = 0;
float humidity = 0;


void setup() {
  // Start the LCD library
  lcd.begin(16, 2);
  dht.begin();

Serial.begin(9600);
  while ( status != WL_CONNECTED) { // Keep trying until connected
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                 
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

}

void loop() {

Serial.println("loop");
  // ledToggle();
 
  Serial.println("making POST request");
  String contentType = "application/json";
  String AuthorizationData = "Bearer " + deviceToken; //Device Token

//delay(2000);
  humidity = dht.readHumidity();
  temp = dht.readTemperature();
 
  int len = loadBuffer(temp, humidity); 
  Serial.println("Sending data "+String(temp));
  Serial.println("Sending data "+String(humidity));
 
  // push the data to the ARTIK Cloud
  client.beginRequest();
  client.post("/v1.1/messages"); //, contentType, buf
  client.sendHeader("Authorization", AuthorizationData);
  client.sendHeader("Content-Type", "application/json");
  client.sendHeader("Content-Length", len);
  client.endRequest();
  client.print(buf);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
 
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait a bit");

 lcd.print("Temp:");
  lcd.print(temp);
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  lcd.print(humidity);
  lcd.setCursor(0, 0);

  delay(30000); // delay 5 min

}

int loadBuffer(float temp, float humidity ) { 
   StaticJsonBuffer<200> jsonBuffer; // reserve spot in memory

   JsonObject& root = jsonBuffer.createObject(); // create root objects
     root["sdid"] =  deviceId; 
     root["type"] = "message";

   JsonObject& dataPair = root.createNestedObject("data"); // create nested objects
     dataPair["temp"] = temp; 
     dataPair["humidity"] = humidity;

   root.printTo(buf, sizeof(buf)); // JSON-print to buffer

   return (root.measureLength()); // also return length
 }

 

Pimpanelli

Updating the SSL certificate solved the problem.

Best,
Thorsten

Go Up