Jump to content









Photo

Data Stream not updated on Carriots

nodemcu dht smartcore data stream carriots api request data

  • Please log in to reply
No replies to this topic

#1 iamkt23

iamkt23

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 25 January 2020 - 05:55 PM

I have been trying to send temperature values from my node module to data streams on altair smartcore. But for some reason, the connection establishes but there is not data stream. 

 

Here is my code - 

 

#include <ESP8266WiFi.h>
#include <SimpleDHT.h>

int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);

// WiFi Setup
const char ssid[]  = "*****";                                                            // WiFi SSID
const char pass[]  = "*****";                                                        // WiFi Password

WiFiClient client_wifi;

// Altair SmartWorks Setup
IPAddress server(51,137,97,16);                                                                   // HTTP IP Server // api.altairsmartcore.com IP Address 
const int http_server_port = 80;                                                                  // HTTP Port Server
const String apikey        = "***********";  // Altair SmartWorks API_Key
const String device        = "defaultDevice@iamkt23.iamkt23";                                     // Altair SmartWorks Device
const String at            = "now";                                                               // Stream Date
const String protocol      = "v2";                                                                // Stream Protocol
const short delayTime      = 2500;

String data                = "{ \"id\": \"23\", \"temp\": \"23\" }";                             // Data to Send

String json                = "";                                                                  // Stream to Send

// WiFi
void printWifiStatus() {

  Serial.println("n------------------------------------------------------------ WiFi Status ------------------------------------------------------------");
  Serial.println("SSID: " + WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.println("IP Address: " + ip.toString());
  
}

void conectWiFi(const char* ssid, const char* password) {
  // WiFi SSID information to connect
  Serial.println();
  Serial.println("--------------------------------------------------- [[Trying to Connect to WiFi]] ---------------------------------------------------");

  // Start the connection
  WiFi.begin(ssid, password);

  // Connecting...
  Serial.print("Connecting");
  printWifiStatus();
  evalWiFiStatusConnected();

  Serial.println("nn");
}

void evalWiFiStatusConnected() {
  // Connecting to Wifi...
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("nWiFi Status: -OKn");
}

// HTTP
void evalHTTPConnection (WiFiClient &client_wifi, IPAddress server, int port) {
  // Try Wifi Connection
  if (!client_wifi.connect(server, port)) {
    Serial.println("--------------------------------------------------------- Connection Failed ---------------------------------------------------------n");
    return;
  }

}

// Stream
void sendStream(WiFiClient &client_wifi, String apikey, String device, String at, String protocol, String data) {
  Serial.println();
  // Build the data field

 String json = "{ \"protocol \":\"" + protocol + "\", \"checksum:\"\", \"device\":\"" + device + "\",\"at\":\"" + at + "\",\"data:\"" + data + "}";
  
  
String post_request = "POST /streams HTTP/1.1 Host: api.altairsmartcore.com Accept: application/json Content-Type: application/json User-Agent: Arduino-SmartCore carriots.apikey: " + apikey + " Content-Length: " + json.length() +  json;
  Serial.println(post_request);
  client_wifi.println(post_request);
}

void setup() {
  Serial.begin(115200);
}

void loop() {
  // Connection Status Evaluation
  evalWiFiStatusConnected();
  evalHTTPConnection(client_wifi, server, http_server_port);

  float temperature = 0;
  float humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
    return;
  }
  
  Serial.print("Temperature: ");
  Serial.print((float)temperature); Serial.println(" *C, ");
  Serial.print("Humidity: ");
  Serial.print((float)humidity); Serial.println(" RH%");
  
  // DHT22 sampling rate is 0.5HZ.
  delay(2500);

  // Sending the Stream
    //data = "{"Temperature":"" + (float)temperature) + """ + ","Humidity":" + """ + (float)humidity))  + ""}";
    sendStream(client_wifi, apikey, device, at, protocol, data);
  
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client_wifi.available()) {
    char c = client_wifi.read();
    Serial.write(c);
  }

  delay(delayTime);
}
    






Also tagged with one or more of these keywords: nodemcu, dht, smartcore, data stream, carriots, api, request, data

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users