2022年12月30日 星期五

BW16更新為Micropython

BW16更新為Micropython

 

Micropython燒錄檔下載

l   首先進入Ameba IoT site on GitHubhttps://github.com/ambiot/micropython/

l   點選右邊Releases,或直接進入 https://github.com/ambiot/micropython/releases/

 


l   下載程式更新工具與Micropython燒錄檔

 


上傳燒錄檔至開發板方法

方法一:

l   Firmware_and_DownloadTool.zip解壓縮

l   依所使用作業系統執行Firmware_and_DownloadTool下程式,以Windows為例,執行”Double-Click-Me-to-Upload.cmd”

 



l   輸入開發板型號,如:BW16。輸入開發板連接埠,如:COM7

 


方法二:

l   下載程式更新工具https://github.com/ambiot/ambd_sdk

l   進入”ambd_sdk/tools/AmebaD/Image_Tool/”目錄

 


Bin file

Address

km0_boot_all.bin

0x08000000

km4_boot_all.bin

0x08004000

km0_km4_image2.bin

0x08006000

 

參考操作說明: https://amebaiotdocuments.readthedocs.io/zh_TW/latest/ambd_sdk/source/getting_started/getting_started.html

 

2022年12月23日 星期五

WiFi模組通訊實作 – LineNotify

WiFi模組通訊實作 – LineNotify

 

程式列表

LINE_Notify_Ameba.ino

#include <HttpClient.h>

#include <WiFi.h>

#include <WiFiClient.h>

#include "sys_api.h"

 

#define port 443

 

// Ameba A1_Lite

#define YelloLED 9 //PA15

 

// HUB 5168

#define GreenLED 3 //PA30

#define BlueLED 9 //PA15

 

char ssid1[] = " ";      // your network SSID (name)

char pass1[] = " ";      // your network password

char ssid2[] = " ";      // your network SSID (name)

char pass2[] = " ";      // your network password

char ssid3[] = " ";  //Your WiFi SSID

char pass3[] = " ";  //Your WiFi Password

char ssid4[] = " ";  //Your WiFi SSID

char pass4[] = " ";  //Your WiFi Password

 

String Linetoken = " ";

char host[] = "notify-api.line.me";  //LINE Notify API URL

String url = "/api/notify";  //POST header

 

int status = WL_IDLE_STATUS;

 

// Initialize the Wifi client library

WiFiSSLClient client;

 

void wifiConnect(void);

void printWifiStatus(void);

void sendMessage(String token, String message);

 

void setup(void) {

  pinMode(BlueLED, OUTPUT);

  digitalWrite(BlueLED, HIGH);   // turn the LED on (HIGH is the voltage level)

 

  // start serial port:

  Serial.begin(115200);

  // check for the presence of the shield:

  if (WiFi.status() == WL_NO_SHIELD) {

    Serial.println("WiFi shield not present");

    // don't continue:

    while (true);

  }

 

  Serial.print("Configuring access point...");

  wifiConnect();

 

  // you're connected now, so print out the status:

  printWifiStatus();

 

  int number = 55688;

  String MESSAGE = "Test string\r\n" + String(number);

  sendMessage(Linetoken, MESSAGE);

}

 

void loop(void) {

  // if there are incoming bytes available

  // from the server, read them and print them:

  while (client.available()) {

    char c = client.read();

    Serial.write(c);

  }

 

  // if the server's disconnected, stop the client:

  if (!client.connected()) {

    Serial.println();

    Serial.println("disconnecting from server.");

    client.stop();

 

    // do nothing forevermore:

    while (true);

  }

}

 

void wifiConnect(void) {

  char ssid[20],pass[20],num=0;

  unsigned long WifiConnectingTimeout; //連線逾時時間

 

  // scan for nearby networks:

  Serial.println("** Scan Networks **");

  int numSsid = WiFi.scanNetworks();

  if (numSsid == -1) {

    Serial.println("Couldn't get a wifi connection");

    while (true);

  }

 

  for (int thisNet = 0; thisNet < numSsid; thisNet++) {

    strcpy(ssid, WiFi.SSID(thisNet));

    if(strcmp(ssid, ssid1) == 0)

    {

      strcpy(pass, pass1);

      num=1;

      break;

    }

    else if(strcmp(ssid, ssid2) == 0)

    {

      strcpy(pass, pass2);

      num=2;

      break;

    }

    else if(strcmp(ssid, ssid3) == 0)

    {

      strcpy(pass, pass3);

      num=3;

      break;

    }

    else if(strcmp(ssid, ssid4) == 0)

    {

      strcpy(pass, pass4);

      num=4;

      break;

    }

  }

 

  if (num == 0) {

    Serial.println("Couldn't get a wifi connection");

    while (true);

  }

 

  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:

  Serial.print("Connecting to ");

  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  WifiConnectingTimeout = millis();

  while (WiFi.status() != WL_CONNECTED) {

    Serial.print("Attempting to connect to SSID: ");

    Serial.println(ssid);

    if ((millis() - WifiConnectingTimeout) > 10000) //等待 10

      break;

    delay(500);

    Serial.print(".");

  }

}

 

void printWifiStatus(void) {

  // print the SSID of the network you're attached to:

  Serial.print("SSID: ");

  Serial.println(WiFi.SSID());

 

  // print your WiFi shield's IP address:

  Serial.println("WiFi connected");

  Serial.print("IP Address: ");

  IPAddress ip = WiFi.localIP();

  Serial.println(ip);

 

  // print the received signal strength:

  long rssi = WiFi.RSSI();

  Serial.print("signal strength (RSSI):");

  Serial.print(rssi);

  Serial.println(" dBm");

}

 

void sendMessage(String token, String message) {

  // if there's a successful connection:

  if (client.connect(host, port)) {

    message = "message=" + message;

 

    String request =

              /* Post url */

              "POST " + url + " HTTP/1.1\r\n" +

              /* Headers */

              "Host: " + host + "\r\n" +

              "Connection: close\r\n" +

              "Authorization: Bearer " + token + "\r\n" +

              "Content-Length: " + String(message.length()) +  "\r\n" +

              "Content-Type: application/x-www-form-urlencoded\r\n\r\n" +

              /* Body */

              message + "\r\n";

    client.print(request);

    Serial.print(request);

 

    delay(2000);

    String response = "";

    response = client.readString();

    Serial.println(response); //Display the result of responsing

    Serial.println("Updated!");

  }

  else {

    Serial.println("connected fail");

    sys_reset();

  }

}

 

執行結果

本範例只是傳輸框架,傳輸遞增資料,實際傳輸可改成自己的資料