Wireless Programming 101 (or learn from my mistakes :) - (windows)

Started by Reck_law, August 17, 2014, 05:01:48 PM

Reck_law

This page assumes that you already have python and the Arduino ide.

1a:    Download Felix's rfm12b or rfm69, SPIFlash, and WirelessHEX library
      https://github.com/LowPowerLab/RFM69
      https://github.com/LowPowerLab/RFM12B
      https://github.com/LowPowerLab/SPIFlash
      https://github.com/LowPowerLab/WirelessProgramming/tree/master/WirelessHEX
      https://github.com/LowPowerLab/WirelessProgramming/tree/master/WirelessHEX69

1b:   use Felix's example code for wirelessprogramming_gateway and wirelessprogramming_node to start.  Found in his examples directory

2:   Download Felix's python script wirelessprogramming.py
      https://github.com/LowPowerLab/WirelessProgramming

3a:   locate your hex file. I found this at
http://electronics.stackexchange.com/questions/16321/creating-a-sketch-hex-file-from-a-pde-file
I just tried a build and the .HEX file was created in a directory called build2849146248544311107.tmp inside my Temp directory (C:\Users\USERNAME\AppData\Local\Temp\ on Windows 7). Presumably the numbers in the build2849146248544311107.tmp directory name will change. 
Use file xxx.cpp.hex.  Double check with date time stamp

3b:   Copy your hex file to the same directory as wirelessprogramming.py. 

4:   Bring up a command prompt.  Cd to the directory with both the python script and your .cpp.hex file.
Type : where xx is the target moteino,  yyy.cpp.hex is your hex file and comzz is your serial port.
   
Python wirelessprogramming.py –t xx –f yyy.cpp.hex –s comzz
----

Mistakes that I made so you don't have to

Make sure that your gateway node is identified the same in both your gateway and your target.

You probably should put both the node id and code revision in your codes name (so you know what you should be targeting).

You might want to have some way in your code to identify that your code on the target is what
your want it to be (IE I wrote to the wrong moteino so I had two with the same address oops)

please correct any mistakes I have made or any hints that you have to ease the process or if you have done it on another OS.

Antony

And in case it helps others, I'll add what helped me get wireless programming going flawlessly on Linux and OS X:
- upgrade to Felix's latest (~Sept 2014) version of RFM69, or equivalent library for your particular moteino;
- look through your gateway's and nodes' codes and replace the deprecated radio.ACK_REQUESTED variable with radio.ACKRequested() method. (My handshake didn't work until both my node and gateway code were updated.)
- ensure you have CheckForWirelessHEX(radio, flash, true) within radio.receiveDone() in your node's loop() method. Without it your node is unable to listen out for the wireless programming handshake from the gateway.

hallonstedt

Thank you for the radio.ACKrequested() tip. The FLX? handshake errors were driving me nuts.

For those of you looking for that hex-file; In the Arduino IDE, open File / Preferences. Check the "Show verbose output during: compilation." - Now you will see the hex-file name on the last row in the compiler output. It can be copied and pasted to the python program.

Now that I successfully uploaded a sketch wirelessly (although WirelessProgramming.py tells me IMG was refused by target) I find myself still somewhat confused; How do I force the Moteino back to "node mode" so that I can make another change to the actual sketch running on it and overwrite the old one?

It can't be that I need to upload the node using serial only to upload the sketch I want using wireless so I must be missing something. How does this work with dual boot?

Felix

Did you read Antony's points?

Quote- ensure you have CheckForWirelessHEX(radio, flash, true) within radio.receiveDone() in your node's loop() method. Without it your node is unable to listen out for the wireless programming handshake from the gateway.

You can upload ANY sketch via serial. But to upload as sketch wirelessly: your previously uploaded sketch (either first time via serial or Nth time wirelessly) will need to have the CheckForWirelessHEX() call as mentioned above.

hallonstedt

Quote from: Felix on October 01, 2014, 09:08:07 PM
Did you read Antony's points?

Thanks Felix. I did read it and I apologize for asking questions that may seem obvious to most on this forum. I have very limited programming experience and used your example node sketch that at first glance looked significantly more complex than just this function.

You'd be surprised how much functionality can be programmed using nothing but other people's code and Googled syntax. It does leave some gaps here and there though.  :D

Ric

Thanks for the breakdown Reck_Law! I performed all the steps but unfortunately still can't run the Python script. I renamed the file WirelessProgramming.py to just WP.py and I'm getting this error:

Traceback (most recent call last):
  File"WP.py", line 18, in (module)
    import tyme, sys, serial
ImportError: No module named serial


This happens even when I run "Python WP.py ?"

I installed Python 2.7.3 and got the same message. I made sure that I got the latest WirelessProgramming.py file from Felix's GitHub and installed the newest libraries as well.

Any ideas where I"m failing? I really want to install my SwitchMote on the wall but I don't want to do it until I am able to test this correctly.

Thanks,
Ric

Felix



K1JOS

I want to add this capability to my project.  After browsing the examples and the Python script, it seems the key aspect is make sure to have in my node Loop() the line: "CheckForWirelessHEX(radio, flash, true)"  How much delay does this command introduce into the Loop? 

My remote MegaMoteino already has some heavy time sensitive functions in my Loop() and I am afraid that adding one thing more into the loop such as the "CheckForWirelessHEX(radio, flash, true)" may cause problems.  Right now I send commands from my Gateway to the remote node and the remote manages each command through a list of subroutines :

  • listening for command packets from the Gateway
  • quickly acting on command packets - turning on/off motor relays via RS485 or requesting magnetic compass headings via I2C
  • processing compass data to correct for azimuth errors
  • sending compass data and other remote status data back to Gateway

And I still want to add a temp sensor for frequency correction !!

Since I use a command structure to select various functions for the node Moteino, could i instead have a command that would then call a subroutine like:

void FlashCommand_Rcvd()
{
  if (radio.receiveDone())
  {
    CheckForWirelessHEX(radio, flash, true);
  }
    
  // when done reset
   resetUsingWatchdog(true);
}


I need my Gateway Moteino therefore to use my project's specific data packet structure and specific command code to send to the node to activate the Flash subroutine.  I don't know Python and I am just getting comfortable as a advanced beginner in Arduino C.   Is there any way using the Arduino IDE and/or other libraries to add code to my Gateway program to allow me to open the a HEX file from my PC and send it byte by byte to the node? 

Jerry


Felix

CheckForWirelessHEX needs to be called after receiveDone() and before you clear the radio.DATA buffer or send anything (which will also clear the buffer).
The function simply checks if the last received message has the wireless programming "signature" handshake. If it does, it will start the wireless programming sequence and restart the MCU via watchdog so you don't need to include anything else.
If the signature is not detected it will exit and your code will continue.
In your sample code you should NOT include the resetUsingWatchdog(). That will execute regardless if CheckForWirelessHEX() runs or not.
It does not add significant delay, except obviously when the wireless programming starts to run, that will interrupt any normal code that was running.
The code is in here, quite easy to follow: https://github.com/LowPowerLab/WirelessProgramming/tree/master/WirelessHEX69

K1JOS

Thanks Felix, So it sounds real easy on the node side to implement this.  I will pop it into my current node Loop() after radioreceive and see if it causes any functional delays. 
jerry

greg

Learn from my mistakes too... make sure you have python 2 not 3.
if you send a sketch successfully, just hangs after it receives it, make sure
you add moteino and mega support into arduino ide:
https://github.com/LowPowerLab/Moteino/tree/master/MEGA/Core
compile with correct board and this stuff really works slick.

ivars211

Another advice - when you are trying to integrate wireless programming in existing software - check that EncryptKey is the same as yours, because I spent an hour trying to figure out, why it doesn't work and the problem was the inconsistency in the keys - in the node-getway the KEY is "thisIsEncryptKey", but in wireless programming example: "sampleEncryptKey".
It would really help if all of the examples would be consistent, you know :d

jarrods

Quote from: ivars211 on May 03, 2015, 01:35:25 PM
Another advice - when you are trying to integrate wireless programming in existing software - check that EncryptKey is the same as yours, because I spent an hour trying to figure out, why it doesn't work and the problem was the inconsistency in the keys - in the node-ge[vimeo][/vimeo]tway the KEY is "thisIsEncryptKey", but in wireless programming example: "sampleEncryptKey".
It would really help if all of the examples would be consistent, you know :d

All this code is in the LPL Git Repo https://github.com/LowPowerLab/RFM69/tree/master/Examples. Your welcome to submit a pull request fixing this. Great way to help out Felix for all the fine work he has done.

Felix

Quote from: ivars211 on May 03, 2015, 01:35:25 PM
in the node-getway the KEY is "thisIsEncryptKey", but in wireless programming example: "sampleEncryptKey".
It would really help if all of the examples would be consistent, you know :d

Can you please point me where the inconsistency is (ie provide links)?