Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:12 24 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : using HC-12

     Page 1 of 3    
Author Message
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1970
Posted: 01:59am 20 Dec 2018
Copy link to clipboard 
Print this post

My first time trying to send data from one MM to another. I have the HC-12s connected and I know they work because I have used them as a serial link.
I'm not having much luck. On the send side I have
OPEN "COM1:9600" as #1
PRINT #1, X$
on the receive side I am not sure how to use INPUT or LINE INPUT
can someone give me a short snippet of code to show how it is done.
I have spent all morning going through the manuals and tried many things but the best I received was a Y with two dots above it.
Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6230
Posted: 02:17am 20 Dec 2018
Copy link to clipboard 
Print this post

Y with two dots sounds like a baud rate mismatch.

On the receive side you can set up an interrupt to trigger with a set number of characters or with the latest MMBasic, you can use a specific character to trigger the interrupt. This save polling the com port looking for the CR

OPEN "COM1:9600, 256, MyInt, =13" AS #1
DO:LOOP
SUB MyInt
x$ = INPUT$(LOC(), #1)
end function

I think thats right.

JimEdited by TassyJim 2018-12-21
VK7JH
MMedit
 
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 02:24am 20 Dec 2018
Copy link to clipboard 
Print this post

I think the line
x$ = INPUT$(LOC(), #1)


should be
x$ = INPUT$(LOC(#1), #1)


Cheers,
Phil.

Update: and End Function should be End SubEdited by erbp 2018-12-21
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9511
Posted: 03:05am 20 Dec 2018
Copy link to clipboard 
Print this post

Yes, that is pretty much how I do it for line-at-a-time. The beauty of the MM serial port buffer, is that it will take care of any data that arrives, and all you have to do, is check the buffer as part of your main loop and act if there is anything there.

For byte-at-a-time receiving, I use something like this:

[code]
If LOC(#1)<>0 then
DO
D$=INPUT$(1,#1) 'Suck a byte from the buffer
T$=T$+D$ 'Add byte to string
Loop Until D$=CHR$(13)

...

[/code]

In the example above, I am looking for an EOM(end of message) marker byte. It can be anything, but I tend to just use CR.(decimal 13)

The MM buffer is really awesome for this kind of thing, as the messages are then self-queuing in the buffer(provided you make sure the buffer is big enough), and you just read the messages out one after another to process them. The code does not need to worry about missing a message while you are processing the last one, as the buffer will take care of all of that for you.
Smoke makes things work. When the smoke gets out, it stops!
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:22am 21 Dec 2018
Copy link to clipboard 
Print this post

Hey Grogster, you seem to be the HC-12 slayer, I have ordered a few of these from one vendor and I think they are genuine. However, if not, I know to only use units from the same vendor due to the weird off freq. stuff you discovered in another thread.

I would like to run these on 3.3v as I don't want to have to add more components for 5v just for this module. Do you find these work o.k. on 3.3v, or is this an "iffy" thing to do? What have you experienced...

Just for fun I also ordered a few of THESE. (The 915Mhz Version). It seems like I can get a bit of distance with these and they are spec'ed to work on 3.3v which is great. I plan on running these between two warehouses that are about 1.5 miles apart to control a few things with, of course, a micromite. They are sitting on my bench just waiting for a bit of play time. Have you ever tried these?Edited by viscomjim 2018-12-22
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9511
Posted: 03:22am 21 Dec 2018
Copy link to clipboard 
Print this post

Hey there Jim.

I DON'T recommend running the HC12 on 3v3. The PDF says you can, but I have found them to be flaky at that voltage. The input supply is stated as 3v2 to 5v5, but as 3v3 is only 100mV above the minimum voltage, there is only a tiny amount of headroom there, so your 3v3 supply needs to be VERY clean, and rock-solid. Any kind of voltage-drop on the 3v3 rail will cause the HC12 to get confused and do odd things due probably to the brown-out effect. I run all my HC12s on 5v, and just use a 5v tolerant COM port. If you don't have any spare 5v COM ports, you can get very cheap bi-directional level shifter things on eBay to allow you to put the 5v into a 3v3 pin, and to translate the 3v3 back to 5v for the module. See this link for an example, and these things work beautifully to get around the 3v3 vs 5v0 logic for MCU pins.

Those LoRa modules you found look interesting. What did you pay for them? There is no price on the webpage.

EDIT: I've just had a quick look at the specs of these things, and they are quite impressive, really. Supposedly have FCC approval, although, that might just be sales talk. A -148dBm receiver sensitivity is excellent if it really is that. This would make them very good listeners indeed. -4dBm to +15dBm adjustable output power, 868MHz/915MHz, up to 15km range on a good day. Nice find, Jim. I like them. Edited by Grogster 2018-12-22
Smoke makes things work. When the smoke gets out, it stops!
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1636
Posted: 06:19am 21 Dec 2018
Copy link to clipboard 
Print this post

You can find them on eBay here

Bill
Keep safe. Live long and prosper.
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 11:14am 21 Dec 2018
Copy link to clipboard 
Print this post

Grogster, I paid $15 a piece for those modules. A bit pricey, but for the distance I am anticipating, it was worth it. I believe you can buy just the SX1276 for less than half that, but reyax put a nice little mcu on the board to let you use a serial port with simple AT commands. The SX1276 uses spi and looks a little confusing to me. I checked out the arduino library (radiohead) for this to see if it could be ported to mmbasic, but way over my head, so the mcu + SX1276 for $15 was worth it to me to start using them. I do believe they are truly FCC approved from what I can read on line. There is also an SX1278 for 433Mhz.

I also ordered a couple of THESE at 1 W and THESE at 100mW. Both of which are apparently FCC approved. They use a different setup, but I found documentation for using them. Both use uart also. Look on Youtube for the reyax units, they seem to work quite nice... especially THIS ONE. Also check out TTN network. Very interesting stuff.

I think there was a shedder that uses these or something very similar to them (srnet???). I believe he did the satellite for $50 or something of that nature. I will do a search on TBS to see if I can locate that thread. I also believe he used something like these.

On the HC-12... Thanks for the info, looks like 5v it is. After a bit of googling, I see that at 5v they recommend using a diode to drop the voltage a bit if you are planning on transmitting a lot to keep the on board reg. cooler. Is this something you've noticed being necessary?Edited by viscomjim 2018-12-22
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 982
Posted: 12:53am 22 Dec 2018
Copy link to clipboard 
Print this post

  Quote  THESE at 1 W and THESE at 100mW


I have purchased around 20 of these units over the past year from many different suppliers on Ebay and Aliexpress and I have been having trouble with some of them working in a network. I am wondering if there is a similar situation with cloned units from other sources. There seem to be some with the E-byte branding, V1.0, V3.0 and V6.0 as well as some without the E-byte brand that have the same model number.




Has any one else had any issues?

OA47
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1970
Posted: 01:08am 22 Dec 2018
Copy link to clipboard 
Print this post

OK all good nearly, I got the HC-12s talking but I am wanting to send 3 values so to send I have
PRINT #1, X$, Y$, Z$

How do I receive this and place it into 3 variables. One thought was to look for the comma?

Paul.Edited by palcal 2018-12-23
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6230
Posted: 01:43am 22 Dec 2018
Copy link to clipboard 
Print this post

Does the length of the three variables vary much?
One simple method I use is to format the data as fixed length strings so I can use MID$() to slice it up. It also looks nice when printing out, neatly aligned, ready for copy/paste into other programs

If you are keeping it within MMBasic, you should be able to use

INPUT #1,var1$, var2$, var3$


I haven't tried this in combination with the serial port interrupt.

Jim


Edited by TassyJim 2018-12-23
VK7JH
MMedit
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1970
Posted: 01:47am 22 Dec 2018
Copy link to clipboard 
Print this post

That gives me something to work on, Thanks Jim.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1970
Posted: 02:38am 22 Dec 2018
Copy link to clipboard 
Print this post

I took your advice Jim and formatted the 3 strings then sent them as one string and broke the string up on the receive end. Works perfect. Thanks.

Edit... Just found the sleep current of the HC-12 is 22uA so with an Armmite L4 and E-Ink display should be able to make a decent battery powered project.Edited by palcal 2018-12-23
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9511
Posted: 03:46am 23 Dec 2018
Copy link to clipboard 
Print this post

  viscomjim said   Grogster, I paid $15 a piece for those modules. A bit pricey, but for the distance I am anticipating, it was worth it. I believe you can buy just the SX1276 for less than half that, but reyax put a nice little mcu on the board to let you use a serial port with simple AT commands. The SX1276 uses spi and looks a little confusing to me. I checked out the arduino library (radiohead) for this to see if it could be ported to mmbasic, but way over my head, so the mcu + SX1276 for $15 was worth it to me to start using them. I do believe they are truly FCC approved from what I can read on line. There is also an SX1278 for 433Mhz.

I also ordered a couple of THESE at 1 W and THESE at 100mW. Both of which are apparently FCC approved. They use a different setup, but I found documentation for using them. Both use uart also. Look on Youtube for the reyax units, they seem to work quite nice... especially THIS ONE. Also check out TTN network. Very interesting stuff.

I think there was a shedder that uses these or something very similar to them (srnet???). I believe he did the satellite for $50 or something of that nature. I will do a search on TBS to see if I can locate that thread. I also believe he used something like these.

On the HC-12... Thanks for the info, looks like 5v it is. After a bit of googling, I see that at 5v they recommend using a diode to drop the voltage a bit if you are planning on transmitting a lot to keep the on board reg. cooler. Is this something you've noticed being necessary?


You only really need the series diode to drop 0.6v or so to relieve the heat stress on the LDO regulator on the module, IF you are transmitting a continuous stream of data - like a 100% duty cycle. If you are only transmitting sporadically or with a 50% duty cycle or less, you don't need the diode.
Smoke makes things work. When the smoke gets out, it stops!
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1970
Posted: 01:23am 28 Dec 2018
Copy link to clipboard 
Print this post

I have got my HC-12's talking OK. But on the receive end the data does not change when it updates. At the moment for testing I have it updating every minute. I close the COM port at the end of the loop wait 60 secs. then loop and reopen the COM port, but when I input the data it is the same as before. If I switch off and then on again I find that the data is updated. I thought that closing the COM port cleared the buffer. What am I doing wrong.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9511
Posted: 01:35am 28 Dec 2018
Copy link to clipboard 
Print this post

Closing the COM port does clear the buffer. Can you post your two codes? One from the transmitting end, the other from the receiving end.
Smoke makes things work. When the smoke gets out, it stops!
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1970
Posted: 02:47am 28 Dec 2018
Copy link to clipboard 
Print this post

Ok here it is, my coding is maybe not the best.
This is the SEND code


SetTick 30000, RESET 'resets rain to zero at 9:00am
RTC GETTIME
Dim Temp,Hum
Dim Information$
Dim rain,R$,RY$,RM$,T$,H$
SetPin 18,CIN ' pin connected to rain gauge
'VAR RESTORE
Do
Open "COM2:9600" As #1
HUMID 2, Temp, Hum ' get Temp and Humidity from DHT22
Temp=Cint(Temp)
Hum=Cint(Hum)
T$= Str$(Temp,2)
H$= Str$(Hum,2)
Print Cint(Temp);" ";Cint(hum)
Print Time$
Rain=Pin(18)*.45 ' convert gauge tips to mm.
rain=55 'rain gauge not connected(for display setup)
R$=Str$(rain,3)
RY$=" 55" ' rain yesterday for display setup
RM$="172" ' rain month for display setup

Information$=T$+H$+R$+RY$+RM$ ' convert data into one string
Print Information$
Print #1, Information$
Pause 500
Close #1
'VAR Save RM$ ' to save rain data if power lost
Pause 30000
Loop

Sub Reset
If Left$(Time$,2)="09" And Mid$(Time$,4,2)="00" Then
RainY = Rain
RY$ = Str$(RainY,3)
RainM = RainM + Rain
RM$ = Str$(RainM,3)
Rain = 0
Pin(18) = 0
RTC GETTIME
EndIf

End Sub

This code also has the DHT22 C code at the bottom

And this is the receive code


CLS
' Setup the E-Ink display
Box 0,0,MM.HRes,MM.VRes
Text 20,MM.VRes-145,"TEMPERATURE HUMIDITY",RMU,2,2
Text 40,280,"In",RMU,2,2
Text 60,275,"Out",RMU,2,2
Text 90,140,"RAIN YESTERDAY MONTH",RMU,2,2
Refresh
Do
Open "COM1:9600" As #1
Do
If Loc(#1)<>0 Then
D$=Input$(1,#1)
Information$=Information$+D$ ' Information$ contains all the data
EndIf
Loop Until D$=Chr$(13)
Close #1
Temp$=Left$(Information$,2) 'split up information$ to regain data
Hum$=Mid$(Information$,3,2)
Rain$=Mid$(Information$,5,3)
Rain_Yesterday$=Mid$(Information$,8,3)
Rain_Month$=Right$(Information$,4)

Print Temp$;" ";Hum$;" ";Rain$

' send data to E-Ink display
Text 60,150,Temp$+"c "+Hum$+"%",RMU,2,2
Text 125,140,Rain$+" "+Rain_Yesterday$+" "+Rain_Month$, RMU,2,2
Refresh
Pause 60000
Loop


Also the line VAR RESTORE in the send code throws an error so it is commented out.Edited by palcal 2018-12-29
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 04:12am 28 Dec 2018
Copy link to clipboard 
Print this post

You need to clear the data you have accumulated into Information$ on the first receive cycle before starting the next cycle. I suggest you add the following line immediately prior to the Pause 60000 at the end of the loop in your receive code:

Information$ = ""


Cheers,
Phil.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6230
Posted: 04:13am 28 Dec 2018
Copy link to clipboard 
Print this post

You might need
  Quote  Information$= ""

when you open the serial port otherwise you are just adding the new data onto the end of the old.


Edit: Typing too slow!Edited by TassyJim 2018-12-29
VK7JH
MMedit
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 983
Posted: 04:21am 28 Dec 2018
Copy link to clipboard 
Print this post

In the receive code I think you need an
Information$=""
at the end of the loop. Now I think it continues to append and your keep pulling the same data off the front of the string. It probably eventually grows bigger than 256 bytes and errors if you let it run long enough.

Regards
Gerry

PS Too Slow!

PS again:
Honest Jim , I didn't copy.Edited by disco4now 2018-12-29
Latest F4 Latest H7 FotS
 
     Page 1 of 3    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025