Connecting ESP32 to Tago

@Adrian Tromp

Hi, struggling to connect an ESP32 to Tago, are there any examples available for Arduino IDE, either for HTTP or MQTT?

Thanks

@Vitor Lima

Hi @adrian ,
Do this documentation work for you? Arduino - TagoIO

@Adrian Tromp

Hi Vitor

I’ve gone through the Arduino example in the documentation, but it won’t compile for the ESP32 board.

I’ve tried to adapt the sketch for the ESP32 board but haven’t been able to connect to Tago yet.

Any examples using this board would be appreciated.

Thanks!

Adrian Tromp

Ph: 082 449 5015

Water, Ozone & Monitoring Solutions

www.earthsmart.co.za

@Felipe Lima

Hi @adrian, we do not have an example using ESP32, but the implementation is pretty close, can you share the error you are getting?

Hi All,

An code example using ESP32 + MQTT, publishing and subscribing, to TAGO.io will be great.

Hi Flavio,

You can use this code as a basis to build your integration:

NOTE: This is an implementation for Arduino and HTTP.

See here the TagoIO MQTT documentation.

*  Send data to Tago - Wifi101 This sketch connects to the TAGO server and post a data using an Arduino Wifi 101 shield. Circuit: * WiFi shield attached to pins SPI pins and pin 7 Based on the the Wifi Web Client from http://arduino.cc/en/Tutorial/WifiWebClientRepeating. */#include <SPI.h>#include <WiFi101.h>char ssid[] = "## YOUR NETWORK HERE ##";      //  your network SSID (name)char pass[] = "## YOUR NETWORK PASSWORD HERE ##";   // your network passwordString Device_Token = "## INSERT THE TOKEN FOR YOUR DEVICE HERE ##";int keyIndex = 0;         // your network key Index number (needed only for WEP)int sensorPin = A0;       // select the input pin for the analog inputint rawvoltage = 0;       // variable to store the value coming from the sensorfloat sensorValue = 0;String value_string = "";int status = WL_IDLE_STATUS;// Initialize the Wifi client libraryWiFiClient client;// server address:char server[] = "api.tago.io";unsigned long lastConnectionTime = 0;            // last time you connected to the server, in millisecondsconst unsigned long postingInterval = 2L * 1000L; // delay between updates, in millisecondsvoid setup() {  //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  }  // check for the presence of the shield:  if (WiFi.status() == WL_NO_SHIELD) {    Serial.println("WiFi shield not present");    // don't continue:    while (true);  }  // attempt to connect to Wifi network:  while ( status != WL_CONNECTED) {    Serial.print("Attempting to connect to SSID: ");    Serial.println(ssid);    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    status = WiFi.begin(ssid, pass);    // wait 10 seconds for connection:    delay(10000);  }  // you're connected now, so print out the status:  printWifiStatus();}void loop() {  // if there's incoming data from the net connection.  // send it out the serial port.  This is for debugging  // purposes only:  while (client.available()) {    char c = client.read();    Serial.write(c);  }  // if TWO seconds have passed since your last connection,  // then connect again and send data:  if (millis() - lastConnectionTime > postingInterval) {    // read the value from the sensor:    rawvoltage = analogRead(sensorPin);    // converting that reading to voltage, for 3.3v voltage    float voltage = rawvoltage * 3.3;    voltage /= 1024.0;    // converting to Celsius    float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset                                                  //to degrees ((voltage - 500mV) times 100)    int i = (int) temperatureC;                   //convert data format from float to int    value_string =String(i);                      //end of conversion, to finally get it in the String format(Celsius)    Serial.println(value_string);    // then, send data to Tago    httpRequest();  }}// this method makes a HTTP connection to the server:void httpRequest() {    // close any connection before send a new request.    // This will free the socket on the WiFi shield    client.stop();    Serial.println("\nStarting connection to server...");    // if you get a connection, report back via serial:    String PostData = String("{\"variable\":\"temperature\", \"value\":") + String(value_string)+ String(",\"unit\":\"C\"}");    String Dev_token = String("Device-Token: ")+ String(Device_Token);    if (client.connect(server,80)) {                      // we will use non-secured connnection (HTTP) for tests    Serial.println("connected to server");    // Make a HTTP request:    client.println("POST /data? HTTP/1.1");    client.println("Host: api.tago.io");    client.println("_ssl: false");                        // for non-secured connection, use this option "_ssl: false"    client.println(Dev_token);    client.println("Content-Type: application/json");    client.print("Content-Length: ");    client.println(PostData.length());    client.println();    client.println(PostData);    // note the time that the connection was made:    lastConnectionTime = millis();  }  else {    // if you couldn't make a connection:    Serial.println("connection failed");  }}void printWifiStatus() {  // print the SSID of the network you're attached to:  Serial.print("SSID: ");  Serial.println(WiFi.SSID());  // print your WiFi shield's IP address:  IPAddress ip = WiFi.localIP();  Serial.print("IP Address: ");  Serial.println(ip);  // print the received signal strength:  long rssi = WiFi.RSSI();  Serial.print("signal strength (RSSI):");  Serial.print(rssi);  Serial.println(" dBm");}

Any example for MQTT subscribing? Like TAGO.io dashboard send a bool to the MCU to reset it.

I’m not an expert in hardware development. But I would start by connecting to the TagoIO broker using the Paho library, see here: https://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/pubsync.html

Hi All,

I created this github + wiki with an example of how to connect an ESP8266 to Tago.io by MQTT, as well as all the tricks to do it.

https://github.com/flaviopuhl/TAGOioESP8266MQTTSimpleDemo

Enjoy

check out this video it worked for me

https://www.youtube.com/watch?v=Bf_UUYYHosY