Magic Switch IOT Project

Thread Starter

visionofast

Joined Oct 17, 2018
106
Hi,
I'm working on an IOT project, based on a html switch that is able to pay my far away sister's unpaid bills ...by just a "pressing on" action.
Indeed,it sends a HTTP POST request to a server (established on nodemcu board) in order to make a SIM800 to execute some USSD codes (that are determined to pay the bills).
The HTML switch is supposed to be on a linux hosting (permanently accessible with a public domain).
I remember there was a signal called "magic packet" for ethernet ports of old motherboard's that could wake them up ,so i called it "magic switch".
the first part is main switch's html code by sending HTTP POST request (as a client) via javascript:

Code:
<div class="switch demo">
                    <input type="checkbox" name="dark_light" onchange="loadDoc()">
                    <label></label>
                </div>
<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("POST", "http://test.duckdns.org:8888/body", true);

  xhttp.send("hello buddy");
}

</script>
For the above part , I'm still looking for a fancy css code for the "switch demo" class...

the next part is Arduino code for Nodemce 12E, that makes the board listen to http requests through a DDNS configured with a static IP on my DSL modem via an opened port :

Code:
t #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EasyDDNS.h>
ESP8266WebServer server(8888);

const char* ssid = "SSID";
const char* password =  "PASS";

void setup() {

    Serial.begin(9600);
    IPAddress ip(192,168,1,20);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
IPAddress primaryDNS(8, 8, 8, 8);
WiFi.config(ip, gateway, subnet,primaryDNS);
    WiFi.begin(ssid, password);  //Connect to the WiFi network

    while (WiFi.status() != WL_CONNECTED) {  //Wait for connection

        delay(500);
        Serial.println("Waiting to connect...");

    }

    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());  //Print the local IP

    server.on("/body", handleBody); //Associate the handler function to the path

    server.begin(); //Start the server
    Serial.println("Server listening");
    EasyDDNS.service("duckdns");    // Enter your DDNS Service Name - "duckdns" / "noip"
EasyDDNS.client("test.duckdns.org","token");    // Enter ddns Domain & Token | Example - "esp.duckdns.org","1234567"
EasyDDNS.update(1000);
Serial.println("DNS Initiated...");

}

void loop() {

    server.handleClient(); //Handling of incoming requests

}

void handleBody() { //Handler for the body path

      if (server.hasArg("plain")== false){ //Check if body received

            server.send(200, "text/plain", "Body not received");
            return;

      }

      String message = "Body received:\n";
             message += server.arg("plain");
             message += "\n";

      server.send(200, "text/plain", message);
      Serial.println(message);
      if (server.arg("plain")=="hello buddy")
      {
        Serial.println("matched");
      }

}
it brings up the board waiting for a default string "hello buddy" as signal and then print it on serial output.
I didn't use nodemcu as switch's host,due to security ,easy access and keeping the switch permanently available to send other possible requests in the future.
For the next part, I still doubt about the version of SIM800 module to be compatible with Nodemcu's 3.3v serial interface and a proper library for it?!
thanks for help.
 
Last edited:
Top