mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
150 lines
3.9 KiB
C
150 lines
3.9 KiB
C
/*
|
|
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
|
|
*
|
|
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*
|
|
* ----------------------------------------------------------------------------
|
|
* This file is designed to be parsed during the build process
|
|
*
|
|
* Contains built-in functions for Espressif ESP8266 WiFi Access
|
|
* ----------------------------------------------------------------------------
|
|
*/
|
|
|
|
#include "jswrap_esp8266.h"
|
|
#include "jshardware.h"
|
|
#include "jsinteractive.h"
|
|
#include "network.h"
|
|
#include "network_esp8266.h"
|
|
|
|
|
|
/*JSON{
|
|
"type" : "library",
|
|
"class" : "ESP8266"
|
|
}
|
|
Library for the Espressif ESP8266 WiFi Module
|
|
*/
|
|
/*JSON{
|
|
"type" : "staticmethod",
|
|
"class" : "ESP8266",
|
|
"name" : "connect",
|
|
"generate" : "jswrap_esp8266_connect",
|
|
"params" : [
|
|
["serial","JsVar","The Serial port used for communications with the ESP8266 (must already be setup)"]
|
|
],
|
|
"return" : ["JsVar","An ESP8266 Object"],
|
|
"return_object" : "ESP8266"
|
|
}
|
|
Initialise the WIZnet module and return an Ethernet object
|
|
*/
|
|
JsVar *jswrap_esp8266_connect(JsVar *usart) {
|
|
|
|
IOEventFlags usartDevice;
|
|
usartDevice = jsiGetDeviceFromClass(usart);
|
|
if (!DEVICE_IS_USART(usartDevice)) {
|
|
jsExceptionHere(JSET_ERROR, "Expecting USART device, got %q", usart);
|
|
return 0;
|
|
}
|
|
|
|
JsNetwork net;
|
|
networkCreate(&net, JSNETWORKTYPE_ESP8266);
|
|
net.data.device = usartDevice;
|
|
networkSet(&net);
|
|
|
|
JsVar *ethObj = 0;
|
|
|
|
JsVar *cmd = jsvNewFromString("AT+RST");
|
|
esp8266_send(cmd);
|
|
jsvUnLock(cmd);
|
|
if (esp8266_wait_for("OK", 100)) {
|
|
if (esp8266_wait_for("ready", 500)) {
|
|
networkState = NETWORKSTATE_ONLINE;
|
|
ethObj = jspNewObject(0, "ESP8266");
|
|
} else {
|
|
jsExceptionHere(JSET_ERROR, "Module not ready");
|
|
}
|
|
} else {
|
|
jsExceptionHere(JSET_ERROR, "No Acknowledgement");
|
|
}
|
|
|
|
|
|
networkFree(&net);
|
|
|
|
return ethObj;
|
|
}
|
|
|
|
/*JSON{
|
|
"type" : "class",
|
|
"class" : "ESP8266"
|
|
}
|
|
An instantiation of an ESP8266 network adaptor
|
|
*/
|
|
|
|
/*JSON{
|
|
"type" : "method",
|
|
"class" : "Ethernet",
|
|
"name" : "getIP",
|
|
"generate" : "jswrap_esp8266_getIP",
|
|
"return" : ["JsVar",""]
|
|
}
|
|
Get the current IP address
|
|
*/
|
|
JsVar *jswrap_esp8266_getIP(JsVar *wlanObj) {
|
|
NOT_USED(wlanObj);
|
|
|
|
if (networkState != NETWORKSTATE_ONLINE) {
|
|
jsError("Not connected to the internet");
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* If byte 1 is 0 we don't have a valid address */
|
|
JsVar *data = jsvNewWithFlags(JSV_OBJECT);
|
|
/* networkPutAddressAsString(data, "ip", &gWIZNETINFO.ip[0], 4, 10, '.');
|
|
networkPutAddressAsString(data, "subnet", &gWIZNETINFO.sn[0], 4, 10, '.');
|
|
networkPutAddressAsString(data, "gateway", &gWIZNETINFO.gw[0], 4, 10, '.');
|
|
networkPutAddressAsString(data, "dns", &gWIZNETINFO.dns[0], 4, 10, '.');
|
|
networkPutAddressAsString(data, "mac", &gWIZNETINFO.mac[0], 6, 16, 0);*/
|
|
return data;
|
|
}
|
|
|
|
|
|
static void _eth_getIP_set_address(JsVar *options, char *name, unsigned char *ptr) {
|
|
JsVar *info = jsvObjectGetChild(options, name, 0);
|
|
if (info) {
|
|
char buf[64];
|
|
jsvGetString(info, buf, sizeof(buf));
|
|
*(unsigned long*)ptr = networkParseIPAddress(buf);
|
|
jsvUnLock(info);
|
|
}
|
|
}
|
|
|
|
/*JSON{
|
|
"type" : "method",
|
|
"class" : "Ethernet",
|
|
"name" : "setIP",
|
|
"generate" : "jswrap_esp8266_setIP",
|
|
"params" : [
|
|
["options","JsVar","Object containing IP address options `{ ip : '1,2,3,4', subnet, gateway, dns }`, or do not supply an object in otder to force DHCP."]
|
|
],
|
|
"return" : ["bool","True on success"]
|
|
}
|
|
Set the current IP address for get an IP from DHCP (if no options object is specified)
|
|
*/
|
|
bool jswrap_esp8266_setIP(JsVar *wlanObj, JsVar *options) {
|
|
NOT_USED(wlanObj);
|
|
|
|
if (networkState != NETWORKSTATE_ONLINE) {
|
|
jsError("Not connected to the internet");
|
|
return false;
|
|
}
|
|
|
|
bool success = false;
|
|
|
|
return success;
|
|
}
|
|
|