ThingSpeak
ThingSpeak.com ist ein kostenloser Dienst/Plattform, der es ermöglicht, Daten von IoT mit einfachen Mittel darzustellen und zu teilen.
Mein erster Versuch ist eine klassische Temperaturmessung via TMP100 und anschliessender Darstellung in ThingSpeak.com
Im Beispiel unten muss natürlich noch SSID, PW sowie ThingSpeak-Channel angepasst werden.
' ------------------------------------------------------------------------------
' µC misst über TMP100 Temperatur und stellt diese dem Webserver bereit
'
' HW:tr045-R06
' - ESP8266 am seriellen Port
' - zweiter serieller Port für Debug
'
' V0.4: deutlich verlängerte Wartezeit, scheint zu laufen
' aktuelle FW auf Basis esp_iot_sdk_v0.9.5
'
' Info: mehrdimensionale Daten nach dem Verfahren "“&field2=xx&field3=yy" senden
' ------------------------------------------------------------------------------
$Regfile="m8def.dat"
$Crystal=8000000
$hwstack = 40
$swstack = 40
$framesize = 60
$baud=9600
$lib "i2c_twi.lib"
'OSCCAL=173 '0xAD bei 5V
OSCCAL=182 ' bei 3V
' ------------------ Konstanten
const Adr_TMP101w = 144
' ------------------ Subroutinen deklarieren
declare Function Temp_tmp100(byval Adresse As Byte) As Single
declare Sub Init_tmp100(byval Adresse As Byte)
declare sub Warte_OK_Error(byval Suchstring as string*10)
' ------------------ Deklarationen
' Variablen
dim i as byte, j as byte
dim Lbyte as byte, Hbyte as byte,Big_Byte as integer at lbyte overlay
dim Temperature as single
dim Empfangs_Ticker as word
dim Sendestring as string * 250, headerstring as string * 100
dim strdummy as string *10
dim Flagbyte as byte
Flag_ok alias Flagbyte.0
Flag_Error alias Flagbyte.1
Dim Ereigniszaehler as word
' UART ISR
const Uart_string_laenge=100'45 ' uart_string war 15 lang (20.7.14)
Dim Uart_get_char As Byte , Uart_string_len As Byte , Uart_char As String * 1 , Uart_string As String * Uart_string_laenge ', Str_laenge As Byte
' Timer
Config Timer0 = Timer , Prescale = 64
Const Timer0_preload = 131' Timer1 tickt im 1ms Takt (siehe Const Timer1_preload Wert)
On Timer0 Timer0_isr
'I2C
Config Scl = Portc.5
Config Sda = Portc.4
' weiterer UART
Open "Comd.4:9600,8,n,1" For Output As #1
Open "Comd.5:9600,8,n,1" For Input As #2
' Ports/LED
Led1 Alias Portd.3 : Config Led1 = Output : Set Led1
wait 1:reset led1
' Interrupts
On Urxc Uart_isr
' ------------------ Hauptschleife
Enable Urxc
enable interrupts
enable timer0
' ESP8266 initialisieren
wait 3
print"AT+RST"
call Warte_OK_Error("")
wait 3
print "AT+CIPMUX=1"
call Warte_OK_Error("")
print "AT+CIPSERVER=1,8080"
call Warte_OK_Error("")
print "AT+CIPSTO=0"
call Warte_OK_Error("")
print "AT+CWJAP={034}<SSID>{034},{034}<PW>{034}"
call Warte_OK_Error("")
Uart_string=""
print "AT+CIFSR"
call Warte_OK_Error("")
Empfangs_Ticker=0
wait 5
do
if Empfangs_Ticker> 1000 then
Empfangs_Ticker=0 'messe 5 Sekunden aus
Uart_string="" ' und Empfangsvariable löschen
end if
incr Ereigniszaehler
' lese Temp vom Sensor
Temperature = Temp_tmp100(Adr_TMP101w)
strdummy = fusing(temperature,"#.#")
Sendestring = "AT+CIPSTART=4," + Chr(34) + "TCP" + Chr(34) + "," + Chr(34) + "184.106.153.149" + Chr(34) + ",80" '+ chr(13) + chr(10)
print Sendestring
wait 2
call Warte_OK_Error ("")
'wait 3
' ###
headerstring = "GET /update?key=<TS Channel>&field1=" +strdummy +chr(10) '+ chr(13) + chr(10) ' kein crlf NUR LF in V0.4.1,
i=Len(headerstring)
Sendestring = "AT+CIPSEND=4," + str(i) '+ chr(13) + chr(10)
print sendestring
call Warte_OK_Error(">")
print headerstring; ' Semikolon ist wichtig damit nicht noch ein Zeilenumbruch gesendet wird!!
call Warte_OK_Error("")
wait 60
loop
end
' ------------------ Subroutinen -----------------------------------------------
Function Temp_tmp100(byval Adresse As Byte) As Single ' Vollständige Umrechnung mit Zweierkomplement
Local Tmp100_w As Byte , Tmp100_r As Byte , Sign as byte
Tmp100_w = Adresse
Tmp100_r = Tmp100_w + 1
I2cstart 'repeated start
I2cwbyte Tmp100_r 'slave address (read)
I2crbyte Hbyte , Ack 'read byte
I2crbyte Lbyte , Ack
I2cstop
Sign=hbyte 'Vorzeichen sichern
hbyte.7=0 ' Vorzeichen löschen
shift big_byte,right,4
Temp_tmp100 = big_byte * 0.0625
if sign.7=1 then
temp_tmp100=Temp_tmp100-128
end if
End Function
Sub Init_tmp100(byval Adresse As Byte)
Local Tmp100_w As Byte
Tmp100_w = Adresse
' Temperatursensor initialisieren
I2cstart
I2cwbyte Tmp100_w 'Addressw 'slave adsress
I2cwbyte 1
I2cwbyte &H20 'vorher &H60
I2cstop
I2cstart 'generate start
I2cwbyte Tmp100_w 'Addressw 'slave adsress
I2cwbyte 0
I2cstop
End Sub
Sub Warte_OK_Error(byval Suchstring as string*10)
if Suchstring="" then
' warte bis OK oder ERROR empfangen wird
while instr(Uart_string,"OK")=0 AND instr(uart_string,"ERROR")=0
if Empfangs_Ticker >20000 then
exit while 'Notausgang
end if
wend
else
while instr(Uart_string,Suchstring)=0
if Empfangs_Ticker >20000 then
exit while 'Notausgang
end if
wend
end if
if instr(Uart_string,"busy s..")>0 then
set led1
goto 0 ' Notfall RESET
end if
if instr(Uart_string,"ERROR")>0 then
toggle led1
end if
Uart_string=""
Flag_error=0
Flag_ok=0
Empfangs_Ticker=0
end sub
' ISR Routinene -------------------------------------------------------------
Uart_isr:
Uart_get_char = Udr
Uart_char = Chr(uart_get_char)
Uart_string =Uart_string + Uart_char
' warte auf Schlüsselwörter
Empfangs_Ticker=0 ' Wartewatchdog zurücksetzten, da Daten reinkamen
if instr(Uart_string,"OK")>0 then
Flag_ok=1
elseif instr(uart_string,"ERROR")>0 then
Flag_Error=1
else
Flag_ok=0
Flag_Error=0
end if
Return
' Diese Routine wird jede mSekunde aufgerufen wenn Timer1 aktiviert ist
Timer0_isr:
' Timer1 wieder neu vorladen
Timer0 = Timer0_preload
incr Empfangs_Ticker
return
Das Ergebnis kann unter dem folgenden Link betrachtet werden: https://thingspeak.com/channels/23605
Links
http://www.instructables.com/id/ESP8266-Wifi-Temperature-Logger/