[{"data":1,"prerenderedAt":918},["ShallowReactive",2],{"post:\u002F2018\u002F10\u002F24\u002Fa-labour-of-love":3},{"id":4,"title":5,"body":6,"date":910,"description":12,"extension":911,"meta":912,"navigation":131,"path":913,"seo":914,"stem":915,"subtitle":916,"__hash__":917},"posts\u002F2018\u002F10\u002F24\u002Fa-labour-of-love.md","A labour of love",{"type":7,"value":8,"toc":905},"minimark",[9,13,21,24,29,32,48,57,72,76,79,483,487,496,883,898,901],[10,11,12],"p",{},"From the dawn of time men have famously had their own primary drive, something that they will go without food\u002Fsleep to see complete. In the way that Neil Armstrong felt the need for aviation and later space exploration, the way George Bool created a seemingly useless form of mathematics to which every device we use now owes its existence. I, it would seem, found my time best spent shamelessly trying to impress a girl.",[10,14,15,16,20],{},"Some backstory, I'm terrible for looking at my phone. It's perpetually on silent, because I feel that I should be the one who dictates when it gets my attention. Because of this people are ",[17,18,19],"em",{},"sometimes"," left on read.",[10,22,23],{},"Cue, what I have come to call the Megan Beacon",[25,26,28],"h2",{"id":27},"a-quick-overview","A quick overview",[10,30,31],{},"I have a raspberry pi on my desk, that's perpetually running small programs that I have written, from sensor readings to a python script to enter me in this weekly competition for a free brunch.",[10,33,34,35,42,43],{},"A ",[36,37,41],"a",{"href":38,"rel":39},"https:\u002F\u002Fmedia.4rgos.it\u002Fi\u002FArgos\u002F8266619_R_Z002A?$Web$&$DefaultPDP1240$",[40],"nofollow","Speaker",", the very basic ",[36,44,47],{"href":45,"rel":46},"https:\u002F\u002Fwww.argos.co.uk\u002Fproduct\u002F8266619?rec=PDP%5B4284183%5D:bottomSlider:P1:OHAT:alternative:8266619:53nlbKjTsxsfQBlMl41d",[40],"Bush Cube Wireless Speaker",[10,49,50,51,56],{},"She has an iphone, ",[36,52,55],{"href":53,"rel":54},"https:\u002F\u002Fdeveloper.apple.com\u002Fswift\u002F",[40],"because of course she does",".",[10,58,59,60,65,66,71],{},"So, this project will be a mobile app written in swift, that will send a message using ",[36,61,64],{"href":62,"rel":63},"https:\u002F\u002Fdweet.io\u002F",[40],"dweet"," via the library ",[36,67,70],{"href":68,"rel":69},"https:\u002F\u002Fgithub.com\u002Fpaddycarey\u002Fdweepy",[40],"dweepy",", to be heard by my raspberry pi using Python.",[25,73,75],{"id":74},"the-script","The script",[10,77,78],{},"The script is super easy. Its just a small Python script that will compare a stored string in a text file against a string sent through dweet from the app. If there's a discrepancy then the audio file plays and the text file is updated. Simple",[80,81,86],"pre",{"className":82,"code":83,"language":84,"meta":85,"style":85},"language-python shiki shiki-themes github-dark","import dweepy\nimport json\nimport time\nimport os\n\nfrom gtts import gTTS\ntts = gTTS('pay attention to me')\ntts.save('sound.mp3')\n\nwhile True:\n    try:\n        with open('need_token.txt', 'r') as myfile:\n            file_data = myfile.read()\n        dweet = dweepy.get_latest_dweet_for('MY-SECRET-DWEET-PATH')\n        value = str(dweet[0][\"content\"][\"token\"])\n        if file_data == value:\n            print(\"yep\")\n            time.sleep(10)\n        else:\n            print(\"nope\")\n            # play audio file\n            # write new number to token file\n            print(value)\n            os.system(\"afplay sound.mp3\")\n            with open('need_token.txt', 'w') as myfile:\n                myfile.write(str(value))\n            print(\"updated old value \" + file_data + \" file with \" + value)\n            time.sleep(10)\n    except:\n        print(\"there was nothing there\")\n        time.sleep(10)\n\n","python","",[87,88,89,102,110,118,126,133,147,166,177,182,195,203,233,244,260,291,306,319,330,338,350,357,363,371,382,405,417,443,452,460,473],"code",{"__ignoreMap":85},[90,91,94,98],"span",{"class":92,"line":93},"line",1,[90,95,97],{"class":96},"snl16","import",[90,99,101],{"class":100},"s95oV"," dweepy\n",[90,103,105,107],{"class":92,"line":104},2,[90,106,97],{"class":96},[90,108,109],{"class":100}," json\n",[90,111,113,115],{"class":92,"line":112},3,[90,114,97],{"class":96},[90,116,117],{"class":100}," time\n",[90,119,121,123],{"class":92,"line":120},4,[90,122,97],{"class":96},[90,124,125],{"class":100}," os\n",[90,127,129],{"class":92,"line":128},5,[90,130,132],{"emptyLinePlaceholder":131},true,"\n",[90,134,136,139,142,144],{"class":92,"line":135},6,[90,137,138],{"class":96},"from",[90,140,141],{"class":100}," gtts ",[90,143,97],{"class":96},[90,145,146],{"class":100}," gTTS\n",[90,148,150,153,156,159,163],{"class":92,"line":149},7,[90,151,152],{"class":100},"tts ",[90,154,155],{"class":96},"=",[90,157,158],{"class":100}," gTTS(",[90,160,162],{"class":161},"sU2Wk","'pay attention to me'",[90,164,165],{"class":100},")\n",[90,167,169,172,175],{"class":92,"line":168},8,[90,170,171],{"class":100},"tts.save(",[90,173,174],{"class":161},"'sound.mp3'",[90,176,165],{"class":100},[90,178,180],{"class":92,"line":179},9,[90,181,132],{"emptyLinePlaceholder":131},[90,183,185,188,192],{"class":92,"line":184},10,[90,186,187],{"class":96},"while",[90,189,191],{"class":190},"sDLfK"," True",[90,193,194],{"class":100},":\n",[90,196,198,201],{"class":92,"line":197},11,[90,199,200],{"class":96},"    try",[90,202,194],{"class":100},[90,204,206,209,212,215,218,221,224,227,230],{"class":92,"line":205},12,[90,207,208],{"class":96},"        with",[90,210,211],{"class":190}," open",[90,213,214],{"class":100},"(",[90,216,217],{"class":161},"'need_token.txt'",[90,219,220],{"class":100},", ",[90,222,223],{"class":161},"'r'",[90,225,226],{"class":100},") ",[90,228,229],{"class":96},"as",[90,231,232],{"class":100}," myfile:\n",[90,234,236,239,241],{"class":92,"line":235},13,[90,237,238],{"class":100},"            file_data ",[90,240,155],{"class":96},[90,242,243],{"class":100}," myfile.read()\n",[90,245,247,250,252,255,258],{"class":92,"line":246},14,[90,248,249],{"class":100},"        dweet ",[90,251,155],{"class":96},[90,253,254],{"class":100}," dweepy.get_latest_dweet_for(",[90,256,257],{"class":161},"'MY-SECRET-DWEET-PATH'",[90,259,165],{"class":100},[90,261,263,266,268,271,274,277,280,283,285,288],{"class":92,"line":262},15,[90,264,265],{"class":100},"        value ",[90,267,155],{"class":96},[90,269,270],{"class":190}," str",[90,272,273],{"class":100},"(dweet[",[90,275,276],{"class":190},"0",[90,278,279],{"class":100},"][",[90,281,282],{"class":161},"\"content\"",[90,284,279],{"class":100},[90,286,287],{"class":161},"\"token\"",[90,289,290],{"class":100},"])\n",[90,292,294,297,300,303],{"class":92,"line":293},16,[90,295,296],{"class":96},"        if",[90,298,299],{"class":100}," file_data ",[90,301,302],{"class":96},"==",[90,304,305],{"class":100}," value:\n",[90,307,309,312,314,317],{"class":92,"line":308},17,[90,310,311],{"class":190},"            print",[90,313,214],{"class":100},[90,315,316],{"class":161},"\"yep\"",[90,318,165],{"class":100},[90,320,322,325,328],{"class":92,"line":321},18,[90,323,324],{"class":100},"            time.sleep(",[90,326,327],{"class":190},"10",[90,329,165],{"class":100},[90,331,333,336],{"class":92,"line":332},19,[90,334,335],{"class":96},"        else",[90,337,194],{"class":100},[90,339,341,343,345,348],{"class":92,"line":340},20,[90,342,311],{"class":190},[90,344,214],{"class":100},[90,346,347],{"class":161},"\"nope\"",[90,349,165],{"class":100},[90,351,353],{"class":92,"line":352},21,[90,354,356],{"class":355},"sAwPA","            # play audio file\n",[90,358,360],{"class":92,"line":359},22,[90,361,362],{"class":355},"            # write new number to token file\n",[90,364,366,368],{"class":92,"line":365},23,[90,367,311],{"class":190},[90,369,370],{"class":100},"(value)\n",[90,372,374,377,380],{"class":92,"line":373},24,[90,375,376],{"class":100},"            os.system(",[90,378,379],{"class":161},"\"afplay sound.mp3\"",[90,381,165],{"class":100},[90,383,385,388,390,392,394,396,399,401,403],{"class":92,"line":384},25,[90,386,387],{"class":96},"            with",[90,389,211],{"class":190},[90,391,214],{"class":100},[90,393,217],{"class":161},[90,395,220],{"class":100},[90,397,398],{"class":161},"'w'",[90,400,226],{"class":100},[90,402,229],{"class":96},[90,404,232],{"class":100},[90,406,408,411,414],{"class":92,"line":407},26,[90,409,410],{"class":100},"                myfile.write(",[90,412,413],{"class":190},"str",[90,415,416],{"class":100},"(value))\n",[90,418,420,422,424,427,430,432,435,438,440],{"class":92,"line":419},27,[90,421,311],{"class":190},[90,423,214],{"class":100},[90,425,426],{"class":161},"\"updated old value \"",[90,428,429],{"class":96}," +",[90,431,299],{"class":100},[90,433,434],{"class":96},"+",[90,436,437],{"class":161}," \" file with \"",[90,439,429],{"class":96},[90,441,442],{"class":100}," value)\n",[90,444,446,448,450],{"class":92,"line":445},28,[90,447,324],{"class":100},[90,449,327],{"class":190},[90,451,165],{"class":100},[90,453,455,458],{"class":92,"line":454},29,[90,456,457],{"class":96},"    except",[90,459,194],{"class":100},[90,461,463,466,468,471],{"class":92,"line":462},30,[90,464,465],{"class":190},"        print",[90,467,214],{"class":100},[90,469,470],{"class":161},"\"there was nothing there\"",[90,472,165],{"class":100},[90,474,476,479,481],{"class":92,"line":475},31,[90,477,478],{"class":100},"        time.sleep(",[90,480,327],{"class":190},[90,482,165],{"class":100},[25,484,486],{"id":485},"the-app","The App",[10,488,489,490,495],{},"I've never created an IOS app before so this was a huge learning curve that involved getting to grips with swift, but in the end it was actually fine, and proved to be a nice environment. The app itself is quite simple, It's just a single button that when pressed generates a random eight character string and send it to Dweet.io via a post request. when run it also makes use of the ",[36,491,494],{"href":492,"rel":493},"https:\u002F\u002Fgithub.com\u002Fpndurette\u002FgTTS",[40],"Google Text to Speach"," library to create an audio file to play. This could easily be replaced by, in my case a WhatApp voice message.",[80,497,499],{"className":82,"code":498,"language":84,"meta":85,"style":85},"import UIKit\nimport Alamofire\n\nclass ViewController: UIViewController {\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \u002F\u002F Do any additional setup after loading the view, typically from a nib.\n    }\n    \n    @IBAction func UIButton(_ sender: UIButton) {\n        let parameters = [\n            \"app\": \"megan\",\n            \u002F\u002F calling upon random string function, passing in the arguement of 8 for 8 characters\n            \"token\": randomString(length: 8)\n        ]\n        \n        let alertController = UIAlertController(title: \"PRIVATE ENDEERING MESSAGE\", message:\n            randomString(length: 8), preferredStyle: UIAlertController.Style.alert)\n        alertController.addAction(UIAlertAction(title: \"Dismiss\", style: UIAlertAction.Style.default,handler: nil))\n        \n        self.present(alertController, animated: true, completion: nil)\n        Alamofire.request(\"HTTPS PATH TO SECRET DWEET URL\", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)\n\n    }\n    \n    \u002F\u002Ffunction to generate a random sting\n    func randomString(length: Int) -> String {\n        \n        let letters : NSString = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"\n        let len = UInt32(letters.length)\n        \n        var randomString = \"\"\n        \n        for _ in 0 ..\u003C length {\n            let rand = arc4random_uniform(len)\n            var nextChar = letters.character(at: Int(rand))\n            randomString += NSString(characters: &nextChar, length: 1) as String\n        }\n        \n        return randomString\n    }\n\n}\n",[87,500,501,508,515,519,531,535,540,548,567,572,577,585,590,604,630,642,647,652,663,673,684,688,696,707,711,715,719,727,739,743,751,762,767,776,781,804,814,820,847,853,858,867,872,877],{"__ignoreMap":85},[90,502,503,505],{"class":92,"line":93},[90,504,97],{"class":96},[90,506,507],{"class":100}," UIKit\n",[90,509,510,512],{"class":92,"line":104},[90,511,97],{"class":96},[90,513,514],{"class":100}," Alamofire\n",[90,516,517],{"class":92,"line":112},[90,518,132],{"emptyLinePlaceholder":131},[90,520,521,524,528],{"class":92,"line":120},[90,522,523],{"class":96},"class",[90,525,527],{"class":526},"svObZ"," ViewController",[90,529,530],{"class":100},": UIViewController {\n",[90,532,533],{"class":92,"line":128},[90,534,132],{"emptyLinePlaceholder":131},[90,536,537],{"class":92,"line":135},[90,538,539],{"class":100},"    override func viewDidLoad() {\n",[90,541,542,545],{"class":92,"line":149},[90,543,544],{"class":190},"        super",[90,546,547],{"class":100},".viewDidLoad()\n",[90,549,550,553,556,559,562,564],{"class":92,"line":168},[90,551,552],{"class":96},"        \u002F\u002F",[90,554,555],{"class":100}," Do ",[90,557,558],{"class":190},"any",[90,560,561],{"class":100}," additional setup after loading the view, typically ",[90,563,138],{"class":96},[90,565,566],{"class":100}," a nib.\n",[90,568,569],{"class":92,"line":179},[90,570,571],{"class":100},"    }\n",[90,573,574],{"class":92,"line":184},[90,575,576],{"class":100},"    \n",[90,578,579,582],{"class":92,"line":197},[90,580,581],{"class":96},"    @",[90,583,584],{"class":100},"IBAction func UIButton(_ sender: UIButton) {\n",[90,586,587],{"class":92,"line":205},[90,588,589],{"class":100},"        let parameters = [\n",[90,591,592,595,598,601],{"class":92,"line":235},[90,593,594],{"class":161},"            \"app\"",[90,596,597],{"class":100},": ",[90,599,600],{"class":161},"\"megan\"",[90,602,603],{"class":100},",\n",[90,605,606,609,612,615,618,621,624,627],{"class":92,"line":246},[90,607,608],{"class":96},"            \u002F\u002F",[90,610,611],{"class":100}," calling upon random string function, passing ",[90,613,614],{"class":96},"in",[90,616,617],{"class":100}," the arguement of ",[90,619,620],{"class":190},"8",[90,622,623],{"class":96}," for",[90,625,626],{"class":190}," 8",[90,628,629],{"class":100}," characters\n",[90,631,632,635,638,640],{"class":92,"line":262},[90,633,634],{"class":161},"            \"token\"",[90,636,637],{"class":100},": randomString(length: ",[90,639,620],{"class":190},[90,641,165],{"class":100},[90,643,644],{"class":92,"line":293},[90,645,646],{"class":100},"        ]\n",[90,648,649],{"class":92,"line":308},[90,650,651],{"class":100},"        \n",[90,653,654,657,660],{"class":92,"line":321},[90,655,656],{"class":100},"        let alertController = UIAlertController(title: ",[90,658,659],{"class":161},"\"PRIVATE ENDEERING MESSAGE\"",[90,661,662],{"class":100},", message:\n",[90,664,665,668,670],{"class":92,"line":332},[90,666,667],{"class":100},"            randomString(length: ",[90,669,620],{"class":190},[90,671,672],{"class":100},"), preferredStyle: UIAlertController.Style.alert)\n",[90,674,675,678,681],{"class":92,"line":340},[90,676,677],{"class":100},"        alertController.addAction(UIAlertAction(title: ",[90,679,680],{"class":161},"\"Dismiss\"",[90,682,683],{"class":100},", style: UIAlertAction.Style.default,handler: nil))\n",[90,685,686],{"class":92,"line":352},[90,687,651],{"class":100},[90,689,690,693],{"class":92,"line":359},[90,691,692],{"class":190},"        self",[90,694,695],{"class":100},".present(alertController, animated: true, completion: nil)\n",[90,697,698,701,704],{"class":92,"line":365},[90,699,700],{"class":100},"        Alamofire.request(",[90,702,703],{"class":161},"\"HTTPS PATH TO SECRET DWEET URL\"",[90,705,706],{"class":100},", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)\n",[90,708,709],{"class":92,"line":373},[90,710,132],{"emptyLinePlaceholder":131},[90,712,713],{"class":92,"line":384},[90,714,571],{"class":100},[90,716,717],{"class":92,"line":407},[90,718,576],{"class":100},[90,720,721,724],{"class":92,"line":419},[90,722,723],{"class":96},"    \u002F\u002F",[90,725,726],{"class":100},"function to generate a random sting\n",[90,728,729,732,736],{"class":92,"line":445},[90,730,731],{"class":100},"    func randomString(length: Int) ",[90,733,735],{"class":734},"s6RL2","->",[90,737,738],{"class":100}," String {\n",[90,740,741],{"class":92,"line":454},[90,742,651],{"class":100},[90,744,745,748],{"class":92,"line":462},[90,746,747],{"class":100},"        let letters : NSString = ",[90,749,750],{"class":161},"\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"\n",[90,752,753,756,759],{"class":92,"line":475},[90,754,755],{"class":100},"        let ",[90,757,758],{"class":190},"len",[90,760,761],{"class":100}," = UInt32(letters.length)\n",[90,763,765],{"class":92,"line":764},32,[90,766,651],{"class":100},[90,768,770,773],{"class":92,"line":769},33,[90,771,772],{"class":100},"        var randomString = ",[90,774,775],{"class":161},"\"\"\n",[90,777,779],{"class":92,"line":778},34,[90,780,651],{"class":100},[90,782,784,787,790,792,795,798,801],{"class":92,"line":783},35,[90,785,786],{"class":96},"        for",[90,788,789],{"class":100}," _ ",[90,791,614],{"class":96},[90,793,794],{"class":190}," 0",[90,796,797],{"class":100}," ..",[90,799,800],{"class":96},"\u003C",[90,802,803],{"class":100}," length {\n",[90,805,807,810,812],{"class":92,"line":806},36,[90,808,809],{"class":100},"            let rand = arc4random_uniform(",[90,811,758],{"class":190},[90,813,165],{"class":100},[90,815,817],{"class":92,"line":816},37,[90,818,819],{"class":100},"            var nextChar = letters.character(at: Int(rand))\n",[90,821,823,826,828,831,834,837,840,842,844],{"class":92,"line":822},38,[90,824,825],{"class":100},"            randomString ",[90,827,434],{"class":96},[90,829,830],{"class":100},"= NSString(characters: ",[90,832,833],{"class":96},"&",[90,835,836],{"class":100},"nextChar, length: ",[90,838,839],{"class":190},"1",[90,841,226],{"class":100},[90,843,229],{"class":96},[90,845,846],{"class":100}," String\n",[90,848,850],{"class":92,"line":849},39,[90,851,852],{"class":100},"        }\n",[90,854,856],{"class":92,"line":855},40,[90,857,651],{"class":100},[90,859,861,864],{"class":92,"line":860},41,[90,862,863],{"class":96},"        return",[90,865,866],{"class":100}," randomString\n",[90,868,870],{"class":92,"line":869},42,[90,871,571],{"class":100},[90,873,875],{"class":92,"line":874},43,[90,876,132],{"emptyLinePlaceholder":131},[90,878,880],{"class":92,"line":879},44,[90,881,882],{"class":100},"}\n",[10,884,885,886,891,892,897],{},"The I used ",[36,887,890],{"href":888,"rel":889},"http:\u002F\u002Fhttps:\u002F\u002Fgithub.com\u002FAlamofire\u002FAlamofire",[40],"alamofire"," for the post request, and installed it using ",[36,893,896],{"href":894,"rel":895},"https:\u002F\u002Fcocoapods.org\u002F",[40],"cocoapods",". I'll link the repositories at a later date for any of the fellow romantics out there.",[10,899,900],{},"For what it's worth, it worked. She remains my girlfriend to this day, but she did change to android. Tune in again for part 2...",[902,903,904],"style",{},"html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .s6RL2, html code.shiki .s6RL2{--shiki-default:#FDAEB7;--shiki-default-font-style:italic}",{"title":85,"searchDepth":104,"depth":104,"links":906},[907,908,909],{"id":27,"depth":104,"text":28},{"id":74,"depth":104,"text":75},{"id":485,"depth":104,"text":486},"2018-10-24 13:41","md",{},"\u002F2018\u002F10\u002F24\u002Fa-labour-of-love",{"title":5,"description":12},"2018\u002F10\u002F24\u002Fa-labour-of-love","A slice of raspberry pi with a side of hot coffee","1PTt9z6FXTFpdb5_bQyumZvXIAScoHEcEh5am1f8kEw",1791634646002]