0

Description: While enrolling Arduino device on WSO2 IoT server, I made necessary(WiFi to Ethernet connection) changes in the sketch that I downloaded from the server and uploaded it. The following message was seen on the Serial Monitor:

Internal Temperature Sensor
My IP: 192.168.1.166
Host: 192.168.1.57:9443
JSON Payload: {"event":{"metaData":{"owner":"admin@carbon.super","deviceId":"1iepgvrk0whtf"}, "payloadData":{
-------------------------------
connected
Temperature is
34.99
POST /endpoints/arduino_receiver?deviceId=1iepgvrk0whtf HTTP/1.1
Host: 192.168.1.57:9443
Content-Type: application/json
Content-Length: 117

{"event":{"metaData":{"owner":"admin@carbon.super","deviceId":"1iepgvrk0whtf"}, "payloadData":{"temperature":34.99}}}
���
-------------------------------
connected
Read Controls
Started..
GET /arduino/device/1iepgvrk0whtf/controls HTTP/1.1
Host: 192.168.1.57:9443
Content-Type: application/json

 ���

Ended..
Polling Response: ���
-------------------------------
My IP: 192.168.1.166

There is no message(no Error, no Warning, no Connected message) from IoT-Server.

Affected Product Version: WSO2 IoT Server 3.3.0 Oracle JAVA 1.8.0_181

OS, DB, other environment details and versions: Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-131-generic x86_64) Arduino IDE 1.8.7 Ethernet Library

Steps to reproduce:

  1. mvn clean install -f ./wso2iot-3.3.0/samples/device-plugins-deployer.xml
  2. ./wso2iot-3.3.0/scripts/change-ip.sh From "localhost" to "192.168.1.57"
  3. Start the server

    ./wso2iot-3.3.0/bin/broker.sh

    ./wso2iot-3.3.0/bin/iot-server.sh

    ./wso2iot-3.3.0/bin/analytics.sh

  4. Login to IoT Server (https://192.168.1.57:9443/devicemgt) with"admin"/"admin"

  5. Enroll device with type Arduino with name "Arduino"
  6. Download archive with firmware. Unzip it. In all files, code changed for Ethernet.
  7. Uploaded it to Arduino Uno with Ethernet Shield.
  8. Power on Arduino.

ArduinoBoardSketch.ino:

#include "ArduinoBoardSketch.h"
#include <Ethernet.h>
#include <SPI.h>

EthernetClient httpClient;

void setup()
{
    Serial.begin(115200);
    Serial.println("Internal Temperature Sensor");
    pinMode(6, OUTPUT);
    pinMode(13, OUTPUT);
    connectHttp();
    setupResource();

}

void loop()
{
    while( !httpClient.connected() ){
        connectHttp();

    }


    cpuTemperature=getBoardTemp();

    Serial.println("Internal Temperature is");
    Serial.print(cpuTemperature);


    if(millis() - pushTimestamp > PUSH_INTERVAL){
        while (!httpClient.connected()) {
            connectHttp();
        }
        pushData();

        pushTimestamp = millis();
    }

    //Serial.println("PUSHED");



    if(millis() - pollTimestamp > POLL_INTERVAL){
        while (!httpClient.connected()) {
            connectHttp();
        }
        Serial.println("Read Controls");
        readControls();

        pollTimestamp = millis();

    }


}

ArduinoBoardSketch.h:

#ifndef ArduinoEthernetAgent_H
#define ArduinoEthernetAgent_H

#include "Arduino.h"

byte mac[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09};

#define DEVICE_OWNER "admin@carbon.super"
#define DEVICE_ID "1iepgvrk0whtf"
#define DEVICE_TOKEN "f9f94dce-cc13-3319-9d33-a1e54e90f2ba"
#define REFRESH_DEVICE_TOKEN "00f6d01f-cb1f-38ed-9990-1c719bd66f0c"
#define DEVICE_TYPE  "arduino"
#define TIME  0
#define SUPER_TENANT "carbon.super"

#define DAS_SERVICE_EPOINT "/endpoints/arduino_receiver?deviceId=1iepgvrk0whtf"

#define IOT_SERVICE_EPOINT "/arduino/device/1iepgvrk0whtf/controls"

#define POLL_INTERVAL 1000
#define PUSH_INTERVAL 10000
#define DEBUG true
#define CON_DEBUG true

#define SERVICE_PORT 9763                 //http port of iot server

byte server[4] = {192,168,1,57};        //Ip address of iot server

//set static Ip

byte deviceIP[4] = { 192, 168, 1, 166};   //Ststic ip address of arduino
byte dns2[] = { 192, 168, 1, 1 };             //Ststic dns of arduino
byte subnet[] = { 255, 255, 255, 0 };     //Ststic subnet of arduino
byte gateway[] = { 192, 168, 1, 1 };      //Ststic gateway of arduino


String host, jsonPayLoad, replyMsg;
String responseMsg, subStrn;
double cpuTemperature = 0;
static unsigned long pushTimestamp = 0;
static unsigned long pollTimestamp = 0;
char charBuf[10];
String payLoad;


#endif

Connect.ino:

#include "ArduinoBoardSketch.h"

String connecting = "connecting.... ";

void connectHttp() {
   if(DEBUG) Serial.println("-------------------------------");

   Ethernet.begin(mac, deviceIP, dns2, gateway, subnet);
   delay(2000);

   if(DEBUG) {
   Serial.print("My IP: ");
   Serial.println(Ethernet.localIP());
   }

   connecting += httpClient.connect(server, SERVICE_PORT);
   delay(2000);
   if(DEBUG) Serial.println(connecting);

   if (httpClient.connected()) {
     if(DEBUG) Serial.println("connected");
   } else {
     if(DEBUG) Serial.println("connection failed");

     while(!httpClient.connected()){
       if(DEBUG) Serial.println("retrying to connect......");
       httpClient.connect(server, SERVICE_PORT);
       delay(2000);
     }

     if(DEBUG) Serial.println("connected to server!");
   }
   if(DEBUG) Serial.println("-------------------------------");
}


void setupResource(){
    String hostIP = getHostIP(server);
    String port = String(SERVICE_PORT);

    host = "Host: " + hostIP + ":" + port;
    if(DEBUG) Serial.println(host);

    jsonPayLoad = "{\"event\":{\"metaData\":";
    jsonPayLoad += "{\"owner\":\"";
    jsonPayLoad += String(DEVICE_OWNER);
    jsonPayLoad += "\",\"deviceId\":\"";
    jsonPayLoad += String(DEVICE_ID);
    jsonPayLoad += "\"}, \"payloadData\":{";

    if(DEBUG) {
        Serial.print("JSON Payload: ");
        Serial.println(jsonPayLoad);
        Serial.println("-------------------------------");
    }
}

String getHostIP(byte server[4]){
    String hostIP = String(server[0]);

    for ( int index = 1; index < 4; index++) {
        hostIP += "." + String(server[index]);
    }

    return hostIP;
}

String getMyIP(){
  String myIP = "";
  myIP = String(Ethernet.localIP()[0]);

  for ( int index = 1; index < 4; index++) {
    myIP += "." + String(Ethernet.localIP()[index]);
  }  
  return myIP;
}

PollServer.ino:

#include "ArduinoBoardSketch.h"
void readControls() {
    String responseMsg;

    Serial.println("Started..");

    httpClient.print("GET ");
    httpClient.print(IOT_SERVICE_EPOINT);
    httpClient.println(" HTTP/1.1");
    httpClient.println("\n");
    httpClient.println(host.c_str());httpClient.println("\n"); 
    httpClient.println("Authorization: Bearer "); httpClient.println(DEVICE_TOKEN); httpClient.println("\n");
    httpClient.println("\n");
    httpClient.println("protocol: HTTP\n");

    httpClient.println();

    if(DEBUG) {
        Serial.print("GET ");
        Serial.print(IOT_SERVICE_EPOINT);
        Serial.print(" HTTP/1.1"); Serial.println();
        Serial.print(host); Serial.println();
        Serial.print("Content-Type: application/json"); Serial.println();
        Serial.println();
    }

    delay(1000);

    while (httpClient.available()) {
        char response = httpClient.read();
        if(DEBUG) Serial.print(response);
        responseMsg += response;

    }
    Serial.println();
    Serial.println("Ended..");
    int index = responseMsg.lastIndexOf(":");
    int newLine = responseMsg.lastIndexOf("\n");
    subStrn = responseMsg.substring(index + 1);
    responseMsg = responseMsg.substring(newLine + 1, index);

    if(DEBUG) {
        Serial.print("Polling Response: ");
        Serial.print(responseMsg);
        Serial.println();
        Serial.println("-------------------------------");
    }

    if (subStrn.equals("ON")) {
        Serial.println("ITS ON");
        digitalWrite(13, HIGH);
        digitalWrite(6, HIGH);
    } else if (subStrn.equals("OFF")){

        Serial.println("ITS OFF");
        digitalWrite(13, LOW);
        digitalWrite(6, LOW);

    }
    responseMsg = "";

}

PushData.ino

#include "ArduinoBoardSketch.h"

void pushData(){

    payLoad = "\"temperature\":";
    payLoad += dtostrf(cpuTemperature, 3, 2, charBuf);
    payLoad += "}}}";

    httpClient.println("POST ");
    httpClient.println(DAS_SERVICE_EPOINT);
    httpClient.println(" HTTP/1.1"); httpClient.println("\n");
    httpClient.println(host.c_str()); httpClient.println("\n");
    httpClient.println("Authorization: Bearer "); httpClient.println(DEVICE_TOKEN); httpClient.println("\n");
    httpClient.println("Content-Type: application/json"); httpClient.println("\n");
    httpClient.println("Accept: application/json"); httpClient.println("\n");
    httpClient.println("Content-Length: ");

    int payLength = jsonPayLoad.length() + payLoad.length();

    httpClient.println(String(payLength).c_str()); httpClient.println("\n");
    httpClient.println("\n");

    if(DEBUG) {
        Serial.print("POST ");
        Serial.print(DAS_SERVICE_EPOINT);
        Serial.print(" HTTP/1.1"); Serial.println();
        Serial.print(host); Serial.println();
        Serial.print("Content-Type: application/json"); Serial.println();
        Serial.print("Content-Length: ");
        Serial.print(payLength); Serial.println();
        Serial.println();
    }


    int chunkSize = 50;

    for (int i = 0; i < jsonPayLoad.length(); i++) {
        if ( (i+1)*chunkSize > jsonPayLoad.length()) {
            httpClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
            if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
            i = jsonPayLoad.length();
        } else {
            httpClient.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize));
            if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize));
        }
    }

    for (int i = 0; i < payLoad.length(); i++) {
        if ( (i+1)*chunkSize > payLoad.length()) {
            httpClient.print(payLoad.substring(i*chunkSize, payLoad.length()));
            if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length()));
            i = payLoad.length();
        } else {
            httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
            if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
        }
    }

    httpClient.println(F("\n"));
    if(DEBUG) Serial.println();

    delay(1000);


    while (httpClient.available()) {
        char response = httpClient.read();
        if(DEBUG) Serial.print(response);
    }


    if(DEBUG)  {
        Serial.println();
        Serial.println("-------------------------------");
    }

    payLoad = "";
}


double getBoardTemp(void)
{
    unsigned int wADC;
    double t;

    // The internal temperature has to be used
    // with the internal reference of 1.1V.
    // Channel 8 can not be selected with
    // the analogRead function yet.

    // Set the internal reference and mux.
    ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
    ADCSRA |= _BV(ADEN);  // enable the ADC

    delay(20);            // wait for voltages to become stable.

    ADCSRA |= _BV(ADSC);  // Start the ADC

    // Detect end-of-conversion
    while (bit_is_set(ADCSRA,ADSC));

    // Reading register "ADCW" takes care of how to read ADCL and ADCH.
    wADC = ADCW;

    // The offset of 324.31 could be wrong. It is just an indication.
    t = (wADC - 324.31 ) / 1.22;

    // The returned temperature is in degrees Celcius.
    return (t);
}

migrated from stackoverflow.com Nov 15 '18 at 13:54

This question came from our site for professional and enthusiast programmers.

  • you want to access https from Uno? – Juraj Nov 13 '18 at 13:17
  • @Juraj yes. I want to send data from Uno to the WSO2 IoT server. – Shaheen Khan Nov 14 '18 at 5:27
  • I ask about SSL secure protocol. Uno can't do it. – Juraj Nov 14 '18 at 5:40
  • I was following this ( wso2-oxygen-tank.10903.n7.nabble.com/… )where issue regarding HTTPS was mentioned. However, not for heavy traffic. – Shaheen Khan Nov 14 '18 at 6:17
  • setup for your server a port without SSL. a plain HTTP port, not a secure HTTPS one. the link doesn't mention https or SSL. It is about limitations for HTTP. – Juraj Nov 14 '18 at 6:22
1

Setup for your server a port without SSL. A plain HTTP port, not a secure HTTPS one. Arduino Uno has not the computing power and memory for required security hashes.


Your line endings in the request are wrong. You add empty line with println("\n"). Remove all httpClient.println("\n");. You add \n before \r\n sent by println in println("protocol: HTTP\n"). HTTP protocol requires only \r\n. You have a \r\n inside the Authorization header before the token. Change println("Authorization: Bearer ") to print("Authorization: Bearer ").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.