{"id":6516,"date":"2024-11-21T16:19:22","date_gmt":"2024-11-21T15:19:22","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=6516"},"modified":"2024-11-22T09:21:01","modified_gmt":"2024-11-22T08:21:01","slug":"arduino-audiotools-streaming-audio-via-http-post","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/","title":{"rendered":"Arduino AudioTools: Streaming audio via http post"},"content":{"rendered":"<p>In my <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">Arduino AudioTools<\/a> project I am providing many examples that show how to <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/tree\/main\/examples\/examples-communication\">distribute audio over the network<\/a>.<\/p>\n<p>One scenario that is easy to implement, is to <strong>write the data via http post<\/strong>. On the server side usually one of the following options is expected:<\/p>\n<ul>\n<li>the <strong>content length<\/strong> and we can just write the audio data to the stream after the http header<\/li>\n<li>no content length, adding <strong>transfer-encoding: chunked<\/strong> to the post header and writing in chunked format.<\/li>\n<\/ul>\n<h3>Arduino Sketch<\/h3>\n<p>Here is the corresponding <strong>Arduino Sketch<\/strong> that posts the audio data <strong>in chunked mode<\/strong>:<\/p>\n<pre><code>#include \"AudioTools.h\"\n\nconst char *ssid = \"your SSID\";\nconst char *password = \"your PASSWORD\";\nconst char *url_str = \"http:\/\/192.168.1.44:9988\";\nAudioInfo info(44100, 2, 16);\nSineWaveGenerator&lt;int16_t&gt; sineWave(32000);\nGeneratedSoundStream&lt;int16_t&gt; sound(sineWave);\nWiFiClient client;\nHttpRequest http(client);\nStreamCopy copier(http, sound);  \/\/ copy kit to kit\n\nvoid startWiFi() {\n  WiFi.mode(WIFI_STA);\n  WiFi.begin(ssid, password);\n  Serial.print(\"Connecting to WiFi ..\");\n  while (WiFi.status() != WL_CONNECTED) {\n    Serial.print('.');\n    delay(1000);\n  }\n  Serial.println();\n  Serial.println(WiFi.localIP());\n}\n\n\/\/ Arduino Setup\nvoid setup(void) {\n  \/\/ Open Serial\n  Serial.begin(115200);\n  AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);\n\n  startWiFi();\n\n  \/\/ Setup sine wave\n  sineWave.begin(info, N_B4);\n\n  \/\/ start post\n  Url url(url_str);\n  http.header().put(TRANSFER_ENCODING, CHUNKED); \/\/ uncomment if chunked\n  if (!http.processBegin(POST, url, \"audio\/pcm\")){\n    Serial.println(\"post failed\");\n    stop();\n  }\n}\n\n\/\/ Arduino loop - copy sound to out\nvoid loop() {\n  \/\/ posting the data\n  if (copier.copy() == 0) {\n    \/\/ closing the post\n    http.processEnd();\n  }\n}\n<\/code><\/pre>\n<p>The logic is the following:<\/p>\n<ul>\n<li>we initialize\n<ul>\n<li>the wifi<\/li>\n<li>the sine generator<\/li>\n<\/ul>\n<\/li>\n<li>we submit the post by makeing sure that the request header contains transfer-encoding: chunked.<\/li>\n<li>finally we can just copy the sound data from the generator to http which automatically calls the chunked writes.<\/li>\n<\/ul>\n<p>The (potentially updated) sketch can be found in the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/examples\/examples-communication\/http-client\/streams-http_post\/streams-http_post.ino\">examples folder<\/a> of the project<\/p>\n<h3>The Python Server<\/h3>\n<p>Here is a <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/examples\/examples-communication\/http-server\/python-post-server\/server.py\">simple example server in python<\/a> that shows how to process the <strong>chunked data<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my Arduino AudioTools project I am providing many examples that show how to distribute audio over the network. One scenario that is easy to implement, is to write the data via http post. On the server side usually one of the following options is expected: the content length and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[20,22],"tags":[],"class_list":["post-6516","post","type-post","status-publish","format-standard","hentry","category-arduino","category-machine-sound"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Arduino AudioTools: Streaming audio via http post - Phil Schatzmann<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Arduino AudioTools: Streaming audio via http post - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"In my Arduino AudioTools project I am providing many examples that show how to distribute audio over the network. One scenario that is easy to implement, is to write the data via http post. On the server side usually one of the following options is expected: the content length and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-21T15:19:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-22T08:21:01+00:00\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Arduino AudioTools: Streaming audio via http post\",\"datePublished\":\"2024-11-21T15:19:22+00:00\",\"dateModified\":\"2024-11-22T08:21:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/\"},\"wordCount\":180,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/\",\"name\":\"Arduino AudioTools: Streaming audio via http post - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"datePublished\":\"2024-11-21T15:19:22+00:00\",\"dateModified\":\"2024-11-22T08:21:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/11\\\/21\\\/arduino-audiotools-streaming-audio-via-http-post\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Arduino AudioTools: Streaming audio via http post\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Arduino AudioTools: Streaming audio via http post - Phil Schatzmann","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/","og_locale":"en_US","og_type":"article","og_title":"Arduino AudioTools: Streaming audio via http post - Phil Schatzmann","og_description":"In my Arduino AudioTools project I am providing many examples that show how to distribute audio over the network. One scenario that is easy to implement, is to write the data via http post. On the server side usually one of the following options is expected: the content length and [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/","og_site_name":"Phil Schatzmann","article_published_time":"2024-11-21T15:19:22+00:00","article_modified_time":"2024-11-22T08:21:01+00:00","author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Arduino AudioTools: Streaming audio via http post","datePublished":"2024-11-21T15:19:22+00:00","dateModified":"2024-11-22T08:21:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/"},"wordCount":180,"commentCount":2,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/","url":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/","name":"Arduino AudioTools: Streaming audio via http post - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"datePublished":"2024-11-21T15:19:22+00:00","dateModified":"2024-11-22T08:21:01+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/11\/21\/arduino-audiotools-streaming-audio-via-http-post\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Arduino AudioTools: Streaming audio via http post"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/6516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=6516"}],"version-history":[{"count":11,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/6516\/revisions"}],"predecessor-version":[{"id":6527,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/6516\/revisions\/6527"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=6516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=6516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=6516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}