Sunday, December 15, 2019

Sending data from ESP to Android

Click here for an index of all of my stories

I have written about many projects that use an Android Phone or Tablet to command an ESP. There are several ways to do that.
You can use a dedicated APP written in Mit's App Inventor like this story explains: http://lucstechblog.blogspot.nl/2018/02/wifi-voice-commander.html or this story https://lucstechblog.blogspot.nl/2018/01/wifi-relay.html
You can also have a web-page on the ESP that has buttons and sliders which can be accessed from any browser including your phone's or tablet's like this story (programmed in LUA) https://lucstechblog.blogspot.nl/2016/05/esp8266-controlled-ledstrip.html or this story https://lucstechblog.blogspot.nl/2016/08/rain-sensor-using-esp8266.html or the Relay server https://lucstechblog.blogspot.nl/2016/09/esp-relayserver.html and of course the obvious thermometer: http://lucstechblog.blogspot.nl/2017/11/oh-no-not-another-wifi-thermometer.html

All these projects have one thing in common. The Android phone or Tablet sends data to the ESP. It is never the other way round.

Searching the internet I did not find any project in which an ESP8266 send data to an Android device where the Phone or Tablet is used to process the data or just to display it. And for a certain project (still in devellopment) that was just what I needed.

So I had to find it out for myself. And actually it turned out to be much easier as I expected.

There are 2 sides to this story The ESP side and the Android side.

The ESP side

I wanted a quick and dirty solution so I used ESP-Basic for this. If you want an introduction to lean to work with ESP-Basic read this:  https://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

The basic (pun intended) point is that we want to have the ESP react on an incoming request. I did that before when I let the ESP react on a message send to it from IFTTT in this story: https://lucstechblog.blogspot.nl/2017/05/google-home-and-esp8266.html

And actually I did the same in Arduino language (C++) in this story:
https://lucstechblog.blogspot.nl/2018/01/wifi-relay.html

Both these stories let the ESP react on an incoming request but that is not what I wanted. I wanted the ESP to send data to the device that send a request. And that involves just a light modification of the code.



msgbranch [mytestbranch]
wait

[mytestbranch]
a = a+1
myreturn = "test number " & str(a)
print myreturn
msgreturn myreturn
wait

The msbrach just waits till the ESP receives a message. It is not important what the contents of the message is. We do not even check it.

When a message is received the program jumps to the [mytestbranch] routine.
In there the variable "a" is increased and a return message is send to the device that send the request.

Piece of cake.

The Android side

The Android program is made with Mit's APP-Inventor. If you do not have any experience with writing APP's for Android please take a look at APP-Inventor. It is easy to use and works like Scratch on the Raspberry or other block-oriented programming environments.



Start with designing a screen that contains a button and a label.
The label is the small green dot just below the button.

Now switch over to the block side and here is the program:



This part has a few flaws. It works but remember this is only a proof of concept.

The first block defines what happens when the button is clicked. And that is easy. A 'get' request is send to the ESP's IP adress. Finding the ESP's IP adress can be done in several ways. In ESP basic:

print ip()

will do the trick. Another way is to open your router's web page and look there for the IP number of the ESP. I described that in several stories here on this weblog. For example in this story:
https://lucstechblog.blogspot.nl/2018/02/esp-voice-control-with-esp-basic.html

You could make this program universal by adding a text field in which you can type the IP-adress. That way this small program can be usefull for getting information from a number of ESP's. Just be creative.

The next block is activated when a request has been received and a return message has been send.
The text in the label is then set to the response.

That is all folks.

Real world

The Basic program prints on the screen of it's web page what is send to the Android device. As you can see I have pressed 8 times on the button on my Android Phone.


The Android device, which is my phone but could also be a tablet or whatever, displays the last received data from the ESP.

Expanding the code

Here are several ideas to expand the code.

- Like stated above add a text field in the Android program/screen so you can fill in the ESP's IP-number
- Send multiple data to the Android Device
- Make this a two way conversation. For example the Android Device asks for certain information like a number, temperature, sensor data or whatever and let the ESP respond with the right information.
- Let the Android Device switch on a lamp, pump, motor etc and let the ESP respond if it is done so you are sure things work as you'd expect.

I think you can come up with a lot of projects and ideas for this.

Till next time
Have fun

Luc Volders

Friday, December 6, 2019

Dweet with Arduino

For an index of all my stories click this text

Oh no not Dweet again !!!
Well yes but due to popular demand.

Introduction

For those that do not know what Dweet is I give you a short introduction.

Dweet is a free service for IOT projects found at https://dweet.io/
Dweet is like Twitter for IOT projects. You send a message (sensor value or whatever) to Dweet.io and that message is stored for 24 hour. After that it is removed. Dweet.io remembers the last 5 messages you send to it. You can retrieve the message as many times as you like within those 24 hour. Dweet is totally anonymous. You even do not have to make an account for using the service. Dweet.io is used by many people all over the world. So how do you recognise which message is yours. You give your message a name called a 'thing' and a value. As long as you know the name of the 'thing' you can retrieve it's last 5 messages.

You can have multiple 'things' that all can Dweet and from each of them the last 5 messages are remembered for 24 hour.

You can have for example 3 ESP8266's which you give the thing names ESP1, ESP2, and ESP3. Each of these 'things' can send a message to Dweet containing sensor values. And if you want to know the value for the third ESP you just get the information for 'thing' ESP3 from Dweet.

I wrote 4 previous stories about Dweet.io on this weblog. The first described how to send and retrieve messages directly from your browser. Read that story here: 

https://lucstechblog.blogspot.com/2019/05/dweet-volatile-iot-cloud-service.html

The second story described how to send and retrieve Dweets with an ESP8266 using ESP-Basic. Read that story here:

https://lucstechblog.blogspot.com/2019/08/dweet-again.html

To get Dweeting with Android read this story:
https://lucstechblog.blogspot.com/2019/09/dweet-with-android.html 

And we can Dweet with the Raspberry:
https://lucstechblog.blogspot.com/2019/09/dweet-with-raspberry.html
 

So why another story ?

Popular demand like I said.
Well actually I received some mails in which people stated that they wanted to use Dweet with an ESP8266 but were not willing to switch to ESP-Basic. They wanted a solution in C++ (Arduino). Well here you go.

The Arduino Code



#include <ESP8266WiFi.h>

const char *ssid     = "ROUTERSNAME";
const char *password = "ROUTERSPASSWORD";

String thingName="Arduintemp";
const char* dweetsite = "dweet.io";

int stopval = 1;

void setup() 
  { 
  Serial.begin(9600); 
  Serial.println(); 
  Serial.println(); 
  Serial.print("Connecting to "); 
  Serial.println(ssid); 
  WiFi.begin(ssid, password);   
  int retries = 0; 
  while ((WiFi.status() != WL_CONNECTED) && (retries < 15)) 
    { 
    retries++; 
    delay(500); 
    Serial.print("."); 
    } 
  if(retries>14)
    { 
    Serial.println(F("WiFi conection FAILED")); 
    } 
  if (WiFi.status() == WL_CONNECTED) 
    { 
    Serial.println("WiFi connected"); 
    Serial.println("IP address: "); 
    Serial.println(WiFi.localIP()); 
    Serial.println("\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/");  
    } 
  Serial.println("Connected and ready to send");  
} 

void loop() 
  {
    dweetdata();
  }

void dweetdata()
  {
  WiFiClient client; 
  const int httpPort = 80; 
  if (!client.connect(dweetsite, httpPort)) 
    { 
    Serial.println("connection failed"); 
    return; 
    } 

  String dweetstring="GET /dweet/for/";
  dweetstring=dweetstring+String(thingName)+"?";
  dweetstring=dweetstring+"temp=";
  dweetstring=dweetstring+"12";
  dweetstring=dweetstring+" HTTP/1.1\r\n"+
      "Host: " + 
      dweetsite + 
      "\r\n" + 
      "Connection: close\r\n\r\n";
    
  client.print(dweetstring); 
  delay(10);//wait a bit for stability 
  while(client.available())
    { 
    String line = client.readStringUntil('\r'); 
    Serial.print(line); 
    } 
  Serial.println();
  Serial.println("Finished");
  while (stopval ==1)  
  {
    delay(10000);
  }
}


The program is straightforward and anybody who claims that Arduino code is their favorite should be able to decipher it easily.

I will highlite some bits though.

const char *ssid     = "ROUTERSNAME";
const char *password = "ROUTERSPASSWORD";

Fill in your own credentials otherwise you will have no connection to the internet.

The first part in the setup routine should give you no problems in understanding what is happening. It is the loop where you should pay attention to.

  String dweetstring="GET /dweet/for/";
  dweetstring=dweetstring+String(thingName)+"?";
  dweetstring=dweetstring+"temp=";
  dweetstring=dweetstring+"12";
  dweetstring=dweetstring+" HTTP/1.1\r\n"+
      "Host: " +
      dweetsite +
      "\r\n" +
      "Connection: close\r\n\r\n";
   
  client.print(dweetstring);

This is the part that actually builds the message and Dweets it.

String thingName="Arduintemp";

Thingname was defined at the start of the program.

const char* dweetsite = "dweet.io";

And so is dweetsite the name of the host.

dweetstring wil be the text that is Dweeted. The variable name will be "temp" and its fixed value in this example = "12"

Feel free to replace the 'thing' name with any name you like and do the same for the variable and it's value.

  while(client.available())
    {
    String line = client.readStringUntil('\r');
    Serial.print(line);
    }

These lines clear the wifi buffer.

  while (stopval ==1) 
  {
    delay(10000);
  }

And this makes sure that the routine is run only once.

If this is going to be used to send sensor information at a regular interval you could use Deepsleep here to save power. If you do not have any idea on what Deepsleep is read this story http://lucstechblog.blogspot.com/2018/07/esp-power-reduction-withn-deepsleep.html

The Result




Like I demonstrated in the first story about Dweet I just pulled the information from Dweet.io with my browser.

Famous Last Words

Well here you have it. Arduino Code to Dweet with the ESP. Now send me a mail with a link or description of the projects you made with this.Now go back to the ESP-Basic version and look how I get the same result with just a few lines of code. You choose !!

Till next time
Have fun !!!

Luc Volders

Friday, November 15, 2019

ESP-01 programming board (new)

For an index to all my stories just click this line

First let me refresh your memory. Next to the NodeMCU and Wemos D1 Mini boards there is still the ESP-01 board available. It is not as popular as the other versions because it only has 2 I/O ports and no USB interface. However for building simple sensors like a thermometer or movement detector etc it is excellent. It uses not a lot power because the lack of USB chips etc.




Long time ago, November 2015 to be exact, I wrote a story about building your own ESP-01 programmer. You can re read that story here.

http://lucstechblog.blogspot.com/2015/11/esp8266-01-programming-board.html 
It is bulky and needs an FTDI (USB to serial adapter) but does the job flawlessly.





Now there is an alternative. And it is much cheaper as my DIY version. You do not need a separate FTDI, nor a piece of PCB, buttons etc. It is all inclusive, and a lot less bulky.




The trick is on this side.




The button puts the module in programming mode when slided to the ESP header. When the button is slided towards the USB connector the module is in communication mode. That's all. I use it frequent lately on a Windows 10 computer system without any problems.



The only flaw in my opinion is that it lacks a reset button. So to reset it you have to cut the power supply. The good part is it's small size and has a cheap price.

So when you are starting with using ESP-01 modules and want a cheap programmer this is a good solution. Just beware that you buy the one with the switch cause there are a lot of these look-alikes without the switch and those are just usable for communication and not for programming.

For those who need a quick reminder I also give you here the link to my story about the programming software for the ESP-01. Only needed if you are going to use ESP-Basic or Lua.
http://lucstechblog.blogspot.com/2015/11/flashing-esp8266.html

Till next time.
Have Fun

Luc

Friday, October 25, 2019

IoTtweet part 1

For a complete index of all my stories click this text

This page is removed as the IOTTweet service ceased to exist.

I also removed all references to IOTTweet in other stories on this weblog.

This was really frustrating as they did not give any notion upfront. They even did not bother sending an email to their users to tell them they stopped. So anybody using their service was suddenly in the dark !!

Maybe we should all stop relying on cloud services and invest a small amount of money in our own servers.

Luc Volders

Friday, October 11, 2019

Freeboard

For an index to all my stories click this text

Freeboard is a cloud based free service that allows you to display your sensor (IOT) data in a pretty way. However to display your data on freeboard you will have to Dweet your sensor values. So before you start reading this story I urge you to read the previous stories that discuss Dweet in depth. You can find those stories in the following links:

Dweet - A volatile IOT Cloud service 
Dweet again - Have your ESP8266 send data to Dweet with ESPBasic
Dweet with Android - Design an APP to use Dweet on your Phone
Dweet with Raspberry - Completing the line you can dweet now from any device
 
As you recall from these stories we can send data to Dweet and retrieve it again as text. This can be very usefull however most of the time you would like your data to be presented in a nice graphical way. That is where Freeboard comes in. Freeboard is a service that present the data from Dweet in a nice graphical way. When I tested Freeboard and wrote this story the service was free to use. Bad thing is that Freeboard is no longer free. A paid account is required. However you can try and use it for a month for free. Within this month you may decide wether you find it worth it.



There is a way to achieve the same as described here with Node-Red. And Node-red is free to use. You can build a Freeboard-like server with a Raspberry Pi and Node-Red. This is something I will adress in a future story.

First step is to go to the Freeboard.io website and register for a account.



Pressing START NOW brings you to the page where you can register for an account. If you already have an account you can SIGN IN on the top right side of the screen.

On the next page you can choose to alter your account settings and have the option MY FREEBOARDS. When this is your first time working with Freeboard choose for creating a new board. I called it My Home.



At first you have an empty screen.
This is where all the data will be displayed

As said before, Freeboard displays data that is Dweeted. Therefore you need first to Dweet some data. In the stories about Dweet I showed you how to do that. You can now Dweet some fake data. You can also use the values from my personal thermometer. I told you thermometers are stupid, however usefull for testing purposes......... So let´s have a look at my thermometer Dweets. My ´Thing´ is called Lucstemp2018 and the thermometer value is in the variable `temp´. Let us use that.

The first thing to do is to tell Freeboard where the data is coming from. On the top right side of your screen where it says DATA SOURCES click on ADD



Choose Dweet.io




Now give this datasource a name. I called it (obviously) thermometer and fill in your things name. And save.

For my thermometer I am going to make two graphical presentations. First a Gauge and second a line diagram.


Start by clicking on the left top side of the screen on ADD PANE. As you can see a new pane has been added to the screen.
You can click it and move it around on the screen to place it where you want it. For now let is be where it is.


Click on the wrench so you can edit the basic information for this pane. As the title I chose Mancave Temperature and did not alter the number of columns.




Now press the + sign and a screen opens in which you can enter the details of your widget. Choose Gauge.


Fill in the details on the Gauge. I named it Actual Temperature. In the value field click on the line next to it: Datasource and you will be presented by the name of your 'thing' and the data from it (temp). For units I choose Degrees C and as Minimum and Maximum values I choose -5 and 60. Choose SAVE again and that's it.



As you can see the Gauge is displayed immediately. And as you can see it is not very hot in my work room.

Now let's add the actual time to this information board.

Start with Adding a DATASOURCE (top right side of your screen) and choose Clock.


Fill in the details. I just called it time and left the refresh rate at every second. Save.


Now add a pane again. You can see on the top right side of the page which data sources are accessible at this moment (Thermometer and Time).


Click on the wrench of this pane to give it a name.


Choose text


Again fill in the details of this data source. Give it a name and the size you want it displayed. And use as DATASOURCE the just added TIME and use 'full string value


And there you are. You can actually see when I wrote this story........

Now let us move back to the MANCAVE TEMPERATURE pane and press on the + sign.


As type choose sparkline.


And fill in the details. I gave it the title TEMPERATURE HISTORY and as data source I used the same Thermometer and the variable 'temp'

And as you can see after a while a nice diagram is showing that displays the temperature over time.

Rests only one last possibillity.
Remember the sory about how you could PING IP adresses to see if a network attached device is working or if someone is at home ??? Well re-read the sory here. 


https://lucstechblog.blogspot.com/2019/08/dweet-again.html

In that sory we added a line to the PING program to Dweet the information. Well we are going to pick that info up and display it on our freeboard.

tosend$ = "dweet.io/dweet/for/Lucshome?therm="&str(therm2)&"&domo="&str(domo2)&"&lucphone="&str(lucphone2)&"&elsphone="&str(elsphone2)
a$= wget(tosend$,80)


These were the lines in our basic program that send the information to Dweet.
So thing 'thing' is called Lucshome and the variables are therm, domo, lucphone and elsphone.

What I want is just a simple indicator that displays wether the Thermometer is working, Domoticz is working and wether me or my girplfriend are at home.

First add a new DATASOURCE





Again use Dweet.io as the type, and Lucshome as the name of the 'thing'



Now make a new pane. I called it "Who is at home"





Add a widget and choose Indicator light as the type.


Fill in the details.
The widget will indicate if I am at home or not. So I called it "Where is Luc". The DATASOURCE is the just added "Who is at home" and the variable is "lucphone".



In the same pane click again on the + sign and add another widget. This time for Els'es phone.


And as you can see we have now 2 indicator lights that show wether Els and I are at home.

And that's about it.

There are many more possibillities with Freeboard. You can add a Google Map with location details, add pictures, add a pointer that works like a compass, add text and HTML code. So you can have a lot of widgets on your screen. And as you saw in the example above. You can have information from multiple ESP's on your screen. Did you know by the way that you can also Dweet with a Raspberry. That opens even more possibillities.

And best of all you can have multiple boards on one account. So you can make boards for your home, work, garden, aquarium etc etc etc.

Privacy, what privacy ??

Well as you have learned from the Dweet stories: everybody can see your data when they know your 'things'name. There is however so much data Dweeted every moment that, when you choose a secret 'thing' name it will be difficult to find your specific data.

Freeboard needs you to log in to alter the boards and widgets.
However when a board is finished or being build you can see in the URL of your browser the ID of the board.


So if anyone is looking over your shoulder when you are building your board and notes down the URL he can look at your board without having to log in. So be carefull what information you give/show to who.

Till next time.
Have fun

Luc

Friday, October 4, 2019

Choosing colors for a webpage or for neopixels

For an index to all my stories click this text

This article was first published in my book about the ESP32 which is called ESP32 uitgelegd and is available in Dutch only at this moment. You can buy my book here: https://www.boekenbestellen.nl/boek/esp32-uitgelegd/9789463456814
You can find more information on this book here: http://lucstechblog.blogspot.com/2019/07/esp32-uitgelegd-nu-verkrijgbaar.html

In previous articles on this weblog I often published a program that runs on an ESP8266 or ESP32  and produces a webpage.

To fancy-up these webpages I gave them a backgound color of my liking. My chosing of colors may however differ from your likings. So I first show you how to pick colors the easy way.

<body bgcolor=PowderBlue>

Look at this line. It sets the background color for an entire webpage to PowderBlue. Where does that name come from ?

You can find all pre-defined HTML color names on the following webpage:

https://www.w3schools.com/colors/colors_names.asp

There are 140 names pre-defined.



Above you can see a few of the predefined names that you can use.

Likely you want still other colors. Well you can do that by alterning the code in such a way that it uses RGB codes which gives you much more freedom. The way to change the names in RGB code is as follows:

<body style = 'background-color:rgb(247, 2, 235)'>

But how to find these RGB codes. Well here is a neat trick.



Just open your browser and with your favorite search engine type the following search code:
rgb color picker

As you can see you are presented a large rectangle that is set at the hue's of a certain color. With the slider you can change the color and with the round dot you can specify the hue.

Beneath the rectangle you can see the values of that color presented in HEX, RGB, HSV HSL and CMYK.

Our interest is in the RGB values. So just alter the RGB values in your HTML code with the found RGB values in the color picker and you are all set.


Naturally this also applies when you are building an APP with MIT's APP-Inventor. By altering the RGB values you can alter the background color of your app.

Neopixels Bonus

As a bonus you can also use these RGB values to set the colors of your neopixels. There is just one flaw. It is difficult to produce brown colors. They will mostly tend to look orange-ish.

That is something I already noticed when building my resistor code watch:
https://lucstechblog.blogspot.com/2016/10/nuw-resistor-color-code-clock.html

Till next time
keep tinkering and have fun


Luc Volders

Friday, September 27, 2019

Dweet with Raspberry

For a complete index of all stories click this text


Introduction

This is my fourth story on Dweet but for those who have no knowledge about this service I will do a short introduction.

Dweet is a free service for IOT projects found at https://dweet.io/
Dweet is like Twitter for IOT projects. You send a message (sensor value or whatever) to Dweet.io and that message is stored for 24 hour. After that it is removed. Dweet.io remembers the last 5 messages you send to it. You can retrieve the message as many times as you like within those 24 hour. Dweet.io is totally anonymous. You even do not have to make an account for using the service. Dweet.io is used by many people all over the world. So how do you recognise which message is yours. You give your message a name called a 'thing' and a value. As long as you know the name of the 'thing' you can retrieve it's last 5 messages.

You can have multiple 'things' that all can Dweet and from each of them the last 5 messages are remembered for 24 hour.

For example you can have 3 ESP8266's which you give the thing names ESP1, ESP2, and ESP3. Each of these 'things' can send a message to Dweet containing sensor values. And if you want to know the value for the third ESP you just get the information for 'thing' ESP3 from Dweet.

I wrote 3 previous stories about Dweet.io on this weblog. The first described how to send and retrieve messages directly from your browser. Read that story here:

https://lucstechblog.blogspot.com/2019/05/dweet-volatile-iot-cloud-service.html

The second story described how to send and retrieve Dweets with an ESP8266 using ESP-Basic. Read that story here:

https://lucstechblog.blogspot.com/2019/08/dweet-again.html

And then I wrote a story on sending and retrieving Dweets with your Android phone using an dedicated app written with App Inventor. Re-read that story here:
https://lucstechblog.blogspot.com/2019/09/dweet-with-android.html

In this story I am going to show you how to send and retrieve Dweets with the Raspberry Pi. So when this story is completed you will have the means to send data using Dweet to any of your devices.

Raspberry Pi-Zero






I doubt that this small wonder needs an introduction. There are 2 versions. One with Wifi and Bluetooth and one without.
The Raspberry Pi Zero is more expensive as an ESP8266. The ESP costs about 3 Euro and the Raspberry around 11 euro. You do need a SD card to operate it which adds to the costs. However the Raspberry is a full Linux computer which is much faster as an ESP and has more memory and I/O ports. It can be equipped with a screen, keyboard and mouse like a full-fledged computer. However that not needed for IOT projects.

In this story I am not going to tell you how to set up a Raspberry. Neither am I going to tell you how you can use it headless (without monitor, keyboard and mouse) you can find all that information in about a 1000 stories on the web. In doubt go to the Raspberry Pi's official website and start there: https://www.raspberrypi.org/

Dweet with the Raspberry

For Dweeting with the Raspberry Pi I am going to use Python3. It is a bit more complicated as Dweeting with an ESP programmed in ESP-Basic but still no rocket-science. If you know the principles of programming in Python and are familiar with lists and dictionaries you can Dweet. If you need to learn Python I recommend SAMS book: https://lucstechblog.blogspot.nl/2017/08/learn-python.html

There is a Python library for working with Dweet called Dweepy. I have looked at it and find working with it not really easier as working without that library. So I am not going to use Dweepy but work directly from Python.

You do need 2 libraries however being REQUESTS and JSON. Both these libraries are included in the Raspberry's version of Python so nothing has to be installed.



Fire up your Raspberry and go to the Applications menu and start Python3 idle. I'll start with demonstrating how to Dweet with direct commands and put it all in a small program which you can modify for yourself later on.

Sending a Dweet with Python Idle

First thing to do is to import the REQUESTS library for internet information exchange and the JSON library to parse Dweet's answers.

>>> import requests
>>> import json


Now we need a value which we are going to send to Dweet. Let's use a fake temperature value and use that:

>>> temperature is 17.5

Dweet works with strings and the temperature value is a floating point variable. So lets change that:

>>> tempstring = str(temperature)

We now have the value that we are going to Dweet. Now lets make up a name for our 'thing'. For examples sake I call it Pyttemp.

Dweet says on its documentatio page that to send a Dweet you need to use the following format:

https://dweet.io/dweet/for/my-thing-name?hello=world

So let us translate that to Python:

>>> s = requests.get('https://dweet.io/dweet/for/Pyttemp?temp='+tempstring)

I put the command in the variable s so the answer of Dweet will not be printed on the screen.
That's all.

Go to your browser and in the URL field fill in the command for retrieving the Dweet from your thing which is in general form:

https://dweet.io/get/latest/dweet/for/my-thing-name

and in our particular case:

https://dweet.io/get/latest/dweet/for/Pyttemp
 

And there is your Dweet.

So this works. Easy !!

Retrieving a Dweet with Python Idle

Now we know that the Dweet has successfully been send we can retrieve it. This invloves a bit more work.

First we need Python to collect the Dweet. From the browser we just did that with the following command:

https://dweet.io/get/latest/dweet/for/Pyttemp

in Python this is translated in:

>>> r = requests.get('https://dweet.io/get/latest/dweet/for/Pyttemp')

The prompt will return but nothing is displayed. So lets see if the dweet was indeed retrieved:

>>> print (r)
<Response [200]>

Hey !! we do get a response but not what we expected. Actually the response tells us that the Dweet is indeed retrieved succesfully (code 200). However we want to get the actual information.
That is where the JSON library jumps in.

The JSON library parses the data from the JSON information we get back from Dweet.io. This is achieved by the following command:

>>> a=r.json()

Now we can print the information

>>> print (a)
{'by': 'getting', 'the': 'dweets', 'this': 'succeeded', 'with': [{'content': {'temp': 17.5}, 'created': '2018-02-18T14:47:14.080Z', 'thing': 'Pyttemp'}]}
And there it is. Next step is to distill only the information we need (Pyttemp) from this.

so let's see what a is:

>>> type (a)
<class 'dict'>

The variable a is a dictionary. That opens possibillities. We can retrieve the individual dictionary entries. First we examine what the entries are:

>>> for f in a: print (f)

by
the
this
with


We are interested in what is in the 'with'part. The following line retrieves that from the dictionary:

>>> b=a['with']

We now have retrieved part of the response. Let's have a look:

>>> print (b)
[{'thing': 'Luctemp182', 'created': '2018-02-18T10:28:46.312Z', 'content': {'temp': 19.5}}]

>>> type (b)
<class 'list'>

So b is a list. We have to get the first part out:

>>> b[0]
{'thing': 'Luctemp182', 'created': '2018-02-18T10:28:46.312Z', 'content': {'temp': 19.5}}

b[0] is a dictionary if all is well:

>>> type (b[0])
<class 'dict'>

From this dictionary we need to retrieve the 'content' entry which by the way is a dictionary in itself:

>>> c = b[0].get('content')

so the last step is:

>>> d=c.get('temp')

There we are:

>>> print (d)
19.5

Wow, Pfoeee !!!!!




The program

So it works in idle. Let's make a program out of it.



Start the program Editor Geany and paste the following code in:






import requests
import json
# next line dweets information
# it takes a floating point and converts it to a string
temperature = 19.5
tempstring = str(temperature)
s = requests.get('https://dweet.io/dweet/for/Pyttemp?temp='+tempstring)
# next line retrieves the dweeted information
r = requests.get('https://dweet.io/get/latest/dweet/for/Pyttemp')


a=r.json()
print (a)
b= (a.get('with'))
c=b[0]
d=c.get('content')
e=d.get('temp')
print() 
print()
print (e)


Run the program and:


We've done it.

Concluding

I do admit that this is a lot more complicated as Dweeting with an ESP8266 programmed with ESP-Basic. However this opens a lot of new possibillities. The Raspberry is cheap and has loads of IO ports. It's multitasking capabilities can have it Dweeting in the background while it is doing other tasks as well.

Till next time
Have fun !!!!

Luc Volders

Friday, September 20, 2019

Dweet with Android

You can find an index of all my stories by clicking this text

For one reason or another I am still fascinated by Dweet. The possibillity to send and retrieve data anonymous intrigues me. Even more as that data is volatile. It disappears in 24 hours so the timespan to retrieve it is short. I see possibillities however have not yet a firm plan for using Dweet.

With Dweet you can send data from one or multiple ESP's, and you can retrieve it with one or multiple ESP's. It would be nice if you could Dweet with various apparatus and not only with ESP's. And actually you can.

In this story I am going to show you how you can send and retrieve data to Dweet with a self-build Android APP. In an upcoming story we are going to Dweet with a Raspberry Pi.

For those who do not have a clue on what I am ravelling about read the first two stories on Dweet.io: http://lucstechblog.blogspot.com/2019/05/dweet-volatile-iot-cloud-service.html and 

THIS One: http://lucstechblog.blogspot.com/2019/08/dweet-again.html

App Inventor

Building APP's for Android phones and tablets is much easier as you might think. The MIT offers a complete setup which is free to use. The only thing you have to do is to get a free account and you can start devellopping Android App's no strings attached. You can even distribute them on Google Playstore and earn some mony with your APP's.

App inventor is a block-oriented programming technique just like Scratch on the Raspberry Pi or KBIDE for the ESP32 or Ardublock for the Arduino's.

I have used App Inventor form various projects on this web-log. Just look for them in my index-page: http://lucstechblog.blogspot.nl/p/index-of-my-stories.html

So start by pointing your web-browser to the App-Inventor http://appinventor.mit.edu/explore/
page. Start an account, look at the tutorials and documentation and start coding your own APP's.

Sending a Dweet



Let's look at the Designer screen. The App has 2 Labels that indicate what you need to fill into the textboxes. Next it has 1 button that will initiate the code that will send the Dweet and there is a hidden Web attribute.

In the first Textbox called Thingname you will fill in the name of your 'Thing' and in the second Textbox called Thingvalue you just fill in the valuen that you want to be Dweeted. The value can be a figure or a text. Whatever you want. This is just for testing purposes, we are not going to send real data.

Press the button and the data is send to Dweet.io

Let's have a look at the program block.


First we initialise a global variable called "Testval"

The program waits till the button is pressed. When that happens the variable "Testval" is filled with a string that contains all the information to send a Dweet. The first line is

https://dweet.io/dweet/for/

Then we add the value found in the textbox "Thingname"

?val=

is the next part of the line and the last part is the value found in the textbox "Testval"

Suppose we filled the name Dweettest in as Thingname and 125 as Thingvalue the complete text in the variable Testval would become:

https://dweet.io/dweet/for/Dweettest?val=125

And that is exactly what Dweet expects to receive. In the green part of the block the URL is set to get "Testval" which has the value https://dweet.io/dweet/for/Dweettest?val=125
And lastly the function is executed.





This is how it looks on the Android simulator and that's also how it will look on your phone's or tablet's screen.

Does it work ?

Well the only thing you have to do is to fill in the following URL in your browser:

https://dweet.io/get/latest/dweet/for/Dweetest and the result will be shown:





Receiving a Dweet.

So sending works. Now let's see if we can retrieve Dweets.

I build a seperate APP for that. You could combine it in one APP with two screens (one for sending and one for retrieving) or even do both on the same screen.



As you can see the top of the screen has a textbox in which you can fill in the Things name. Then there is the button that will perform the action when pressed. And further there are two textboxes that will display the received Dweet and an analysed version.
Lastly the listbox will show the actual received values.
There is one hidden component and that is the web component called Dweetconnect

When the screen has been setup you can move over to programming the blocks. This is more complicated.


Clicking the image will enlarge it which will make it easier to replicate the blocks for your own purposes.

First two variables are defined called DweetJson and Initval. These will be used to distill the information from the Dweet.

Next the program waits till the button is pressed and then it gets the Dweet for whatever is filled in into the Thingname textbox.

Then the real magic begins. Initval is set to the information which is in the "content" part of the Dweet. That is where the actual values are stored.

And the last part is where DweetJson is filled with the actual content.


As you can see it actually works as expected. When the name Dweettest is filled in in the Thingname field and the button is pressed the value 125 which was send previously is retrieved.

A practical example.

Remember the Thermometer http://lucstechblog.blogspot.nl/2017/11/oh-no-not-another-wifi-thermometer.html In the previous story I altered the software so it sends information to a webpage, Thingspeak and Dweet.io

If everything works well we should be able to retrieve the last temperature.



Above is the information on the thermometers web-page


And if you fill in "Lucstemp2018" as the Thingsname the temperature in my work-room is retrieved.

How about multiple fields ??



Here you can see the information that is found with my program that ping's my wifi devices like I described in THIS STORY and that I had the information Dweeted in this story.


And this is what it looks in my simple Android APP.

That's it for now. I bet you can find some usefull purpose for this. This POC (proof of concept) demonstrated how using Dweet.io it is easy to send information from an ESP to Android or from Android to an ESP.

Beware: keep in mind that this information is publically available for anyone who is actively looking for it. So be carefull with what you Dweet and do not use obvious names for your Thing.

Till next time
Have fun

Luc Volders