Arduino Integration?


(Peter Sun) #1

I am just playing around to see if I can get my Wifi-Connected Arduino to talk with a smartapp. I created a Webservice using the Smartthings Tutorial. I tried to connect, but I get an error from the serial:

<title>301 Moved Permanently</title>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://https/api/smartapps/installations/APITOKEN/switches/">here</a>.</p>
<address>SmartThings Server at https Port 80</address>

Here is my sendData method (trying to get list of switches):

void sendData() {

char servername[]=“”;
String result = “”;
if (client.connect(servername, 80)) { //starts client connection, checks for connection
client.println(“GET /api/smartapps/installations//switches/ HTTP/1.1”);
client.println(“User-Agent: ArduinoWiFi/1.1”);
client.println(“Connection: close”);
client.println(); }
else {
Serial.println(“connection failed”); //error message if no client connect
Serial.println(“Waiting for response”);
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c =; //gets byte from ethernet buffer
result += c;
client.stop(); //stop client
Serial.println(“Client Stoped”);
Serial.println("Response: " + result);

Any ideas? Or even possible?

(Dan) #2

Here’s how I integrate Arduino with SmartThings… :grinning:

This integration is done via the ST Hub, not directly to a SmartApp. Both ways are valid, however this is the design I am working with currently as it mimics the old ST ThingShield.

(Peter Sun) #3

Could this work with an ultrasonic sensor?

(Dan) #4

What are you trying to do with an ultrasonic sensor? If you can describe the use, I can tell you how I’d approach the integration.

(Peter Sun) #5

I have some components lying around. I was going to use the ultrasonic sensor to measure the door height from the ceiling to see if the garage door is opened or not. So if greater than X inches are measured… the door is opened. if less than X inches the door is closed.

(Dan) #6

Very interesting use case for an ultrasonic sensor. If you already have an Arduino sketch that accurately measures the distance, and compares it to a target distance, you are half way there.

What WiFi solution are you using for the Arduino? Which Arduino are you using?

In my github, there are two main libraries, “SmartThings” and “ST_Anything”. “SmartThings” can be used as a standalone Arduino library to integrate your existing sketch with the SmartThings cloud via TCP/IP communications with your ST Hub (v2 tested). It follows the same basic model as the original ThingShield library, sending and receiving strings between the Arduino and the Device Handler. In fact, it still supports the old ThingShield, as well as numerous other network communications methods. Read the ReadMe in my SmartThings library for more details.

ST_Anything is more of a complete architecture/solution, which uses my SmartThings library for communications to the ST cloud. ST_Anything implements various SmartThings Capabilities, making it simple to use an Arduino very quickly to implement a contact sensor, switch, door control, temperature sensor, humidity sensor, etc…

In your case, since my ST_Anything does not support an ultrasonic sensor, I think you should simply use my SmartThings Arduino library. You simply would send “contact open” or “contact closed” based on the value from your ultrasonic sensor.

I have example Device Handlers as well that have the correct parse() method to receive the data from the Arduino.

Take a look and let me know if you have any questions. Hope this helps!

(Joakim Olerius) #7

Another way to use an ultrasonic detector could be to have it work almost like the fibaro swype. “If something is closer than 30cm do this” in that way u could use very basic hand gestures to make things happen

(Peter Sun) #8

Thanks for the suggestion. I am actually using a WeMOS D1. It’s basically a ESP8266 and an Arduino uno on the same board. I have had some success with it on other projects.

I looked at your examples and they look promising… I am a little confused or just missing it. How does the ST Hub know which device it is suppose to control when it receives instructions from the Arduino? Pardon my N00b-ness, I just started tinker with the ST Hub yesterday, so I am trying to see how all the pieces are still fitting together.

(Dan) #9

The hub does nothing but act as a message broker, transferring data between the Arduino/ESP8266 and the ST Cloud. No custom user software is allowed to run on the local hub at this time.

If you look at the Arduino/ESP8266 examples I have in my libraries, you’ll see that Arduino has to know the local IP address of your hub. This means you need to make sure your hub’s IP address does not change. Use your router’s DHCP Static IP assignment feature to make sure the same local IP address is always given to your ST hub. The Arduino sketch also assign a static IP address to your Arduino. You also need to know your Arduino’s MAC address.

In the ST IDE, you create a custom Device Handler, and then manually add a new device, using the custom device handler. From your phone’s ST app, you configure the settings of the device, including the Arduino’s IP Address, MAC address, and Port. You then press the CONFIGURE tile to set the “DeviceID” = the MAC address of the Arduino. This is where the magic happens… Data sent from your Arduino, via the hub, is routed to the ST Cloud, and eventually the manually added device you just added. The MAC address of the Arduino is what has to match the “DeviceID” of the device. If they match, the parse() routine is called. On the return trip, the IP address and port are used to allow the hub to forward the network message to your Arduino.

It isn’t the simplest solution, but it avoids the entire OAUTH SmartApp solution, and works with an unencrypted network connection. Not all Arduino Ethernet solutions (e.g. W5100?) can handle encrypted connections required for the OAUTH SmartApp solution.

Hope this helps to explain it. If not, feel free to keep the questions coming.

(Kenneth) #10

I posted this same question under one of your other mentions of this project.
Figured I would put it here also.

Help :slight_smile:
I seem to have a small disconnect here’s what I have:

NodeMCU programed with
D1 Switch
D2 Contact

"Node_Device" IP /MAC/Port 8090
"ST_Anything_Ethernet" Handler

Using Simulator I get these results:
Send Commands ON/Off I can see the NODE receives ON/OFF in Serial Monitor but no action taken

Physical NODE:
Change the Contact state “serial out sniffed data on/off” seen in serial monitor
the Device Handler logs remain unaware of any changes.

any suggestions? I don’t think I am at the stage for the ST_Anything application yet, but when I do try and set it up and save it never retains my settings.

(Peter Sun) #11

I configured the Arduino with all the necessary settings in the code. I am not sure if the Arduino is sending the data to the ST cloud. I don’t see anything changing on the ST App. My device status states ‘INACTIVE’. Any reason why that might be?

(Dan) #12

Very hard to tell since you blurred out the important pieces of data in the screen shot above… But it appears that you may have left delimiters in the MAC address. The ReadMe clearly states no delimiters are to be used in the MAC address. It should be entered in the form of ‘A1B2C3D4E5F6’

Give that a try and let me know if it resolves the issue.

(Joel W) #13

OK, I have been trying to follow all these Arduino and Raspberry Pi 3 threads. I am going to ask something that might sound stupid, but here goes. What real time use is this integration? What can be done with this that ST can’t? Also I see people adding sensors etc, but isn’t that a little overboard for adding one sensor. I truly want to get into this as I am a tinkerer, but I need a goal. I would love to have my house talk to me, but is it worth it? I know lots of questions, but I truly would love to know.

(Dan) #14

Good question Joel, not stupid at all. For me, my first use case was that my home had been pre-wired for an alarm system, but no alarm panel was ever installed. So, I had a bunch of unused wires in my walls.

I priced the cost of buying ST battery operated Door/Window sensors. I had 6 doors I wanted to monitor. At the time (2014) that would have cost about $300. Also, I wanted a motion detector and temperature/humidity monitoring in my garage. That would have added another $50-$100. Finally, I wanted to be able to control my 2 garage doors from my smartphone. Adding 2 zigbee/z-wave relays would have been another $100.

So, I was looking at $450 to $500 worth of sensors, most of which would have also required batteries which I loathe changing.

Enter the SmartThings ThingShield. As a hobbyist, it was a perfect solution for me. I was able to buy an Arduino UNO ($25), a ThingShield ($35), and about $25 worth of magnetic reed switches, a DHT22 temperature/humidity sensor, a dual relay module, and a PIR motion sensor. Total parts, about $85.

This project also provided me with a reason and motivation to learn about how SmartThings Device Handlers and SmartApps work. I hacked together an initial solution based on other ThingShield examples I found. It mostly worked, but I knew I could make it better. So, about 2 years ago, my son and I created “ST_Anything” as a library for the Arduino/ThingShield. Our goal was to make it very simple to implement ANY SmartThings Capability with an Arduino and ThingShield. Thus the name, ST_ANYthing.

Recently, SmartThings decided to stop manufacturing the ThingShield. I wanted to make sure that users had an easy to use option still, and thus I worked over the past few weeks to create version 2.x of ST_Anything. This version has support for using Ethernet/LAN connected devices, while maintaining the same look and feel of using the old ThingShield. I wanted to make it simple for users to transition from the old ThingShield, to using inexpensive devices like the $10 NodeMCU v1.0 ESP8266 boards. Had that board been available back in 2014, my $85 project would have only been about $35.

So, for ~$35, you can replace about $500 worth of Zigbee/Z-Wave sensors, with no batteries to ever need replacing.

Hope this provides some insight to my motivation. I know others have created their own sprinkler system controllers, beer home-brewing monitoring and control, control of blinds/curtains, parallel tie-in to existing home alarm systems, replacement of existing home alarm systems, garage door monitoring and operation, water flow monitoring and alarming, temperature monitoring/alarming of fridges and freezers, electrical panel energy usage monitoring/measurements, etc…

For many, home automation is a hobby. It provides an opportunity for them to take their electronics and programming skills and put them to use within their own home. Using an Arduino or Raspberry Pi in conjunction with SmartThings provides a perfect outlet for these skills. Also, one can use a custom solution like this to create something that is not commercially available yet. For example, integration between an old HA system and SmartThings, like an X10 to SmartThings bridge.

Hope this helps to explain at least my motivation.


(Joel W) #15

All I can say holy cow, you really did something great. As far as security goes I already bought all the sensors, but what I am thinking is make a dashboard and a recipe display for the kitchen. By biggest challenge is I don’t have any Windows computers, and I understand I need one to create the OS. Is that correct, or I can use my Mac?

(Dan) #16

Yes, you can use Windows, MAC OS, or Linux to program an Anrduino/ESP8266 or Raspberry Pi. The Arduinos do not run an operating system at all! Just your C/C++ code running natively on the processor. The Raspberry Pi typically run a flavor of Linux called Rasbian. The ESP8266 is more similar to the Arduino with no real operating system running on them.

(Joel W) #17

OK, then to use it as a Dashboard and recipe center, I can use a Pi 3 instead, and just run Rasbian, is that correct?

(Dan) #18

I really don’t understand what you mean by a dashboard and recipe center.

If by Dashboard you mean run the SmartThings iOS or Android app, then the answer is no. If you want to run the SmartTiles/ActionTiles web-based dashboard, I would strongly recommend you simply use an iPad or Android Tablet. If you’re going with total custom solution, the Raspberry Pi may or may not meet your needs.

As for the recipe center… I have no clue. I do know that you can get recipe management apps for the iPad and Android tablets though. You can also easily check email, weather, stocks, run the ST App, etc…

(Peter Sun) #19

Thanks for the info. I changed the MAC ADDRESS to exclude the “:” and I went back and reread the README file to make sure I followed everything. I am still unable to make any kind of contact to the hub.

(Dan) #20

After re-entering the MAC address of the Arduino/ESP8266 in the device settings, did you remember to click th configure tile? Also, the IP address and port in the device’s settings should those of your Arduino/ESP8266, not your hub.

Are you sure you updated the IP address of the HUB in your sketch? The hub’s port is fixed. Do not change that in the sketch. You’ll have to get your hub’s IP address from your router.

And finally, which ST hub are you using. This has only been tested on the v2 ST hub. I do not know if it works on the original v1 hub.