Author Topic: how I can to write and read data in the flash memory?  (Read 97 times)

gpg117

  • Newbie
  • *
  • Posts: 3
    • View Profile
how I can to write and read data in the flash memory?
« on: August 01, 2017, 09:33:13 am »
Hello,

I want to know how to write and read data in the flash memory of the McDemo205

Social Buttons


mc-T2

  • Administrator
  • Full Member
  • *****
  • Posts: 239
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
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: [Select]
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

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: [Select]
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

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: [Select]
Log.Erase()
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!
Need more mc-Modules, mc-Gateways or other mc-Things? Check out our product page: www.mcthings.com/products. mc-Development kits are available too!
Check out a live Dashboard using mcThings and Losant! Click here: https://goo.gl/ST43hB

gpg117

  • Newbie
  • *
  • Posts: 3
    • View Profile
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

  • Administrator
  • Full Member
  • *****
  • Posts: 239
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
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
Need more mc-Modules, mc-Gateways or other mc-Things? Check out our product page: www.mcthings.com/products. mc-Development kits are available too!
Check out a live Dashboard using mcThings and Losant! Click here: https://goo.gl/ST43hB

gpg117

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hello,

Im 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.

mc-T2

  • Administrator
  • Full Member
  • *****
  • Posts: 239
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
Hello,

We will be documenting the Log functions in the next release - it will be updated within the documentation.

Glad that you have the temperature information going into Losant, this means that you have that side of things setup properly. And also good that you were able to update the device and mcStudio to the latest version.

I believe that you are using the GPS function and Log function:

if a GPS acquisition is ongoing, the log read has to wait for that to finish. Currently (this is changing when we do the warm start) both the logging and gps share a single SPI bus so they can't both happen at the same time.... This might lead to some less that elegant behaviours when debugging... for example if it takes too long for the GPS to finish mcStudio might stop and think the device is not responding...
The system will recover itself. So basically the execution of that line will be suspended until the gps acquisition is done.

Try loading the code directly into the device and see how everything works. It sounds like the issue you are having resides strictly within the debugging of the code and could be affected by what we mention above

Hope that helps,
Thanks
Need more mc-Modules, mc-Gateways or other mc-Things? Check out our product page: www.mcthings.com/products. mc-Development kits are available too!
Check out a live Dashboard using mcThings and Losant! Click here: https://goo.gl/ST43hB