Jump to content









Photo

Sendin Temperature via ESP8266

ESP8266

  • Please log in to reply
1 reply to this topic

#1 subasc

subasc

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 15 February 2019 - 01:45 AM

Dear Gurus.

 

I am new to this site and also new to programming. Please find the below simple program that I had done and I have managed to display it on the Compiler Monitor.

 

Could someone please help inser the Smart Core codes so I could display it on the Altair system. I have tried many times to follow the sample codes given in all the example but I can't seem to get the codes in the right place to connect to the system and to stream the data into the system. I have removed all my trial codes as its not working and appreciate if some one can help insert the codes in the attach file.

 

please help

 

Thank you

 

 

 

Attached Files



#2 Yoleon

Yoleon

    Sugar Master

  • Members
  • PipPipPip
  • 57 posts

Posted 13 March 2019 - 05:05 PM

Hi Subasc,

 

I leave you an easy code for beginning with our IoT platform, you only need to replace the default values with your data.

 

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"
#include <string.h>
#include <stdlib.h>

int Start;
int Stop;

float t = 15;
float h = 25;
long measurement;
String statusEngine = "STOP";

#define DHTTYPE DHT11
#define dht_dpin D6

DHT dht(dht_dpin, DHTTYPE);

int vibr_Pin = 0;

RTC_DS1307 RTC; // RTC

const char* ssid = "apache_2G";
const char* password = "1234567890";

//SmartCore parameters
const char* server = "api.altairsmartcore.com";
const String APIKEY = "YOUR_APIKEY"; // Replace with your apikey
const String DEVICE = "YOUR_DEVICE_ID_DEVELOPER"; // Replace with the id_developer of your device


void setup() {
  digitalWrite(2, LOW);
  pinMode(2, OUTPUT);
  Serial.begin(9600);
  delay(10);

  for (int i = 0; i < 20; i++) {
    Serial.print(".");
    delay(1000);
  }
}


void loop() {
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    digitalWrite(2, LOW);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected to");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  digitalWrite(2, HIGH);
  delay(1000);

  Serial.print(F("Signal strength (RSSI): "));
  Serial.print(WiFi.RSSI());
  Serial.println(F(" dBm"));

  Wire.begin();
  RTC.begin();

  if (!RTC.isrunning()) {
    Serial.println("RTC is not running!"); //This will reflect ime that your sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  Start = 0;
  Stop = 0;

  //readSensors();

  if ((measurement > 1) && (Start == 0)) {
    Serial.print("Engine Start Time ");
    statusEngine == "Start";

    DateTime now = RTC.now();
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.println(now.second(), DEC);

    Serial.print("Temperature = ");
    Serial.print(t);
    Serial.println("C  ");
    Serial.print("Humidity = ");
    Serial.print(h);
    Serial.println("hPA  ");
    Serial.print("Engine Status = ");
    Serial.print(statusEngine);

    Start = 1;
    Stop = 0;
    loop;
  } else if ((measurement == 0) && (Start == 1)) {
    Serial.print("Engine Stop Time ");
    statusEngine == "Stop";

    DateTime now = RTC.now();
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.println(now.second(), DEC);

    Serial.print("Temperature = ");
    Serial.print(t);
    Serial.println("C  ");
    Serial.print("Humidity = ");
    Serial.print(h);
    Serial.println("hPA  ");
    Serial.print("Engine Status = ");
    Serial.print(statusEngine);

    Start = 0;
    Stop = 1;
    loop;
  } else {
    loop;
  }

  char TempString[32];

  dtostrf(t, 2, 2, TempString);
  String T = String(TempString);

  dtostrf(h, 2, 2, TempString);
  String H = String(TempString);

  int length = 0;

  String bigData = "{\"protocol\":\"v2\",\"device\":\"" + String(DEVICE) + "\",\"at\":\"now\",\"data\":{\"Humidity\": " + String(H) + ",\"Temperature\": " + String(T) + ",\"Engine_Status\": \"" + statusEngine + "\"}}";

  length = bigData.length();
  Serial.print("Data Length :");
  Serial.println(length);
  Serial.println(); 
  
  //Print all the details for confirmation purposes
  Serial.println("POST /streams HTTP/1.1");
  Serial.println("HOST: api.altairsmartcore.com");
  Serial.println("ACCEPT: application/json");
  Serial.println("User-agent: Arduino-SmartCore");
  Serial.println("Content-type:application/json");
  Serial.println("carriots api key: " + String(APIKEY));
  Serial.println("Content length: " + String(length));
  Serial.println("Connection: Close");
  Serial.println();
  Serial.println(T);
  Serial.println(H);
  Serial.println(statusEngine);
  Serial.println(bigData);

  //connect to internet via client & Make HTTP request
  WiFiClient client;

  if (client.connect(server, 80)) {
    Serial.println(F("Connected to ALTAIRSMARTCORE.COM"));
    Serial.println("Connectedddd!");
    client.println("POST /streams HTTP/1.1");
    client.println("Host: api.altairsmartcore.com");
    client.println("Accept: application/json");
    client.println("User-Agent: Arduino-SmartCore");
    client.println("Content-Type: application/json");
    client.println("carriots.apikey: " + String(APIKEY));
    client.println("Content-Length: " + String(length));
    //client.print("Connection: Close");
    client.println();
  } else {
    // If you didn't get a connection to the server:
    Serial.println(F("connection failed"));
    return;
  }

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    digitalWrite(2, LOW);
    Serial.print(".");
  }
}

void readSensors() {
  delay(10);
  t = dht.readTemperature();
  h = dht.readHumidity();
  measurement = pulseIn(vibr_Pin, HIGH);  //wait for the pin to get HIGH and returns measurement
}

 

Best regards!







Also tagged with one or more of these keywords: ESP8266

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users