mc-Demo205 > mc-Demo205
how I can to write and read data in the flash memory?
gpg117:
Hello,
I want to know how to write and read data in the flash memory of the McDemo205
mc-T2:
Hello,
There is a log function that is not yet described in our documentation. In an upcoming release, we will be releasing this information within the new documentation for mcStudio and the mcDemo205. That said, we do have some code that you can modify for your use to log information into the flash of the device. The below code is for an example using our partner, Losant, so you may want to modify this for your needs. The code is meant to log GPS coordinates and temperature and then, when in range of a gateway, you would press SW1 and the information would then be transferred to the Losant cloud to populate onto a map onto a dashboard.
here is the code and there are also some additional notes below:
--- Code: ---Class gpsTracker
Shared Locations As ListOfObject
Const GPS_TIMEOUT_uS As Integer = 120000000 '120s
'Const GPS_TIMEOUT_uS As Integer = 1000000 '1s
Const GPS_MIN_SATS As Integer = 3 '3 sattelites minimum
Const LosantDeviceID As String = "YOUR LOSANT DEVICE ID"
Const LosantTopic As String = "losant/" + LosantDeviceID + "/state"
Shared Event Boot()
Locations = New ListOfObject()
End Event
Shared Event StartLocationAcquisition() RaiseEvent Every 7 Minutes
Led2 = True
Thread.Sleep(100000)
Led2 = False
Device.StartGPS(GPS_TIMEOUT_uS, GPS_MIN_SATS)
End Event
Shared Event LocationDelivery()
Dim lat As Float = Device.GetLatitude()
Dim lon As Float = Device.GetLongitude()
Dim tmp As Float = TempSensor.GetTemp()
If lat.IsNaN() Or lon.IsNaN() Then
Else
Dim entry As ListOfByte = New ListOfByte()
entry.AddFloat(tmp)
entry.AddFloat(lat)
entry.AddFloat(lon)
Log.Write(0, entry)
End If
End Event
Shared Event SW1FallingEdge()
Led3 = True
Thread.Sleep(100000)
Led3 = False
Dim count As Integer = 0
Dim offset As Integer = 0
Dim lo As ListOfObject
lo = Log.Read(offset)
While lo <> Nothing
Dim o As Object
o = lo(0)
offset = o.Cast(Integer)
o = lo(1)
Dim dtm As DateTime = o.Cast(DateTime)
o = lo(2)
Dim type As Integer = o.Cast(Integer)
o = lo(3)
Dim entry As ListOfByte = o.Cast(ListOfByte)
Dim tmp As Float = entry.ExtractFloat(0)
Dim lat As Float = entry.ExtractFloat(4)
Dim lon As Float = entry.ExtractFloat(8)
Dim jDate As Json = New Json()
jDate.Add("$date", dtm)
Dim jData As Json = New Json()
jData.Add("temperature", tmp)
Dim locStr As String = lat.ToString() + "," + lon.ToString()
jData.Add("location", locStr)
Dim jPayload As Json = New Json()
jPayload.Add("time", jDate)
jPayload.Add("data", jData)
While Not Lplan.Publish2(LosantTopic, jPayload.ToListOfByte)
Thread.Sleep(10000)
End While
count += 1
If count = 30 Then
Thread.Sleep(30000000)
count = 0
End If
lo = Log.Read(offset)
End While
End Event
End Class
--- End code ---
Additional notes -
1) you'll also want to add the temperature sensor library and the below GPS library if you want to use the code above with Losant:
--- Code: ---Class GpsLocation
Public Latitude As Float
Public Longitude As Float
Public Timestamp As DateTime
Public Sub New(lat As Float, lon As Float, dtm As DateTime)
Latitude = lat
Longitude = lon
Timestamp = dtm
End Sub
End Class
--- End code ---
2) I believe the flash will keep storing until you have enough data to start overwriting. If you do want to clear the flash, you can include or run the below function on a new script
--- Code: ---Log.Erase()
--- End code ---
3) if you are interested in working with the above code for a test, keep in mind that the device needs to be powered and in-range of an mcGateway for a suggested 30 minutes to ensure that the device grabs the time from the gateway. This is so that you can associated a time with the GPS coordinates
4) And, if you are going to use the above code and leave it unmodified to work with Losant, you'll notice at the end of the code that we have a time delay. This is due to how the Losant platform works which can only consumer a certain amount of data every 30 seconds. The time delay allows the device to send out a batch of data then wait and keep sending to ensure that Losant receives all the messages.
Hope that helps!
gpg117:
Thanks for your answer, it was very useful. I modified the code a little and I use SW1 and SW2 for the gps coordinates and to transfer the information, respectively, but losant doesn't receive the information.
mc-T2:
Hello,
1) Have you setup Losant properly? By that I mean, have you setup the device properly within Losant and entered the correct device ID into your code for the device?
2) Have you configured your gateway properly to direct the information to your account within Losant?
3) You do need to ensure that you are in range of the gateway so that you can send the information out
4) When you first power the device, you need to ensure that it is in range of the mcGateway for a period of time so that it can capture the current time from the gateway. If you don't do this, the device will still try to capture GPS data however it will not know the time and hence, the data will not be relevant as there will be no time stamp along with it
To help, check out the below walk-through for setting up Losant:
https://www.losant.com/blog/getting-started-with-mc-things-and-losant
You can also check out this video that describes the configuration and setup within Losant. The example in this video does not use the above code but should give you a good idea of how to setup and configure your equipment and their cloud to receive data:
https://youtu.be/PVyjyIwcuUc
Hope that helps, let us know how your progress
Thanks
gpg117:
Hello,
I´m working with another guy on this and we have a problem with the line "lo = Log.Read(offset)". We implemented the temperature example proyect to send the information to Losant with the mcgate and it works. But when we debug the code that you passed us, debugging stops in the line of the instruction "Log.Read". We don`t know in what adress saves the information the line "Log.write(0,entry)" and if the problem is the adress(offset) to read the information.
We update the mcDemo with the firmware v0.9.615 and the release of the mcStudio is v0.9.534.0.
Thanks.
Navigation
[0] Message Index
[#] Next page
Go to full version