Sorin Trimbitas
Published © GPL3+

AWS IoT for any device that is Internet aware (with example)

Want to use the AWS IoT on any device that has a connection to the Internet? Use this tutorial to know how.

BeginnerProtip4,308
AWS IoT for any device that is Internet aware (with example)

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

AWS IoT
Amazon Web Services AWS IoT

Story

Read more

Code

MQTT Proxy Example

Arduino
Example on how to use the proxy with NodeMCU
#include <ESP8266WiFi.h>

const char* ssid = "WIFI_SSID";
const char* password = "WIFI_PASSWORD";
const char* proxy_host = "62.117.70.43";
const char* proxy_path = "/test/proxy.php";

int is_connected = 0;

void setup()
{
  connect_to_wifi_ap();
}

void loop() {
  // put your main code here, to run repeatedly:

}

int send_data_to_proxy(String topic, String msg)
{
  force_connect_if_offline();
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if ( ! client.connect(proxy_host, 80)) {
    return -1;
  }
  
  // This will send the request to the server
  client.print(String("GET ") + proxy_path + "?topic=" + topic + "&msg=" + msg + " HTTP/1.1\r\n" +
               "Host: " + proxy_host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(100);
  
  int cnt = 0;
  while( ! client.available()){
    delay(10);
  }
  String request;
  while (client.available()){
    // We connected :)
    request = client.readStringUntil('\r\n'); 
    // You can do whatever you want with the output of the PHP proxy stored in request
    // ...
  }
  return 1;
}

void force_connect_if_offline()
{
  if (WiFi.status() != WL_CONNECTED)
  {
      connect_to_wifi_ap();
  }
}

void connect_to_wifi_ap()
{
  WiFi.begin(ssid, password);
  delay(2000);
  is_connected = 0;
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
  }
  is_connected = 1;
}

Github

Credits

Sorin Trimbitas

Sorin Trimbitas

5 projects • 30 followers
Software developer.

Comments