Re: Tips på billig WIFI-modul (ESP8266)
Postat: 2 mars 2015, 00:14:08
Hur ser uC-koden ut?
MVH: Mikael
MVH: Mikael
Svenskt forum för elektroniksnack.
https://elektronikforumet.com/forum/
Kod: Markera allt
'****************************************************************
'* Name : pH-sens_lua.BAS *
'* Author : Marcus Xxxxxxxxx *
'* Notice : Copyright (c) 2015 Marcus Xxxxxxxxxxx *
'* : All Rights Reserved *
'* Date : 2015-03-01 *
'* Version : 0.0.0.5 *
'* Notes : *
'* : *
'****************************************************************
'µP
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings
Device = 16F690
Config FOSC_INTRCIO, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOREN_OFF, IESO_OFF, FCMEN_OFF
;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Reminders = OFF
'µP speed
Xtal = 8
'Bootloader
Declare Bootloader = 0
'ALL_DIGITAL
Declare All_Digital = TRUE
' -----[ I/O Definitions ]-------------------------------------------------
Symbol LED = PORTC.2
Symbol IO1 = PORTC.6
Symbol LEDr = PORTA.4
Symbol LEDg = PORTA.5
'Hw RX/TX
Declare Hserial_Baud = 9600 ' Set baud rate to 9600
Declare Hserial_RCSTA = %10010000 ' Enable serial port and continuous receive
Declare Hserial_TXSTA = %00100000 ' Enable transmit and asynchronous mode
Declare Hserial_Clear = On ' Clear the buffer before receiving
'Sw RX/TX
Declare Rsin_Pin = PORTC.1
Declare Rsout_Pin = PORTC.0
Declare Serial_Baud = 9600
Declare Rsin_Mode = 0
Declare Rsout_Mode = 0
Declare Rsin_Timeout = 10000
' -----[ Constants ]-------------------------------------------------------
Dim CR As $0D ' Carriage Return
Dim LF As $0A ' Line Feed
Dim DQ As $22 ' Double Quote "
' -----[ Variables ]-------------------------------------------------------
Dim I2cData As Byte ' Data on i2c bus
Dim Loop As Byte ' Loopcounter
Dim SerData As Word ' Serial information
Dim PH[20] As Byte
Dim Cal As Byte
' -----[ Initialization ]--------------------------------------------------
OSCCON = %01110000 ' 8MHz
Input IO1
Low LEDg
Low LEDr
High LED
RSOut "E", CR
DelayMS 2000
RSOut "E", CR
DelayMS 1000
RSOut "15.00", CR
'DelayMS 60000
DelayMS 4000
GoSub resetWIFI
' -----[ Program Code ]----------------------------------------------------
Main:
Low LED
RSOut "R", CR
Loop = 0
While 1 = 1
I2cData = RSIn, {TimeOut}
If I2cData = CR Then Break
If Loop = 19 Then Break
PH[Loop] = I2cData
Inc Loop
Wend
PH[Loop] = 0
If PH[0] > "9" Or PH[0] < "0" Then GoTo TimeOut
DelayMS 2000
GoSub postDATAts
DelayMS 1000
Low LEDg
For Loop = 1 To 240
DelayMS 30000
If IO1 = 1 Then
High LEDr
GoSub UnButton
GoSub Calibration
EndIf
Next
GoTo Main
End
' -----[ Subroutines ]-----------------------------------------------------
Calibration:
DelayMS 2000
RSOut "C", CR
DelayMS 1000
DelayMS 2000
Repeat Until IO1 = 1
GoSub UnButton
DelayMS 60000
DelayMS 60000
High LEDg
RSOut "S", CR
DelayMS 1000
Repeat Until IO1 = 1
GoSub UnButton
Low LEDg
DelayMS 60000
DelayMS 60000
High LEDg
RSOut "F", CR
DelayMS 1000
Repeat Until IO1 = 1
GoSub UnButton
Low LEDg
DelayMS 60000
DelayMS 60000
High LEDg
RSOut "T", CR
DelayMS 1000
RSOut "E", CR
DelayMS 1000
Repeat Until IO1 = 1
GoSub UnButton
Low LEDr
Low LEDg
Return
resetWIFI:
HSerOut ["node.restart()", CR, LF]
HSerIn 15000, TimeOut, [Wait("Connected to WIFI!")]
High LEDg
DelayMS 200
Low LEDg
Return
postDATAts:
HSerOut ["token=", DQ, CStr API, DQ, CR, LF]
HSerIn 5000, TimeOut, [Wait("> ")]
HSerOut ["f1=1", CR, LF]
HSerIn 5000, TimeOut, [Wait("> ")]
Loop = 0
HSerOut ["t1="]
While 1 = 1
If PH[Loop] = 0 Then Break
HSerOut [PH[Loop]]
Inc Loop
Wend
HSerOut [CR, LF]
HSerIn 5000, TimeOut, [Wait("> ")]
HSerOut ["dofile(", DQ, "Thingspeak2.lua", DQ, ")", CR, LF]
HSerIn 15000, TimeOut, [Wait("Connected", CR, LF), Dec SerData]
If SerData = 0 Then
DelayMS 5000
GoSub resetWIFI
GoTo Main
EndIf
High LEDg
Return
TimeOut:
High LED
GoSub resetWIFI
DelayMS 2000
GoTo Main
UnButton:
Repeat Until IO1 = 0
DelayMS 300
Return
End
' -----[ CData ]-----------------------------------------------------------
API: CData "--token--", 0