Arduino MKR1000 -> thinger.io


#1

Does thinger.io support integration with Arduino MKR1000?


#2

Yes! You should choose the example for Arduino Wifi, and it should work out of the box! Tell us if it works! :slight_smile:


#3

Hey Alvaro, thanks for your answer.
I tried it with no luck.

Here you can see the sketch I uploaded. The only thing I added is this:

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

To see all the messages from the start. (anyway, without that , it also fails with the same messages.

Here you can see the code, and the Console logs:

I tried the basic Arduino wifi example, just in case it was a problem with my network, but it was able to connect without issues.
Here you can see the Arduino Sketch connecting to my WiFi network:

[Well… you can’t, since I´m a new user and can upload only ona image in the post, but I have the screengrab if needed]

If you have any idea of what can be happening, count with me to test this integration.

Cheers!


#4

Hi! I think I have some clues about what can be happening! Try changing your include #include <WiFi.h> with #include <WiFi101.h>, so it can include the proper libraries for your Wifi chip. If it works, I will create an example with this fixed! So, please, tell me if it works! :slight_smile:


#5

There we go!!

Thanks Alvaro!!!

NOTE: Remember to remove this from the code, otherwise it will not start unless you have a serial connection!!

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


#6

Wow!! Cool! :grinning: It works! Thanks for testing it!

I will update the Wifi examples for covering the Arduino MKR1000! I will try also to update also the documentation for this new board.

What are you trying to do with your Arduino?


#7

To be truth, I only wanted to connect something to thinger.io to test it while I wait for my NodeMCU devices.

My real idea is to connect a NodeMCU, and create some stuff for gardening. I’ll post the project if I get to something that works :slight_smile:

Count on me if you need to test something with Arduino MKR1000.


#8

I have something else about MKR1000 integration.

I added to the server a switch and a historical chart.

The chart is tight to millis, as in your example, that works great.

The switch though, I made these changes to adapt the sketch to the pin 6 LED in MKR1000 (maybe if you make an example specific for MKR1000, you can change this too).
> pinMode(6, OUTPUT);
> // pin control example (i.e. turning on/off a light, a relay, etc)
> thing[“led”] << digitalPin(6);

With this change, I can turn on the onboard led in pin 6, but the swith in the website, keeps getting to off, even though the led is on. So I cant turn it off.

If I restart the board. The led is off, and I can again turn it on from the website. But I cant go the other way.

Cheers!


#9

Hi! this is strange. We can try to debug what is happening here by setting in the setup:

Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
}
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
int current = digitalRead(6);
Serial.println(current == HIGH ? "HIGH" : "LOW");

This code will set the digital pin to HIGH, and then read its current value and print it on Serial debug. The expected output should be “HIGH”. But it should be returning “LOW” in the MKR1000. Will try it on a stock arduino and different boards. Let me know your result in the MKR1000!

Thanks!


#10

I ran something similar, I think it makes sense also.

So it looks like… MKR1000 (at least mine) doesn’t return a valid value when you read from a pin that is set as OUTPUT.

This is clearly not related with thinger.io, since in the test I didn’t use anything from that library.

I also tried setting the pin as input before reading. But when I do that, then the LED stops blinking…


#11

Yes! this is exactly the problem. The digitalRead is not working properly when the pin is in output mode. Maybe it is a bug from the library or framework for the MKR1000, or this kind of operation has an undefined behaviour. I need to test the same sketch with different boards, and also review the Arduino documentation. Maybe I need to change the code and not make the assumption that digitalRead should be working when the pin is in output mode :disappointed_relieved:


#12

Good luck then :slight_smile:

Count on me to test on the MKR1000 any sketch you need to test.

Meanwhile, I’m trying to fin a work around using a variable, instead of direct pin access…


#13

I got something new…

Any idea what can I do with this?


#14

Well, a workaround for the led in the MKR1000 can be something like the following code:

int ledState = LOW;

void setup() {
  pinMode(2, OUTPUT);

  thing["led"] << [](pson& in){
    if(in.is_empty()){
      in = ledState;
    }else{
      ledState = in ? HIGH : LOW;
      digitalWrite(2, ledState);
    }
  };
}

For your new “problem”, try fully closing your browser and opening the dashboard again. Sometimes the browser does not close the websockets properly (specially if you refresh the browser or dashboard quite often), and you rapidly reach the maximum number of websocket per device (2 at this moment). Tell me if that works!


#15

I restarted the browser ant the connections issue was solved.

About the workaround code, it works, (considering the LED is on pin 6).

For anyone else going throw this posts, this is the full code used to control a led in an Arduino MKR1000

#include <SPI.h>
#include <WiFi101.h>
#include <ThingerWifi.h>

#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

const int LedPin = 6;
int ledState = LOW; // defined as a global variable

void setup() {
  Serial.begin(9600);

  // configure wifi network
  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode(LedPin, OUTPUT);

  // pin control example (i.e. turning on/off a light, a relay, etc)
  thing["led"] << [](pson& in){
      if(in.is_empty()){
        in = ledState;
      }else{
        ledState = in ? HIGH : LOW;
        digitalWrite(LedPin, ledState);
      }
    };
  
  // resource output example (i.e. reading a sensor value, a variable, etc)
  thing["millis"] >> outputValue(millis());

  // more details at http://docs.thinger.io/arduino/
}

void loop() {
  thing.handle();
}

#16

I’ve been working on the weekend, and this is the result:

The thing is now connected to a DHT11 (temperature and humidity sensors), a light sensor, soil moisture sensor, and a Relay module, that activates an acuarium pump. looks pretty ugly, but works… that is the first step.

I cant believe how easy was to integrate everything thanks to thinger.io library. I hope I can get access soon to the web api part, since there are some things that I’d like to work on, (like keeping a history of the readings, being able to configure a refresh rate on a donut chart, different than the refresh rate of the history chart, for the same device reading type… etc.

Anyway, I’m really happy with the speed I was able to get this working thanks to the library, and how simple and clean the code ended up looking.

#define _DEBUG_

#include <SPI.h>
#include <WiFi101.h>
#include <ThingerWifi.h>
#include <DHT.h>

#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

const int LedPin = 6;

// PIN configuration
const int DhtSensorPin = 2;
const int WaterRelayPin = 3;
const int SoilMoistureSensorPin = A5;
const int LightSensorPin = A6;

int ledState = LOW;
int waterRelayState = LOW;

const int DhtType = DHT11;

DHT dht(DhtSensorPin, DhtType);

void setup() {
	Serial.begin(9600);
	dht.begin();

	// configure wifi network
	thing.add_wifi(SSID, SSID_PASSWORD);

	pinMode(LedPin, OUTPUT);
	pinMode(WaterRelayPin, OUTPUT);

	// Configure the LED
	// Need to track the state separatelly from the real pin, since MKR1000 does not respond the correct value when reading an output pin
	thing["led"] << [](pson & in) {
		if (in.is_empty()) {
			in = ledState;
		}
		else {
			ledState = in ? HIGH : LOW;
			digitalWrite(LedPin, ledState);
		}
	};

	// Configure the Water Relay
	// Need to track the state separatelly from the real pin, since MKR1000 does not respond the correct value when reading an output pin
	thing["water"] << [](pson & in) {
		if (in.is_empty()) {
			in = waterRelayState;
		}
		else {
			waterRelayState = in ? HIGH : LOW;
			digitalWrite(WaterRelayPin, waterRelayState);
		}
	};

	thing["dht11"] >> [](pson & out) {
		out["humidity"] = dht.readHumidity();
		out["celsius"] = dht.readTemperature();
		out["fahrenheit"] = dht.readTemperature(true);
	};

	thing["light"] >> [](pson & out) {
		out = map(analogRead(LightSensorPin), 0, 1023, 0, 100);
	};

	thing["moisture"] >> [](pson & out) {
		out = map(analogRead(SoilMoistureSensorPin), 0, 1023, 0, 100);
	};
}

void loop() {
	thing.handle();
}

Arduino MKR 1000 Plant Monitor
#17

Very good work @nahueltaibo! Your setup seems to work pretty fine! I am glad you find the platform easy to use! This was the original idea when designing the platform :smiley:

Regarding your suggestions, historical data logging (and charts) is on the go! It must be available at the end of this month. The refresh rate of the donut is a fix I must implement (an easy one), as currently the refresh rate is set to the minimum configured in the dashboard for the resource, in this case the one configured in your charts, surely. I will keep you updated with new changes!

Thanks for sharing your project! :grinning: