I’ve been doing a little EmonTx v4 & Pi Pico testing to explore what’s possible following @borpin’s recommendation. I’ve been able to get a basic serial to emoncms bridge working so far that meets the core requirement of sending the EmonTx data to emoncms but with a hardcoded SSID and WiFi password of course.
I’ve got an example working with both micropython and the arduino pi pico core. Code examples included below for anyone interested in this.
I think a small adapter pcb could work well for making this a connectivity option on the emonTx v4.
MicroPython
import network
import socket
from time import sleep
import machine
from machine import UART, Pin
import urequests
import uselect
import sys
ssid = 'SSID'
password = 'PASSKEY'
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
try:
connect()
except Exception as e:
machine.reset()
buffered_input = []
uart0 = UART(0) # defaults 115200 baud
while(True):
while uart0.any() > 0:
c = chr(uart0.read(1)[0])
buffered_input.append(c)
if '\n' in buffered_input:
line_ending_index = buffered_input.index('\n')
data = "".join(buffered_input[:line_ending_index]).strip()
buffered_input = []
if data[0:3]=="MSG":
req = "https://emoncms.org/input/post?apikey=apikey&node=emontx4pico&data="+data
print(req)
r = urequests.get(req)
print(r.content)
sleep(0.1)
Arduino Pi Pico core
#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "PASSKEY";
String serverName = "https://emoncms.org/input/post";
String line = "";
String data = "";
String sub = "";
int data_ready = 0;
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
Serial1.begin(115200);
// Operate in WiFi Station mode
WiFi.mode(WIFI_STA);
// Start WiFi with supplied parameters
WiFi.begin(ssid, password);
// Print periods on monitor while establishing connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
}
// Connection established
Serial.println("");
Serial.print("Pico W is connected to WiFi network ");
Serial.println(WiFi.SSID());
// Print IP Address
Serial.print("Assigned IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
while (Serial1.available()) {
char c = char(Serial1.read());
if (c=='\n') {
data = line;
data.trim();
line = "";
sub = data.substring(0, 3);
if (sub=="MSG") {
Serial.println(data);
data_ready = 1;
}
} else {
line = line + c;
}
}
//Send an HTTP POST request every 10 minutes
if (data_ready) {
data_ready = 0;
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = serverName + "?node=pico2&data="+data+"&apikey=APIKEY";
Serial.println(serverPath);
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}