Debug On
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 256
$swstack = 128
$framesize = 256
Config Submode = New
'--- UART 0 USB Arduino (debug)
Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
'Serialin0
'--- UART 1 ESP8266
Config Com2 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "COM2:" For Binary As #2 : Const Esp = 2
'Config Serialin1 = Buffered , Size = 250 ', Bytematch = 10
Config Input2 = Crlf , Echo = Crlf
Echo Off
'--- UART 2 PC
Config Com3 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "COM3:" For Binary As #3 : Const Pc = 3
Config Serialin2 = Buffered , Size = 250 , Bytematch = 13
Config Input3 = Cr , Echo = Cr
Echo Off
'--- TIMER
Config Timer2 = Timer , Prescale = 1024 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
Compare2a = 155 '10ms @16MHz/1024
'--- LED
Test_led_1 Alias Porte.5 : Config Test_led_1 = Output 'pin 3
Test_led_2 Alias Portg.5 : Config Test_led_2 = Output 'pin 4
Led_mega Alias Portb.7 : Config Led_mega = Output 'pin 13
'--- czas
Dim 100ms As Byte , 1s As Byte , 1m As Byte
'--- LED
Dim Led As Byte , Led_ee As Eram Byte
'--- PC
Dim Com3_str As String * 250
Dim Pc_nadaje As Byte
Dim Bufor As Byte
Dim Pc_dane(4) As String * 10
'--- ESP8266
Dim Com2_str As String * 250
Dim Esp_nadaje As Byte
Dim Send_time As Byte
Dim Cmd As String * 250 , Cmd_len As String * 250
Dim Lenght As Byte
Dim State As Byte , Mem_state As Byte
Const Startup = 0
Const Waiting = 1
Const Send_len = 2
Const Sending = 3
'--- obsługa ESP8266
Sub Esp_uart
Select Case State
Case Startup
If Instr(com2_str , "WIFI GOT IP") > 0 Then
Cmd = "AT+CIPSTART={034}TCP{034},{034}192.168.1.2{034},80"
Print #esp , Cmd
Cmd = "GET /receiver.php?key=1234&id=1;11;2;22;3;33"
Cmd = Cmd + "&type=water;volt;gas;volt;elec;volt"
Cmd = Cmd + "&value=0.001;1.11;0.01;2.22;0.0005;3.33"
Cmd = Cmd + "¤t=11;---;22;---;33;---"
Cmd = Cmd + "&device=ip&name=CountNNT_"
Cmd = Cmd + "{032}HTTP/1.1{013}{010}Host:{032}192.168.1.2{013}{010}{013}{010}"
Mem_state = State 'zapamiętaj, że zaczynamy
State = Waiting
'Else
'Debug Com2_str
End If
Case Waiting 'jeśli czekamy na odpowiedź
Select Case Com2_str
Case "OK" 'jeśli zobaczymy OK
Select Case Mem_state
Case Startup 'Poprzedni był auto-start, więc to pierwszy "OK"
Mem_state = Send_len
Lenght = Len(cmd) : Cmd_len = Str(lenght)
Cmd_len = "AT+CIPSEND=" + Cmd_len
Print #esp , Cmd_len
'Debug Cmd_len
Case Send_len 'drugie „OK”, więc wysyłamy Len danych, które chcemy wysłać
Print #esp , Cmd
'Debug Cmd
State = Sending
Mem_state = Sending 'zapamiętaj, że wysyłamy dane do serwera
'do obsługi błędów
End Select
Case "ERROR" 'błąd połączenia
State = Startup : Send_time = 0
Case "CLOSED" 'połączenie zamknięte
State = Startup : Send_time = 0
End Select
Case Sending 'wysyłka danych
Select Case Com2_str
Case "SEND OK" 'wysyłanie powiodło się
State = Startup : Send_time = 0
Case "CLOSED" 'połączenie zamknięte
State = Startup : Send_time = 0
Case "SEND FAIL" 'wysyłanie nie powiodło się
State = Startup : Send_time = 0
End Select
End Select
End Sub
'--- obsługa PC
Sub Pc_uart
Bufor = Split(com3_str , Pc_dane(1) , " ")
Select Case Pc_dane(1)
Case "?"
Print #pc , ":)"
Case Else 'jeżeli odebrany znak to żaden z powyższych to wyczyść bufor
Print #pc , ""
Print #pc , "ehhh... cos poszlo nie tak, sprobuj jeszcze raz..."
'Com3_str = ""
Pc_dane(1) = ""
Pc_dane(2) = ""
Pc_dane(3) = ""
Pc_dane(4) = ""
End Select
'wyczyść bufor i wyjdź
'Com3_str = ""
Pc_dane(1) = ""
Pc_dane(2) = ""
Pc_dane(3) = ""
Pc_dane(4) = ""
End Sub
'--- włącz globalne przerwania
Enable Interrupts
'--- powitanie
Debug "START UART 0 debug"
Print #pc , "START UART 2 PC"
'**********************
'*** PROGRAM GŁÓWNY ***
Do
If Tifr2.ocf2a = 1 Then 'minęło 10ms - flagę ustawia Timer
Tifr2.ocf2a = 1 'kasuj flagę
If 100ms < 9 Then
Incr 100ms
Else 'minęło 100ms
100ms = 0
If 1s < 9 Then
Incr 1s
Else 'minęła 1s
1s = 0
Toggle Led_mega
If 1m < 59 Then
Incr 1m
Else 'minęła 1m
1m = 0
Debug "Start ESP"
Print #esp , "AT+RST" 'budź ESP i ślij dane na serwer
End If
End If
End If
End If
If Ischarwaiting(#esp) > 0 Then
Input #esp , Com2_str
Call Esp_uart
Com2_str = ""
End If
' If Esp_nadaje = 1 Then
' Esp_nadaje = 0
' Call Esp_uart
' End If
If Pc_nadaje = 1 Then
Pc_nadaje = 0
Call Pc_uart
Clear Serialin2
End If
Loop
Close #esp
Close #pc
End
'*** KONIEC GŁÓWNEJ PĘTLI ***
'****************************
'--- przerwanie UART ESP
'Serial1charmatch:
' $asm
' !PUSH R6
' !PUSH R16
' !PUSH R17
' !PUSH R18
' !PUSH R19
' !PUSH R22
' !PUSH R24
' !PUSH R25
' !PUSH R26
' !PUSH R27
' !PUSH R30
' !IN R24, SREG
' !PUSH R24
' $end Asm
' $timeout = 3000
' Input #esp , Com2_str
' Clear Serialin1
' Esp_nadaje = 1
' Tuned with NoSave Tool 1.10
' $asm
' !POP R24
' !OUT SREG, R24
' !POP R30
' !POP R27
' !POP R26
' !POP R25
' !POP R24
' !POP R22
' !POP R19
' !POP R18
' !POP R17
' !POP R16
' !POP R6
' $end Asm
'Return
'--- przerwanie UART PC
Serial2charmatch:
$asm
!PUSH R6
!PUSH R16
!PUSH R17
!PUSH R18
!PUSH R19
!PUSH R22
!PUSH R24
!PUSH R25
!PUSH R26
!PUSH R27
!PUSH R30
!IN R24, SREG
!PUSH R24
$end Asm
$timeout = 3000
Input #pc , Com3_str
'Clear Serialin2
Pc_nadaje = 1
' Tuned with NoSave Tool 1.10
$asm
!POP R24
!OUT SREG, R24
!POP R30
!POP R27
!POP R26
!POP R25
!POP R24
!POP R22
!POP R19
!POP R18
!POP R17
!POP R16
!POP R6
$end Asm
Return