LoginSignup
1
1

More than 5 years have passed since last update.

smallbasicでmqtt

Posted at

概要

smallbasicでsangoにesp8266からmqttのpublishしてみた。
mqttはスクラッチした。

写真

MVC-018S.JPG

画面

mqtt.PNG

サンプルコード

c = Text.GetCharacter(34)
n = Text.GetCharacter(13) + Text.GetCharacter(10)
button0 = Controls.AddButton("open", 30, 20)
button1 = Controls.AddButton("gmr", 90, 20)
button2 = Controls.AddButton("cwjap", 150, 20)
button3 = Controls.AddButton("cipmux", 210, 20)
button4 = Controls.AddButton("start", 30, 80)
button5 = Controls.AddButton("cifsr", 90, 80)
button6 = Controls.AddButton("pub", 150, 80)
button7 = Controls.AddButton("status", 210, 80)
Controls.ButtonClicked = OnButtonClicked
mtextbox0 = Controls.AddMultiLineTextBox(10, 150)
Controls.SetSize(mtextbox0, 300, 200)
Controls.SetTextBoxText(mtextbox0, "send" + n)
mtextbox1 = Controls.AddMultiLineTextBox(320, 30)
Controls.SetSize(mtextbox1, 300, 400)
Controls.SetTextBoxText(mtextbox1, "recv" + n)
Sub OnButtonClicked
  button = Controls.LastClickedButton
  If (button = button0) Then
    class1.OpenPort("COM5", 115200)
    class1.SetHandshake("n")
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">open" + n)
  ElseIf (button = button1) Then
    class1.TXString("AT+GMR" + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">gmr" + n)
    Program.Delay(100)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
  ElseIf (button = button2) Then
    class1.TXString("AT+CWJAP=" + c +"free-spot" + c  + "," + c + c + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">cwjap" + n)
    Program.Delay(3000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
  ElseIf (button = button3) Then
    class1.TXString("AT+CIPMUX=0" + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">cipmux" + n)
    Program.Delay(100)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
  ElseIf (button = button4) Then
    class1.TXString("AT+CIPSTART=" + c + "TCP" + c  + "," + c + "lite.mqtt.shiguredo.jp" + c + ",1883" + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">start" + n)
    Program.Delay(1000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
    class1.TXString("AT+CIPSEND=52" + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">auth" + n)
    Program.Delay(1000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
    buf = ""
    buf = buf + Text.GetCharacter(16)
    buf = buf + Text.GetCharacter(50)
    buf = buf + Text.GetCharacter(0)
    buf = buf + Text.GetCharacter(4) + "MQTT"
    buf = buf + Text.GetCharacter(4)
    class1.TXString(buf)
    class1.TXByte(194)
    buf = ""
    buf = buf + Text.GetCharacter(0)
    buf = buf + Text.GetCharacter(60)
    buf = buf + Text.GetCharacter(0)
    buf = buf + Text.GetCharacter(4) + "papa"
    buf = buf + Text.GetCharacter(0)
    buf = buf + Text.GetCharacter(14) + "ohisama@github"
    buf = buf + Text.GetCharacter(0)
    buf = buf + Text.GetCharacter(16) + "****************"
    class1.TXString(buf)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">" + n)
    Program.Delay(1000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
  ElseIf (button = button6) Then
    While "True"
    class1.TXString("AT+CIPSEND=28" + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">pub" + n)
    Program.Delay(1000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
    buf = ""
    buf = buf + Text.GetCharacter(48)
    buf = buf + Text.GetCharacter(26)
    buf = buf + Text.GetCharacter(0)
    buf = buf + Text.GetCharacter(20) + "ohisama@github/test0"
    buf = buf + "28.5"
    class1.TXString(buf)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">" + n)
    Program.Delay(1000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf
    Program.Delay(19000)
    EndWhile
  ElseIf (button = button5) Then
    class1.TXString("AT+CIFSR" + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">cifsr" + n)
    Program.Delay(1000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf  
  ElseIf (button = button7) Then
    class1.TXString("AT+CIPSTATUS" + n)
    text = Controls.GetTextBoxText(mtextbox0)
    Controls.SetTextBoxText(mtextbox0, text + ">status" + n)
    Program.Delay(1000)
    s = Class1.RXAll()
    if s <> "" then
      text = Controls.GetTextBoxText(mtextbox1) + "@"
      Controls.SetTextBoxText(mtextbox1, text + s + n)
    EndIf  
  EndIf
EndSub

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1