KalbeAbbasRachel Vorster
Published © MIT

Build a Weather Station with XinaBox and Okdo IoT Cloud

Learn how to make your own Weather Station that sends weather readings to OKdo IoT Cloud and displays on OLED xChip OD01.

BeginnerFull instructions provided15 minutes271
Build a Weather Station with XinaBox and Okdo IoT Cloud

Things used in this project

Hardware components

CW01
XinaBox CW01
×1
IP02
XinaBox IP02
×1
OD01
XinaBox OD01
×1
SL01
XinaBox SL01
×1
SW01
XinaBox SW01
×1
XC10
XinaBox XC10
×1

Software apps and online services

Arduino IDE
Arduino IDE
OKdo IoT Cloud

Story

Read more

Code

Code

C/C++
Send XK01 data to OKdo IoT cloud
/*    _   _ _ _____ _    _              _____     _ _     ___ ___  _  __
 *   /_\ | | |_   _| |_ (_)_ _  __ _ __|_   _|_ _| | |__ / __|   \| |/ /
 *  / _ \| | | | | | ' \| | ' \/ _` (_-< | |/ _` | | / / \__ \ |) | ' <
 * /_/ \_\_|_| |_| |_||_|_|_||_\__, /__/ |_|\__,_|_|_\_\ |___/___/|_|\_\
 *                             |___/
 *
 * Copyright 2018 AllThingsTalk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define JSON
//#define CBOR

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <xCore.h>
#include <xSW01.h>
#include <xSL01.h>
#include <xOD01.h>

// Define http and mqtt endpoints
#define http "api.allthingstalk.io"  // API endpoint
#define mqtt "api.allthingstalk.io"  // broker

#include <ATT_IOT.h>
#include <SPI.h>  // required to have support for signed/unsigned long type.

void callback(char* topic, byte* payload, unsigned int length);
WiFiClient espClient;
PubSubClient pubSub(mqtt, 1883, callback, espClient);

ATTDevice device("DEVICE_ID","DEVICE_TOKEN");
xSW01 SW01;
xSL01 SL01;
xOD01 OD01;

#ifdef CBOR
  #include <CborBuilder.h>
  CborBuilder payload(device);
#endif

void setup()
{
  Serial.begin(9600);  // Init serial link for debugging

    // Set the I2C Pins for CW01
  #ifdef ESP8266
    Wire.pins(2, 14);
    Wire.setClockStretchLimit(15000);
  #endif
  
  // Start the I2C Comunication
  Wire.begin();
  
  // Start the  SW01 Sensor
  SW01.begin();
  SL01.begin();
  OD01.begin();
  
  // Enter your WiFi credentials here!
  setupWiFi("SSID", "PASS");

  OD01.println("Weather Station");
  delay(2000);

  OD01.clear();
  //
  
  while(!device.connect(&espClient, http))  // Connect to AllThingsTalk
    Serial.println("retrying");

  // Create device assets
  device.addAsset("temperature", "Temperature", "Temperature in Celsius", "sensor", "{\"type\": \"string\"}");
  device.addAsset("pressure", "Pressure", "Pressure in Pascal", "sensor", "{\"type\": \"string\"}");
  device.addAsset("humidity", "Humidity", "Humidity in %", "sensor", "{\"type\": \"string\"}");
  device.addAsset("lux", "Lux", "Ambient light intensity", "sensor", "{\"type\": \"string\"}");
  device.addAsset("uva", "UVA", "UVA", "sensor", "{\"type\": \"string\"}");
  device.addAsset("uvb", "UVB", "UVB", "sensor", "{\"type\": \"string\"}");
  
  while(!device.subscribe(pubSub))  // Subscribe to mqtt
    Serial.println("retrying"); 
}

void setupWiFi(const char* ssid, const char* password)
{
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
}

unsigned long prevTime;
unsigned int prevVal = 0;
void loop()
{
   float tempC,pres,hum;
   float lux,uva,uvb;
   
   SW01.poll();
   SL01.poll();
   
   tempC = SW01.getTempC();
   pres = SW01.getPressure();
   hum = SW01.getHumidity();
   lux = SL01.getLUX();
   uva = SL01.getUVA();
   uvb = SL01.getUVB();
   
  unsigned long curTime = millis();
  if (curTime > (prevTime + 5000))  // Update and send counter value every 5 seconds
  {
    #ifdef JSON
    device.send(String(tempC), "temperature");
    device.send(String(pres/1000), "pressure");
    device.send(String(hum), "humidity");
    device.send(String(lux), "lux");
    device.send(String(uva), "uva");
    device.send(String(uvb), "uvb");
    #endif

    #ifdef CBOR  // Send data using Cbor
    payload.reset();
    payload.map(1);
    payload.addInteger(counter, "counter");
    payload.send();
    #endif

    OD01.print("Temp.: ");
    OD01.print(tempC);
    OD01.println(" C");
    OD01.print("Press.: ");
    OD01.print(pres/1000);
    OD01.println(" kPa");
    OD01.print("Hum.: ");
    OD01.print(hum);
    OD01.println(" %");
    OD01.print("LUX: ");
    OD01.print(lux);
    OD01.println("LUX");
    OD01.print("UVA: ");
    OD01.print(uva);
    OD01.println(" uW/cm^2");
    OD01.print("UVB: ");
    OD01.print(uvb);
    OD01.println(" uW/cm^2");

    delay(2000);

    OD01.clear();
    
    prevTime = curTime;
  }
  device.process();  // Check for incoming messages
}

/****
 * Callback function
 * Handle messages that were sent from the AllThingsTalk cloud to this device
 */
void callback(char* topic, byte* payload, unsigned int length) 
{ 
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  // Convert payload to json
  StaticJsonBuffer<500> jsonBuffer;
  char json[500];
  for (int i = 0; i < length; i++) {
    json[i] = (char)payload[i];
  }
  json[length] = '\0';
  
  JsonObject& root = jsonBuffer.parseObject(json);

  // Do something
  if(root.success())
  {
    const char* value = root["value"];
    Serial.println(value);
    if (strcmp(value,"true") == 0)
      digitalWrite(LED_BUILTIN, LOW);   // Turn the onboard LED on
    else
      digitalWrite(LED_BUILTIN, HIGH);  // Turn the onboard LED off

    device.send(value, "toggle");  // Send command back as ACK using JSON
  }
  else
    Serial.println("Parsing JSON failed");
}

Credits

KalbeAbbas

KalbeAbbas

25 projects • 7 followers
An enthusiastic and dedicated Electronic engineer graduated from SSUET Karachi, Pakistan. Loves to discover Embedded electronics projects.
Rachel Vorster

Rachel Vorster

6 projects • 2 followers
Thanks to XinaBox.

Comments