Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Thingspeak field update example

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1136

poland.gif
PostPosted: Fri Jan 15, 2016 3:26 pm    Post subject: Thingspeak field update example Reply with quote

Hello. This code is only example and I post it because it work Very Happy
Code react if "WIFI GOT IP" string is detected.
I use Xmega because voltage levels but any micro with enough SRAM should work Wink
Also I test this for someone else and it took me some time to figure out every important "https /NOT htttp, spaces etc."

It will be nice someday if we just can type " my interest" in search engine and solution will pop up Very Happy

You can look how this code work on YT (short video but live update on Thingspeak) https://www.youtube.com/watch?v=AAJZ240QjdE
I`m not string parser master so anyone can wrote own code. This is working example.

Code:
'****************************************************************
'*  UPDATING THINGSPEAK FIELD WITH ESP8266 AND XMEGA            *
'*       ESP8266 AT COMMANDS V.051 SDK 1.5.0                    *
'*            BARTek niveasoft(at)tlen.pl                       *
'****************************************************************

$regfile = "xm128a3udef.dat"
$crystal = 32000000                                         '32MHz
$hwstack = 80                                               'this is for my other tasks so
$swstack = 100                                              'you can modify values to your needs
$framesize = 100

 Config Submode = New

  Const Api_key = "ABCDEFGHIJKLMNOP"                        '' <<<<< YOUR API WRITE KEY

  Const Debuging = 1

' ***************** SYSTEM CLOCK CONFIG **************************
'                INTERNAL 32MHz NO PRESCALE
'
Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
'
' ENABLING AUTOMATIC OSCILLATOR CALIBRATION
    Osc_dfllctrl.0 = 1
     Dfllrc32m_ctrl.0 = 1
'
'*****************************************************************


'************ CONFIGURATION FOR COM1 ON PORTC ********************
'                   ESP8266 ON COM1
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8       'Tx-PC3  Rx-PC2
 Open "COM1:" For Binary As #1
  Config Serialin0 = Buffered , Size = 100                  'for COM1
   Config Input1 = Crlf , Echo = Crlf
'
 Const Max_str_len = 150 : Dim Com1_str As String * Max_str_len
'*****************************************************************

#if Debuging = 1
'*********** CONFIGURATION FOR COM2 ON PORTC *********************
'                COM2 IS FOR DEBUG SO SEPARATE CONFIG
Config Com2 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8       'Tx-PC7  Rx-PC6
 Open "COM2:" For Binary As #2
  Config Serialin1 = Buffered , Size = 50                   'for COM2
   Config Input2 = Cr , Echo = Cr
'
    Dim Com2_str As String * 50
'*****************************************************************
#endif

Dim Char As Byte , Mark As Byte , Count As Byte
Dim Cmd As String * 100 , Cmd_len As String * 15
Dim Field1 As String * 5 , Lenght As Byte

Dim State As Byte , Old_state As Byte , Mem_state As Byte

 Const Startup = 0
 Const Waiting = 1
 Const Send_len = 2
 Const Sending = 3


Led Alias Portf.0 : Config Led = Output

 '*** SUB FOR USART HANDLING ***
 Sub Get_reply

    Com1_str = ""
     Count = 0

       Do
         Char = Inkey(#1)

         If Char > 0 Then
          Select Case Char
           Case 13 : If Com1_str <> "" Then Exit Do
           Case 10 : If Com1_str <> "" Then Exit Do
           Case Else
             Com1_str = Com1_str + Chr(char)
          End Select

          Incr Count
          If Count >= Max_str_len Then
           Com1_str = ""
            Count = 0
          End If

         End If

       Loop

      If Com1_str <> "" Then Mark = 1

 End Sub

 '*** SUB FOR DEBUG ON COM2 ***
 Sub Show_state
  Local St_str As String * 10

    Select Case State
     Case 0 : St_str = "Startup"
     Case 1 : St_str = "Waiting"
     Case 2 : St_str = "Send_len"
     Case 3 : St_str = "Sending"
    End Select

   Old_state = State
       Print #2 , "Now State=" ; St_str
End Sub

 '***    SUB FOR ESP8266    ***
 Sub Esp8266

    Select Case State

      Case Startup

       If Mark = 1 Then
           Mark = 0

           Select Case Com1_str

            Case "WIFI GOT IP"

                      'AT+CIPSTART="TCP","184.106.153.149",80
                Cmd = "AT+CIPSTART={034}TCP{034},{034}184.106.153.149{034},80"
                 Print #1 , Cmd

                Field1 = Str(rnd(100))                      ' <<<<<<  random value for Testing

                #if Debuging = 1
                  Print #2 , "Field value=" ; Field1
                #endif

                Cmd = "GET https://api.thingspeak.com/update?api_key="
                Cmd = Cmd + Api_key
                Cmd = Cmd + "&field1="
                Cmd = Cmd + Field1
                Cmd = Cmd + "{010}{010}"

                 Mem_state = State                          'memorize that we starting
                  State = Waiting

            Case Else

              #if Debuging = 1
               Print #2 , Com1_str
              #endif

           End Select

       End If

      Case Waiting                                          ' if we waiting for answear

       If Mark = 1 Then
           Mark = 0

            Select Case Com1_str

             'Case "CONNECT"

             Case "OK"                                      ' if we see OK

              Select Case Mem_state

                Case Startup                                'previous was Startup so this is first "OK"
                 Mem_state = Send_len

                   Lenght = Len(cmd) : Cmd_len = Str(lenght)
                   Cmd_len = "AT+CIPSEND=" + Cmd_len
                    Print #1 , Cmd_len

                Case Send_len                               'second "OK" so we send Len of data we want send

                  Print #1 , Cmd

                   State = Sending
                    Mem_state = Sending                     'memorize that we send data to server
                                                            '    ( for Error handling )
              End Select

               #if Debuging = 1
                Print #2 , Com1_str
               #endif

             Case "ERROR"

               #if Debuging = 1
                Print #2 , Com1_str
               #endif
                 State = Startup

             Case "CLOSED"

               #if Debuging = 1
                Print #2 , Com1_str
               #endif
                 State = Startup

             Case Else

               #if Debuging = 1
                Print #2 , Com1_str
               #endif

            End Select

       End If

      Case Sending

       If Mark = 1 Then
           Mark = 0

           Select Case Com1_str

             Case "SEND OK"

              State = Startup

           End Select

         #if Debuging = 1
          Print #2 , Com1_str
         #endif
       End If

    End Select

 End Sub


  Enable Interrupts

' *** MAIN LOOP START HERE ****

Do

  Call Esp8266

  #if Debuging = 1
   If Old_state <> State Then Call Show_state
  #endif

  If Ischarwaiting(#1) > 0 Then Call Get_reply

Loop
End

 


Have a nice day.
Back to top
View user's profile Visit poster's website
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Fri Jan 15, 2016 6:29 pm    Post subject: Esp8266 Reply with quote

Thanks for sharing. Very nice example.

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6198
Location: Holland

blank.gif
PostPosted: Mon Jan 18, 2016 2:00 pm    Post subject: Reply with quote

thank you for sharing your code. Yes these are nice modules. it is a pity that you can not query the mac address. further they seem to reset quick when you send wrong info. but when sticking to the convention these are great wifi modules. xmega is a good choice. it can also be that newer versions have fixed firmware but i do not want to brick the modules i have Very Happy
_________________
Mark
Back to top
View user's profile Visit poster's website
Vlado

Bascom Member



Joined: 14 Oct 2005
Posts: 5

slovenia.gif
PostPosted: Mon Feb 08, 2016 6:17 pm    Post subject: Reply with quote

Thank you for sharing your code.

Here are AT commands needed to send data to Thiengspeak with GSM module SIM900.
AT commands were tested with terminal.

Quote:
RDY

+CFUN: 1

+CPIN: READY

Call Ready ' IF BAUD RATE IS FIXED GSM AUTOMATIC ANSWER WITH +CFUN: 1, +CPIN: READY (OR ERROR, THEN YOU MUST PUT PIN CODE), AND +CALL: READY

AT+CSQ ' GET SIGNAL LEVEL

+CSQ: 19,0
OK

AT+SAPBR=3,1,"CONTYPE","GPRS" 'CONNECT TO GPRS
OK

AT+SAPBR=3,1,"APN","internet" ' (FOR USER NAME & PSW USE DATA OF YOUR PROVIDER)
OK

AT+SAPBR=1,1
OK

AT+HTTPINIT
OK

at+sapbr=2,1 'Get GSM local IP
+SAPBR: 1,1,"XXX.XXX.XXX.XXX" 'gsm IP ADDRES GPRS OK. IF ANSWER IS +SAPBR: 1,3,"0.0.0.0" GPRS SI NOT CONNECTED
OK

AT+HTTPPARA="CID",1
OK

AT+HTTPSSL=1 'Enable the SSL function (YOU MUST ENABLE SSL ELSE ANSWER FROM SERVER will be 0,603,0)
OK

AT+HTTPPARA="URL","https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxxx&field1=18&field2=32&field3=33"+CRLF 'SEND URL XXXXX YOUR API KEY
OK

AT+HTTPACTION=0 'GET ANSWER FROM SERVER
OK
'mind: TIME TO RECIVE ANSWER is between 1 and 5 seconds

+HTTPACTION:0,200,3 ' ANSWER OK, SEND BACK 3 BYTE
AT+HTTPREAD ' READ ANSWER FROM SERVER

+HTTPREAD:3 'ANSWER IS 3 BYTE LONG
120 ' RECIVED DATA FROM SERVER
OK

AT+HTTPTERM 'TERMINATE HTTP SERVICE
OK


AT+HTTPINIT 'INITIALISE HTTP SERVICE
OK

AT+HTTPPARA="URL","https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXX&field1=18&field2=32&field3=33" +CRLF 'SEND URL XXXXX YOUR API KEY
OK

AT+HTTPACTION=0
OK
'Mind: TIME TO RECEIVE ANSWER (1 - 5 sec)
+HTTPACTION:0,200,3
AT+HTTPREAD

+HTTPREAD:3
121
OK

AT+HTTPTERM 'TERMINATE HTTP SERVICE
OK

AT+SAPBR=0,1 'DISABLE GPRS
OK


Back to top
View user's profile
nickb

Bascom Member



Joined: 15 Aug 2014
Posts: 1
Location: Perth

australia.gif
PostPosted: Sun Jun 12, 2016 4:24 pm    Post subject: Reply with quote

I purchased another ESP8266 module early this year and it behaved completely different to previous one Shocked
The new one had software 0.25.0.0 and the old 0.17.09.01.

AT+GMR
AT version:0.25.0.0(Jun 5 2015 16:27:16)
SDK version:1.1.1
Ai-Thinker Technology Co. Ltd.


If you type AT+CIFSR you get a list of IP's and MACs for acess point and client if you have both enabled.

+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:e6:2d:20"
+CIFSR:STAIP,"0.0.0.0"
+CIFSR:STAMAC,"18:fe:34:e6:2d:20"

On the up side 0.25.0.0 seems to be more stable then the 0.17.09.01 one...

Nick.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum