Share My Creation ESP-01 IoT

The common way for IoT is to set the ESP8266 to server mode and connect to it from the WWW by entering your router static IP address in a browser. My internet provider don't have the option for static IP address. In this project the ESP is set to station mode and it sends several bytes of data to my website every 20 seconds. I can browse at the data from the internet by logging to a page in my website, read analog and digital input and also send a command to switch an output of the Arduino.
I use an Arduino Uno and ESP-01. The code controls the ESP using AT commands only. I used software serial so it's easier to upload the sketch. The code searches the response from the ESP for the command ON/OFF from the website.
I added a suggestion for a PHP script for the website. file a32.php is a blank file which a31.php writes the data to be recorded, page a33.php is for the user to read the data from the ESP and to send command to the Arduino.
In the code enter your SSID and PASSWORD of your router. Also change the name of my website to yours.
You can use my website to test your setup. http://moty22.co.uk/a33.php
B4X:
Sub Process_Globals
    Public serial1 As Serial
    Public wifi As SoftwareSerial
    Private astream As AsyncStreams
    'Private astream1 As AsyncStreams
    Private tmr1 As Timer
    Private astream As AsyncStreams
    Private bc As ByteConverter
    Private ana, in3, out7 As Pin
    Private eol() As Byte = Array As Byte(13, 10)
    Private q() As Byte = Array As Byte(0x22)
End Sub

Private Sub AppStart
    serial1.Initialize(9600)
    'astream1.Initialize(serial1.Stream, Null, Null)
    wifi.Initialize(9600,6,5)
    astream.Initialize(wifi.Stream, "astream_NewData", Null)
    tmr1.Initialize("tmr1_Tick", 20000)
    tmr1.Enabled = False
    Log("AppStart")
    ana.Initialize(ana.A0, ana.MODE_INPUT)
    in3.Initialize(3, in3.MODE_INPUT_PULLUP)
    out7.Initialize(7, out7.MODE_OUTPUT)
    astream.Write("AT+RST".GetBytes)    'reset wifi
    astream.Write(eol)
    Delay(1000)
    astream.Write("AT+CWMODE=1".GetBytes)    'station mode
    astream.Write(eol)
    Delay(1000)
        'Connect To AP, ADD YOUR ROUTER SSID And PASSWORD
    astream.Write("AT+CWJAP=".GetBytes)
    astream.Write(q)
    astream.Write("Sagemcom-GPHQR3".GetBytes)    'change to your router SSID
    astream.Write(q)
    astream.Write(",".GetBytes)
    astream.Write(q)
    astream.Write("XXXXXX".GetBytes)    'enter your router password
    astream.Write(q)
    astream.Write(eol)
    Delay(5000)
    Delay(500)
    tmr1.Enabled=True
   
End Sub

Sub tmr1_Tick
    Dim msg(8) As String
    Dim sl As Byte
    Dim sl1 As String

    If in3.DigitalRead Then msg(5)="OFF" Else msg(5)="ON"
    msg(0) = "GET /a31.php?v1="
    msg(1) = 0
    msg(2) = "&v2="
    msg(3) = ana.AnalogRead
    msg(4) = "&v3="
    msg(6) = " HTTP/1.1"
    msg(7) = "HOST: moty22.co.uk"    'change To your website address
   
    sl = msg(0).Length+msg(1).Length+msg(2).Length+msg(3).Length+msg(4).Length+msg(5).Length+msg(6).Length+msg(7).Length+6
    sl1=sl
    web_page
    Delay(1000)
    astream.Write("AT+CIPSEND=".GetBytes)
    astream.Write(sl1.GetBytes)
    astream.Write(eol)
    Delay(1000)
    astream.Write(msg(0).GetBytes)
    astream.Write(msg(1).GetBytes)
    astream.Write(msg(2).GetBytes)
    astream.Write(msg(3).GetBytes)
    astream.Write(msg(4).GetBytes)
    astream.Write(msg(5).GetBytes)
    astream.Write(msg(6).GetBytes)    '" HTTP/1.1"
    astream.Write(eol)
    astream.Write(msg(7).GetBytes)    '"HOST: moty22.co.uk"
    astream.Write(eol)
    astream.Write(eol)

End Sub

Sub web_page
    astream.Write("AT+CIPSTART=".GetBytes)
    astream.Write(q)
    astream.Write("TCP".GetBytes)
    astream.Write(q)
    astream.Write(",".GetBytes)
    astream.Write(q)
    astream.Write("moty22.co.uk".GetBytes)
    astream.Write(q)
    astream.Write(",80")
    astream.Write(eol)
End Sub

Sub astream_NewData (Buffer() As Byte)
    Dim reply() As Byte      
    Dim off() As Byte  = Array As Byte(0x4F,0x46)    'OF
    Dim on() As Byte  = Array As Byte(0x4F,0x4E)    'ON
   
    If Buffer.Length > 80 Then
        reply=bc.SubString2(Buffer, Buffer.Length - 3, Buffer.Length-1)
        If bc.ArrayCompare(reply, off)=0 Then out7.DigitalWrite(False)
        If bc.ArrayCompare(reply, on)=0 Then out7.DigitalWrite(True)  

    End If
End Sub
 

Attachments

  • website_php.zip
    1,021 bytes · Views: 345
  • wifi.zip
    1.5 KB · Views: 363
  • esp-01-4x3.jpg
    19.6 KB · Views: 1,497
  • wifiB4R.gif
    4.9 KB · Views: 291
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…