THE first thing we need to talk about is the book.

It has his name on the cover in chunky capitals: JAMES OSWALD. For many years, Oswald wrote novels that were never published but now, finally, it's happened. After self-publishing a crime novel, Natural Causes, online last year and shifting an extraordinary 250,000 copies, Penguin has given him a six-figure deal. And here it is at last: the real thing. He keeps picking the book up and grinning at it.

The second thing we need to talk about is Lamb Cam. On the computer screen in front of Oswald is a webcam showing pictures from the shed next door. Oswald is a full-time farmer and the webcam is pointed at his pregnant ewes. He was up at 8am today helping a ewe give birth and there are still 10 to go. The camera means he can keep an eye on them 24 hours a day.

This slightly strange combination of farming and writing, outdoors and indoors, the real and the imagined, is normal for Oswald. He grew up around farms and in 2008, when his parents were killed in a car crash, took over the family farm near Newburgh in north-east Fife.

It's a beautiful spot. When Oswald greets me, holding a shepherd's crook in his hand, we stop for a few minutes to look at the view. The farm sits high on a hill overlooking the mouth of the Tay. Over there is Dundee and the Sidlaw hills. Behind us, spread out over 350 acres, are Oswald's 12 Highland cattle and 50 New Zealand Romney sheep.

Oswald and his partner Barbara live in a caravan at the top of the hill (they're planning to build a house soon) and at the door Oswald's four dogs, including a labrador called Haggis, give us an enthusiastic welcome; the cat gives us a dirty look. In the corner of the front room is a little ramshackle desk where Oswald writes. He's supposed to be writing his fourth crime novel right now but is struggling to find the time.

He sits down at the desk and tells me he's been waiting 20 years for this. That's how long he's been writing without being published. "I've probably got 10 unpublished manuscripts kicking around – a million words at least," he says. "There's a saying that the first million words are free because that's what you need to do to get up to a good-enough standard of writing."

So why did he keep going for that long, especially when he had to fit the writing in between the blood and guts and hard work of farming? "Pig-headedness," he says. "What's that lovely Scottish word? Thrawn. I'm possessed with thrawn. That's probably why I've got the farm, because I was told I couldn't do it – everybody, all my parents' friends when my dad died, said it. They thought I didn't have the experience or that it would be too much hard work. It chipped away at me. I thought: damn you, I'm going to do this anyway."

Even so, you can see why his parents' friends said what they did – Oswald had no experience of farming other than helping out with the harvest during the holidays. His father was born into a farming family in Easter Ross but for many years, before buying the farm in Fife, worked as a stockbroker in the City. It meant Oswald, who is 45, was brought up in England and Scotland and is a slightly eccentric mix of the two: a Scottish farmer with a gentle English accent.

His crime novel, though, is thoroughly Scottish. It starts with a girl's mutilated body being discovered in a sealed room in Edinburgh and has some of the hallmarks of Val McDermid or Ian Rankin: it's dark, violent, noirish. The detective is Inspector Tony McLean and, living in Edinburgh as he does, he will be compared to Rebus. How is Oswald's detective different? "For a start, he's younger," says Oswald. "He's in his mid-30s in the first book and he's not an alcoholic – he doesn't have an ex-wife. His character is coloured by his experiences – he's had a pretty horrible, torrid time. His parents died when he was four and he was brought up by his grandmother who is in a coma. His fiancee was also abducted and murdered so that colours his character but despite that he's not a basket case. He's thrown himself into the work."

But why set the story in Edinburgh? I point to the view out of the caravan window and ask Oswald why he wasn't inspired to set his novels in the country. "I'm not a huge fan of rural crime books because I live in the countryside and there isn't that sort of crime," he says. "Some kids might get drunk but that's about it. I may bring McLean out into the countryside but the city is another one of the characters in my book."

What Oswald already knows is that McLean is popular. Last year, after 20 years of rejection, Oswald decided to publish the books himself. He paid a designer $80 to create a cover and put Natural Causes on Amazon, initially for free. "The idea was to give it away for free to get noticed," he said. "I thought I might get 100 people a month – as it turned out, it was 50,000 in the first month. First, it was in the top 10, then it was No.1, and it stayed there for six weeks. Something gelled with readers, they thought: 'I'd download that.' It costs nothing and that kicks it up the charts so it gets a bit of promotion. When I decided to self-publish, I thought I'd be pleased to shift 1000 in a year but I was doing 2000 a day at one point."

Oswald then published the sequel, Book Of Souls, charging £2.99, and it sold about 50,000 copies in less than a year. Combined, Natural Causes and Book Of Souls have sold more than 350,000 downloads. That's when he came to the notice of Penguin and four other publishers who entered a bidding war. Although Oswald is keen on self-publishing online and e-books, he is delighted at getting a traditional publishing deal and points out most readers still prefer hard copies of a book. And besides, there's nothing like the thrill of seeing your books on the shelves, as he will do at the launch of Natural Causes in Waterstones in Dundee next month. "That's been my dream for the last 20 years," he says.

And is the next dream an escape from farming? After all, Oswald works hard for not very much money. "Livestock farming is hard graft," he says. "I don't do it to get rich, but then you don't write to get rich either. People look at farmers swanning around in Range Rovers and think they're loaded but they're asset rich and cash poor; all the new kit is on tick."

Besides, Oswald doesn't want to escape from farming. When his parents David and Juliet were killed, Oswald had to deal with the shock, then decide if he wanted to take over the farm. "The shock of my parents' deaths was terrible," he says, "and knocked all the enthusiasm out of me for writing and everything else. It would be wrong to say I had the farm thrust upon me, though. I have two brothers and a sister, and the division of my parents' estate was very amicable. Ultimately, the decision to take over the farm rather than sell up and take the money was mine."

Oswald says his father was bemused but supportive of his son's writing and even he is slightly bewildered it paid off. For most of his working life, Oswald did a number of jobs but he says the writing was always there. "They were just jobs," he says. "I've never had a career. It was all to support my writing habit."

The result is James Oswald, the man with two lives. "Farming is so different from the writing side of things," he says. "But you can do the two and it gives you a break from the other." And he now knows what he wants to be. "I'll do a bit of writing in the morning then go up the hill with the dogs and look at my cows, then come back and do some more writing," he says. "I will be a writer-farmer."

Natural Causes is published by Penguin on Thursday, priced £7.99. Book Of Souls will be published in July.